blob: 21e265725926570a75b2a8907b5a9d4da0e19640 [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) {
32 bool simple = SimpleBinding(static_cast<const BINDABLE &>(image_state)) || image_state.is_swapchain_image ||
33 (VK_NULL_HANDLE != image_state.bind_swapchain);
34
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
John Zulaufb02c1eb2020-10-06 16:33:36 -0600228static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) {
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600229 const auto *binding = bindable.Binding();
230 return binding ? binding->offset + binding->mem_state->fake_base_address : 0;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600231}
John Zulauf29d00532021-03-04 13:28:54 -0700232static VkDeviceSize ResourceBaseAddress(const IMAGE_STATE &image_state) {
233 VkDeviceSize base_address;
234 if (image_state.is_swapchain_image || (VK_NULL_HANDLE != image_state.bind_swapchain)) {
235 base_address = image_state.swapchain_fake_address;
236 } else {
237 base_address = ResourceBaseAddress(static_cast<const BINDABLE &>(image_state));
238 }
239 return base_address;
240}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600241
locke-lunarg3c038002020-04-30 23:08:08 -0600242inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
243 if (size == VK_WHOLE_SIZE) {
244 return (whole_size - offset);
245 }
246 return size;
247}
248
John Zulauf3e86bf02020-09-12 10:47:57 -0600249static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
250 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
251}
252
John Zulauf16adfc92020-04-08 10:28:33 -0600253template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600254static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600255 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
256}
257
John Zulauf355e49b2020-04-24 15:11:15 -0600258static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600259
John Zulauf3e86bf02020-09-12 10:47:57 -0600260static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
261 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
262}
263
264static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
265 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
266}
267
John Zulauf4a6105a2020-11-17 15:11:05 -0700268// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
269//
John Zulauf10f1f522020-12-18 12:00:35 -0700270// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
271//
John Zulauf4a6105a2020-11-17 15:11:05 -0700272// Usage:
273// Constructor() -- initializes the generator to point to the begin of the space declared.
274// * -- the current range of the generator empty signfies end
275// ++ -- advance to the next non-empty range (or end)
276
277// A wrapper for a single range with the same semantics as the actual generators below
278template <typename KeyType>
279class SingleRangeGenerator {
280 public:
281 SingleRangeGenerator(const KeyType &range) : current_(range) {}
John Zulaufd5115702021-01-18 12:34:33 -0700282 const KeyType &operator*() const { return current_; }
283 const KeyType *operator->() const { return &current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700284 SingleRangeGenerator &operator++() {
285 current_ = KeyType(); // just one real range
286 return *this;
287 }
288
289 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
290
291 private:
292 SingleRangeGenerator() = default;
293 const KeyType range_;
294 KeyType current_;
295};
296
297// Generate the ranges that are the intersection of range and the entries in the FilterMap
298template <typename FilterMap, typename KeyType = typename FilterMap::key_type>
299class FilteredRangeGenerator {
300 public:
John Zulaufd5115702021-01-18 12:34:33 -0700301 // Default constructed is safe to dereference for "empty" test, but for no other operation.
302 FilteredRangeGenerator() : range_(), filter_(nullptr), filter_pos_(), current_() {
303 // Default construction for KeyType *must* be empty range
304 assert(current_.empty());
305 }
John Zulauf4a6105a2020-11-17 15:11:05 -0700306 FilteredRangeGenerator(const FilterMap &filter, const KeyType &range)
307 : range_(range), filter_(&filter), filter_pos_(), current_() {
308 SeekBegin();
309 }
John Zulaufd5115702021-01-18 12:34:33 -0700310 FilteredRangeGenerator(const FilteredRangeGenerator &from) = default;
311
John Zulauf4a6105a2020-11-17 15:11:05 -0700312 const KeyType &operator*() const { return current_; }
313 const KeyType *operator->() const { return &current_; }
314 FilteredRangeGenerator &operator++() {
315 ++filter_pos_;
316 UpdateCurrent();
317 return *this;
318 }
319
320 bool operator==(const FilteredRangeGenerator &other) const { return current_ == other.current_; }
321
322 private:
John Zulauf4a6105a2020-11-17 15:11:05 -0700323 void UpdateCurrent() {
324 if (filter_pos_ != filter_->cend()) {
325 current_ = range_ & filter_pos_->first;
326 } else {
327 current_ = KeyType();
328 }
329 }
330 void SeekBegin() {
331 filter_pos_ = filter_->lower_bound(range_);
332 UpdateCurrent();
333 }
334 const KeyType range_;
335 const FilterMap *filter_;
336 typename FilterMap::const_iterator filter_pos_;
337 KeyType current_;
338};
John Zulaufd5115702021-01-18 12:34:33 -0700339using SingleAccessRangeGenerator = SingleRangeGenerator<ResourceAccessRange>;
John Zulauf4a6105a2020-11-17 15:11:05 -0700340using EventSimpleRangeGenerator = FilteredRangeGenerator<SyncEventState::ScopeMap>;
341
342// Templated to allow for different Range generators or map sources...
343
344// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulauf4a6105a2020-11-17 15:11:05 -0700345template <typename FilterMap, typename RangeGen, typename KeyType = typename FilterMap::key_type>
346class FilteredGeneratorGenerator {
347 public:
John Zulaufd5115702021-01-18 12:34:33 -0700348 // Default constructed is safe to dereference for "empty" test, but for no other operation.
349 FilteredGeneratorGenerator() : filter_(nullptr), gen_(), filter_pos_(), current_() {
350 // Default construction for KeyType *must* be empty range
351 assert(current_.empty());
352 }
353 FilteredGeneratorGenerator(const FilterMap &filter, RangeGen &gen) : filter_(&filter), gen_(gen), filter_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700354 SeekBegin();
355 }
John Zulaufd5115702021-01-18 12:34:33 -0700356 FilteredGeneratorGenerator(const FilteredGeneratorGenerator &from) = default;
John Zulauf4a6105a2020-11-17 15:11:05 -0700357 const KeyType &operator*() const { return current_; }
358 const KeyType *operator->() const { return &current_; }
359 FilteredGeneratorGenerator &operator++() {
360 KeyType gen_range = GenRange();
361 KeyType filter_range = FilterRange();
362 current_ = KeyType();
363 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
364 if (gen_range.end > filter_range.end) {
365 // if the generated range is beyond the filter_range, advance the filter range
366 filter_range = AdvanceFilter();
367 } else {
368 gen_range = AdvanceGen();
369 }
370 current_ = gen_range & filter_range;
371 }
372 return *this;
373 }
374
375 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
376
377 private:
378 KeyType AdvanceFilter() {
379 ++filter_pos_;
380 auto filter_range = FilterRange();
381 if (filter_range.valid()) {
382 FastForwardGen(filter_range);
383 }
384 return filter_range;
385 }
386 KeyType AdvanceGen() {
John Zulaufd5115702021-01-18 12:34:33 -0700387 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700388 auto gen_range = GenRange();
389 if (gen_range.valid()) {
390 FastForwardFilter(gen_range);
391 }
392 return gen_range;
393 }
394
395 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
John Zulaufd5115702021-01-18 12:34:33 -0700396 KeyType GenRange() const { return *gen_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700397
398 KeyType FastForwardFilter(const KeyType &range) {
399 auto filter_range = FilterRange();
400 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700401 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700402 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
403 if (retry_count < kRetryLimit) {
404 ++filter_pos_;
405 filter_range = FilterRange();
406 retry_count++;
407 } else {
408 // Okay we've tried walking, do a seek.
409 filter_pos_ = filter_->lower_bound(range);
410 break;
411 }
412 }
413 return FilterRange();
414 }
415
416 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
417 // faster.
418 KeyType FastForwardGen(const KeyType &range) {
419 auto gen_range = GenRange();
420 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
John Zulaufd5115702021-01-18 12:34:33 -0700421 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700422 gen_range = GenRange();
423 }
424 return gen_range;
425 }
426
427 void SeekBegin() {
428 auto gen_range = GenRange();
429 if (gen_range.empty()) {
430 current_ = KeyType();
431 filter_pos_ = filter_->cend();
432 } else {
433 filter_pos_ = filter_->lower_bound(gen_range);
434 current_ = gen_range & FilterRange();
435 }
436 }
437
John Zulauf4a6105a2020-11-17 15:11:05 -0700438 const FilterMap *filter_;
John Zulaufd5115702021-01-18 12:34:33 -0700439 RangeGen gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700440 typename FilterMap::const_iterator filter_pos_;
441 KeyType current_;
442};
443
444using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
445
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700446static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700447
John Zulauf3e86bf02020-09-12 10:47:57 -0600448ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
449 VkDeviceSize stride) {
450 VkDeviceSize range_start = offset + first_index * stride;
451 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600452 if (count == UINT32_MAX) {
453 range_size = buf_whole_size - range_start;
454 } else {
455 range_size = count * stride;
456 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600457 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600458}
459
locke-lunarg654e3692020-06-04 17:19:15 -0600460SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
461 VkShaderStageFlagBits stage_flag) {
462 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
463 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
464 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
465 }
466 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
467 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
468 assert(0);
469 }
470 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
471 return stage_access->second.uniform_read;
472 }
473
474 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
475 // Because if write hazard happens, read hazard might or might not happen.
476 // But if write hazard doesn't happen, read hazard is impossible to happen.
477 if (descriptor_data.is_writable) {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700478 return stage_access->second.storage_write;
locke-lunarg654e3692020-06-04 17:19:15 -0600479 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700480 // TODO: sampled_read
481 return stage_access->second.storage_read;
locke-lunarg654e3692020-06-04 17:19:15 -0600482}
483
locke-lunarg37047832020-06-12 13:44:45 -0600484bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
485 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
486 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
487 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
488 ? true
489 : false;
490}
491
492bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
493 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
494 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
495 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
496 ? true
497 : false;
498}
499
John Zulauf355e49b2020-04-24 15:11:15 -0600500// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600501template <typename Action>
502static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
503 Action &action) {
504 // At this point the "apply over range" logic only supports a single memory binding
505 if (!SimpleBinding(image_state)) return;
506 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600507 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700508 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
509 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600510 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700511 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600512 }
513}
514
John Zulauf7635de32020-05-29 17:14:15 -0600515// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
516// Used by both validation and record operations
517//
518// The signature for Action() reflect the needs of both uses.
519template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700520void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
521 uint32_t subpass) {
John Zulauf7635de32020-05-29 17:14:15 -0600522 const auto &rp_ci = rp_state.createInfo;
523 const auto *attachment_ci = rp_ci.pAttachments;
524 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
525
526 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
527 const auto *color_attachments = subpass_ci.pColorAttachments;
528 const auto *color_resolve = subpass_ci.pResolveAttachments;
529 if (color_resolve && color_attachments) {
530 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
531 const auto &color_attach = color_attachments[i].attachment;
532 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
533 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
534 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700535 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ,
536 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600537 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700538 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
539 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600540 }
541 }
542 }
543
544 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700545 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600546 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
547 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
548 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
549 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
550 const auto src_ci = attachment_ci[src_at];
551 // The formats are required to match so we can pick either
552 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
553 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
554 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
John Zulauf7635de32020-05-29 17:14:15 -0600555
556 // Figure out which aspects are actually touched during resolve operations
557 const char *aspect_string = nullptr;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700558 AttachmentViewGen::Gen gen_type = AttachmentViewGen::Gen::kRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600559 if (resolve_depth && resolve_stencil) {
John Zulauf7635de32020-05-29 17:14:15 -0600560 aspect_string = "depth/stencil";
561 } else if (resolve_depth) {
562 // Validate depth only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700563 gen_type = AttachmentViewGen::Gen::kDepthOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600564 aspect_string = "depth";
565 } else if (resolve_stencil) {
566 // Validate all stencil only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700567 gen_type = AttachmentViewGen::Gen::kStencilOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600568 aspect_string = "stencil";
569 }
570
John Zulaufd0ec59f2021-03-13 14:25:08 -0700571 if (aspect_string) {
572 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at], gen_type,
573 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster);
574 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at], gen_type,
575 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulauf7635de32020-05-29 17:14:15 -0600576 }
577 }
578}
579
580// Action for validating resolve operations
581class ValidateResolveAction {
582 public:
John Zulauffaea0ee2021-01-14 14:01:32 -0700583 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context,
John Zulauf64ffe552021-02-06 10:25:07 -0700584 const CommandExecutionContext &ex_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600585 : render_pass_(render_pass),
586 subpass_(subpass),
587 context_(context),
John Zulauf64ffe552021-02-06 10:25:07 -0700588 ex_context_(ex_context),
John Zulauf7635de32020-05-29 17:14:15 -0600589 func_name_(func_name),
590 skip_(false) {}
591 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 -0700592 const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage,
593 SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600594 HazardResult hazard;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700595 hazard = context_.DetectHazard(view_gen, gen_type, current_usage, ordering_rule);
John Zulauf7635de32020-05-29 17:14:15 -0600596 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -0700597 skip_ |=
John Zulauf64ffe552021-02-06 10:25:07 -0700598 ex_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700599 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
600 " to resolve attachment %" PRIu32 ". Access info %s.",
601 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name,
John Zulauf64ffe552021-02-06 10:25:07 -0700602 attachment_name, src_at, dst_at, ex_context_.FormatUsage(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600603 }
604 }
605 // Providing a mechanism for the constructing caller to get the result of the validation
606 bool GetSkip() const { return skip_; }
607
608 private:
609 VkRenderPass render_pass_;
610 const uint32_t subpass_;
611 const AccessContext &context_;
John Zulauf64ffe552021-02-06 10:25:07 -0700612 const CommandExecutionContext &ex_context_;
John Zulauf7635de32020-05-29 17:14:15 -0600613 const char *func_name_;
614 bool skip_;
615};
616
617// Update action for resolve operations
618class UpdateStateResolveAction {
619 public:
620 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
John Zulaufd0ec59f2021-03-13 14:25:08 -0700621 void operator()(const char *, const char *, uint32_t, uint32_t, const AttachmentViewGen &view_gen,
622 AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600623 // Ignores validation only arguments...
John Zulaufd0ec59f2021-03-13 14:25:08 -0700624 context_.UpdateAccessState(view_gen, gen_type, current_usage, ordering_rule, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600625 }
626
627 private:
628 AccessContext &context_;
629 const ResourceUsageTag &tag_;
630};
631
John Zulauf59e25072020-07-17 10:55:21 -0600632void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700633 const SyncStageAccessFlags &prior_, const ResourceUsageTag &tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600634 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
635 usage_index = usage_index_;
636 hazard = hazard_;
637 prior_access = prior_;
638 tag = tag_;
639}
640
John Zulauf540266b2020-04-06 18:54:53 -0600641AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
642 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600643 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600644 Reset();
645 const auto &subpass_dep = dependencies[subpass];
John Zulauf22aefed2021-03-11 18:14:35 -0700646 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
647 prev_.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
John Zulauf355e49b2020-04-24 15:11:15 -0600648 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600649 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600650 const auto prev_pass = prev_dep.first->pass;
651 const auto &prev_barriers = prev_dep.second;
652 assert(prev_dep.second.size());
653 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
654 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700655 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600656
657 async_.reserve(subpass_dep.async.size());
658 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700659 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600660 }
John Zulauf22aefed2021-03-11 18:14:35 -0700661 if (has_barrier_from_external) {
662 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
663 prev_.emplace_back(external_context, queue_flags, subpass_dep.barrier_from_external);
664 src_external_ = &prev_.back();
John Zulaufe5da6e52020-03-18 15:32:18 -0600665 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600666 if (subpass_dep.barrier_to_external.size()) {
667 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600668 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700669}
670
John Zulauf5f13a792020-03-10 07:31:21 -0600671template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700672HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600673 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600674 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600675 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600676
677 HazardResult hazard;
678 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
679 hazard = detector.Detect(prev);
680 }
681 return hazard;
682}
683
John Zulauf4a6105a2020-11-17 15:11:05 -0700684template <typename Action>
685void AccessContext::ForAll(Action &&action) {
686 for (const auto address_type : kAddressTypes) {
687 auto &accesses = GetAccessStateMap(address_type);
688 for (const auto &access : accesses) {
689 action(address_type, access);
690 }
691 }
692}
693
John Zulauf3d84f1b2020-03-09 13:33:25 -0600694// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
695// the DAG of the contexts (for example subpasses)
696template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700697HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600698 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600699 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600700
John Zulauf1a224292020-06-30 14:52:13 -0600701 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600702 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
703 // so we'll check these first
704 for (const auto &async_context : async_) {
705 hazard = async_context->DetectAsyncHazard(type, detector, range);
706 if (hazard.hazard) return hazard;
707 }
John Zulauf5f13a792020-03-10 07:31:21 -0600708 }
709
John Zulauf1a224292020-06-30 14:52:13 -0600710 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600711
John Zulauf69133422020-05-20 14:55:53 -0600712 const auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600713 const auto the_end = accesses.cend(); // End is not invalidated
714 auto pos = accesses.lower_bound(range);
John Zulauf69133422020-05-20 14:55:53 -0600715 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600716
John Zulauf3cafbf72021-03-26 16:55:19 -0600717 while (pos != the_end && pos->first.begin < range.end) {
John Zulauf69133422020-05-20 14:55:53 -0600718 // Cover any leading gap, or gap between entries
719 if (detect_prev) {
720 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
721 // Cover any leading gap, or gap between entries
722 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600723 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600724 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600725 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600726 if (hazard.hazard) return hazard;
727 }
John Zulauf69133422020-05-20 14:55:53 -0600728 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
729 gap.begin = pos->first.end;
730 }
731
732 hazard = detector.Detect(pos);
733 if (hazard.hazard) return hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600734 ++pos;
John Zulauf69133422020-05-20 14:55:53 -0600735 }
736
737 if (detect_prev) {
738 // Detect in the trailing empty as needed
739 gap.end = range.end;
740 if (gap.non_empty()) {
741 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600742 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600743 }
744
745 return hazard;
746}
747
748// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
749template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700750HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
751 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600752 auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600753 auto pos = accesses.lower_bound(range);
754 const auto the_end = accesses.end();
John Zulauf16adfc92020-04-08 10:28:33 -0600755
John Zulauf3d84f1b2020-03-09 13:33:25 -0600756 HazardResult hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600757 while (pos != the_end && pos->first.begin < range.end) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700758 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3cafbf72021-03-26 16:55:19 -0600759 if (hazard.hazard) break;
760 ++pos;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600761 }
John Zulauf16adfc92020-04-08 10:28:33 -0600762
John Zulauf3d84f1b2020-03-09 13:33:25 -0600763 return hazard;
764}
765
John Zulaufb02c1eb2020-10-06 16:33:36 -0600766struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700767 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600768 void operator()(ResourceAccessState *access) const {
769 assert(access);
770 access->ApplyBarriers(barriers, true);
771 }
772 const std::vector<SyncBarrier> &barriers;
773};
774
John Zulauf22aefed2021-03-11 18:14:35 -0700775struct ApplyTrackbackStackAction {
776 explicit ApplyTrackbackStackAction(const std::vector<SyncBarrier> &barriers_,
777 const ResourceAccessStateFunction *previous_barrier_ = nullptr)
778 : barriers(barriers_), previous_barrier(previous_barrier_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600779 void operator()(ResourceAccessState *access) const {
780 assert(access);
781 assert(!access->HasPendingState());
782 access->ApplyBarriers(barriers, false);
783 access->ApplyPendingBarriers(kCurrentCommandTag);
John Zulauf22aefed2021-03-11 18:14:35 -0700784 if (previous_barrier) {
785 assert(bool(*previous_barrier));
786 (*previous_barrier)(access);
787 }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600788 }
789 const std::vector<SyncBarrier> &barriers;
John Zulauf22aefed2021-03-11 18:14:35 -0700790 const ResourceAccessStateFunction *previous_barrier;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600791};
792
793// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
794// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
795// *different* map from dest.
796// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
797// range [first, last)
798template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600799static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
800 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600801 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600802 auto at = entry;
803 for (auto pos = first; pos != last; ++pos) {
804 // Every member of the input iterator range must fit within the remaining portion of entry
805 assert(at->first.includes(pos->first));
806 assert(at != dest->end());
807 // Trim up at to the same size as the entry to resolve
808 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600809 auto access = pos->second; // intentional copy
810 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600811 at->second.Resolve(access);
812 ++at; // Go to the remaining unused section of entry
813 }
814}
815
John Zulaufa0a98292020-09-18 09:30:10 -0600816static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
817 SyncBarrier merged = {};
818 for (const auto &barrier : barriers) {
819 merged.Merge(barrier);
820 }
821 return merged;
822}
823
John Zulaufb02c1eb2020-10-06 16:33:36 -0600824template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700825void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600826 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
827 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600828 if (!range.non_empty()) return;
829
John Zulauf355e49b2020-04-24 15:11:15 -0600830 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
831 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600832 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600833 if (current->pos_B->valid) {
834 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600835 auto access = src_pos->second; // intentional copy
836 barrier_action(&access);
837
John Zulauf16adfc92020-04-08 10:28:33 -0600838 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600839 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
840 trimmed->second.Resolve(access);
841 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600842 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600843 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600844 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600845 }
John Zulauf16adfc92020-04-08 10:28:33 -0600846 } else {
847 // we have to descend to fill this gap
848 if (recur_to_infill) {
John Zulauf22aefed2021-03-11 18:14:35 -0700849 ResourceAccessRange recurrence_range = current_range;
850 // The current context is empty for the current range, so recur to fill the gap.
851 // Since we will be recurring back up the DAG, expand the gap descent to cover the full range for which B
852 // is not valid, to minimize that recurrence
853 if (current->pos_B.at_end()) {
854 // Do the remainder here....
855 recurrence_range.end = range.end;
John Zulauf355e49b2020-04-24 15:11:15 -0600856 } else {
John Zulauf22aefed2021-03-11 18:14:35 -0700857 // Recur only over the range until B becomes valid (within the limits of range).
858 recurrence_range.end = std::min(range.end, current->pos_B->lower_bound->first.begin);
John Zulauf355e49b2020-04-24 15:11:15 -0600859 }
John Zulauf22aefed2021-03-11 18:14:35 -0700860 ResolvePreviousAccessStack(type, recurrence_range, resolve_map, infill_state, barrier_action);
861
John Zulauf355e49b2020-04-24 15:11:15 -0600862 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
863 // iterator of the outer while.
864
865 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
866 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
867 // we stepped on the dest map
John Zulauf22aefed2021-03-11 18:14:35 -0700868 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 -0600869 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600870 current.seek(seek_to);
871 } else if (!current->pos_A->valid && infill_state) {
872 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
873 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
874 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600875 }
John Zulauf5f13a792020-03-10 07:31:21 -0600876 }
John Zulauf16adfc92020-04-08 10:28:33 -0600877 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600878 }
John Zulauf1a224292020-06-30 14:52:13 -0600879
880 // Infill if range goes passed both the current and resolve map prior contents
881 if (recur_to_infill && (current->range.end < range.end)) {
882 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
John Zulauf22aefed2021-03-11 18:14:35 -0700883 ResolvePreviousAccessStack<BarrierAction>(type, trailing_fill_range, resolve_map, infill_state, barrier_action);
John Zulauf1a224292020-06-30 14:52:13 -0600884 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600885}
886
John Zulauf22aefed2021-03-11 18:14:35 -0700887template <typename BarrierAction>
888void AccessContext::ResolvePreviousAccessStack(AccessAddressType type, const ResourceAccessRange &range,
889 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
890 const BarrierAction &previous_barrier) const {
891 ResourceAccessStateFunction stacked_barrier(std::ref(previous_barrier));
892 ResolvePreviousAccess(type, range, descent_map, infill_state, &stacked_barrier);
893}
894
John Zulauf43cc7462020-12-03 12:33:12 -0700895void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
John Zulauf22aefed2021-03-11 18:14:35 -0700896 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
897 const ResourceAccessStateFunction *previous_barrier) const {
898 if (prev_.size() == 0) {
John Zulauf5f13a792020-03-10 07:31:21 -0600899 if (range.non_empty() && infill_state) {
John Zulauf22aefed2021-03-11 18:14:35 -0700900 // Fill the empty poritions of descent_map with the default_state with the barrier function applied (iff present)
901 ResourceAccessState state_copy;
902 if (previous_barrier) {
903 assert(bool(*previous_barrier));
904 state_copy = *infill_state;
905 (*previous_barrier)(&state_copy);
906 infill_state = &state_copy;
907 }
908 sparse_container::update_range_value(*descent_map, range, *infill_state,
909 sparse_container::value_precedence::prefer_dest);
John Zulauf5f13a792020-03-10 07:31:21 -0600910 }
911 } else {
912 // Look for something to fill the gap further along.
913 for (const auto &prev_dep : prev_) {
John Zulauf22aefed2021-03-11 18:14:35 -0700914 const ApplyTrackbackStackAction barrier_action(prev_dep.barriers, previous_barrier);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600915 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600916 }
John Zulauf5f13a792020-03-10 07:31:21 -0600917 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600918}
919
John Zulauf4a6105a2020-11-17 15:11:05 -0700920// Non-lazy import of all accesses, WaitEvents needs this.
921void AccessContext::ResolvePreviousAccesses() {
922 ResourceAccessState default_state;
John Zulauf22aefed2021-03-11 18:14:35 -0700923 if (!prev_.size()) return; // If no previous contexts, nothing to do
924
John Zulauf4a6105a2020-11-17 15:11:05 -0700925 for (const auto address_type : kAddressTypes) {
926 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
927 }
928}
929
John Zulauf43cc7462020-12-03 12:33:12 -0700930AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
931 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -0600932}
933
John Zulauf1507ee42020-05-18 11:33:09 -0600934static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
935 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
936 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
937 return stage_access;
938}
939static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
940 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
941 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
942 return stage_access;
943}
944
John Zulauf7635de32020-05-29 17:14:15 -0600945// Caller must manage returned pointer
946static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -0700947 uint32_t subpass, const AttachmentViewGenVector &attachment_views) {
John Zulauf7635de32020-05-29 17:14:15 -0600948 auto *proxy = new AccessContext(context);
John Zulaufd0ec59f2021-03-13 14:25:08 -0700949 proxy->UpdateAttachmentResolveAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
950 proxy->UpdateAttachmentStoreAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600951 return proxy;
952}
953
John Zulaufb02c1eb2020-10-06 16:33:36 -0600954template <typename BarrierAction>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700955void AccessContext::ResolveAccessRange(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
956 BarrierAction &barrier_action, ResourceAccessRangeMap *descent_map,
957 const ResourceAccessState *infill_state) const {
958 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
959 if (!attachment_gen) return;
960
961 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
962 const AccessAddressType address_type = view_gen.GetAddressType();
963 for (; range_gen->non_empty(); ++range_gen) {
964 ResolveAccessRange(address_type, *range_gen, barrier_action, descent_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600965 }
John Zulauf62f10592020-04-03 12:20:02 -0600966}
967
John Zulauf7635de32020-05-29 17:14:15 -0600968// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf64ffe552021-02-06 10:25:07 -0700969bool AccessContext::ValidateLayoutTransitions(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600970 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -0700971 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600972 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600973 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
974 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
975 // those affects have not been recorded yet.
976 //
977 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
978 // to apply and only copy then, if this proves a hot spot.
979 std::unique_ptr<AccessContext> proxy_for_prev;
980 TrackBack proxy_track_back;
981
John Zulauf355e49b2020-04-24 15:11:15 -0600982 const auto &transitions = rp_state.subpass_transitions[subpass];
983 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600984 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
985
986 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
John Zulauf22aefed2021-03-11 18:14:35 -0700987 assert(track_back);
John Zulauf7635de32020-05-29 17:14:15 -0600988 if (prev_needs_proxy) {
989 if (!proxy_for_prev) {
John Zulaufd0ec59f2021-03-13 14:25:08 -0700990 proxy_for_prev.reset(
991 CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass, attachment_views));
John Zulauf7635de32020-05-29 17:14:15 -0600992 proxy_track_back = *track_back;
993 proxy_track_back.context = proxy_for_prev.get();
994 }
995 track_back = &proxy_track_back;
996 }
997 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600998 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600999 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001000 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1001 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
1002 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1003 string_VkImageLayout(transition.old_layout),
1004 string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07001005 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001006 }
1007 }
1008 return skip;
1009}
1010
John Zulauf64ffe552021-02-06 10:25:07 -07001011bool AccessContext::ValidateLoadOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001012 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001013 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001014 bool skip = false;
1015 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufa0a98292020-09-18 09:30:10 -06001016
John Zulauf1507ee42020-05-18 11:33:09 -06001017 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1018 if (subpass == rp_state.attachment_first_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001019 const auto &view_gen = attachment_views[i];
1020 if (!view_gen.IsValid()) continue;
John Zulauf1507ee42020-05-18 11:33:09 -06001021 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001022
1023 // Need check in the following way
1024 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1025 // vs. transition
1026 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1027 // for each aspect loaded.
1028
1029 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001030 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001031 const bool is_color = !(has_depth || has_stencil);
1032
1033 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001034 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001035
John Zulaufaff20662020-06-01 14:07:58 -06001036 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001037 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001038
John Zulaufb02c1eb2020-10-06 16:33:36 -06001039 bool checked_stencil = false;
1040 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001041 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea, load_index, SyncOrdering::kColorAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001042 aspect = "color";
1043 } else {
1044 if (has_depth) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001045 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_index,
1046 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001047 aspect = "depth";
1048 }
1049 if (!hazard.hazard && has_stencil) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001050 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, stencil_load_index,
1051 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001052 aspect = "stencil";
1053 checked_stencil = true;
1054 }
1055 }
1056
1057 if (hazard.hazard) {
1058 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001059 const auto &sync_state = ex_context.GetSyncState();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001060 if (hazard.tag == kCurrentCommandTag) {
1061 // Hazard vs. ILT
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001062 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulaufb02c1eb2020-10-06 16:33:36 -06001063 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1064 " aspect %s during load with loadOp %s.",
1065 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1066 } else {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001067 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauf1507ee42020-05-18 11:33:09 -06001068 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001069 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001070 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf64ffe552021-02-06 10:25:07 -07001071 ex_context.FormatUsage(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001072 }
1073 }
1074 }
1075 }
1076 return skip;
1077}
1078
John Zulaufaff20662020-06-01 14:07:58 -06001079// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1080// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1081// store is part of the same Next/End operation.
1082// The latter is handled in layout transistion validation directly
John Zulauf64ffe552021-02-06 10:25:07 -07001083bool AccessContext::ValidateStoreOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufaff20662020-06-01 14:07:58 -06001084 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001085 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001086 bool skip = false;
1087 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001088
1089 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1090 if (subpass == rp_state.attachment_last_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001091 const AttachmentViewGen &view_gen = attachment_views[i];
1092 if (!view_gen.IsValid()) continue;
John Zulaufaff20662020-06-01 14:07:58 -06001093 const auto &ci = attachment_ci[i];
1094
1095 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1096 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1097 // sake, we treat DONT_CARE as writing.
1098 const bool has_depth = FormatHasDepth(ci.format);
1099 const bool has_stencil = FormatHasStencil(ci.format);
1100 const bool is_color = !(has_depth || has_stencil);
1101 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1102 if (!has_stencil && !store_op_stores) continue;
1103
1104 HazardResult hazard;
1105 const char *aspect = nullptr;
1106 bool checked_stencil = false;
1107 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001108 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
1109 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001110 aspect = "color";
1111 } else {
1112 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
John Zulaufaff20662020-06-01 14:07:58 -06001113 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001114 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1115 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001116 aspect = "depth";
1117 }
1118 if (!hazard.hazard && has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001119 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1120 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001121 aspect = "stencil";
1122 checked_stencil = true;
1123 }
1124 }
1125
1126 if (hazard.hazard) {
1127 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1128 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001129 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001130 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1131 " %s aspect during store with %s %s. Access info %s",
1132 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect,
John Zulauf64ffe552021-02-06 10:25:07 -07001133 op_type_string, store_op_string, ex_context.FormatUsage(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001134 }
1135 }
1136 }
1137 return skip;
1138}
1139
John Zulauf64ffe552021-02-06 10:25:07 -07001140bool AccessContext::ValidateResolveOperations(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001141 const VkRect2D &render_area, const AttachmentViewGenVector &attachment_views,
1142 const char *func_name, uint32_t subpass) const {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001143 ValidateResolveAction validate_action(rp_state.renderPass(), subpass, *this, ex_context, func_name);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001144 ResolveOperation(validate_action, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001145 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001146}
1147
John Zulauf3d84f1b2020-03-09 13:33:25 -06001148class HazardDetector {
1149 SyncStageAccessIndex usage_index_;
1150
1151 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001152 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001153 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1154 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001155 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001156 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001157};
1158
John Zulauf69133422020-05-20 14:55:53 -06001159class HazardDetectorWithOrdering {
1160 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001161 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001162
1163 public:
1164 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001165 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001166 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001167 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1168 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001169 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001170 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001171};
1172
John Zulauf16adfc92020-04-08 10:28:33 -06001173HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001174 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001175 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001176 const auto base_address = ResourceBaseAddress(buffer);
1177 HazardDetector detector(usage_index);
1178 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001179}
1180
John Zulauf69133422020-05-20 14:55:53 -06001181template <typename Detector>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001182HazardResult AccessContext::DetectHazard(Detector &detector, const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1183 DetectOptions options) const {
1184 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1185 if (!attachment_gen) return HazardResult();
1186
1187 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1188 const auto address_type = view_gen.GetAddressType();
1189 for (; range_gen->non_empty(); ++range_gen) {
1190 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1191 if (hazard.hazard) return hazard;
1192 }
1193
1194 return HazardResult();
1195}
1196
1197template <typename Detector>
John Zulauf69133422020-05-20 14:55:53 -06001198HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1199 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1200 const VkExtent3D &extent, DetectOptions options) const {
1201 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001202 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001203 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1204 base_address);
1205 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001206 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001207 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001208 if (hazard.hazard) return hazard;
1209 }
1210 return HazardResult();
1211}
John Zulauf110413c2021-03-20 05:38:38 -06001212template <typename Detector>
1213HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1214 const VkImageSubresourceRange &subresource_range, DetectOptions options) const {
1215 if (!SimpleBinding(image)) return HazardResult();
1216 const auto base_address = ResourceBaseAddress(image);
1217 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1218 const auto address_type = ImageAddressType(image);
1219 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf110413c2021-03-20 05:38:38 -06001220 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1221 if (hazard.hazard) return hazard;
1222 }
1223 return HazardResult();
1224}
John Zulauf69133422020-05-20 14:55:53 -06001225
John Zulauf540266b2020-04-06 18:54:53 -06001226HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1227 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1228 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001229 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1230 subresource.layerCount};
John Zulauf110413c2021-03-20 05:38:38 -06001231 HazardDetector detector(current_usage);
1232 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf1507ee42020-05-18 11:33:09 -06001233}
1234
1235HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf110413c2021-03-20 05:38:38 -06001236 const VkImageSubresourceRange &subresource_range) const {
John Zulauf69133422020-05-20 14:55:53 -06001237 HazardDetector detector(current_usage);
John Zulauf110413c2021-03-20 05:38:38 -06001238 return DetectHazard(detector, image, subresource_range, DetectOptions::kDetectAll);
John Zulauf69133422020-05-20 14:55:53 -06001239}
1240
John Zulaufd0ec59f2021-03-13 14:25:08 -07001241HazardResult AccessContext::DetectHazard(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1242 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) const {
1243 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
1244 return DetectHazard(detector, view_gen, gen_type, DetectOptions::kDetectAll);
1245}
1246
John Zulauf69133422020-05-20 14:55:53 -06001247HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001248 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001249 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001250 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001251 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001252}
1253
John Zulauf3d84f1b2020-03-09 13:33:25 -06001254class BarrierHazardDetector {
1255 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001256 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001257 SyncStageAccessFlags src_access_scope)
1258 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1259
John Zulauf5f13a792020-03-10 07:31:21 -06001260 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1261 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001262 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001263 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001264 // 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 -07001265 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001266 }
1267
1268 private:
1269 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001270 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001271 SyncStageAccessFlags src_access_scope_;
1272};
1273
John Zulauf4a6105a2020-11-17 15:11:05 -07001274class EventBarrierHazardDetector {
1275 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001276 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001277 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
1278 const ResourceUsageTag &scope_tag)
1279 : usage_index_(usage_index),
1280 src_exec_scope_(src_exec_scope),
1281 src_access_scope_(src_access_scope),
1282 event_scope_(event_scope),
1283 scope_pos_(event_scope.cbegin()),
1284 scope_end_(event_scope.cend()),
1285 scope_tag_(scope_tag) {}
1286
1287 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1288 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1289 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1290 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1291 if (scope_pos_ == scope_end_) return HazardResult();
1292 if (!scope_pos_->first.intersects(pos->first)) {
1293 event_scope_.lower_bound(pos->first);
1294 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1295 }
1296
1297 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1298 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1299 }
1300 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1301 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1302 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1303 }
1304
1305 private:
1306 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001307 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001308 SyncStageAccessFlags src_access_scope_;
1309 const SyncEventState::ScopeMap &event_scope_;
1310 SyncEventState::ScopeMap::const_iterator scope_pos_;
1311 SyncEventState::ScopeMap::const_iterator scope_end_;
1312 const ResourceUsageTag &scope_tag_;
1313};
1314
Jeremy Gebben40a22942020-12-22 14:22:06 -07001315HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001316 const SyncStageAccessFlags &src_access_scope,
1317 const VkImageSubresourceRange &subresource_range,
1318 const SyncEventState &sync_event, DetectOptions options) const {
1319 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1320 // first access scope map to use, and there's no easy way to plumb it in below.
1321 const auto address_type = ImageAddressType(image);
1322 const auto &event_scope = sync_event.FirstScope(address_type);
1323
1324 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1325 event_scope, sync_event.first_scope_tag);
John Zulauf110413c2021-03-20 05:38:38 -06001326 return DetectHazard(detector, image, subresource_range, options);
John Zulauf4a6105a2020-11-17 15:11:05 -07001327}
1328
John Zulaufd0ec59f2021-03-13 14:25:08 -07001329HazardResult AccessContext::DetectImageBarrierHazard(const AttachmentViewGen &view_gen, const SyncBarrier &barrier,
1330 DetectOptions options) const {
1331 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, barrier.src_exec_scope.exec_scope,
1332 barrier.src_access_scope);
1333 return DetectHazard(detector, view_gen, AttachmentViewGen::Gen::kViewSubresource, options);
1334}
1335
Jeremy Gebben40a22942020-12-22 14:22:06 -07001336HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001337 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001338 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001339 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001340 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
John Zulauf110413c2021-03-20 05:38:38 -06001341 return DetectHazard(detector, image, subresource_range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001342}
1343
Jeremy Gebben40a22942020-12-22 14:22:06 -07001344HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001345 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001346 const VkImageMemoryBarrier &barrier) const {
1347 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1348 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1349 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1350}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001351HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07001352 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope.exec_scope,
John Zulauf110413c2021-03-20 05:38:38 -06001353 image_barrier.barrier.src_access_scope, image_barrier.range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001354}
John Zulauf355e49b2020-04-24 15:11:15 -06001355
John Zulauf9cb530d2019-09-30 14:14:10 -06001356template <typename Flags, typename Map>
1357SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1358 SyncStageAccessFlags scope = 0;
1359 for (const auto &bit_scope : map) {
1360 if (flag_mask < bit_scope.first) break;
1361
1362 if (flag_mask & bit_scope.first) {
1363 scope |= bit_scope.second;
1364 }
1365 }
1366 return scope;
1367}
1368
Jeremy Gebben40a22942020-12-22 14:22:06 -07001369SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags2KHR stages) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001370 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1371}
1372
Jeremy Gebben40a22942020-12-22 14:22:06 -07001373SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags2KHR accesses) {
1374 return AccessScopeImpl(sync_utils::ExpandAccessFlags(accesses), syncStageAccessMaskByAccessBit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001375}
1376
Jeremy Gebben40a22942020-12-22 14:22:06 -07001377// Getting from stage mask and access mask to stage/access masks is something we need to be good at...
1378SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001379 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1380 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1381 // 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 -06001382 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1383}
1384
1385template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001386void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001387 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1388 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001389 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001390 auto pos = accesses->lower_bound(range);
1391 if (pos == accesses->end() || !pos->first.intersects(range)) {
1392 // The range is empty, fill it with a default value.
1393 pos = action.Infill(accesses, pos, range);
1394 } else if (range.begin < pos->first.begin) {
1395 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001396 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001397 } else if (pos->first.begin < range.begin) {
1398 // Trim the beginning if needed
1399 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1400 ++pos;
1401 }
1402
1403 const auto the_end = accesses->end();
1404 while ((pos != the_end) && pos->first.intersects(range)) {
1405 if (pos->first.end > range.end) {
1406 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1407 }
1408
1409 pos = action(accesses, pos);
1410 if (pos == the_end) break;
1411
1412 auto next = pos;
1413 ++next;
1414 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1415 // Need to infill if next is disjoint
1416 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001417 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001418 next = action.Infill(accesses, next, new_range);
1419 }
1420 pos = next;
1421 }
1422}
John Zulaufd5115702021-01-18 12:34:33 -07001423
1424// Give a comparable interface for range generators and ranges
1425template <typename Action>
1426inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1427 assert(range);
1428 UpdateMemoryAccessState(accesses, *range, action);
1429}
1430
John Zulauf4a6105a2020-11-17 15:11:05 -07001431template <typename Action, typename RangeGen>
1432void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1433 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001434 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 -07001435 for (; range_gen->non_empty(); ++range_gen) {
1436 UpdateMemoryAccessState(accesses, *range_gen, action);
1437 }
1438}
John Zulauf9cb530d2019-09-30 14:14:10 -06001439
John Zulaufd0ec59f2021-03-13 14:25:08 -07001440template <typename Action, typename RangeGen>
1441void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, const RangeGen &range_gen_prebuilt) {
1442 RangeGen range_gen(range_gen_prebuilt); // RangeGenerators can be expensive to create from scratch... initialize from built
1443 for (; range_gen->non_empty(); ++range_gen) {
1444 UpdateMemoryAccessState(accesses, *range_gen, action);
1445 }
1446}
John Zulauf9cb530d2019-09-30 14:14:10 -06001447struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001448 using Iterator = ResourceAccessRangeMap::iterator;
1449 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001450 // this is only called on gaps, and never returns a gap.
1451 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001452 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001453 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001454 }
John Zulauf5f13a792020-03-10 07:31:21 -06001455
John Zulauf5c5e88d2019-12-26 11:22:02 -07001456 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001457 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001458 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001459 return pos;
1460 }
1461
John Zulauf43cc7462020-12-03 12:33:12 -07001462 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001463 SyncOrdering ordering_rule_, const ResourceUsageTag &tag_)
1464 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001465 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001466 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001467 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001468 const SyncOrdering ordering_rule;
John Zulauf9cb530d2019-09-30 14:14:10 -06001469 const ResourceUsageTag &tag;
1470};
1471
John Zulauf4a6105a2020-11-17 15:11:05 -07001472// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001473struct PipelineBarrierOp {
1474 SyncBarrier barrier;
1475 bool layout_transition;
1476 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1477 : barrier(barrier_), layout_transition(layout_transition_) {}
1478 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001479 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001480 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1481};
John Zulauf4a6105a2020-11-17 15:11:05 -07001482// The barrier operation for wait events
1483struct WaitEventBarrierOp {
1484 const ResourceUsageTag *scope_tag;
1485 SyncBarrier barrier;
1486 bool layout_transition;
1487 WaitEventBarrierOp(const ResourceUsageTag &scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1488 : scope_tag(&scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
1489 WaitEventBarrierOp() = default;
1490 void operator()(ResourceAccessState *access_state) const {
1491 assert(scope_tag); // Not valid to have a non-scope op executed, default construct included for std::vector support
1492 access_state->ApplyBarrier(*scope_tag, barrier, layout_transition);
1493 }
1494};
John Zulauf1e331ec2020-12-04 18:29:38 -07001495
John Zulauf4a6105a2020-11-17 15:11:05 -07001496// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1497// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1498// of a collection is known/present.
John Zulauf1e331ec2020-12-04 18:29:38 -07001499template <typename BarrierOp>
John Zulauf89311b42020-09-29 16:28:47 -06001500class ApplyBarrierOpsFunctor {
1501 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001502 using Iterator = ResourceAccessRangeMap::iterator;
1503 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001504
John Zulauf5c5e88d2019-12-26 11:22:02 -07001505 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001506 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001507 for (const auto &op : barrier_ops_) {
1508 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001509 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001510
John Zulauf89311b42020-09-29 16:28:47 -06001511 if (resolve_) {
1512 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1513 // another walk
1514 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001515 }
1516 return pos;
1517 }
1518
John Zulauf89311b42020-09-29 16:28:47 -06001519 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulaufd5115702021-01-18 12:34:33 -07001520 ApplyBarrierOpsFunctor(bool resolve, size_t size_hint, const ResourceUsageTag &tag)
1521 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1522 barrier_ops_.reserve(size_hint);
1523 }
1524 void EmplaceBack(const BarrierOp &op) { barrier_ops_.emplace_back(op); }
John Zulauf89311b42020-09-29 16:28:47 -06001525
1526 private:
1527 bool resolve_;
John Zulaufd5115702021-01-18 12:34:33 -07001528 std::vector<BarrierOp> barrier_ops_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001529 const ResourceUsageTag &tag_;
1530};
1531
John Zulauf4a6105a2020-11-17 15:11:05 -07001532// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1533// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1534template <typename BarrierOp>
1535class ApplyBarrierFunctor {
1536 public:
1537 using Iterator = ResourceAccessRangeMap::iterator;
1538 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1539
1540 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1541 auto &access_state = pos->second;
1542 barrier_op_(&access_state);
1543 return pos;
1544 }
1545
1546 ApplyBarrierFunctor(const BarrierOp &barrier_op) : barrier_op_(barrier_op) {}
1547
1548 private:
John Zulaufd5115702021-01-18 12:34:33 -07001549 BarrierOp barrier_op_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001550};
1551
John Zulauf1e331ec2020-12-04 18:29:38 -07001552// This functor resolves the pendinging state.
1553class ResolvePendingBarrierFunctor {
1554 public:
1555 using Iterator = ResourceAccessRangeMap::iterator;
1556 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1557
1558 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1559 auto &access_state = pos->second;
1560 access_state.ApplyPendingBarriers(tag_);
1561 return pos;
1562 }
1563
1564 ResolvePendingBarrierFunctor(const ResourceUsageTag &tag) : tag_(tag) {}
1565
1566 private:
John Zulauf89311b42020-09-29 16:28:47 -06001567 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001568};
1569
John Zulauf8e3c3e92021-01-06 11:19:36 -07001570void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
1571 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
1572 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001573 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001574}
1575
John Zulauf8e3c3e92021-01-06 11:19:36 -07001576void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001577 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001578 if (!SimpleBinding(buffer)) return;
1579 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001580 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001581}
John Zulauf355e49b2020-04-24 15:11:15 -06001582
John Zulauf8e3c3e92021-01-06 11:19:36 -07001583void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf110413c2021-03-20 05:38:38 -06001584 const VkImageSubresourceRange &subresource_range, const ResourceUsageTag &tag) {
1585 if (!SimpleBinding(image)) return;
1586 const auto base_address = ResourceBaseAddress(image);
1587 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1588 const auto address_type = ImageAddressType(image);
1589 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1590 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
1591}
1592void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001593 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001594 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001595 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001596 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001597 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1598 base_address);
1599 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001600 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf110413c2021-03-20 05:38:38 -06001601 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001602}
John Zulaufd0ec59f2021-03-13 14:25:08 -07001603
1604void AccessContext::UpdateAccessState(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1605 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule, const ResourceUsageTag &tag) {
1606 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1607 if (!gen) return;
1608 subresource_adapter::ImageRangeGenerator range_gen(*gen);
1609 const auto address_type = view_gen.GetAddressType();
1610 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1611 ApplyUpdateAction(address_type, action, &range_gen);
John Zulauf7635de32020-05-29 17:14:15 -06001612}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001613
John Zulauf8e3c3e92021-01-06 11:19:36 -07001614void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001615 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1616 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001617 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1618 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001619 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001620}
1621
John Zulaufd0ec59f2021-03-13 14:25:08 -07001622template <typename Action, typename RangeGen>
1623void AccessContext::ApplyUpdateAction(AccessAddressType address_type, const Action &action, RangeGen *range_gen_arg) {
1624 assert(range_gen_arg); // Old Google C++ styleguide require non-const object pass by * not &, but this isn't an optional arg.
1625 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, range_gen_arg);
John Zulauf540266b2020-04-06 18:54:53 -06001626}
1627
1628template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001629void AccessContext::ApplyUpdateAction(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, const Action &action) {
1630 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1631 if (!gen) return;
1632 UpdateMemoryAccessState(&GetAccessStateMap(view_gen.GetAddressType()), action, *gen);
John Zulauf540266b2020-04-06 18:54:53 -06001633}
1634
John Zulaufd0ec59f2021-03-13 14:25:08 -07001635void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state,
1636 const AttachmentViewGenVector &attachment_views, uint32_t subpass,
John Zulauf7635de32020-05-29 17:14:15 -06001637 const ResourceUsageTag &tag) {
1638 UpdateStateResolveAction update(*this, tag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001639 ResolveOperation(update, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001640}
1641
John Zulaufd0ec59f2021-03-13 14:25:08 -07001642void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
1643 uint32_t subpass, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001644 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001645
1646 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1647 if (rp_state.attachment_last_subpass[i] == subpass) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001648 const auto &view_gen = attachment_views[i];
1649 if (!view_gen.IsValid()) continue; // UNUSED
John Zulaufaff20662020-06-01 14:07:58 -06001650
1651 const auto &ci = attachment_ci[i];
1652 const bool has_depth = FormatHasDepth(ci.format);
1653 const bool has_stencil = FormatHasStencil(ci.format);
1654 const bool is_color = !(has_depth || has_stencil);
1655 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1656
1657 if (is_color && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001658 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
1659 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001660 } else {
John Zulaufaff20662020-06-01 14:07:58 -06001661 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001662 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1663 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001664 }
1665 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1666 if (has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001667 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1668 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001669 }
1670 }
1671 }
1672 }
1673}
1674
John Zulauf540266b2020-04-06 18:54:53 -06001675template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001676void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001677 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001678 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001679 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001680 }
1681}
1682
1683void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001684 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1685 auto &context = contexts[subpass_index];
John Zulauf22aefed2021-03-11 18:14:35 -07001686 ApplyTrackbackStackAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001687 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001688 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001689 }
1690 }
1691}
1692
John Zulauf355e49b2020-04-24 15:11:15 -06001693// Suitable only for *subpass* access contexts
John Zulaufd0ec59f2021-03-13 14:25:08 -07001694HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const AttachmentViewGen &attach_view) const {
1695 if (!attach_view.IsValid()) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001696
John Zulauf355e49b2020-04-24 15:11:15 -06001697 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001698 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001699
1700 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001701 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1702 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001703 HazardResult hazard = track_back.context->DetectImageBarrierHazard(attach_view, merged_barrier, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001704 if (!hazard.hazard) {
1705 // The Async hazard check is against the current context's async set.
John Zulaufd0ec59f2021-03-13 14:25:08 -07001706 hazard = DetectImageBarrierHazard(attach_view, merged_barrier, kDetectAsync);
John Zulauf355e49b2020-04-24 15:11:15 -06001707 }
John Zulaufa0a98292020-09-18 09:30:10 -06001708
John Zulauf355e49b2020-04-24 15:11:15 -06001709 return hazard;
1710}
1711
John Zulaufb02c1eb2020-10-06 16:33:36 -06001712void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001713 const AttachmentViewGenVector &attachment_views, const ResourceUsageTag &tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001714 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001715 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001716 for (const auto &transition : transitions) {
1717 const auto prev_pass = transition.prev_pass;
John Zulaufd0ec59f2021-03-13 14:25:08 -07001718 const auto &view_gen = attachment_views[transition.attachment];
1719 if (!view_gen.IsValid()) continue;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001720
1721 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1722 assert(trackback);
1723
1724 // Import the attachments into the current context
1725 const auto *prev_context = trackback->context;
1726 assert(prev_context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001727 const auto address_type = view_gen.GetAddressType();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001728 auto &target_map = GetAccessStateMap(address_type);
1729 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001730 prev_context->ResolveAccessRange(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action, &target_map,
1731 &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001732 }
1733
John Zulauf86356ca2020-10-19 11:46:41 -06001734 // If there were no transitions skip this global map walk
1735 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001736 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001737 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001738 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001739}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001740
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001741void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
1742 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
John Zulauf669dfd52021-01-27 17:15:28 -07001743
1744 auto *events_context = GetCurrentEventsContext();
1745 assert(events_context);
1746 for (auto &event_pair : *events_context) {
John Zulauf4a6105a2020-11-17 15:11:05 -07001747 assert(event_pair.second); // Shouldn't be storing empty
1748 auto &sync_event = *event_pair.second;
1749 // 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 -07001750 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
1751 sync_event.barriers |= dst.exec_scope;
1752 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
John Zulauf4a6105a2020-11-17 15:11:05 -07001753 }
1754 }
1755}
1756
John Zulauf355e49b2020-04-24 15:11:15 -06001757
locke-lunarg61870c22020-06-09 14:51:50 -06001758bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1759 const char *func_name) const {
1760 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001761 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001762 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001763 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001764 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001765 return skip;
1766 }
1767
1768 using DescriptorClass = cvdescriptorset::DescriptorClass;
1769 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1770 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1771 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1772 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1773
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001774 for (const auto &stage_state : pipe->stage_state) {
1775 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1776 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001777 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001778 }
locke-lunarg61870c22020-06-09 14:51:50 -06001779 for (const auto &set_binding : stage_state.descriptor_uses) {
1780 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1781 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1782 set_binding.first.second);
1783 const auto descriptor_type = binding_it.GetType();
1784 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1785 auto array_idx = 0;
1786
1787 if (binding_it.IsVariableDescriptorCount()) {
1788 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1789 }
1790 SyncStageAccessIndex sync_index =
1791 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1792
1793 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1794 uint32_t index = i - index_range.start;
1795 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1796 switch (descriptor->GetClass()) {
1797 case DescriptorClass::ImageSampler:
1798 case DescriptorClass::Image: {
1799 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001800 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001801 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001802 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1803 img_view_state = image_sampler_descriptor->GetImageViewState();
1804 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001805 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001806 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1807 img_view_state = image_descriptor->GetImageViewState();
1808 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001809 }
1810 if (!img_view_state) continue;
John Zulauf361fb532020-07-22 10:45:39 -06001811 HazardResult hazard;
John Zulauf110413c2021-03-20 05:38:38 -06001812 const IMAGE_STATE *img_state = img_view_state->image_state.get();
John Zulauf361fb532020-07-22 10:45:39 -06001813 const auto &subresource_range = img_view_state->normalized_subresource_range;
John Zulauf110413c2021-03-20 05:38:38 -06001814
1815 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1816 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1817 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
John Zulauf361fb532020-07-22 10:45:39 -06001818 // Input attachments are subject to raster ordering rules
1819 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001820 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001821 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001822 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range);
John Zulauf361fb532020-07-22 10:45:39 -06001823 }
John Zulauf110413c2021-03-20 05:38:38 -06001824
John Zulauf33fc1d52020-07-17 11:01:10 -06001825 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001826 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001827 img_view_state->image_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001828 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1829 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001830 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001831 sync_state_->report_data->FormatHandle(img_view_state->image_view()).c_str(),
1832 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1833 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001834 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1835 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
John Zulauffaea0ee2021-01-14 14:01:32 -07001836 set_binding.first.second, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001837 }
1838 break;
1839 }
1840 case DescriptorClass::TexelBuffer: {
1841 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1842 if (!buf_view_state) continue;
1843 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001844 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001845 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001846 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001847 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001848 buf_view_state->buffer_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001849 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1850 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001851 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view()).c_str(),
1852 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1853 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001854 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1855 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001856 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001857 }
1858 break;
1859 }
1860 case DescriptorClass::GeneralBuffer: {
1861 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1862 auto buf_state = buffer_descriptor->GetBufferState();
1863 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001864 const ResourceAccessRange range =
1865 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001866 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001867 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001868 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001869 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001870 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1871 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001872 sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
1873 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1874 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001875 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1876 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001877 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001878 }
1879 break;
1880 }
1881 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1882 default:
1883 break;
1884 }
1885 }
1886 }
1887 }
1888 return skip;
1889}
1890
1891void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1892 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001893 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001894 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001895 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001896 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001897 return;
1898 }
1899
1900 using DescriptorClass = cvdescriptorset::DescriptorClass;
1901 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1902 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1903 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1904 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1905
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001906 for (const auto &stage_state : pipe->stage_state) {
1907 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1908 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001909 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001910 }
locke-lunarg61870c22020-06-09 14:51:50 -06001911 for (const auto &set_binding : stage_state.descriptor_uses) {
1912 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1913 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1914 set_binding.first.second);
1915 const auto descriptor_type = binding_it.GetType();
1916 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1917 auto array_idx = 0;
1918
1919 if (binding_it.IsVariableDescriptorCount()) {
1920 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1921 }
1922 SyncStageAccessIndex sync_index =
1923 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1924
1925 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1926 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1927 switch (descriptor->GetClass()) {
1928 case DescriptorClass::ImageSampler:
1929 case DescriptorClass::Image: {
1930 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1931 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1932 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1933 } else {
1934 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1935 }
1936 if (!img_view_state) continue;
1937 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001938 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
John Zulauf110413c2021-03-20 05:38:38 -06001939 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1940 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1941 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kRaster,
1942 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001943 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001944 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kNonAttachment,
1945 img_view_state->normalized_subresource_range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001946 }
locke-lunarg61870c22020-06-09 14:51:50 -06001947 break;
1948 }
1949 case DescriptorClass::TexelBuffer: {
1950 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1951 if (!buf_view_state) continue;
1952 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001953 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001954 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001955 break;
1956 }
1957 case DescriptorClass::GeneralBuffer: {
1958 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1959 auto buf_state = buffer_descriptor->GetBufferState();
1960 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001961 const ResourceAccessRange range =
1962 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07001963 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001964 break;
1965 }
1966 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1967 default:
1968 break;
1969 }
1970 }
1971 }
1972 }
1973}
1974
1975bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1976 bool skip = false;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001977 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001978 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06001979 return skip;
1980 }
1981
1982 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1983 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001984 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06001985
1986 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001987 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06001988 if (binding_description.binding < binding_buffers_size) {
1989 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06001990 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001991
locke-lunarg1ae57d62020-11-18 10:49:19 -07001992 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001993 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1994 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07001995 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06001996 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001997 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001998 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
1999 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
2000 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002001 }
2002 }
2003 }
2004 return skip;
2005}
2006
2007void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002008 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002009 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002010 return;
2011 }
2012 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2013 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002014 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002015
2016 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002017 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002018 if (binding_description.binding < binding_buffers_size) {
2019 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002020 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002021
locke-lunarg1ae57d62020-11-18 10:49:19 -07002022 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002023 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2024 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002025 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2026 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002027 }
2028 }
2029}
2030
2031bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2032 bool skip = false;
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002033 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->Destroyed()) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002034 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002035 }
locke-lunarg61870c22020-06-09 14:51:50 -06002036
locke-lunarg1ae57d62020-11-18 10:49:19 -07002037 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002038 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002039 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2040 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002041 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002042 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002043 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002044 index_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
2045 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer()).c_str(),
2046 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002047 }
2048
2049 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2050 // We will detect more accurate range in the future.
2051 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2052 return skip;
2053}
2054
2055void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002056 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 -06002057
locke-lunarg1ae57d62020-11-18 10:49:19 -07002058 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002059 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002060 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2061 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002062 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002063
2064 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2065 // We will detect more accurate range in the future.
2066 RecordDrawVertex(UINT32_MAX, 0, tag);
2067}
2068
2069bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002070 bool skip = false;
2071 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002072 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002073 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002074}
2075
2076void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002077 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002078 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002079 }
locke-lunarg61870c22020-06-09 14:51:50 -06002080}
2081
John Zulauf64ffe552021-02-06 10:25:07 -07002082void CommandBufferAccessContext::RecordBeginRenderPass(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2083 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2084 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002085 // Create an access context the current renderpass.
John Zulauf64ffe552021-02-06 10:25:07 -07002086 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002087 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf64ffe552021-02-06 10:25:07 -07002088 current_renderpass_context_->RecordBeginRenderPass(tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002089 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002090}
2091
John Zulauf64ffe552021-02-06 10:25:07 -07002092void CommandBufferAccessContext::RecordNextSubpass(CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002093 assert(current_renderpass_context_);
John Zulauffaea0ee2021-01-14 14:01:32 -07002094 auto prev_tag = NextCommandTag(command);
2095 auto next_tag = NextSubcommandTag(command);
John Zulauf64ffe552021-02-06 10:25:07 -07002096 current_renderpass_context_->RecordNextSubpass(prev_tag, next_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002097 current_context_ = &current_renderpass_context_->CurrentContext();
2098}
2099
John Zulauf64ffe552021-02-06 10:25:07 -07002100void CommandBufferAccessContext::RecordEndRenderPass(CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002101 assert(current_renderpass_context_);
2102 if (!current_renderpass_context_) return;
2103
John Zulauf64ffe552021-02-06 10:25:07 -07002104 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, NextCommandTag(command));
John Zulauf355e49b2020-04-24 15:11:15 -06002105 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002106 current_renderpass_context_ = nullptr;
2107}
2108
John Zulauf4a6105a2020-11-17 15:11:05 -07002109void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2110 // Erase is okay with the key not being
John Zulauf669dfd52021-01-27 17:15:28 -07002111 const auto *event_state = sync_state_->Get<EVENT_STATE>(event);
2112 if (event_state) {
2113 GetCurrentEventsContext()->Destroy(event_state);
John Zulaufd5115702021-01-18 12:34:33 -07002114 }
2115}
2116
John Zulauf64ffe552021-02-06 10:25:07 -07002117bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
John Zulauffaea0ee2021-01-14 14:01:32 -07002118 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002119 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002120 const auto &sync_state = ex_context.GetSyncState();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002121 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002122 if (!pipe ||
2123 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002124 return skip;
2125 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002126 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002127 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg37047832020-06-12 13:44:45 -06002128
John Zulauf1a224292020-06-30 14:52:13 -06002129 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002130 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002131 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2132 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002133 if (location >= subpass.colorAttachmentCount ||
2134 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002135 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002136 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002137 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2138 if (!view_gen.IsValid()) continue;
2139 HazardResult hazard =
2140 current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
2141 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment);
locke-lunarg96dc9632020-06-10 17:22:18 -06002142 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002143 const VkImageView view_handle = view_gen.GetViewState()->image_view();
John Zulaufd0ec59f2021-03-13 14:25:08 -07002144 skip |= sync_state.LogError(view_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002145 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002146 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002147 sync_state.report_data->FormatHandle(view_handle).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002148 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002149 location, ex_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002150 }
2151 }
2152 }
locke-lunarg37047832020-06-12 13:44:45 -06002153
2154 // PHASE1 TODO: Add layout based read/vs. write selection.
2155 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002156 const uint32_t depth_stencil_attachment =
2157 GetSubpassDepthStencilAttachmentIndex(pipe->graphicsPipelineCI.pDepthStencilState, subpass.pDepthStencilAttachment);
2158
2159 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2160 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2161 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002162 bool depth_write = false, stencil_write = false;
2163
2164 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002165 if (!FormatIsStencilOnly(view_state.create_info.format) && pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002166 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002167 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2168 depth_write = true;
2169 }
2170 // PHASE1 TODO: It needs to check if stencil is writable.
2171 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2172 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2173 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002174 if (!FormatIsDepthOnly(view_state.create_info.format) && pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002175 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2176 stencil_write = true;
2177 }
2178
2179 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2180 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002181 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2182 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2183 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002184 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002185 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002186 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002187 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002188 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002189 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2190 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002191 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002192 }
2193 }
2194 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002195 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2196 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2197 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002198 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002199 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002200 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002201 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002202 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002203 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2204 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002205 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002206 }
locke-lunarg61870c22020-06-09 14:51:50 -06002207 }
2208 }
2209 return skip;
2210}
2211
John Zulauf64ffe552021-02-06 10:25:07 -07002212void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag &tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002213 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002214 if (!pipe ||
2215 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002216 return;
2217 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002218 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002219 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002220
John Zulauf1a224292020-06-30 14:52:13 -06002221 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002222 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002223 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2224 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002225 if (location >= subpass.colorAttachmentCount ||
2226 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002227 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002228 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002229 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2230 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2231 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2232 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002233 }
2234 }
locke-lunarg37047832020-06-12 13:44:45 -06002235
2236 // PHASE1 TODO: Add layout based read/vs. write selection.
2237 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002238 const uint32_t depth_stencil_attachment =
2239 GetSubpassDepthStencilAttachmentIndex(pipe->graphicsPipelineCI.pDepthStencilState, subpass.pDepthStencilAttachment);
2240 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2241 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2242 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002243 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002244 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2245 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002246
2247 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002248 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002249 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2250 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002251 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2252 depth_write = true;
2253 }
2254 // PHASE1 TODO: It needs to check if stencil is writable.
2255 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2256 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2257 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002258 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002259 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002260 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2261 stencil_write = true;
2262 }
2263
John Zulaufd0ec59f2021-03-13 14:25:08 -07002264 if (depth_write || stencil_write) {
2265 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2266 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2267 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2268 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002269 }
locke-lunarg61870c22020-06-09 14:51:50 -06002270 }
2271}
2272
John Zulauf64ffe552021-02-06 10:25:07 -07002273bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002274 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002275 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002276 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002277 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002278 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002279 func_name);
2280
John Zulauf355e49b2020-04-24 15:11:15 -06002281 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002282 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002283 skip |=
2284 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002285 if (!skip) {
2286 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2287 // on a copy of the (empty) next context.
2288 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2289 AccessContext temp_context(next_context);
2290 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002291 skip |=
2292 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002293 }
John Zulauf7635de32020-05-29 17:14:15 -06002294 return skip;
2295}
John Zulauf64ffe552021-02-06 10:25:07 -07002296bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002297 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002298 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002299 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002300 current_subpass_);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002301 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_,
2302
2303 attachment_views_, func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002304 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002305 return skip;
2306}
2307
John Zulauf64ffe552021-02-06 10:25:07 -07002308AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002309 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002310}
2311
John Zulauf64ffe552021-02-06 10:25:07 -07002312bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2313 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002314 bool skip = false;
2315
John Zulauf7635de32020-05-29 17:14:15 -06002316 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2317 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2318 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2319 // to apply and only copy then, if this proves a hot spot.
2320 std::unique_ptr<AccessContext> proxy_for_current;
2321
John Zulauf355e49b2020-04-24 15:11:15 -06002322 // Validate the "finalLayout" transitions to external
2323 // Get them from where there we're hidding in the extra entry.
2324 const auto &final_transitions = rp_state_->subpass_transitions.back();
2325 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002326 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002327 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2328 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002329 auto *context = trackback.context;
2330
2331 if (transition.prev_pass == current_subpass_) {
2332 if (!proxy_for_current) {
2333 // 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 -07002334 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002335 }
2336 context = proxy_for_current.get();
2337 }
2338
John Zulaufa0a98292020-09-18 09:30:10 -06002339 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2340 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002341 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002342 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002343 skip |= ex_context.GetSyncState().LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002344 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07002345 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2346 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2347 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2348 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002349 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002350 }
2351 }
2352 return skip;
2353}
2354
2355void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2356 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002357 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002358}
2359
John Zulauf64ffe552021-02-06 10:25:07 -07002360void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag &tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002361 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2362 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002363
2364 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2365 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002366 const AttachmentViewGen &view_gen = attachment_views_[i];
2367 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002368
2369 const auto &ci = attachment_ci[i];
2370 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002371 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002372 const bool is_color = !(has_depth || has_stencil);
2373
2374 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002375 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, ColorLoadUsage(ci.loadOp),
2376 SyncOrdering::kColorAttachment, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002377 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002378 if (has_depth) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002379 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2380 DepthStencilLoadUsage(ci.loadOp), SyncOrdering::kDepthStencilAttachment, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002381 }
2382 if (has_stencil) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002383 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2384 DepthStencilLoadUsage(ci.stencilLoadOp),
2385 SyncOrdering::kDepthStencilAttachment, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002386 }
2387 }
2388 }
2389 }
2390}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002391AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2392 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2393 AttachmentViewGenVector view_gens;
2394 VkExtent3D extent = CastTo3D(render_area.extent);
2395 VkOffset3D offset = CastTo3D(render_area.offset);
2396 view_gens.reserve(attachment_views.size());
2397 for (const auto *view : attachment_views) {
2398 view_gens.emplace_back(view, offset, extent);
2399 }
2400 return view_gens;
2401}
John Zulauf64ffe552021-02-06 10:25:07 -07002402RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2403 VkQueueFlags queue_flags,
2404 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2405 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002406 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002407 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002408 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002409 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002410 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002411 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002412 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002413}
2414void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
2415 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002416 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002417 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002418 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002419}
John Zulauf1507ee42020-05-18 11:33:09 -06002420
John Zulauf64ffe552021-02-06 10:25:07 -07002421void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag &prev_subpass_tag,
John Zulauffaea0ee2021-01-14 14:01:32 -07002422 const ResourceUsageTag &next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002423 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulaufd0ec59f2021-03-13 14:25:08 -07002424 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
2425 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002426
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002427 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2428 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002429 current_subpass_++;
2430 assert(current_subpass_ < subpass_contexts_.size());
John Zulauffaea0ee2021-01-14 14:01:32 -07002431 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2432 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002433 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002434}
2435
John Zulauf64ffe552021-02-06 10:25:07 -07002436void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002437 // Add the resolve and store accesses
John Zulaufd0ec59f2021-03-13 14:25:08 -07002438 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, tag);
2439 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002440
John Zulauf355e49b2020-04-24 15:11:15 -06002441 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002442 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002443
2444 // Add the "finalLayout" transitions to external
2445 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002446 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2447 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2448 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002449 const auto &final_transitions = rp_state_->subpass_transitions.back();
2450 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002451 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002452 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002453 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002454 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002455 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002456 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002457 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002458 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002459 }
2460}
2461
Jeremy Gebben40a22942020-12-22 14:22:06 -07002462SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002463 SyncExecScope result;
2464 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002465 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2466 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002467 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2468 return result;
2469}
2470
Jeremy Gebben40a22942020-12-22 14:22:06 -07002471SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002472 SyncExecScope result;
2473 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002474 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2475 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002476 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2477 return result;
2478}
2479
2480SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002481 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002482 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002483 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002484 dst_access_scope = 0;
2485}
2486
2487template <typename Barrier>
2488SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002489 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002490 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002491 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002492 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2493}
2494
2495SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002496 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2497 if (barrier) {
2498 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002499 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002500 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002501
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002502 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002503 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002504 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2505
2506 } else {
2507 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002508 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002509 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2510
2511 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002512 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002513 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2514 }
2515}
2516
2517template <typename Barrier>
2518SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2519 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2520 src_exec_scope = src.exec_scope;
2521 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2522
2523 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002524 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002525 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002526}
2527
John Zulaufb02c1eb2020-10-06 16:33:36 -06002528// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2529void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2530 for (const auto &barrier : barriers) {
2531 ApplyBarrier(barrier, layout_transition);
2532 }
2533}
2534
John Zulauf89311b42020-09-29 16:28:47 -06002535// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2536// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2537// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002538void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2539 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002540 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002541 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002542 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002543 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002544 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002545 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002546}
John Zulauf9cb530d2019-09-30 14:14:10 -06002547HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2548 HazardResult hazard;
2549 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002550 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002551 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002552 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002553 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002554 }
2555 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002556 // Write operation:
2557 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2558 // If reads exists -- test only against them because either:
2559 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2560 // * 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
2561 // the current write happens after the reads, so just test the write against the reades
2562 // Otherwise test against last_write
2563 //
2564 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002565 if (last_reads.size()) {
2566 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002567 if (IsReadHazard(usage_stage, read_access)) {
2568 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2569 break;
2570 }
2571 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002572 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002573 // Write-After-Write check -- if we have a previous write to test against
2574 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002575 }
2576 }
2577 return hazard;
2578}
2579
John Zulauf8e3c3e92021-01-06 11:19:36 -07002580HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering &ordering_rule) const {
2581 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06002582 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2583 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002584 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002585 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002586 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2587 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002588 if (IsRead(usage_bit)) {
2589 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2590 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2591 if (is_raw_hazard) {
2592 // NOTE: we know last_write is non-zero
2593 // See if the ordering rules save us from the simple RAW check above
2594 // First check to see if the current usage is covered by the ordering rules
2595 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2596 const bool usage_is_ordered =
2597 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2598 if (usage_is_ordered) {
2599 // Now see of the most recent write (or a subsequent read) are ordered
2600 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2601 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002602 }
2603 }
John Zulauf4285ee92020-09-23 10:20:52 -06002604 if (is_raw_hazard) {
2605 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2606 }
John Zulauf361fb532020-07-22 10:45:39 -06002607 } else {
2608 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002609 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002610 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002611 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002612 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002613 if (usage_write_is_ordered) {
2614 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2615 ordered_stages = GetOrderedStages(ordering);
2616 }
2617 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2618 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002619 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002620 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2621 if (IsReadHazard(usage_stage, read_access)) {
2622 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2623 break;
2624 }
John Zulaufd14743a2020-07-03 09:42:39 -06002625 }
2626 }
John Zulauf4285ee92020-09-23 10:20:52 -06002627 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002628 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002629 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002630 }
John Zulauf69133422020-05-20 14:55:53 -06002631 }
2632 }
2633 return hazard;
2634}
2635
John Zulauf2f952d22020-02-10 11:34:51 -07002636// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002637HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002638 HazardResult hazard;
2639 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002640 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2641 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2642 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002643 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002644 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002645 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002646 }
2647 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002648 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002649 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002650 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002651 // 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 -07002652 for (const auto &read_access : last_reads) {
2653 if (read_access.tag.index >= start_tag.index) {
2654 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002655 break;
2656 }
2657 }
John Zulauf2f952d22020-02-10 11:34:51 -07002658 }
2659 }
2660 return hazard;
2661}
2662
Jeremy Gebben40a22942020-12-22 14:22:06 -07002663HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002664 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002665 // Only supporting image layout transitions for now
2666 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2667 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002668 // only test for WAW if there no intervening read operations.
2669 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002670 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002671 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002672 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002673 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002674 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002675 break;
2676 }
2677 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002678 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2679 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2680 }
2681
2682 return hazard;
2683}
2684
Jeremy Gebben40a22942020-12-22 14:22:06 -07002685HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07002686 const SyncStageAccessFlags &src_access_scope,
2687 const ResourceUsageTag &event_tag) const {
2688 // Only supporting image layout transitions for now
2689 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2690 HazardResult hazard;
2691 // only test for WAW if there no intervening read operations.
2692 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2693
John Zulaufab7756b2020-12-29 16:10:16 -07002694 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002695 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
2696 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07002697 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002698 if (read_access.tag.IsBefore(event_tag)) {
2699 // The read is in the events first synchronization scope, so we use a barrier hazard check
2700 // If the read stage is not in the src sync scope
2701 // *AND* not execution chained with an existing sync barrier (that's the or)
2702 // then the barrier access is unsafe (R/W after R)
2703 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
2704 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2705 break;
2706 }
2707 } else {
2708 // The read not in the event first sync scope and so is a hazard vs. the layout transition
2709 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2710 }
2711 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002712 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002713 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
2714 if (write_tag.IsBefore(event_tag)) {
2715 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
2716 // So do a normal barrier hazard check
2717 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2718 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2719 }
2720 } else {
2721 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06002722 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2723 }
John Zulaufd14743a2020-07-03 09:42:39 -06002724 }
John Zulauf361fb532020-07-22 10:45:39 -06002725
John Zulauf0cb5be22020-01-23 12:18:22 -07002726 return hazard;
2727}
2728
John Zulauf5f13a792020-03-10 07:31:21 -06002729// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2730// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2731// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2732void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2733 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002734 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2735 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002736 *this = other;
2737 } else if (!other.write_tag.IsBefore(write_tag)) {
2738 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2739 // dependency chaining logic or any stage expansion)
2740 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002741 pending_write_barriers |= other.pending_write_barriers;
2742 pending_layout_transition |= other.pending_layout_transition;
2743 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002744
John Zulaufd14743a2020-07-03 09:42:39 -06002745 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07002746 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06002747 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07002748 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002749 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002750 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002751 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002752 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2753 // but we should wait on profiling data for that.
2754 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002755 auto &my_read = last_reads[my_read_index];
2756 if (other_read.stage == my_read.stage) {
2757 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002758 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002759 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002760 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002761 my_read.pending_dep_chain = other_read.pending_dep_chain;
2762 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2763 // May require tracking more than one access per stage.
2764 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002765 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06002766 // Since I'm overwriting the fragement stage read, also update the input attachment info
2767 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002768 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002769 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002770 } else if (other_read.tag.IsBefore(my_read.tag)) {
2771 // The read tags match so merge the barriers
2772 my_read.barriers |= other_read.barriers;
2773 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002774 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002775
John Zulauf5f13a792020-03-10 07:31:21 -06002776 break;
2777 }
2778 }
2779 } else {
2780 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07002781 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06002782 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002783 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002784 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002785 }
John Zulauf5f13a792020-03-10 07:31:21 -06002786 }
2787 }
John Zulauf361fb532020-07-22 10:45:39 -06002788 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002789 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2790 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07002791
2792 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
2793 // of the copy and other into this using the update first logic.
2794 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
2795 // of the other first_accesses... )
2796 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
2797 FirstAccesses firsts(std::move(first_accesses_));
2798 first_accesses_.clear();
2799 first_read_stages_ = 0U;
2800 auto a = firsts.begin();
2801 auto a_end = firsts.end();
2802 for (auto &b : other.first_accesses_) {
2803 // TODO: Determine whether "IsBefore" or "IsGloballyBefore" is needed...
2804 while (a != a_end && a->tag.IsBefore(b.tag)) {
2805 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2806 ++a;
2807 }
2808 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
2809 }
2810 for (; a != a_end; ++a) {
2811 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2812 }
2813 }
John Zulauf5f13a792020-03-10 07:31:21 -06002814}
2815
John Zulauf8e3c3e92021-01-06 11:19:36 -07002816void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag &tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002817 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2818 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002819 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002820 // Mulitple outstanding reads may be of interest and do dependency chains independently
2821 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2822 const auto usage_stage = PipelineStageBit(usage_index);
2823 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002824 for (auto &read_access : last_reads) {
2825 if (read_access.stage == usage_stage) {
2826 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002827 break;
2828 }
2829 }
2830 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07002831 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002832 last_read_stages |= usage_stage;
2833 }
John Zulauf4285ee92020-09-23 10:20:52 -06002834
2835 // 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 -07002836 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002837 // TODO Revisit re: multiple reads for a given stage
2838 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002839 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002840 } else {
2841 // Assume write
2842 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002843 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002844 }
John Zulauffaea0ee2021-01-14 14:01:32 -07002845 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06002846}
John Zulauf5f13a792020-03-10 07:31:21 -06002847
John Zulauf89311b42020-09-29 16:28:47 -06002848// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2849// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2850// We can overwrite them as *this* write is now after them.
2851//
2852// 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 -07002853void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002854 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06002855 last_read_stages = 0;
2856 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002857 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002858
2859 write_barriers = 0;
2860 write_dependency_chain = 0;
2861 write_tag = tag;
2862 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002863}
2864
John Zulauf89311b42020-09-29 16:28:47 -06002865// Apply the memory barrier without updating the existing barriers. The execution barrier
2866// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2867// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2868// replace the current write barriers or add to them, so accumulate to pending as well.
2869void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2870 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2871 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002872 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
2873 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
2874 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
2875 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07002876 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06002877 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07002878 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002879 }
John Zulauf89311b42020-09-29 16:28:47 -06002880 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2881 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002882
John Zulauf89311b42020-09-29 16:28:47 -06002883 if (!pending_layout_transition) {
2884 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2885 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002886 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06002887 // 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 -07002888 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
2889 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002890 }
2891 }
John Zulaufa0a98292020-09-18 09:30:10 -06002892 }
John Zulaufa0a98292020-09-18 09:30:10 -06002893}
2894
John Zulauf4a6105a2020-11-17 15:11:05 -07002895// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
2896// changes the "chaining" state, but to keep barriers independent. See discussion above.
2897void ResourceAccessState::ApplyBarrier(const ResourceUsageTag &scope_tag, const SyncBarrier &barrier, bool layout_transition) {
2898 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
2899 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
2900 // in order to know if it's in the excecution scope
2901 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
2902 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
2903 // errors w.r.t. "most recent" accesses.
2904 if (layout_transition || ((write_tag.IsBefore(scope_tag)) && (barrier.src_access_scope & last_write).any())) {
2905 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07002906 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002907 }
2908 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2909 pending_layout_transition |= layout_transition;
2910
2911 if (!pending_layout_transition) {
2912 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2913 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002914 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002915 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
2916 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
2917 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
2918 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
2919 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
2920 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
2921 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulaufc523bf62021-02-16 08:20:34 -07002922 if (read_access.tag.IsBefore(scope_tag) &&
2923 (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers))) {
2924 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002925 }
2926 }
2927 }
2928}
John Zulauf89311b42020-09-29 16:28:47 -06002929void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
2930 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06002931 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2932 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07002933 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf89311b42020-09-29 16:28:47 -06002934 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002935 }
John Zulauf89311b42020-09-29 16:28:47 -06002936
2937 // Apply the accumulate execution barriers (and thus update chaining information)
2938 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07002939 for (auto &read_access : last_reads) {
2940 read_access.barriers |= read_access.pending_dep_chain;
2941 read_execution_barriers |= read_access.barriers;
2942 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06002943 }
2944
2945 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2946 write_dependency_chain |= pending_write_dep_chain;
2947 write_barriers |= pending_write_barriers;
2948 pending_write_dep_chain = 0;
2949 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002950}
2951
John Zulauf59e25072020-07-17 10:55:21 -06002952// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07002953VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
2954 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002955
John Zulaufab7756b2020-12-29 16:10:16 -07002956 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002957 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06002958 barriers = read_access.barriers;
2959 break;
John Zulauf59e25072020-07-17 10:55:21 -06002960 }
2961 }
John Zulauf4285ee92020-09-23 10:20:52 -06002962
John Zulauf59e25072020-07-17 10:55:21 -06002963 return barriers;
2964}
2965
Jeremy Gebben40a22942020-12-22 14:22:06 -07002966inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002967 assert(IsRead(usage));
2968 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
2969 // * the previous reads are not hazards, and thus last_write must be visible and available to
2970 // any reads that happen after.
2971 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2972 // 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 -07002973 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06002974}
2975
Jeremy Gebben40a22942020-12-22 14:22:06 -07002976VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002977 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07002978 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06002979 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002980 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06002981 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06002982 // 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 -07002983 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06002984 }
2985
2986 return ordered_stages;
2987}
2988
John Zulauffaea0ee2021-01-14 14:01:32 -07002989void ResourceAccessState::UpdateFirst(const ResourceUsageTag &tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
2990 // Only record until we record a write.
2991 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07002992 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07002993 if (0 == (usage_stage & first_read_stages_)) {
2994 // If this is a read we haven't seen or a write, record.
2995 first_read_stages_ |= usage_stage;
2996 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
2997 }
2998 }
2999}
3000
John Zulaufd1f85d42020-04-15 12:23:15 -06003001void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003002 auto *access_context = GetAccessContextNoInsert(command_buffer);
3003 if (access_context) {
3004 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003005 }
3006}
3007
John Zulaufd1f85d42020-04-15 12:23:15 -06003008void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3009 auto access_found = cb_access_state.find(command_buffer);
3010 if (access_found != cb_access_state.end()) {
3011 access_found->second->Reset();
3012 cb_access_state.erase(access_found);
3013 }
3014}
3015
John Zulauf9cb530d2019-09-30 14:14:10 -06003016bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3017 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3018 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003019 const auto *cb_context = GetAccessContext(commandBuffer);
3020 assert(cb_context);
3021 if (!cb_context) return skip;
3022 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003023
John Zulauf3d84f1b2020-03-09 13:33:25 -06003024 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003025 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003026 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003027
3028 for (uint32_t region = 0; region < regionCount; region++) {
3029 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003030 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003031 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003032 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003033 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003034 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003035 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003036 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003037 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003038 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003039 }
John Zulauf16adfc92020-04-08 10:28:33 -06003040 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003041 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003042 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003043 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003044 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003045 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003046 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003047 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003048 }
3049 }
3050 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003051 }
3052 return skip;
3053}
3054
3055void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3056 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003057 auto *cb_context = GetAccessContext(commandBuffer);
3058 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003059 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003060 auto *context = cb_context->GetCurrentAccessContext();
3061
John Zulauf9cb530d2019-09-30 14:14:10 -06003062 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003063 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003064
3065 for (uint32_t region = 0; region < regionCount; region++) {
3066 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003067 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003068 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003069 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003070 }
John Zulauf16adfc92020-04-08 10:28:33 -06003071 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003072 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003073 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003074 }
3075 }
3076}
3077
John Zulauf4a6105a2020-11-17 15:11:05 -07003078void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3079 // Clear out events from the command buffer contexts
3080 for (auto &cb_context : cb_access_state) {
3081 cb_context.second->RecordDestroyEvent(event);
3082 }
3083}
3084
Jeff Leger178b1e52020-10-05 12:22:23 -04003085bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3086 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3087 bool skip = false;
3088 const auto *cb_context = GetAccessContext(commandBuffer);
3089 assert(cb_context);
3090 if (!cb_context) return skip;
3091 const auto *context = cb_context->GetCurrentAccessContext();
3092
3093 // If we have no previous accesses, we have no hazards
3094 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3095 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3096
3097 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3098 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3099 if (src_buffer) {
3100 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003101 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003102 if (hazard.hazard) {
3103 // TODO -- add tag information to log msg when useful.
3104 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3105 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3106 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003107 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003108 }
3109 }
3110 if (dst_buffer && !skip) {
3111 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003112 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003113 if (hazard.hazard) {
3114 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3115 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3116 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003117 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003118 }
3119 }
3120 if (skip) break;
3121 }
3122 return skip;
3123}
3124
3125void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3126 auto *cb_context = GetAccessContext(commandBuffer);
3127 assert(cb_context);
3128 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3129 auto *context = cb_context->GetCurrentAccessContext();
3130
3131 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3132 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3133
3134 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3135 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3136 if (src_buffer) {
3137 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003138 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003139 }
3140 if (dst_buffer) {
3141 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003142 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003143 }
3144 }
3145}
3146
John Zulauf5c5e88d2019-12-26 11:22:02 -07003147bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3148 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3149 const VkImageCopy *pRegions) const {
3150 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003151 const auto *cb_access_context = GetAccessContext(commandBuffer);
3152 assert(cb_access_context);
3153 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003154
John Zulauf3d84f1b2020-03-09 13:33:25 -06003155 const auto *context = cb_access_context->GetCurrentAccessContext();
3156 assert(context);
3157 if (!context) return skip;
3158
3159 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3160 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003161 for (uint32_t region = 0; region < regionCount; region++) {
3162 const auto &copy_region = pRegions[region];
3163 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003164 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003165 copy_region.srcOffset, copy_region.extent);
3166 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003167 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003168 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003169 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003170 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003171 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003172 }
3173
3174 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003175 VkExtent3D dst_copy_extent =
3176 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003177 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003178 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003179 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003180 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003181 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003182 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003183 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003184 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003185 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003186 }
3187 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003188
John Zulauf5c5e88d2019-12-26 11:22:02 -07003189 return skip;
3190}
3191
3192void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3193 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3194 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003195 auto *cb_access_context = GetAccessContext(commandBuffer);
3196 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003197 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003198 auto *context = cb_access_context->GetCurrentAccessContext();
3199 assert(context);
3200
John Zulauf5c5e88d2019-12-26 11:22:02 -07003201 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003202 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003203
3204 for (uint32_t region = 0; region < regionCount; region++) {
3205 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003206 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003207 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003208 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003209 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003210 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003211 VkExtent3D dst_copy_extent =
3212 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003213 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003214 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003215 }
3216 }
3217}
3218
Jeff Leger178b1e52020-10-05 12:22:23 -04003219bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3220 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3221 bool skip = false;
3222 const auto *cb_access_context = GetAccessContext(commandBuffer);
3223 assert(cb_access_context);
3224 if (!cb_access_context) return skip;
3225
3226 const auto *context = cb_access_context->GetCurrentAccessContext();
3227 assert(context);
3228 if (!context) return skip;
3229
3230 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3231 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3232 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3233 const auto &copy_region = pCopyImageInfo->pRegions[region];
3234 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003235 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003236 copy_region.srcOffset, copy_region.extent);
3237 if (hazard.hazard) {
3238 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3239 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3240 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003241 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003242 }
3243 }
3244
3245 if (dst_image) {
3246 VkExtent3D dst_copy_extent =
3247 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003248 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003249 copy_region.dstOffset, dst_copy_extent);
3250 if (hazard.hazard) {
3251 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3252 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3253 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003254 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003255 }
3256 if (skip) break;
3257 }
3258 }
3259
3260 return skip;
3261}
3262
3263void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3264 auto *cb_access_context = GetAccessContext(commandBuffer);
3265 assert(cb_access_context);
3266 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3267 auto *context = cb_access_context->GetCurrentAccessContext();
3268 assert(context);
3269
3270 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3271 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3272
3273 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3274 const auto &copy_region = pCopyImageInfo->pRegions[region];
3275 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003276 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003277 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003278 }
3279 if (dst_image) {
3280 VkExtent3D dst_copy_extent =
3281 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003282 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003283 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003284 }
3285 }
3286}
3287
John Zulauf9cb530d2019-09-30 14:14:10 -06003288bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3289 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3290 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3291 uint32_t bufferMemoryBarrierCount,
3292 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3293 uint32_t imageMemoryBarrierCount,
3294 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3295 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003296 const auto *cb_access_context = GetAccessContext(commandBuffer);
3297 assert(cb_access_context);
3298 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003299
John Zulauf36ef9282021-02-02 11:47:24 -07003300 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3301 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3302 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3303 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003304 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003305 return skip;
3306}
3307
3308void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3309 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3310 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3311 uint32_t bufferMemoryBarrierCount,
3312 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3313 uint32_t imageMemoryBarrierCount,
3314 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003315 auto *cb_access_context = GetAccessContext(commandBuffer);
3316 assert(cb_access_context);
3317 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003318
John Zulauf36ef9282021-02-02 11:47:24 -07003319 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3320 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3321 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3322 pImageMemoryBarriers);
3323 pipeline_barrier.Record(cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003324}
3325
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003326bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3327 const VkDependencyInfoKHR *pDependencyInfo) const {
3328 bool skip = false;
3329 const auto *cb_access_context = GetAccessContext(commandBuffer);
3330 assert(cb_access_context);
3331 if (!cb_access_context) return skip;
3332
3333 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3334 skip = pipeline_barrier.Validate(*cb_access_context);
3335 return skip;
3336}
3337
3338void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3339 auto *cb_access_context = GetAccessContext(commandBuffer);
3340 assert(cb_access_context);
3341 if (!cb_access_context) return;
3342
3343 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3344 pipeline_barrier.Record(cb_access_context);
3345}
3346
John Zulauf9cb530d2019-09-30 14:14:10 -06003347void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3348 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3349 // The state tracker sets up the device state
3350 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3351
John Zulauf5f13a792020-03-10 07:31:21 -06003352 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3353 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003354 // TODO: Find a good way to do this hooklessly.
3355 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3356 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3357 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3358
John Zulaufd1f85d42020-04-15 12:23:15 -06003359 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3360 sync_device_state->ResetCommandBufferCallback(command_buffer);
3361 });
3362 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3363 sync_device_state->FreeCommandBufferCallback(command_buffer);
3364 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003365}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003366
John Zulauf355e49b2020-04-24 15:11:15 -06003367bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf64ffe552021-02-06 10:25:07 -07003368 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd, const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003369 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003370 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003371 if (cb_context) {
3372 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo, cmd_name);
3373 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003374 }
John Zulauf355e49b2020-04-24 15:11:15 -06003375 return skip;
3376}
3377
3378bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3379 VkSubpassContents contents) const {
3380 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003381 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003382 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003383 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003384 return skip;
3385}
3386
3387bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003388 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003389 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003390 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003391 return skip;
3392}
3393
John Zulauf64ffe552021-02-06 10:25:07 -07003394static const char *kBeginRenderPass2KhrName = "vkCmdBeginRenderPass2KHR";
John Zulauf355e49b2020-04-24 15:11:15 -06003395bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3396 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003397 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003398 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003399 skip |=
3400 ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2, kBeginRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003401 return skip;
3402}
3403
John Zulauf3d84f1b2020-03-09 13:33:25 -06003404void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3405 VkResult result) {
3406 // The state tracker sets up the command buffer state
3407 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3408
3409 // Create/initialize the structure that trackers accesses at the command buffer scope.
3410 auto cb_access_context = GetAccessContext(commandBuffer);
3411 assert(cb_access_context);
3412 cb_access_context->Reset();
3413}
3414
3415void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf64ffe552021-02-06 10:25:07 -07003416 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd, const char *cmd_name) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003417 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003418 if (cb_context) {
John Zulauf64ffe552021-02-06 10:25:07 -07003419 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo, cmd_name);
3420 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003421 }
3422}
3423
3424void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3425 VkSubpassContents contents) {
3426 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003427 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003428 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003429 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003430}
3431
3432void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3433 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3434 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003435 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003436}
3437
3438void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3439 const VkRenderPassBeginInfo *pRenderPassBegin,
3440 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3441 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003442 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2, kBeginRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003443}
3444
Mike Schuchardt2df08912020-12-15 16:28:09 -08003445bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf64ffe552021-02-06 10:25:07 -07003446 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd, const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003447 bool skip = false;
3448
3449 auto cb_context = GetAccessContext(commandBuffer);
3450 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003451 if (!cb_context) return skip;
3452 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo, cmd_name);
3453 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003454}
3455
3456bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3457 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003458 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003459 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003460 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003461 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3462 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003463 return skip;
3464}
3465
John Zulauf64ffe552021-02-06 10:25:07 -07003466static const char *kNextSubpass2KhrName = "vkCmdNextSubpass2KHR";
Mike Schuchardt2df08912020-12-15 16:28:09 -08003467bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3468 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003469 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003470 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2, kNextSubpass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003471 return skip;
3472}
3473
3474bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3475 const VkSubpassEndInfo *pSubpassEndInfo) const {
3476 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003477 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003478 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003479}
3480
3481void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf64ffe552021-02-06 10:25:07 -07003482 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd, const char *cmd_name) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003483 auto cb_context = GetAccessContext(commandBuffer);
3484 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003485 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003486
John Zulauf64ffe552021-02-06 10:25:07 -07003487 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo, cmd_name);
3488 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003489}
3490
3491void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3492 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003493 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003494 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003495 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003496}
3497
3498void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3499 const VkSubpassEndInfo *pSubpassEndInfo) {
3500 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003501 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003502}
3503
3504void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3505 const VkSubpassEndInfo *pSubpassEndInfo) {
3506 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003507 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2, kNextSubpass2KhrName);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003508}
3509
John Zulauf64ffe552021-02-06 10:25:07 -07003510bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd,
3511 const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003512 bool skip = false;
3513
3514 auto cb_context = GetAccessContext(commandBuffer);
3515 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003516 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003517
John Zulauf64ffe552021-02-06 10:25:07 -07003518 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo, cmd_name);
3519 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003520 return skip;
3521}
3522
3523bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3524 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003525 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003526 return skip;
3527}
3528
Mike Schuchardt2df08912020-12-15 16:28:09 -08003529bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003530 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003531 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003532 return skip;
3533}
3534
John Zulauf64ffe552021-02-06 10:25:07 -07003535const static char *kEndRenderPass2KhrName = "vkEndRenderPass2KHR";
John Zulauf355e49b2020-04-24 15:11:15 -06003536bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003537 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003538 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003539 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2, kEndRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003540 return skip;
3541}
3542
John Zulauf64ffe552021-02-06 10:25:07 -07003543void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd,
3544 const char *cmd_name) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003545 // Resolve the all subpass contexts to the command buffer contexts
3546 auto cb_context = GetAccessContext(commandBuffer);
3547 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003548 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003549
John Zulauf64ffe552021-02-06 10:25:07 -07003550 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo, cmd_name);
3551 sync_op.Record(cb_context);
3552 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003553}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003554
John Zulauf33fc1d52020-07-17 11:01:10 -06003555// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3556// updates to a resource which do not conflict at the byte level.
3557// TODO: Revisit this rule to see if it needs to be tighter or looser
3558// TODO: Add programatic control over suppression heuristics
3559bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3560 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3561}
3562
John Zulauf3d84f1b2020-03-09 13:33:25 -06003563void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003564 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003565 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003566}
3567
3568void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003569 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003570 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003571}
3572
3573void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf64ffe552021-02-06 10:25:07 -07003574 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2, kEndRenderPass2KhrName);
John Zulauf5a1a5382020-06-22 17:23:25 -06003575 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003576}
locke-lunarga19c71d2020-03-02 18:17:04 -07003577
Jeff Leger178b1e52020-10-05 12:22:23 -04003578template <typename BufferImageCopyRegionType>
3579bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3580 VkImageLayout dstImageLayout, uint32_t regionCount,
3581 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003582 bool skip = false;
3583 const auto *cb_access_context = GetAccessContext(commandBuffer);
3584 assert(cb_access_context);
3585 if (!cb_access_context) return skip;
3586
Jeff Leger178b1e52020-10-05 12:22:23 -04003587 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3588 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3589
locke-lunarga19c71d2020-03-02 18:17:04 -07003590 const auto *context = cb_access_context->GetCurrentAccessContext();
3591 assert(context);
3592 if (!context) return skip;
3593
3594 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003595 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3596
3597 for (uint32_t region = 0; region < regionCount; region++) {
3598 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003599 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003600 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003601 if (src_buffer) {
3602 ResourceAccessRange src_range =
3603 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003604 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07003605 if (hazard.hazard) {
3606 // PHASE1 TODO -- add tag information to log msg when useful.
3607 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3608 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3609 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003610 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003611 }
3612 }
3613
Jeremy Gebben40a22942020-12-22 14:22:06 -07003614 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07003615 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003616 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003617 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003618 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003619 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003620 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003621 }
3622 if (skip) break;
3623 }
3624 if (skip) break;
3625 }
3626 return skip;
3627}
3628
Jeff Leger178b1e52020-10-05 12:22:23 -04003629bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3630 VkImageLayout dstImageLayout, uint32_t regionCount,
3631 const VkBufferImageCopy *pRegions) const {
3632 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3633 COPY_COMMAND_VERSION_1);
3634}
3635
3636bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3637 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3638 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3639 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3640 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3641}
3642
3643template <typename BufferImageCopyRegionType>
3644void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3645 VkImageLayout dstImageLayout, uint32_t regionCount,
3646 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003647 auto *cb_access_context = GetAccessContext(commandBuffer);
3648 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003649
3650 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3651 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3652
3653 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003654 auto *context = cb_access_context->GetCurrentAccessContext();
3655 assert(context);
3656
3657 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003658 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003659
3660 for (uint32_t region = 0; region < regionCount; region++) {
3661 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07003662 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003663 if (src_buffer) {
3664 ResourceAccessRange src_range =
3665 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003666 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003667 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07003668 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003669 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003670 }
3671 }
3672}
3673
Jeff Leger178b1e52020-10-05 12:22:23 -04003674void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3675 VkImageLayout dstImageLayout, uint32_t regionCount,
3676 const VkBufferImageCopy *pRegions) {
3677 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3678 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3679}
3680
3681void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3682 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3683 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3684 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3685 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3686 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3687}
3688
3689template <typename BufferImageCopyRegionType>
3690bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3691 VkBuffer dstBuffer, uint32_t regionCount,
3692 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003693 bool skip = false;
3694 const auto *cb_access_context = GetAccessContext(commandBuffer);
3695 assert(cb_access_context);
3696 if (!cb_access_context) return skip;
3697
Jeff Leger178b1e52020-10-05 12:22:23 -04003698 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3699 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3700
locke-lunarga19c71d2020-03-02 18:17:04 -07003701 const auto *context = cb_access_context->GetCurrentAccessContext();
3702 assert(context);
3703 if (!context) return skip;
3704
3705 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3706 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06003707 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
locke-lunarga19c71d2020-03-02 18:17:04 -07003708 for (uint32_t region = 0; region < regionCount; region++) {
3709 const auto &copy_region = pRegions[region];
3710 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003711 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003712 copy_region.imageOffset, copy_region.imageExtent);
3713 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003714 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003715 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003716 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003717 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003718 }
John Zulauf477700e2021-01-06 11:41:49 -07003719 if (dst_mem) {
3720 ResourceAccessRange dst_range =
3721 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003722 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07003723 if (hazard.hazard) {
3724 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3725 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3726 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003727 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003728 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003729 }
3730 }
3731 if (skip) break;
3732 }
3733 return skip;
3734}
3735
Jeff Leger178b1e52020-10-05 12:22:23 -04003736bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3737 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3738 const VkBufferImageCopy *pRegions) const {
3739 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3740 COPY_COMMAND_VERSION_1);
3741}
3742
3743bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3744 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3745 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3746 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3747 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3748}
3749
3750template <typename BufferImageCopyRegionType>
3751void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3752 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3753 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003754 auto *cb_access_context = GetAccessContext(commandBuffer);
3755 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003756
3757 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3758 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3759
3760 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003761 auto *context = cb_access_context->GetCurrentAccessContext();
3762 assert(context);
3763
3764 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003765 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06003766 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06003767 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003768
3769 for (uint32_t region = 0; region < regionCount; region++) {
3770 const auto &copy_region = pRegions[region];
3771 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003772 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003773 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003774 if (dst_buffer) {
3775 ResourceAccessRange dst_range =
3776 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003777 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003778 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003779 }
3780 }
3781}
3782
Jeff Leger178b1e52020-10-05 12:22:23 -04003783void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3784 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3785 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3786 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3787}
3788
3789void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3790 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3791 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3792 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3793 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3794 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3795}
3796
3797template <typename RegionType>
3798bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3799 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3800 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003801 bool skip = false;
3802 const auto *cb_access_context = GetAccessContext(commandBuffer);
3803 assert(cb_access_context);
3804 if (!cb_access_context) return skip;
3805
3806 const auto *context = cb_access_context->GetCurrentAccessContext();
3807 assert(context);
3808 if (!context) return skip;
3809
3810 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3811 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3812
3813 for (uint32_t region = 0; region < regionCount; region++) {
3814 const auto &blit_region = pRegions[region];
3815 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003816 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3817 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3818 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3819 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3820 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3821 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003822 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003823 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003824 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003825 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003826 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003827 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003828 }
3829 }
3830
3831 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003832 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3833 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3834 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3835 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3836 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3837 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003838 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003839 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003840 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003841 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003842 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003843 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003844 }
3845 if (skip) break;
3846 }
3847 }
3848
3849 return skip;
3850}
3851
Jeff Leger178b1e52020-10-05 12:22:23 -04003852bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3853 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3854 const VkImageBlit *pRegions, VkFilter filter) const {
3855 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3856 "vkCmdBlitImage");
3857}
3858
3859bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3860 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3861 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3862 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3863 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3864}
3865
3866template <typename RegionType>
3867void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3868 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3869 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003870 auto *cb_access_context = GetAccessContext(commandBuffer);
3871 assert(cb_access_context);
3872 auto *context = cb_access_context->GetCurrentAccessContext();
3873 assert(context);
3874
3875 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003876 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003877
3878 for (uint32_t region = 0; region < regionCount; region++) {
3879 const auto &blit_region = pRegions[region];
3880 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003881 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3882 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3883 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3884 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3885 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3886 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003887 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003888 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003889 }
3890 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003891 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3892 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3893 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3894 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3895 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3896 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003897 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003898 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003899 }
3900 }
3901}
locke-lunarg36ba2592020-04-03 09:42:04 -06003902
Jeff Leger178b1e52020-10-05 12:22:23 -04003903void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3904 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3905 const VkImageBlit *pRegions, VkFilter filter) {
3906 auto *cb_access_context = GetAccessContext(commandBuffer);
3907 assert(cb_access_context);
3908 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3909 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3910 pRegions, filter);
3911 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3912}
3913
3914void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3915 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3916 auto *cb_access_context = GetAccessContext(commandBuffer);
3917 assert(cb_access_context);
3918 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3919 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3920 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3921 pBlitImageInfo->filter, tag);
3922}
3923
John Zulauffaea0ee2021-01-14 14:01:32 -07003924bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
3925 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
3926 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
3927 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003928 bool skip = false;
3929 if (drawCount == 0) return skip;
3930
3931 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3932 VkDeviceSize size = struct_size;
3933 if (drawCount == 1 || stride == size) {
3934 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003935 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003936 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3937 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003938 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003939 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003940 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003941 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003942 }
3943 } else {
3944 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003945 const ResourceAccessRange range = MakeRange(offset + i * stride, 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),
3950 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 break;
3953 }
3954 }
3955 }
3956 return skip;
3957}
3958
locke-lunarg61870c22020-06-09 14:51:50 -06003959void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3960 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3961 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003962 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3963 VkDeviceSize size = struct_size;
3964 if (drawCount == 1 || stride == size) {
3965 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003966 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003967 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003968 } else {
3969 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003970 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003971 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
3972 tag);
locke-lunargff255f92020-05-13 18:53:52 -06003973 }
3974 }
3975}
3976
John Zulauffaea0ee2021-01-14 14:01:32 -07003977bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
3978 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3979 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003980 bool skip = false;
3981
3982 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003983 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003984 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3985 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003986 skip |= LogError(count_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003987 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003988 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003989 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003990 }
3991 return skip;
3992}
3993
locke-lunarg61870c22020-06-09 14:51:50 -06003994void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003995 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003996 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003997 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003998}
3999
locke-lunarg36ba2592020-04-03 09:42:04 -06004000bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004001 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004002 const auto *cb_access_context = GetAccessContext(commandBuffer);
4003 assert(cb_access_context);
4004 if (!cb_access_context) return skip;
4005
locke-lunarg61870c22020-06-09 14:51:50 -06004006 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004007 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004008}
4009
4010void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004011 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004012 auto *cb_access_context = GetAccessContext(commandBuffer);
4013 assert(cb_access_context);
4014 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004015
locke-lunarg61870c22020-06-09 14:51:50 -06004016 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004017}
locke-lunarge1a67022020-04-29 00:15:36 -06004018
4019bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004020 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004021 const auto *cb_access_context = GetAccessContext(commandBuffer);
4022 assert(cb_access_context);
4023 if (!cb_access_context) return skip;
4024
4025 const auto *context = cb_access_context->GetCurrentAccessContext();
4026 assert(context);
4027 if (!context) return skip;
4028
locke-lunarg61870c22020-06-09 14:51:50 -06004029 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004030 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4031 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004032 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004033}
4034
4035void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004036 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004037 auto *cb_access_context = GetAccessContext(commandBuffer);
4038 assert(cb_access_context);
4039 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4040 auto *context = cb_access_context->GetCurrentAccessContext();
4041 assert(context);
4042
locke-lunarg61870c22020-06-09 14:51:50 -06004043 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4044 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004045}
4046
4047bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4048 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004049 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004050 const auto *cb_access_context = GetAccessContext(commandBuffer);
4051 assert(cb_access_context);
4052 if (!cb_access_context) return skip;
4053
locke-lunarg61870c22020-06-09 14:51:50 -06004054 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4055 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4056 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004057 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004058}
4059
4060void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4061 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004062 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004063 auto *cb_access_context = GetAccessContext(commandBuffer);
4064 assert(cb_access_context);
4065 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004066
locke-lunarg61870c22020-06-09 14:51:50 -06004067 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4068 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4069 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004070}
4071
4072bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4073 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004074 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004075 const auto *cb_access_context = GetAccessContext(commandBuffer);
4076 assert(cb_access_context);
4077 if (!cb_access_context) return skip;
4078
locke-lunarg61870c22020-06-09 14:51:50 -06004079 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4080 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4081 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004082 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004083}
4084
4085void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4086 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004087 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004088 auto *cb_access_context = GetAccessContext(commandBuffer);
4089 assert(cb_access_context);
4090 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004091
locke-lunarg61870c22020-06-09 14:51:50 -06004092 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4093 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4094 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004095}
4096
4097bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4098 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004099 bool skip = false;
4100 if (drawCount == 0) return skip;
4101
locke-lunargff255f92020-05-13 18:53:52 -06004102 const auto *cb_access_context = GetAccessContext(commandBuffer);
4103 assert(cb_access_context);
4104 if (!cb_access_context) return skip;
4105
4106 const auto *context = cb_access_context->GetCurrentAccessContext();
4107 assert(context);
4108 if (!context) return skip;
4109
locke-lunarg61870c22020-06-09 14:51:50 -06004110 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4111 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004112 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4113 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004114
4115 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4116 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4117 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004118 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004119 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004120}
4121
4122void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4123 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004124 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004125 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004126 auto *cb_access_context = GetAccessContext(commandBuffer);
4127 assert(cb_access_context);
4128 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4129 auto *context = cb_access_context->GetCurrentAccessContext();
4130 assert(context);
4131
locke-lunarg61870c22020-06-09 14:51:50 -06004132 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4133 cb_access_context->RecordDrawSubpassAttachment(tag);
4134 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004135
4136 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4137 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4138 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004139 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004140}
4141
4142bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4143 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004144 bool skip = false;
4145 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004146 const auto *cb_access_context = GetAccessContext(commandBuffer);
4147 assert(cb_access_context);
4148 if (!cb_access_context) return skip;
4149
4150 const auto *context = cb_access_context->GetCurrentAccessContext();
4151 assert(context);
4152 if (!context) return skip;
4153
locke-lunarg61870c22020-06-09 14:51:50 -06004154 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4155 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004156 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4157 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004158
4159 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4160 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4161 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004162 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004163 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004164}
4165
4166void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4167 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004168 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004169 auto *cb_access_context = GetAccessContext(commandBuffer);
4170 assert(cb_access_context);
4171 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4172 auto *context = cb_access_context->GetCurrentAccessContext();
4173 assert(context);
4174
locke-lunarg61870c22020-06-09 14:51:50 -06004175 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4176 cb_access_context->RecordDrawSubpassAttachment(tag);
4177 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004178
4179 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4180 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4181 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004182 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004183}
4184
4185bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4186 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4187 uint32_t stride, const char *function) const {
4188 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004189 const auto *cb_access_context = GetAccessContext(commandBuffer);
4190 assert(cb_access_context);
4191 if (!cb_access_context) return skip;
4192
4193 const auto *context = cb_access_context->GetCurrentAccessContext();
4194 assert(context);
4195 if (!context) return skip;
4196
locke-lunarg61870c22020-06-09 14:51:50 -06004197 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4198 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004199 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4200 maxDrawCount, stride, function);
4201 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004202
4203 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4204 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4205 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004206 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004207 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004208}
4209
4210bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4211 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4212 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004213 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4214 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004215}
4216
4217void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4218 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4219 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004220 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4221 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004222 auto *cb_access_context = GetAccessContext(commandBuffer);
4223 assert(cb_access_context);
4224 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
4225 auto *context = cb_access_context->GetCurrentAccessContext();
4226 assert(context);
4227
locke-lunarg61870c22020-06-09 14:51:50 -06004228 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4229 cb_access_context->RecordDrawSubpassAttachment(tag);
4230 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4231 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004232
4233 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4234 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4235 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004236 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004237}
4238
4239bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4240 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4241 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004242 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4243 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004244}
4245
4246void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4247 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4248 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004249 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4250 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004251 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004252}
4253
4254bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4255 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4256 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004257 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4258 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004259}
4260
4261void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4262 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4263 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004264 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4265 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004266 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4267}
4268
4269bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4270 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4271 uint32_t stride, const char *function) const {
4272 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004273 const auto *cb_access_context = GetAccessContext(commandBuffer);
4274 assert(cb_access_context);
4275 if (!cb_access_context) return skip;
4276
4277 const auto *context = cb_access_context->GetCurrentAccessContext();
4278 assert(context);
4279 if (!context) return skip;
4280
locke-lunarg61870c22020-06-09 14:51:50 -06004281 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4282 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004283 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4284 offset, maxDrawCount, stride, function);
4285 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004286
4287 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4288 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4289 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004290 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004291 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004292}
4293
4294bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4295 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4296 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004297 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4298 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004299}
4300
4301void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4302 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4303 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004304 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4305 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004306 auto *cb_access_context = GetAccessContext(commandBuffer);
4307 assert(cb_access_context);
4308 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4309 auto *context = cb_access_context->GetCurrentAccessContext();
4310 assert(context);
4311
locke-lunarg61870c22020-06-09 14:51:50 -06004312 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4313 cb_access_context->RecordDrawSubpassAttachment(tag);
4314 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4315 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004316
4317 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4318 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004319 // We will update the index and vertex buffer in SubmitQueue in the future.
4320 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004321}
4322
4323bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4324 VkDeviceSize offset, VkBuffer countBuffer,
4325 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4326 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004327 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4328 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004329}
4330
4331void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4332 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4333 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004334 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4335 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004336 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4337}
4338
4339bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4340 VkDeviceSize offset, VkBuffer countBuffer,
4341 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4342 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004343 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4344 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004345}
4346
4347void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4348 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4349 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004350 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4351 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004352 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4353}
4354
4355bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4356 const VkClearColorValue *pColor, uint32_t rangeCount,
4357 const VkImageSubresourceRange *pRanges) const {
4358 bool skip = false;
4359 const auto *cb_access_context = GetAccessContext(commandBuffer);
4360 assert(cb_access_context);
4361 if (!cb_access_context) return skip;
4362
4363 const auto *context = cb_access_context->GetCurrentAccessContext();
4364 assert(context);
4365 if (!context) return skip;
4366
4367 const auto *image_state = Get<IMAGE_STATE>(image);
4368
4369 for (uint32_t index = 0; index < rangeCount; index++) {
4370 const auto &range = pRanges[index];
4371 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004372 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004373 if (hazard.hazard) {
4374 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004375 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004376 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004377 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004378 }
4379 }
4380 }
4381 return skip;
4382}
4383
4384void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4385 const VkClearColorValue *pColor, uint32_t rangeCount,
4386 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004387 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004388 auto *cb_access_context = GetAccessContext(commandBuffer);
4389 assert(cb_access_context);
4390 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4391 auto *context = cb_access_context->GetCurrentAccessContext();
4392 assert(context);
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 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004400 }
4401 }
4402}
4403
4404bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4405 VkImageLayout imageLayout,
4406 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4407 const VkImageSubresourceRange *pRanges) const {
4408 bool skip = false;
4409 const auto *cb_access_context = GetAccessContext(commandBuffer);
4410 assert(cb_access_context);
4411 if (!cb_access_context) return skip;
4412
4413 const auto *context = cb_access_context->GetCurrentAccessContext();
4414 assert(context);
4415 if (!context) return skip;
4416
4417 const auto *image_state = Get<IMAGE_STATE>(image);
4418
4419 for (uint32_t index = 0; index < rangeCount; index++) {
4420 const auto &range = pRanges[index];
4421 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004422 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004423 if (hazard.hazard) {
4424 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004425 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004426 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004427 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004428 }
4429 }
4430 }
4431 return skip;
4432}
4433
4434void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4435 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4436 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004437 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004438 auto *cb_access_context = GetAccessContext(commandBuffer);
4439 assert(cb_access_context);
4440 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4441 auto *context = cb_access_context->GetCurrentAccessContext();
4442 assert(context);
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 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004450 }
4451 }
4452}
4453
4454bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4455 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4456 VkDeviceSize dstOffset, VkDeviceSize stride,
4457 VkQueryResultFlags flags) const {
4458 bool skip = false;
4459 const auto *cb_access_context = GetAccessContext(commandBuffer);
4460 assert(cb_access_context);
4461 if (!cb_access_context) return skip;
4462
4463 const auto *context = cb_access_context->GetCurrentAccessContext();
4464 assert(context);
4465 if (!context) return skip;
4466
4467 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4468
4469 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004470 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004471 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004472 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004473 skip |=
4474 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4475 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004476 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004477 }
4478 }
locke-lunargff255f92020-05-13 18:53:52 -06004479
4480 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004481 return skip;
4482}
4483
4484void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4485 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4486 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004487 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4488 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004489 auto *cb_access_context = GetAccessContext(commandBuffer);
4490 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004491 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004492 auto *context = cb_access_context->GetCurrentAccessContext();
4493 assert(context);
4494
4495 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4496
4497 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004498 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004499 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004500 }
locke-lunargff255f92020-05-13 18:53:52 -06004501
4502 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004503}
4504
4505bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4506 VkDeviceSize size, uint32_t data) const {
4507 bool skip = false;
4508 const auto *cb_access_context = GetAccessContext(commandBuffer);
4509 assert(cb_access_context);
4510 if (!cb_access_context) return skip;
4511
4512 const auto *context = cb_access_context->GetCurrentAccessContext();
4513 assert(context);
4514 if (!context) return skip;
4515
4516 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4517
4518 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004519 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004520 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004521 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004522 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004523 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004524 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004525 }
4526 }
4527 return skip;
4528}
4529
4530void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4531 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004532 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004533 auto *cb_access_context = GetAccessContext(commandBuffer);
4534 assert(cb_access_context);
4535 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4536 auto *context = cb_access_context->GetCurrentAccessContext();
4537 assert(context);
4538
4539 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4540
4541 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004542 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004543 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004544 }
4545}
4546
4547bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4548 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4549 const VkImageResolve *pRegions) const {
4550 bool skip = false;
4551 const auto *cb_access_context = GetAccessContext(commandBuffer);
4552 assert(cb_access_context);
4553 if (!cb_access_context) return skip;
4554
4555 const auto *context = cb_access_context->GetCurrentAccessContext();
4556 assert(context);
4557 if (!context) return skip;
4558
4559 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4560 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4561
4562 for (uint32_t region = 0; region < regionCount; region++) {
4563 const auto &resolve_region = pRegions[region];
4564 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004565 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004566 resolve_region.srcOffset, resolve_region.extent);
4567 if (hazard.hazard) {
4568 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004569 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004570 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004571 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004572 }
4573 }
4574
4575 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004576 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004577 resolve_region.dstOffset, resolve_region.extent);
4578 if (hazard.hazard) {
4579 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004580 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004581 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004582 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004583 }
4584 if (skip) break;
4585 }
4586 }
4587
4588 return skip;
4589}
4590
4591void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4592 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4593 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004594 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4595 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004596 auto *cb_access_context = GetAccessContext(commandBuffer);
4597 assert(cb_access_context);
4598 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4599 auto *context = cb_access_context->GetCurrentAccessContext();
4600 assert(context);
4601
4602 auto *src_image = Get<IMAGE_STATE>(srcImage);
4603 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4604
4605 for (uint32_t region = 0; region < regionCount; region++) {
4606 const auto &resolve_region = pRegions[region];
4607 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004608 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004609 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004610 }
4611 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004612 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004613 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004614 }
4615 }
4616}
4617
Jeff Leger178b1e52020-10-05 12:22:23 -04004618bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4619 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4620 bool skip = false;
4621 const auto *cb_access_context = GetAccessContext(commandBuffer);
4622 assert(cb_access_context);
4623 if (!cb_access_context) return skip;
4624
4625 const auto *context = cb_access_context->GetCurrentAccessContext();
4626 assert(context);
4627 if (!context) return skip;
4628
4629 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4630 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4631
4632 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4633 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4634 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004635 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004636 resolve_region.srcOffset, resolve_region.extent);
4637 if (hazard.hazard) {
4638 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4639 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4640 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004641 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004642 }
4643 }
4644
4645 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004646 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004647 resolve_region.dstOffset, resolve_region.extent);
4648 if (hazard.hazard) {
4649 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4650 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4651 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004652 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004653 }
4654 if (skip) break;
4655 }
4656 }
4657
4658 return skip;
4659}
4660
4661void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4662 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4663 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4664 auto *cb_access_context = GetAccessContext(commandBuffer);
4665 assert(cb_access_context);
4666 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4667 auto *context = cb_access_context->GetCurrentAccessContext();
4668 assert(context);
4669
4670 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4671 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4672
4673 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4674 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4675 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004676 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004677 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004678 }
4679 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004680 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004681 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004682 }
4683 }
4684}
4685
locke-lunarge1a67022020-04-29 00:15:36 -06004686bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4687 VkDeviceSize dataSize, const void *pData) const {
4688 bool skip = false;
4689 const auto *cb_access_context = GetAccessContext(commandBuffer);
4690 assert(cb_access_context);
4691 if (!cb_access_context) return skip;
4692
4693 const auto *context = cb_access_context->GetCurrentAccessContext();
4694 assert(context);
4695 if (!context) return skip;
4696
4697 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4698
4699 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004700 // VK_WHOLE_SIZE not allowed
4701 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004702 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004703 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004704 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004705 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004706 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004707 }
4708 }
4709 return skip;
4710}
4711
4712void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4713 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004714 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004715 auto *cb_access_context = GetAccessContext(commandBuffer);
4716 assert(cb_access_context);
4717 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4718 auto *context = cb_access_context->GetCurrentAccessContext();
4719 assert(context);
4720
4721 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4722
4723 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004724 // VK_WHOLE_SIZE not allowed
4725 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004726 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004727 }
4728}
locke-lunargff255f92020-05-13 18:53:52 -06004729
4730bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4731 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4732 bool skip = false;
4733 const auto *cb_access_context = GetAccessContext(commandBuffer);
4734 assert(cb_access_context);
4735 if (!cb_access_context) return skip;
4736
4737 const auto *context = cb_access_context->GetCurrentAccessContext();
4738 assert(context);
4739 if (!context) return skip;
4740
4741 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4742
4743 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004744 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004745 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06004746 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004747 skip |=
4748 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4749 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004750 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004751 }
4752 }
4753 return skip;
4754}
4755
4756void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4757 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004758 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004759 auto *cb_access_context = GetAccessContext(commandBuffer);
4760 assert(cb_access_context);
4761 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4762 auto *context = cb_access_context->GetCurrentAccessContext();
4763 assert(context);
4764
4765 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4766
4767 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004768 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004769 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004770 }
4771}
John Zulauf49beb112020-11-04 16:06:31 -07004772
4773bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
4774 bool skip = false;
4775 const auto *cb_context = GetAccessContext(commandBuffer);
4776 assert(cb_context);
4777 if (!cb_context) return skip;
4778
John Zulauf36ef9282021-02-02 11:47:24 -07004779 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004780 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004781}
4782
4783void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4784 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
4785 auto *cb_context = GetAccessContext(commandBuffer);
4786 assert(cb_context);
4787 if (!cb_context) return;
John Zulauf36ef9282021-02-02 11:47:24 -07004788 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4789 set_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004790}
4791
John Zulauf4edde622021-02-15 08:54:50 -07004792bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4793 const VkDependencyInfoKHR *pDependencyInfo) const {
4794 bool skip = false;
4795 const auto *cb_context = GetAccessContext(commandBuffer);
4796 assert(cb_context);
4797 if (!cb_context || !pDependencyInfo) return skip;
4798
4799 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4800 return set_event_op.Validate(*cb_context);
4801}
4802
4803void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4804 const VkDependencyInfoKHR *pDependencyInfo) {
4805 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
4806 auto *cb_context = GetAccessContext(commandBuffer);
4807 assert(cb_context);
4808 if (!cb_context || !pDependencyInfo) return;
4809
4810 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4811 set_event_op.Record(cb_context);
4812}
4813
John Zulauf49beb112020-11-04 16:06:31 -07004814bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
4815 VkPipelineStageFlags stageMask) const {
4816 bool skip = false;
4817 const auto *cb_context = GetAccessContext(commandBuffer);
4818 assert(cb_context);
4819 if (!cb_context) return skip;
4820
John Zulauf36ef9282021-02-02 11:47:24 -07004821 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004822 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004823}
4824
4825void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4826 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
4827 auto *cb_context = GetAccessContext(commandBuffer);
4828 assert(cb_context);
4829 if (!cb_context) return;
4830
John Zulauf36ef9282021-02-02 11:47:24 -07004831 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4832 reset_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004833}
4834
John Zulauf4edde622021-02-15 08:54:50 -07004835bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4836 VkPipelineStageFlags2KHR stageMask) const {
4837 bool skip = false;
4838 const auto *cb_context = GetAccessContext(commandBuffer);
4839 assert(cb_context);
4840 if (!cb_context) return skip;
4841
4842 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4843 return reset_event_op.Validate(*cb_context);
4844}
4845
4846void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4847 VkPipelineStageFlags2KHR stageMask) {
4848 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
4849 auto *cb_context = GetAccessContext(commandBuffer);
4850 assert(cb_context);
4851 if (!cb_context) return;
4852
4853 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4854 reset_event_op.Record(cb_context);
4855}
4856
John Zulauf49beb112020-11-04 16:06:31 -07004857bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4858 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4859 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4860 uint32_t bufferMemoryBarrierCount,
4861 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4862 uint32_t imageMemoryBarrierCount,
4863 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
4864 bool skip = false;
4865 const auto *cb_context = GetAccessContext(commandBuffer);
4866 assert(cb_context);
4867 if (!cb_context) return skip;
4868
John Zulauf36ef9282021-02-02 11:47:24 -07004869 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4870 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4871 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07004872 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004873}
4874
4875void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4876 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4877 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4878 uint32_t bufferMemoryBarrierCount,
4879 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4880 uint32_t imageMemoryBarrierCount,
4881 const VkImageMemoryBarrier *pImageMemoryBarriers) {
4882 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
4883 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
4884 imageMemoryBarrierCount, pImageMemoryBarriers);
4885
4886 auto *cb_context = GetAccessContext(commandBuffer);
4887 assert(cb_context);
4888 if (!cb_context) return;
4889
John Zulauf36ef9282021-02-02 11:47:24 -07004890 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4891 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4892 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
4893 return wait_events_op.Record(cb_context);
John Zulauf4a6105a2020-11-17 15:11:05 -07004894}
4895
John Zulauf4edde622021-02-15 08:54:50 -07004896bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4897 const VkDependencyInfoKHR *pDependencyInfos) const {
4898 bool skip = false;
4899 const auto *cb_context = GetAccessContext(commandBuffer);
4900 assert(cb_context);
4901 if (!cb_context) return skip;
4902
4903 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
4904 skip |= wait_events_op.Validate(*cb_context);
4905 return skip;
4906}
4907
4908void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4909 const VkDependencyInfoKHR *pDependencyInfos) {
4910 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
4911
4912 auto *cb_context = GetAccessContext(commandBuffer);
4913 assert(cb_context);
4914 if (!cb_context) return;
4915
4916 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
4917 wait_events_op.Record(cb_context);
4918}
4919
John Zulauf4a6105a2020-11-17 15:11:05 -07004920void SyncEventState::ResetFirstScope() {
4921 for (const auto address_type : kAddressTypes) {
4922 first_scope[static_cast<size_t>(address_type)].clear();
4923 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07004924 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07004925}
4926
4927// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07004928SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07004929 IgnoreReason reason = NotIgnored;
4930
John Zulauf4edde622021-02-15 08:54:50 -07004931 if ((CMD_WAITEVENTS2KHR == cmd) && (CMD_SETEVENT == last_command)) {
4932 reason = SetVsWait2;
4933 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
4934 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07004935 } else if (unsynchronized_set) {
4936 reason = SetRace;
4937 } else {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004938 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07004939 if (missing_bits) reason = MissingStageBits;
4940 }
4941
4942 return reason;
4943}
4944
Jeremy Gebben40a22942020-12-22 14:22:06 -07004945bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07004946 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
4947 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
4948 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07004949}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004950
John Zulauf36ef9282021-02-02 11:47:24 -07004951SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
4952 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4953 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07004954 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
4955 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
4956 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07004957 : SyncOpBase(cmd), barriers_(1) {
4958 auto &barrier_set = barriers_[0];
4959 barrier_set.dependency_flags = dependencyFlags;
4960 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
4961 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004962 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07004963 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
4964 pMemoryBarriers);
4965 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
4966 bufferMemoryBarrierCount, pBufferMemoryBarriers);
4967 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
4968 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004969}
4970
John Zulauf4edde622021-02-15 08:54:50 -07004971SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
4972 const VkDependencyInfoKHR *dep_infos)
4973 : SyncOpBase(cmd), barriers_(event_count) {
4974 for (uint32_t i = 0; i < event_count; i++) {
4975 const auto &dep_info = dep_infos[i];
4976 auto &barrier_set = barriers_[i];
4977 barrier_set.dependency_flags = dep_info.dependencyFlags;
4978 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
4979 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
4980 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
4981 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
4982 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
4983 dep_info.pMemoryBarriers);
4984 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
4985 dep_info.pBufferMemoryBarriers);
4986 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
4987 dep_info.pImageMemoryBarriers);
4988 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07004989}
4990
John Zulauf36ef9282021-02-02 11:47:24 -07004991SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07004992 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4993 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
4994 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
4995 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
4996 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07004997 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07004998 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
4999
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005000SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5001 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005002 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005003
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005004bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5005 bool skip = false;
5006 const auto *context = cb_context.GetCurrentAccessContext();
5007 assert(context);
5008 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005009 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5010
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005011 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005012 const auto &barrier_set = barriers_[0];
5013 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5014 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5015 const auto *image_state = image_barrier.image.get();
5016 if (!image_state) continue;
5017 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5018 if (hazard.hazard) {
5019 // PHASE1 TODO -- add tag information to log msg when useful.
5020 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005021 const auto image_handle = image_state->image();
John Zulauf6fdf3d02021-03-05 16:50:47 -07005022 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5023 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5024 string_SyncHazard(hazard.hazard), image_barrier.index,
5025 sync_state.report_data->FormatHandle(image_handle).c_str(),
5026 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005027 }
5028 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005029 return skip;
5030}
5031
John Zulaufd5115702021-01-18 12:34:33 -07005032struct SyncOpPipelineBarrierFunctorFactory {
5033 using BarrierOpFunctor = PipelineBarrierOp;
5034 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5035 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5036 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5037 using BufferRange = ResourceAccessRange;
5038 using ImageRange = subresource_adapter::ImageRangeGenerator;
5039 using GlobalRange = ResourceAccessRange;
5040
5041 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5042 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5043 }
5044 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
5045 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5046 }
5047 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5048 return GlobalBarrierOpFunctor(barrier, false);
5049 }
5050
5051 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5052 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5053 const auto base_address = ResourceBaseAddress(buffer);
5054 return (range + base_address);
5055 }
John Zulauf110413c2021-03-20 05:38:38 -06005056 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005057 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005058
5059 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005060 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005061 return range_gen;
5062 }
5063 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5064};
5065
5066template <typename Barriers, typename FunctorFactory>
5067void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
5068 AccessContext *context) {
5069 for (const auto &barrier : barriers) {
5070 const auto *state = barrier.GetState();
5071 if (state) {
5072 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5073 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5074 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5075 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5076 }
5077 }
5078}
5079
5080template <typename Barriers, typename FunctorFactory>
5081void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
5082 AccessContext *access_context) {
5083 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5084 for (const auto &barrier : barriers) {
5085 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5086 }
5087 for (const auto address_type : kAddressTypes) {
5088 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5089 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5090 }
5091}
5092
John Zulauf36ef9282021-02-02 11:47:24 -07005093void SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005094 SyncOpPipelineBarrierFunctorFactory factory;
5095 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005096 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005097
John Zulauf4edde622021-02-15 08:54:50 -07005098 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5099 assert(barriers_.size() == 1);
5100 const auto &barrier_set = barriers_[0];
5101 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5102 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5103 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
5104
5105 if (barrier_set.single_exec_scope) {
5106 cb_context->ApplyGlobalBarriersToEvents(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
5107 } else {
5108 for (const auto &barrier : barrier_set.memory_barriers) {
5109 cb_context->ApplyGlobalBarriersToEvents(barrier.src_exec_scope, barrier.dst_exec_scope);
5110 }
5111 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005112}
5113
John Zulauf4edde622021-02-15 08:54:50 -07005114void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5115 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5116 const VkMemoryBarrier *barriers) {
5117 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005118 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005119 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005120 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005121 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005122 }
5123 if (0 == memory_barrier_count) {
5124 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005125 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005126 }
John Zulauf4edde622021-02-15 08:54:50 -07005127 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005128}
5129
John Zulauf4edde622021-02-15 08:54:50 -07005130void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5131 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5132 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5133 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005134 for (uint32_t index = 0; index < barrier_count; index++) {
5135 const auto &barrier = barriers[index];
5136 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5137 if (buffer) {
5138 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5139 const auto range = MakeRange(barrier.offset, barrier_size);
5140 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005141 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005142 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005143 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005144 }
5145 }
5146}
5147
John Zulauf4edde622021-02-15 08:54:50 -07005148void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
5149 uint32_t memory_barrier_count, const VkMemoryBarrier2KHR *barriers) {
5150 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005151 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005152 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005153 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5154 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5155 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005156 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005157 }
John Zulauf4edde622021-02-15 08:54:50 -07005158 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005159}
5160
John Zulauf4edde622021-02-15 08:54:50 -07005161void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5162 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5163 const VkBufferMemoryBarrier2KHR *barriers) {
5164 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005165 for (uint32_t index = 0; index < barrier_count; index++) {
5166 const auto &barrier = barriers[index];
5167 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5168 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5169 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5170 if (buffer) {
5171 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5172 const auto range = MakeRange(barrier.offset, barrier_size);
5173 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005174 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005175 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005176 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005177 }
5178 }
5179}
5180
John Zulauf4edde622021-02-15 08:54:50 -07005181void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5182 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5183 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5184 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005185 for (uint32_t index = 0; index < barrier_count; index++) {
5186 const auto &barrier = barriers[index];
5187 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5188 if (image) {
5189 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5190 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005191 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005192 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005193 image_memory_barriers.emplace_back();
5194 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005195 }
5196 }
5197}
John Zulaufd5115702021-01-18 12:34:33 -07005198
John Zulauf4edde622021-02-15 08:54:50 -07005199void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5200 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5201 const VkImageMemoryBarrier2KHR *barriers) {
5202 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005203 for (uint32_t index = 0; index < barrier_count; index++) {
5204 const auto &barrier = barriers[index];
5205 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5206 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5207 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5208 if (image) {
5209 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5210 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005211 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005212 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005213 image_memory_barriers.emplace_back();
5214 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005215 }
5216 }
5217}
5218
John Zulauf36ef9282021-02-02 11:47:24 -07005219SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005220 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5221 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5222 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5223 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005224 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005225 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5226 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005227 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005228}
5229
John Zulauf4edde622021-02-15 08:54:50 -07005230SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5231 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5232 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5233 MakeEventsList(sync_state, eventCount, pEvents);
5234 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5235}
5236
John Zulaufd5115702021-01-18 12:34:33 -07005237bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005238 const char *const ignored = "Wait operation is ignored for this event.";
5239 bool skip = false;
5240 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005241 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005242
John Zulauf4edde622021-02-15 08:54:50 -07005243 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5244 const auto &barrier_set = barriers_[barrier_set_index];
5245 if (barrier_set.single_exec_scope) {
5246 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5247 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5248 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5249 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5250 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5251 } else {
5252 const auto &barriers = barrier_set.memory_barriers;
5253 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5254 const auto &barrier = barriers[barrier_index];
5255 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5256 const std::string vuid =
5257 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5258 skip =
5259 sync_state.LogInfo(command_buffer_handle, vuid,
5260 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5261 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5262 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5263 }
5264 }
5265 }
5266 }
John Zulaufd5115702021-01-18 12:34:33 -07005267 }
5268
Jeremy Gebben40a22942020-12-22 14:22:06 -07005269 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005270 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005271 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005272 const auto *events_context = cb_context.GetCurrentEventsContext();
5273 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005274 size_t barrier_set_index = 0;
5275 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5276 for (size_t event_index = 0; event_index < events_.size(); event_index++)
5277 for (const auto &event : events_) {
5278 const auto *sync_event = events_context->Get(event.get());
5279 const auto &barrier_set = barriers_[barrier_set_index];
5280 if (!sync_event) {
5281 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5282 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5283 // new validation error... wait without previously submitted set event...
5284 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives at *record time*
5285 barrier_set_index += barrier_set_incr;
5286 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005287 }
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005288 const auto event_handle = sync_event->event->event();
John Zulauf4edde622021-02-15 08:54:50 -07005289 // TODO add "destroyed" checks
5290
5291 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5292 const auto &src_exec_scope = barrier_set.src_exec_scope;
5293 event_stage_masks |= sync_event->scope.mask_param;
5294 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5295 if (ignore_reason) {
5296 switch (ignore_reason) {
5297 case SyncEventState::ResetWaitRace:
5298 case SyncEventState::Reset2WaitRace: {
5299 // Four permuations of Reset and Wait calls...
5300 const char *vuid =
5301 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5302 if (ignore_reason == SyncEventState::Reset2WaitRace) {
5303 vuid =
Jeremy Gebben476f5e22021-03-01 15:27:20 -07005304 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2KHR-event-03831" : "VUID-vkCmdResetEvent2KHR-event-03832";
John Zulauf4edde622021-02-15 08:54:50 -07005305 }
5306 const char *const message =
5307 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5308 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5309 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
5310 CommandTypeString(sync_event->last_command), ignored);
5311 break;
5312 }
5313 case SyncEventState::SetRace: {
5314 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5315 // this event
5316 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5317 const char *const message =
5318 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5319 const char *const reason = "First synchronization scope is undefined.";
5320 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5321 sync_state.report_data->FormatHandle(event_handle).c_str(),
5322 CommandTypeString(sync_event->last_command), reason, ignored);
5323 break;
5324 }
5325 case SyncEventState::MissingStageBits: {
5326 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5327 // Issue error message that event waited for is not in wait events scope
5328 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5329 const char *const message =
5330 "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5331 ". Bits missing from srcStageMask %s. %s";
5332 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5333 sync_state.report_data->FormatHandle(event_handle).c_str(),
5334 sync_event->scope.mask_param, src_exec_scope.mask_param,
5335 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), ignored);
5336 break;
5337 }
5338 case SyncEventState::SetVsWait2: {
5339 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2KHR-pEvents-03837",
5340 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5341 sync_state.report_data->FormatHandle(event_handle).c_str(),
5342 CommandTypeString(sync_event->last_command));
5343 break;
5344 }
5345 default:
5346 assert(ignore_reason == SyncEventState::NotIgnored);
5347 }
5348 } else if (barrier_set.image_memory_barriers.size()) {
5349 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5350 const auto *context = cb_context.GetCurrentAccessContext();
5351 assert(context);
5352 for (const auto &image_memory_barrier : image_memory_barriers) {
5353 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5354 const auto *image_state = image_memory_barrier.image.get();
5355 if (!image_state) continue;
John Zulauf110413c2021-03-20 05:38:38 -06005356 const auto &subresource_range = image_memory_barrier.range;
John Zulauf4edde622021-02-15 08:54:50 -07005357 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5358 const auto hazard =
5359 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5360 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5361 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005362 skip |= sync_state.LogError(image_state->image(), string_SyncHazardVUID(hazard.hazard),
John Zulauf4edde622021-02-15 08:54:50 -07005363 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5364 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005365 sync_state.report_data->FormatHandle(image_state->image()).c_str(),
John Zulauf4edde622021-02-15 08:54:50 -07005366 cb_context.FormatUsage(hazard).c_str());
5367 break;
5368 }
John Zulaufd5115702021-01-18 12:34:33 -07005369 }
5370 }
John Zulauf4edde622021-02-15 08:54:50 -07005371 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5372 // 03839
5373 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005374 }
John Zulaufd5115702021-01-18 12:34:33 -07005375
5376 // 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 -07005377 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 -07005378 if (extra_stage_bits) {
5379 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005380 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5381 const char *const vuid =
5382 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2KHR-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005383 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005384 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulaufd5115702021-01-18 12:34:33 -07005385 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005386 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005387 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005388 " vkCmdSetEvent may be in previously submitted command buffer.");
5389 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005390 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005391 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005392 }
5393 }
5394 return skip;
5395}
5396
5397struct SyncOpWaitEventsFunctorFactory {
5398 using BarrierOpFunctor = WaitEventBarrierOp;
5399 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5400 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5401 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5402 using BufferRange = EventSimpleRangeGenerator;
5403 using ImageRange = EventImageRangeGenerator;
5404 using GlobalRange = EventSimpleRangeGenerator;
5405
5406 // Need to restrict to only valid exec and access scope for this event
5407 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5408 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005409 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005410 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5411 return barrier;
5412 }
5413 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5414 auto barrier = RestrictToEvent(barrier_arg);
5415 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5416 }
5417 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
5418 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5419 }
5420 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5421 auto barrier = RestrictToEvent(barrier_arg);
5422 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5423 }
5424
5425 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5426 const AccessAddressType address_type = GetAccessAddressType(buffer);
5427 const auto base_address = ResourceBaseAddress(buffer);
5428 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5429 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5430 return filtered_range_gen;
5431 }
John Zulauf110413c2021-03-20 05:38:38 -06005432 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07005433 if (!SimpleBinding(image)) return ImageRange();
5434 const auto address_type = GetAccessAddressType(image);
5435 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005436 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005437 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5438
5439 return filtered_range_gen;
5440 }
5441 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5442 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5443 }
5444 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5445 SyncEventState *sync_event;
5446};
5447
John Zulauf36ef9282021-02-02 11:47:24 -07005448void SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
5449 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07005450 auto *access_context = cb_context->GetCurrentAccessContext();
5451 assert(access_context);
5452 if (!access_context) return;
John Zulauf669dfd52021-01-27 17:15:28 -07005453 auto *events_context = cb_context->GetCurrentEventsContext();
5454 assert(events_context);
5455 if (!events_context) return;
John Zulaufd5115702021-01-18 12:34:33 -07005456
5457 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
5458 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
5459 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
5460 access_context->ResolvePreviousAccesses();
5461
John Zulaufd5115702021-01-18 12:34:33 -07005462 // TODO... this needs change the SyncEventContext it's using depending on whether this is replay... the recorded
5463 // 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 -07005464 size_t barrier_set_index = 0;
5465 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5466 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07005467 for (auto &event_shared : events_) {
5468 if (!event_shared.get()) continue;
5469 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07005470
John Zulauf4edde622021-02-15 08:54:50 -07005471 sync_event->last_command = cmd_;
John Zulaufd5115702021-01-18 12:34:33 -07005472
John Zulauf4edde622021-02-15 08:54:50 -07005473 const auto &barrier_set = barriers_[barrier_set_index];
5474 const auto &dst = barrier_set.dst_exec_scope;
5475 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07005476 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
5477 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
5478 // of the barriers is maintained.
5479 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07005480 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5481 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5482 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07005483
5484 // Apply the global barrier to the event itself (for race condition tracking)
5485 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
5486 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
5487 sync_event->barriers |= dst.exec_scope;
5488 } else {
5489 // We ignored this wait, so we don't have any effective synchronization barriers for it.
5490 sync_event->barriers = 0U;
5491 }
John Zulauf4edde622021-02-15 08:54:50 -07005492 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005493 }
5494
5495 // Apply the pending barriers
5496 ResolvePendingBarrierFunctor apply_pending_action(tag);
5497 access_context->ApplyToContext(apply_pending_action);
5498}
5499
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005500bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5501 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5502 bool skip = false;
5503 const auto *cb_access_context = GetAccessContext(commandBuffer);
5504 assert(cb_access_context);
5505 if (!cb_access_context) return skip;
5506
5507 const auto *context = cb_access_context->GetCurrentAccessContext();
5508 assert(context);
5509 if (!context) return skip;
5510
5511 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5512
5513 if (dst_buffer) {
5514 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5515 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
5516 if (hazard.hazard) {
5517 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5518 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
5519 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
5520 string_UsageTag(hazard.tag).c_str());
5521 }
5522 }
5523 return skip;
5524}
5525
John Zulauf669dfd52021-01-27 17:15:28 -07005526void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07005527 events_.reserve(event_count);
5528 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
John Zulauf669dfd52021-01-27 17:15:28 -07005529 events_.emplace_back(sync_state.GetShared<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07005530 }
5531}
John Zulauf6ce24372021-01-30 05:56:25 -07005532
John Zulauf36ef9282021-02-02 11:47:24 -07005533SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005534 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005535 : SyncOpBase(cmd),
5536 event_(sync_state.GetShared<EVENT_STATE>(event)),
5537 exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005538
5539bool SyncOpResetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005540 auto *events_context = cb_context.GetCurrentEventsContext();
5541 assert(events_context);
5542 bool skip = false;
5543 if (!events_context) return skip;
5544
5545 const auto &sync_state = cb_context.GetSyncState();
5546 const auto *sync_event = events_context->Get(event_);
5547 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5548
5549 const char *const set_wait =
5550 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5551 "hazards.";
5552 const char *message = set_wait; // Only one message this call.
5553 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
5554 const char *vuid = nullptr;
5555 switch (sync_event->last_command) {
5556 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005557 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005558 // Needs a barrier between set and reset
5559 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
5560 break;
John Zulauf4edde622021-02-15 08:54:50 -07005561 case CMD_WAITEVENTS:
5562 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07005563 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
5564 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
5565 break;
5566 }
5567 default:
5568 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07005569 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
5570 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07005571 break;
5572 }
5573 if (vuid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005574 skip |= sync_state.LogError(event_->event(), vuid, message, CmdName(),
5575 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005576 CommandTypeString(sync_event->last_command));
5577 }
5578 }
5579 return skip;
5580}
5581
John Zulauf36ef9282021-02-02 11:47:24 -07005582void SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005583 auto *events_context = cb_context->GetCurrentEventsContext();
5584 assert(events_context);
5585 if (!events_context) return;
5586
5587 auto *sync_event = events_context->GetFromShared(event_);
5588 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5589
5590 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07005591 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005592 sync_event->unsynchronized_set = CMD_NONE;
5593 sync_event->ResetFirstScope();
5594 sync_event->barriers = 0U;
5595}
5596
John Zulauf36ef9282021-02-02 11:47:24 -07005597SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005598 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005599 : SyncOpBase(cmd),
5600 event_(sync_state.GetShared<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07005601 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
5602 dep_info_() {}
5603
5604SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
5605 const VkDependencyInfoKHR &dep_info)
5606 : SyncOpBase(cmd),
5607 event_(sync_state.GetShared<EVENT_STATE>(event)),
5608 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
5609 dep_info_(new safe_VkDependencyInfoKHR(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005610
5611bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
5612 // 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 -07005613 bool skip = false;
5614
5615 const auto &sync_state = cb_context.GetSyncState();
5616 auto *events_context = cb_context.GetCurrentEventsContext();
5617 assert(events_context);
5618 if (!events_context) return skip;
5619
5620 const auto *sync_event = events_context->Get(event_);
5621 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5622
5623 const char *const reset_set =
5624 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5625 "hazards.";
5626 const char *const wait =
5627 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
5628
5629 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07005630 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07005631 const char *message = nullptr;
5632 switch (sync_event->last_command) {
5633 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005634 case CMD_RESETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005635 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07005636 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07005637 message = reset_set;
5638 break;
5639 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005640 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005641 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07005642 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07005643 message = reset_set;
5644 break;
5645 case CMD_WAITEVENTS:
John Zulauf4edde622021-02-15 08:54:50 -07005646 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005647 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07005648 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07005649 message = wait;
5650 break;
5651 default:
5652 // The only other valid last command that wasn't one.
5653 assert(sync_event->last_command == CMD_NONE);
5654 break;
5655 }
John Zulauf4edde622021-02-15 08:54:50 -07005656 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07005657 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07005658 std::string vuid("SYNC-");
5659 vuid.append(CmdName()).append(vuid_stem);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005660 skip |= sync_state.LogError(event_->event(), vuid.c_str(), message, CmdName(),
5661 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005662 CommandTypeString(sync_event->last_command));
5663 }
5664 }
5665
5666 return skip;
5667}
5668
John Zulauf36ef9282021-02-02 11:47:24 -07005669void SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
5670 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07005671 auto *events_context = cb_context->GetCurrentEventsContext();
5672 auto *access_context = cb_context->GetCurrentAccessContext();
5673 assert(events_context);
5674 if (!events_context) return;
5675
5676 auto *sync_event = events_context->GetFromShared(event_);
5677 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5678
5679 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
5680 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
5681 // any issues caused by naive scope setting here.
5682
5683 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
5684 // Given:
5685 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
5686 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
5687
5688 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
5689 sync_event->unsynchronized_set = sync_event->last_command;
5690 sync_event->ResetFirstScope();
5691 } else if (sync_event->scope.exec_scope == 0) {
5692 // We only set the scope if there isn't one
5693 sync_event->scope = src_exec_scope_;
5694
5695 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
5696 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
5697 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
5698 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
5699 }
5700 };
5701 access_context->ForAll(set_scope);
5702 sync_event->unsynchronized_set = CMD_NONE;
5703 sync_event->first_scope_tag = tag;
5704 }
John Zulauf4edde622021-02-15 08:54:50 -07005705 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
5706 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005707 sync_event->barriers = 0U;
5708}
John Zulauf64ffe552021-02-06 10:25:07 -07005709
5710SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
5711 const VkRenderPassBeginInfo *pRenderPassBegin,
5712 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *cmd_name)
5713 : SyncOpBase(cmd, cmd_name) {
5714 if (pRenderPassBegin) {
5715 rp_state_ = sync_state.GetShared<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
5716 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
5717 const auto *fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
5718 if (fb_state) {
5719 shared_attachments_ = sync_state.GetSharedAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
5720 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
5721 // Note that this a safe to presist as long as shared_attachments is not cleared
5722 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08005723 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07005724 attachments_.emplace_back(attachment.get());
5725 }
5726 }
5727 if (pSubpassBeginInfo) {
5728 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
5729 }
5730 }
5731}
5732
5733bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5734 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
5735 bool skip = false;
5736
5737 assert(rp_state_.get());
5738 if (nullptr == rp_state_.get()) return skip;
5739 auto &rp_state = *rp_state_.get();
5740
5741 const uint32_t subpass = 0;
5742
5743 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
5744 // hasn't happened yet)
5745 const std::vector<AccessContext> empty_context_vector;
5746 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
5747 cb_context.GetCurrentAccessContext());
5748
5749 // Validate attachment operations
5750 if (attachments_.size() == 0) return skip;
5751 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07005752
5753 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
5754 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
5755 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
5756 // operations (though it's currently a messy approach)
5757 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
5758 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07005759
5760 // Validate load operations if there were no layout transition hazards
5761 if (!skip) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07005762 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kCurrentCommandTag);
5763 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07005764 }
5765
5766 return skip;
5767}
5768
5769void SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5770 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5771 assert(rp_state_.get());
5772 if (nullptr == rp_state_.get()) return;
5773 const auto tag = cb_context->NextCommandTag(cmd_);
5774 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
5775}
5776
5777SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
5778 const VkSubpassEndInfo *pSubpassEndInfo, const char *name_override)
5779 : SyncOpBase(cmd, name_override) {
5780 if (pSubpassBeginInfo) {
5781 subpass_begin_info_.initialize(pSubpassBeginInfo);
5782 }
5783 if (pSubpassEndInfo) {
5784 subpass_end_info_.initialize(pSubpassEndInfo);
5785 }
5786}
5787
5788bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
5789 bool skip = false;
5790 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5791 if (!renderpass_context) return skip;
5792
5793 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
5794 return skip;
5795}
5796
5797void SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
5798 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5799 cb_context->RecordNextSubpass(cmd_);
5800}
5801
5802SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo,
5803 const char *name_override)
5804 : SyncOpBase(cmd, name_override) {
5805 if (pSubpassEndInfo) {
5806 subpass_end_info_.initialize(pSubpassEndInfo);
5807 }
5808}
5809
5810bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5811 bool skip = false;
5812 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5813
5814 if (!renderpass_context) return skip;
5815 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
5816 return skip;
5817}
5818
5819void SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5820 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5821 cb_context->RecordEndRenderPass(cmd_);
5822}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005823
5824void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5825 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
5826 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
5827 auto *cb_access_context = GetAccessContext(commandBuffer);
5828 assert(cb_access_context);
5829 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5830 auto *context = cb_access_context->GetCurrentAccessContext();
5831 assert(context);
5832
5833 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5834
5835 if (dst_buffer) {
5836 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5837 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
5838 }
5839}
John Zulaufd05c5842021-03-26 11:32:16 -06005840
John Zulaufd0ec59f2021-03-13 14:25:08 -07005841AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
5842 : view_(view), view_mask_(), gen_store_() {
5843 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
5844 const IMAGE_STATE &image_state = *view_->image_state.get();
5845 const auto base_address = ResourceBaseAddress(image_state);
5846 const auto *encoder = image_state.fragment_encoder.get();
5847 if (!encoder) return;
5848 const VkOffset3D zero_offset = {0, 0, 0};
5849 const VkExtent3D &image_extent = image_state.createInfo.extent;
5850 // Intentional copy
5851 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
5852 view_mask_ = subres_range.aspectMask;
5853 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
5854 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5855
5856 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
5857 if (depth && (depth != view_mask_)) {
5858 subres_range.aspectMask = depth;
5859 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5860 }
5861 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
5862 if (stencil && (stencil != view_mask_)) {
5863 subres_range.aspectMask = stencil;
5864 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5865 }
5866}
5867
5868const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
5869 const ImageRangeGen *got = nullptr;
5870 switch (gen_type) {
5871 case kViewSubresource:
5872 got = &gen_store_[kViewSubresource];
5873 break;
5874 case kRenderArea:
5875 got = &gen_store_[kRenderArea];
5876 break;
5877 case kDepthOnlyRenderArea:
5878 got =
5879 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
5880 break;
5881 case kStencilOnlyRenderArea:
5882 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
5883 : &gen_store_[Gen::kStencilOnlyRenderArea];
5884 break;
5885 default:
5886 assert(got);
5887 }
5888 return got;
5889}
5890
5891AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
5892 assert(IsValid());
5893 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
5894 if (depth_op) {
5895 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
5896 if (stencil_op) {
5897 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
5898 return kRenderArea;
5899 }
5900 return kDepthOnlyRenderArea;
5901 }
5902 if (stencil_op) {
5903 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
5904 return kStencilOnlyRenderArea;
5905 }
5906
5907 assert(depth_op || stencil_op);
5908 return kRenderArea;
5909}
5910
5911AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }