blob: 585bdb280d3a8bc00d7bb896b7a640479619eafa [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
John Zulaufd05c5842021-03-26 11:32:16 -060029#ifdef SYNCVAL_DIAGNOSTICS
30struct SyncDiagnostics {
31 void DebugAction() const {
32#if defined(_WIN32)
33 __debugbreak();
34#endif
35 }
36 void Detect(const ResourceAccessRange &range) {
37 std::lock_guard<std::mutex> lock(diag_mutex);
38 if (range.distance() == kConditionValue) {
39 ++condition;
40 DebugAction();
41 }
42 detect_histogram[range.distance()] += 1;
43 }
44 void InstanceDump(VkInstance instance) {
45 std::cout << "# instance handle\n" << instance << "\n";
46 std::cout << "# condition count\n" << condition << "\n";
47 std::cout << "# Detection Size Histogram\n";
48 for (const auto &entry : detect_histogram) {
49 std::cout << "{ " << entry.first << ", " << entry.second << "}\n";
50 }
51 std::cout << std::endl;
52 detect_histogram.clear();
53 }
54 std::map<ResourceAccessRange::index_type, size_t> detect_histogram;
55 uint64_t condition;
56 std::mutex diag_mutex;
57 static const ResourceAccessRangeIndex kConditionValue = ~ResourceAccessRangeIndex(0);
58};
59static SyncDiagnostics sync_diagnostics;
60#endif
61
John Zulauf264cce02021-02-05 14:40:47 -070062static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
63
John Zulauf29d00532021-03-04 13:28:54 -070064static bool SimpleBinding(const IMAGE_STATE &image_state) {
65 bool simple = SimpleBinding(static_cast<const BINDABLE &>(image_state)) || image_state.is_swapchain_image ||
66 (VK_NULL_HANDLE != image_state.bind_swapchain);
67
68 // If it's not simple we must have an encoder.
69 assert(!simple || image_state.fragment_encoder.get());
70 return simple;
71}
72
John Zulauf43cc7462020-12-03 12:33:12 -070073const static std::array<AccessAddressType, static_cast<size_t>(AccessAddressType::kTypeCount)> kAddressTypes = {
74 AccessAddressType::kLinear, AccessAddressType::kIdealized};
75
John Zulaufd5115702021-01-18 12:34:33 -070076static constexpr AccessAddressType GetAccessAddressType(const BUFFER_STATE &) { return AccessAddressType::kLinear; };
John Zulauf264cce02021-02-05 14:40:47 -070077static AccessAddressType GetAccessAddressType(const IMAGE_STATE &image) {
78 return SimpleBinding(image) ? AccessContext::ImageAddressType(image) : AccessAddressType::kIdealized;
79}
John Zulaufd5115702021-01-18 12:34:33 -070080
John Zulauf9cb530d2019-09-30 14:14:10 -060081static const char *string_SyncHazardVUID(SyncHazard hazard) {
82 switch (hazard) {
83 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070084 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060085 break;
86 case SyncHazard::READ_AFTER_WRITE:
87 return "SYNC-HAZARD-READ_AFTER_WRITE";
88 break;
89 case SyncHazard::WRITE_AFTER_READ:
90 return "SYNC-HAZARD-WRITE_AFTER_READ";
91 break;
92 case SyncHazard::WRITE_AFTER_WRITE:
93 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
94 break;
John Zulauf2f952d22020-02-10 11:34:51 -070095 case SyncHazard::READ_RACING_WRITE:
96 return "SYNC-HAZARD-READ-RACING-WRITE";
97 break;
98 case SyncHazard::WRITE_RACING_WRITE:
99 return "SYNC-HAZARD-WRITE-RACING-WRITE";
100 break;
101 case SyncHazard::WRITE_RACING_READ:
102 return "SYNC-HAZARD-WRITE-RACING-READ";
103 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600104 default:
105 assert(0);
106 }
107 return "SYNC-HAZARD-INVALID";
108}
109
John Zulauf59e25072020-07-17 10:55:21 -0600110static bool IsHazardVsRead(SyncHazard hazard) {
111 switch (hazard) {
112 case SyncHazard::NONE:
113 return false;
114 break;
115 case SyncHazard::READ_AFTER_WRITE:
116 return false;
117 break;
118 case SyncHazard::WRITE_AFTER_READ:
119 return true;
120 break;
121 case SyncHazard::WRITE_AFTER_WRITE:
122 return false;
123 break;
124 case SyncHazard::READ_RACING_WRITE:
125 return false;
126 break;
127 case SyncHazard::WRITE_RACING_WRITE:
128 return false;
129 break;
130 case SyncHazard::WRITE_RACING_READ:
131 return true;
132 break;
133 default:
134 assert(0);
135 }
136 return false;
137}
138
John Zulauf9cb530d2019-09-30 14:14:10 -0600139static const char *string_SyncHazard(SyncHazard hazard) {
140 switch (hazard) {
141 case SyncHazard::NONE:
142 return "NONR";
143 break;
144 case SyncHazard::READ_AFTER_WRITE:
145 return "READ_AFTER_WRITE";
146 break;
147 case SyncHazard::WRITE_AFTER_READ:
148 return "WRITE_AFTER_READ";
149 break;
150 case SyncHazard::WRITE_AFTER_WRITE:
151 return "WRITE_AFTER_WRITE";
152 break;
John Zulauf2f952d22020-02-10 11:34:51 -0700153 case SyncHazard::READ_RACING_WRITE:
154 return "READ_RACING_WRITE";
155 break;
156 case SyncHazard::WRITE_RACING_WRITE:
157 return "WRITE_RACING_WRITE";
158 break;
159 case SyncHazard::WRITE_RACING_READ:
160 return "WRITE_RACING_READ";
161 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600162 default:
163 assert(0);
164 }
165 return "INVALID HAZARD";
166}
167
John Zulauf37ceaed2020-07-03 16:18:15 -0600168static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
169 // Return the info for the first bit found
170 const SyncStageAccessInfoType *info = nullptr;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700171 for (size_t i = 0; i < flags.size(); i++) {
172 if (flags.test(i)) {
173 info = &syncStageAccessInfoByStageAccessIndex[i];
174 break;
John Zulauf37ceaed2020-07-03 16:18:15 -0600175 }
176 }
177 return info;
178}
179
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700180static std::string string_SyncStageAccessFlags(const SyncStageAccessFlags &flags, const char *sep = "|") {
John Zulauf59e25072020-07-17 10:55:21 -0600181 std::string out_str;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700182 if (flags.none()) {
John Zulauf389c34b2020-07-28 11:19:35 -0600183 out_str = "0";
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700184 } else {
185 for (size_t i = 0; i < syncStageAccessInfoByStageAccessIndex.size(); i++) {
186 const auto &info = syncStageAccessInfoByStageAccessIndex[i];
187 if ((flags & info.stage_access_bit).any()) {
188 if (!out_str.empty()) {
189 out_str.append(sep);
190 }
191 out_str.append(info.name);
John Zulauf59e25072020-07-17 10:55:21 -0600192 }
John Zulauf59e25072020-07-17 10:55:21 -0600193 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700194 if (out_str.length() == 0) {
195 out_str.append("Unhandled SyncStageAccess");
196 }
John Zulauf59e25072020-07-17 10:55:21 -0600197 }
198 return out_str;
199}
200
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700201static std::string string_UsageTag(const ResourceUsageTag &tag) {
202 std::stringstream out;
203
John Zulauffaea0ee2021-01-14 14:01:32 -0700204 out << "command: " << CommandTypeString(tag.command);
205 out << ", seq_no: " << tag.seq_num;
206 if (tag.sub_command != 0) {
207 out << ", subcmd: " << tag.sub_command;
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700208 }
209 return out.str();
210}
211
John Zulauffaea0ee2021-01-14 14:01:32 -0700212std::string CommandBufferAccessContext::FormatUsage(const HazardResult &hazard) const {
John Zulauf37ceaed2020-07-03 16:18:15 -0600213 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600214 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
215 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600216 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600217 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
218 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf59e25072020-07-17 10:55:21 -0600219 out << "(usage: " << usage_info.name << ", prior_usage: " << stage_access_name;
220 if (IsHazardVsRead(hazard.hazard)) {
221 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
Jeremy Gebben40a22942020-12-22 14:22:06 -0700222 out << ", read_barriers: " << string_VkPipelineStageFlags2KHR(barriers);
John Zulauf59e25072020-07-17 10:55:21 -0600223 } else {
224 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
225 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
226 }
227
John Zulauffaea0ee2021-01-14 14:01:32 -0700228 // PHASE2 TODO -- add comand buffer and reset from secondary if applicable
ZaOniRinku56b86472021-03-23 20:25:05 +0100229 out << ", " << string_UsageTag(tag) << ", reset_no: " << reset_count_ << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600230 return out.str();
231}
232
John Zulaufd14743a2020-07-03 09:42:39 -0600233// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
234// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
235// also reflects this special case for read hazard detection (using access instead of exec scope)
Jeremy Gebben40a22942020-12-22 14:22:06 -0700236static constexpr VkPipelineStageFlags2KHR kColorAttachmentExecScope = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700237static const SyncStageAccessFlags kColorAttachmentAccessScope =
238 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
239 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
240 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
241 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebben40a22942020-12-22 14:22:06 -0700242static constexpr VkPipelineStageFlags2KHR kDepthStencilAttachmentExecScope =
243 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 -0700244static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
245 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
246 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
247 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -0700248static constexpr VkPipelineStageFlags2KHR kRasterAttachmentExecScope = kDepthStencilAttachmentExecScope | kColorAttachmentExecScope;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700249static const SyncStageAccessFlags kRasterAttachmentAccessScope = kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope;
John Zulaufb027cdb2020-05-21 14:25:22 -0600250
John Zulauf8e3c3e92021-01-06 11:19:36 -0700251ResourceAccessState::OrderingBarriers ResourceAccessState::kOrderingRules = {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700252 {{VK_PIPELINE_STAGE_2_NONE_KHR, SyncStageAccessFlags()},
John Zulauf8e3c3e92021-01-06 11:19:36 -0700253 {kColorAttachmentExecScope, kColorAttachmentAccessScope},
254 {kDepthStencilAttachmentExecScope, kDepthStencilAttachmentAccessScope},
255 {kRasterAttachmentExecScope, kRasterAttachmentAccessScope}}};
256
John Zulauf7635de32020-05-29 17:14:15 -0600257// 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 -0700258static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, ResourceUsageTag::kMaxCount,
259 ResourceUsageTag::kMaxCount, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600260
John Zulaufb02c1eb2020-10-06 16:33:36 -0600261static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) {
262 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
263}
John Zulauf29d00532021-03-04 13:28:54 -0700264static VkDeviceSize ResourceBaseAddress(const IMAGE_STATE &image_state) {
265 VkDeviceSize base_address;
266 if (image_state.is_swapchain_image || (VK_NULL_HANDLE != image_state.bind_swapchain)) {
267 base_address = image_state.swapchain_fake_address;
268 } else {
269 base_address = ResourceBaseAddress(static_cast<const BINDABLE &>(image_state));
270 }
271 return base_address;
272}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600273
locke-lunarg3c038002020-04-30 23:08:08 -0600274inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
275 if (size == VK_WHOLE_SIZE) {
276 return (whole_size - offset);
277 }
278 return size;
279}
280
John Zulauf3e86bf02020-09-12 10:47:57 -0600281static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
282 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
283}
284
John Zulauf16adfc92020-04-08 10:28:33 -0600285template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600286static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600287 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
288}
289
John Zulauf355e49b2020-04-24 15:11:15 -0600290static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600291
John Zulauf3e86bf02020-09-12 10:47:57 -0600292static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
293 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
294}
295
296static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
297 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
298}
299
John Zulauf4a6105a2020-11-17 15:11:05 -0700300// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
301//
John Zulauf10f1f522020-12-18 12:00:35 -0700302// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
303//
John Zulauf4a6105a2020-11-17 15:11:05 -0700304// Usage:
305// Constructor() -- initializes the generator to point to the begin of the space declared.
306// * -- the current range of the generator empty signfies end
307// ++ -- advance to the next non-empty range (or end)
308
309// A wrapper for a single range with the same semantics as the actual generators below
310template <typename KeyType>
311class SingleRangeGenerator {
312 public:
313 SingleRangeGenerator(const KeyType &range) : current_(range) {}
John Zulaufd5115702021-01-18 12:34:33 -0700314 const KeyType &operator*() const { return current_; }
315 const KeyType *operator->() const { return &current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700316 SingleRangeGenerator &operator++() {
317 current_ = KeyType(); // just one real range
318 return *this;
319 }
320
321 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
322
323 private:
324 SingleRangeGenerator() = default;
325 const KeyType range_;
326 KeyType current_;
327};
328
329// Generate the ranges that are the intersection of range and the entries in the FilterMap
330template <typename FilterMap, typename KeyType = typename FilterMap::key_type>
331class FilteredRangeGenerator {
332 public:
John Zulaufd5115702021-01-18 12:34:33 -0700333 // Default constructed is safe to dereference for "empty" test, but for no other operation.
334 FilteredRangeGenerator() : range_(), filter_(nullptr), filter_pos_(), current_() {
335 // Default construction for KeyType *must* be empty range
336 assert(current_.empty());
337 }
John Zulauf4a6105a2020-11-17 15:11:05 -0700338 FilteredRangeGenerator(const FilterMap &filter, const KeyType &range)
339 : range_(range), filter_(&filter), filter_pos_(), current_() {
340 SeekBegin();
341 }
John Zulaufd5115702021-01-18 12:34:33 -0700342 FilteredRangeGenerator(const FilteredRangeGenerator &from) = default;
343
John Zulauf4a6105a2020-11-17 15:11:05 -0700344 const KeyType &operator*() const { return current_; }
345 const KeyType *operator->() const { return &current_; }
346 FilteredRangeGenerator &operator++() {
347 ++filter_pos_;
348 UpdateCurrent();
349 return *this;
350 }
351
352 bool operator==(const FilteredRangeGenerator &other) const { return current_ == other.current_; }
353
354 private:
John Zulauf4a6105a2020-11-17 15:11:05 -0700355 void UpdateCurrent() {
356 if (filter_pos_ != filter_->cend()) {
357 current_ = range_ & filter_pos_->first;
358 } else {
359 current_ = KeyType();
360 }
361 }
362 void SeekBegin() {
363 filter_pos_ = filter_->lower_bound(range_);
364 UpdateCurrent();
365 }
366 const KeyType range_;
367 const FilterMap *filter_;
368 typename FilterMap::const_iterator filter_pos_;
369 KeyType current_;
370};
John Zulaufd5115702021-01-18 12:34:33 -0700371using SingleAccessRangeGenerator = SingleRangeGenerator<ResourceAccessRange>;
John Zulauf4a6105a2020-11-17 15:11:05 -0700372using EventSimpleRangeGenerator = FilteredRangeGenerator<SyncEventState::ScopeMap>;
373
374// Templated to allow for different Range generators or map sources...
375
376// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulauf4a6105a2020-11-17 15:11:05 -0700377template <typename FilterMap, typename RangeGen, typename KeyType = typename FilterMap::key_type>
378class FilteredGeneratorGenerator {
379 public:
John Zulaufd5115702021-01-18 12:34:33 -0700380 // Default constructed is safe to dereference for "empty" test, but for no other operation.
381 FilteredGeneratorGenerator() : filter_(nullptr), gen_(), filter_pos_(), current_() {
382 // Default construction for KeyType *must* be empty range
383 assert(current_.empty());
384 }
385 FilteredGeneratorGenerator(const FilterMap &filter, RangeGen &gen) : filter_(&filter), gen_(gen), filter_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700386 SeekBegin();
387 }
John Zulaufd5115702021-01-18 12:34:33 -0700388 FilteredGeneratorGenerator(const FilteredGeneratorGenerator &from) = default;
John Zulauf4a6105a2020-11-17 15:11:05 -0700389 const KeyType &operator*() const { return current_; }
390 const KeyType *operator->() const { return &current_; }
391 FilteredGeneratorGenerator &operator++() {
392 KeyType gen_range = GenRange();
393 KeyType filter_range = FilterRange();
394 current_ = KeyType();
395 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
396 if (gen_range.end > filter_range.end) {
397 // if the generated range is beyond the filter_range, advance the filter range
398 filter_range = AdvanceFilter();
399 } else {
400 gen_range = AdvanceGen();
401 }
402 current_ = gen_range & filter_range;
403 }
404 return *this;
405 }
406
407 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
408
409 private:
410 KeyType AdvanceFilter() {
411 ++filter_pos_;
412 auto filter_range = FilterRange();
413 if (filter_range.valid()) {
414 FastForwardGen(filter_range);
415 }
416 return filter_range;
417 }
418 KeyType AdvanceGen() {
John Zulaufd5115702021-01-18 12:34:33 -0700419 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700420 auto gen_range = GenRange();
421 if (gen_range.valid()) {
422 FastForwardFilter(gen_range);
423 }
424 return gen_range;
425 }
426
427 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
John Zulaufd5115702021-01-18 12:34:33 -0700428 KeyType GenRange() const { return *gen_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700429
430 KeyType FastForwardFilter(const KeyType &range) {
431 auto filter_range = FilterRange();
432 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700433 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700434 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
435 if (retry_count < kRetryLimit) {
436 ++filter_pos_;
437 filter_range = FilterRange();
438 retry_count++;
439 } else {
440 // Okay we've tried walking, do a seek.
441 filter_pos_ = filter_->lower_bound(range);
442 break;
443 }
444 }
445 return FilterRange();
446 }
447
448 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
449 // faster.
450 KeyType FastForwardGen(const KeyType &range) {
451 auto gen_range = GenRange();
452 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
John Zulaufd5115702021-01-18 12:34:33 -0700453 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700454 gen_range = GenRange();
455 }
456 return gen_range;
457 }
458
459 void SeekBegin() {
460 auto gen_range = GenRange();
461 if (gen_range.empty()) {
462 current_ = KeyType();
463 filter_pos_ = filter_->cend();
464 } else {
465 filter_pos_ = filter_->lower_bound(gen_range);
466 current_ = gen_range & FilterRange();
467 }
468 }
469
John Zulauf4a6105a2020-11-17 15:11:05 -0700470 const FilterMap *filter_;
John Zulaufd5115702021-01-18 12:34:33 -0700471 RangeGen gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700472 typename FilterMap::const_iterator filter_pos_;
473 KeyType current_;
474};
475
476using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
477
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700478static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700479
John Zulauf3e86bf02020-09-12 10:47:57 -0600480ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
481 VkDeviceSize stride) {
482 VkDeviceSize range_start = offset + first_index * stride;
483 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600484 if (count == UINT32_MAX) {
485 range_size = buf_whole_size - range_start;
486 } else {
487 range_size = count * stride;
488 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600489 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600490}
491
locke-lunarg654e3692020-06-04 17:19:15 -0600492SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
493 VkShaderStageFlagBits stage_flag) {
494 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
495 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
496 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
497 }
498 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
499 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
500 assert(0);
501 }
502 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
503 return stage_access->second.uniform_read;
504 }
505
506 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
507 // Because if write hazard happens, read hazard might or might not happen.
508 // But if write hazard doesn't happen, read hazard is impossible to happen.
509 if (descriptor_data.is_writable) {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700510 return stage_access->second.storage_write;
locke-lunarg654e3692020-06-04 17:19:15 -0600511 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700512 // TODO: sampled_read
513 return stage_access->second.storage_read;
locke-lunarg654e3692020-06-04 17:19:15 -0600514}
515
locke-lunarg37047832020-06-12 13:44:45 -0600516bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
517 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
518 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
519 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
520 ? true
521 : false;
522}
523
524bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
525 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
526 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
527 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
528 ? true
529 : false;
530}
531
John Zulauf355e49b2020-04-24 15:11:15 -0600532// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600533template <typename Action>
534static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
535 Action &action) {
536 // At this point the "apply over range" logic only supports a single memory binding
537 if (!SimpleBinding(image_state)) return;
538 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600539 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700540 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
541 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600542 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700543 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600544 }
545}
546
John Zulauf7635de32020-05-29 17:14:15 -0600547// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
548// Used by both validation and record operations
549//
550// The signature for Action() reflect the needs of both uses.
551template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700552void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
553 uint32_t subpass) {
John Zulauf7635de32020-05-29 17:14:15 -0600554 const auto &rp_ci = rp_state.createInfo;
555 const auto *attachment_ci = rp_ci.pAttachments;
556 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
557
558 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
559 const auto *color_attachments = subpass_ci.pColorAttachments;
560 const auto *color_resolve = subpass_ci.pResolveAttachments;
561 if (color_resolve && color_attachments) {
562 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
563 const auto &color_attach = color_attachments[i].attachment;
564 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
565 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
566 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700567 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ,
568 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600569 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700570 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
571 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600572 }
573 }
574 }
575
576 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700577 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600578 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
579 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
580 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
581 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
582 const auto src_ci = attachment_ci[src_at];
583 // The formats are required to match so we can pick either
584 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
585 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
586 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
John Zulauf7635de32020-05-29 17:14:15 -0600587
588 // Figure out which aspects are actually touched during resolve operations
589 const char *aspect_string = nullptr;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700590 AttachmentViewGen::Gen gen_type = AttachmentViewGen::Gen::kRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600591 if (resolve_depth && resolve_stencil) {
John Zulauf7635de32020-05-29 17:14:15 -0600592 aspect_string = "depth/stencil";
593 } else if (resolve_depth) {
594 // Validate depth only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700595 gen_type = AttachmentViewGen::Gen::kDepthOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600596 aspect_string = "depth";
597 } else if (resolve_stencil) {
598 // Validate all stencil only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700599 gen_type = AttachmentViewGen::Gen::kStencilOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600600 aspect_string = "stencil";
601 }
602
John Zulaufd0ec59f2021-03-13 14:25:08 -0700603 if (aspect_string) {
604 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at], gen_type,
605 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster);
606 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at], gen_type,
607 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulauf7635de32020-05-29 17:14:15 -0600608 }
609 }
610}
611
612// Action for validating resolve operations
613class ValidateResolveAction {
614 public:
John Zulauffaea0ee2021-01-14 14:01:32 -0700615 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context,
John Zulauf64ffe552021-02-06 10:25:07 -0700616 const CommandExecutionContext &ex_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600617 : render_pass_(render_pass),
618 subpass_(subpass),
619 context_(context),
John Zulauf64ffe552021-02-06 10:25:07 -0700620 ex_context_(ex_context),
John Zulauf7635de32020-05-29 17:14:15 -0600621 func_name_(func_name),
622 skip_(false) {}
623 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 -0700624 const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage,
625 SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600626 HazardResult hazard;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700627 hazard = context_.DetectHazard(view_gen, gen_type, current_usage, ordering_rule);
John Zulauf7635de32020-05-29 17:14:15 -0600628 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -0700629 skip_ |=
John Zulauf64ffe552021-02-06 10:25:07 -0700630 ex_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700631 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
632 " to resolve attachment %" PRIu32 ". Access info %s.",
633 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name,
John Zulauf64ffe552021-02-06 10:25:07 -0700634 attachment_name, src_at, dst_at, ex_context_.FormatUsage(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600635 }
636 }
637 // Providing a mechanism for the constructing caller to get the result of the validation
638 bool GetSkip() const { return skip_; }
639
640 private:
641 VkRenderPass render_pass_;
642 const uint32_t subpass_;
643 const AccessContext &context_;
John Zulauf64ffe552021-02-06 10:25:07 -0700644 const CommandExecutionContext &ex_context_;
John Zulauf7635de32020-05-29 17:14:15 -0600645 const char *func_name_;
646 bool skip_;
647};
648
649// Update action for resolve operations
650class UpdateStateResolveAction {
651 public:
652 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
John Zulaufd0ec59f2021-03-13 14:25:08 -0700653 void operator()(const char *, const char *, uint32_t, uint32_t, const AttachmentViewGen &view_gen,
654 AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600655 // Ignores validation only arguments...
John Zulaufd0ec59f2021-03-13 14:25:08 -0700656 context_.UpdateAccessState(view_gen, gen_type, current_usage, ordering_rule, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600657 }
658
659 private:
660 AccessContext &context_;
661 const ResourceUsageTag &tag_;
662};
663
John Zulauf59e25072020-07-17 10:55:21 -0600664void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700665 const SyncStageAccessFlags &prior_, const ResourceUsageTag &tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600666 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
667 usage_index = usage_index_;
668 hazard = hazard_;
669 prior_access = prior_;
670 tag = tag_;
671}
672
John Zulauf540266b2020-04-06 18:54:53 -0600673AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
674 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600675 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600676 Reset();
677 const auto &subpass_dep = dependencies[subpass];
John Zulauf22aefed2021-03-11 18:14:35 -0700678 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
679 prev_.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
John Zulauf355e49b2020-04-24 15:11:15 -0600680 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600681 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600682 const auto prev_pass = prev_dep.first->pass;
683 const auto &prev_barriers = prev_dep.second;
684 assert(prev_dep.second.size());
685 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
686 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700687 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600688
689 async_.reserve(subpass_dep.async.size());
690 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700691 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600692 }
John Zulauf22aefed2021-03-11 18:14:35 -0700693 if (has_barrier_from_external) {
694 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
695 prev_.emplace_back(external_context, queue_flags, subpass_dep.barrier_from_external);
696 src_external_ = &prev_.back();
John Zulaufe5da6e52020-03-18 15:32:18 -0600697 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600698 if (subpass_dep.barrier_to_external.size()) {
699 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600700 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700701}
702
John Zulauf5f13a792020-03-10 07:31:21 -0600703template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700704HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600705 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600706 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600707 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600708
709 HazardResult hazard;
710 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
711 hazard = detector.Detect(prev);
712 }
713 return hazard;
714}
715
John Zulauf4a6105a2020-11-17 15:11:05 -0700716template <typename Action>
717void AccessContext::ForAll(Action &&action) {
718 for (const auto address_type : kAddressTypes) {
719 auto &accesses = GetAccessStateMap(address_type);
720 for (const auto &access : accesses) {
721 action(address_type, access);
722 }
723 }
724}
725
John Zulauf3d84f1b2020-03-09 13:33:25 -0600726// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
727// the DAG of the contexts (for example subpasses)
728template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700729HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600730 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600731 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600732
John Zulauf1a224292020-06-30 14:52:13 -0600733 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600734 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
735 // so we'll check these first
736 for (const auto &async_context : async_) {
737 hazard = async_context->DetectAsyncHazard(type, detector, range);
738 if (hazard.hazard) return hazard;
739 }
John Zulauf5f13a792020-03-10 07:31:21 -0600740 }
741
John Zulauf1a224292020-06-30 14:52:13 -0600742 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600743
John Zulauf69133422020-05-20 14:55:53 -0600744 const auto &accesses = GetAccessStateMap(type);
745 const auto from = accesses.lower_bound(range);
746 const auto to = accesses.upper_bound(range);
747 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600748
John Zulauf69133422020-05-20 14:55:53 -0600749 for (auto pos = from; pos != to; ++pos) {
750 // Cover any leading gap, or gap between entries
751 if (detect_prev) {
752 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
753 // Cover any leading gap, or gap between entries
754 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600755 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600756 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600757 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600758 if (hazard.hazard) return hazard;
759 }
John Zulauf69133422020-05-20 14:55:53 -0600760 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
761 gap.begin = pos->first.end;
762 }
763
764 hazard = detector.Detect(pos);
765 if (hazard.hazard) return hazard;
766 }
767
768 if (detect_prev) {
769 // Detect in the trailing empty as needed
770 gap.end = range.end;
771 if (gap.non_empty()) {
772 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600773 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600774 }
775
776 return hazard;
777}
778
779// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
780template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700781HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
782 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600783 auto &accesses = GetAccessStateMap(type);
784 const auto from = accesses.lower_bound(range);
785 const auto to = accesses.upper_bound(range);
786
John Zulauf3d84f1b2020-03-09 13:33:25 -0600787 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600788 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700789 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600790 }
John Zulauf16adfc92020-04-08 10:28:33 -0600791
John Zulauf3d84f1b2020-03-09 13:33:25 -0600792 return hazard;
793}
794
John Zulaufb02c1eb2020-10-06 16:33:36 -0600795struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700796 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600797 void operator()(ResourceAccessState *access) const {
798 assert(access);
799 access->ApplyBarriers(barriers, true);
800 }
801 const std::vector<SyncBarrier> &barriers;
802};
803
John Zulauf22aefed2021-03-11 18:14:35 -0700804struct ApplyTrackbackStackAction {
805 explicit ApplyTrackbackStackAction(const std::vector<SyncBarrier> &barriers_,
806 const ResourceAccessStateFunction *previous_barrier_ = nullptr)
807 : barriers(barriers_), previous_barrier(previous_barrier_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600808 void operator()(ResourceAccessState *access) const {
809 assert(access);
810 assert(!access->HasPendingState());
811 access->ApplyBarriers(barriers, false);
812 access->ApplyPendingBarriers(kCurrentCommandTag);
John Zulauf22aefed2021-03-11 18:14:35 -0700813 if (previous_barrier) {
814 assert(bool(*previous_barrier));
815 (*previous_barrier)(access);
816 }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600817 }
818 const std::vector<SyncBarrier> &barriers;
John Zulauf22aefed2021-03-11 18:14:35 -0700819 const ResourceAccessStateFunction *previous_barrier;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600820};
821
822// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
823// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
824// *different* map from dest.
825// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
826// range [first, last)
827template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600828static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
829 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600830 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600831 auto at = entry;
832 for (auto pos = first; pos != last; ++pos) {
833 // Every member of the input iterator range must fit within the remaining portion of entry
834 assert(at->first.includes(pos->first));
835 assert(at != dest->end());
836 // Trim up at to the same size as the entry to resolve
837 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600838 auto access = pos->second; // intentional copy
839 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600840 at->second.Resolve(access);
841 ++at; // Go to the remaining unused section of entry
842 }
843}
844
John Zulaufa0a98292020-09-18 09:30:10 -0600845static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
846 SyncBarrier merged = {};
847 for (const auto &barrier : barriers) {
848 merged.Merge(barrier);
849 }
850 return merged;
851}
852
John Zulaufb02c1eb2020-10-06 16:33:36 -0600853template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700854void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600855 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
856 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600857 if (!range.non_empty()) return;
858
John Zulauf355e49b2020-04-24 15:11:15 -0600859 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
860 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600861 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600862 if (current->pos_B->valid) {
863 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600864 auto access = src_pos->second; // intentional copy
865 barrier_action(&access);
866
John Zulauf16adfc92020-04-08 10:28:33 -0600867 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600868 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
869 trimmed->second.Resolve(access);
870 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600871 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600872 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600873 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600874 }
John Zulauf16adfc92020-04-08 10:28:33 -0600875 } else {
876 // we have to descend to fill this gap
877 if (recur_to_infill) {
John Zulauf22aefed2021-03-11 18:14:35 -0700878 ResourceAccessRange recurrence_range = current_range;
879 // The current context is empty for the current range, so recur to fill the gap.
880 // Since we will be recurring back up the DAG, expand the gap descent to cover the full range for which B
881 // is not valid, to minimize that recurrence
882 if (current->pos_B.at_end()) {
883 // Do the remainder here....
884 recurrence_range.end = range.end;
John Zulauf355e49b2020-04-24 15:11:15 -0600885 } else {
John Zulauf22aefed2021-03-11 18:14:35 -0700886 // Recur only over the range until B becomes valid (within the limits of range).
887 recurrence_range.end = std::min(range.end, current->pos_B->lower_bound->first.begin);
John Zulauf355e49b2020-04-24 15:11:15 -0600888 }
John Zulauf22aefed2021-03-11 18:14:35 -0700889 ResolvePreviousAccessStack(type, recurrence_range, resolve_map, infill_state, barrier_action);
890
John Zulauf355e49b2020-04-24 15:11:15 -0600891 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
892 // iterator of the outer while.
893
894 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
895 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
896 // we stepped on the dest map
John Zulauf22aefed2021-03-11 18:14:35 -0700897 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 -0600898 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600899 current.seek(seek_to);
900 } else if (!current->pos_A->valid && infill_state) {
901 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
902 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
903 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600904 }
John Zulauf5f13a792020-03-10 07:31:21 -0600905 }
John Zulauf16adfc92020-04-08 10:28:33 -0600906 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600907 }
John Zulauf1a224292020-06-30 14:52:13 -0600908
909 // Infill if range goes passed both the current and resolve map prior contents
910 if (recur_to_infill && (current->range.end < range.end)) {
911 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
John Zulauf22aefed2021-03-11 18:14:35 -0700912 ResolvePreviousAccessStack<BarrierAction>(type, trailing_fill_range, resolve_map, infill_state, barrier_action);
John Zulauf1a224292020-06-30 14:52:13 -0600913 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600914}
915
John Zulauf22aefed2021-03-11 18:14:35 -0700916template <typename BarrierAction>
917void AccessContext::ResolvePreviousAccessStack(AccessAddressType type, const ResourceAccessRange &range,
918 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
919 const BarrierAction &previous_barrier) const {
920 ResourceAccessStateFunction stacked_barrier(std::ref(previous_barrier));
921 ResolvePreviousAccess(type, range, descent_map, infill_state, &stacked_barrier);
922}
923
John Zulauf43cc7462020-12-03 12:33:12 -0700924void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
John Zulauf22aefed2021-03-11 18:14:35 -0700925 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
926 const ResourceAccessStateFunction *previous_barrier) const {
927 if (prev_.size() == 0) {
John Zulauf5f13a792020-03-10 07:31:21 -0600928 if (range.non_empty() && infill_state) {
John Zulauf22aefed2021-03-11 18:14:35 -0700929 // Fill the empty poritions of descent_map with the default_state with the barrier function applied (iff present)
930 ResourceAccessState state_copy;
931 if (previous_barrier) {
932 assert(bool(*previous_barrier));
933 state_copy = *infill_state;
934 (*previous_barrier)(&state_copy);
935 infill_state = &state_copy;
936 }
937 sparse_container::update_range_value(*descent_map, range, *infill_state,
938 sparse_container::value_precedence::prefer_dest);
John Zulauf5f13a792020-03-10 07:31:21 -0600939 }
940 } else {
941 // Look for something to fill the gap further along.
942 for (const auto &prev_dep : prev_) {
John Zulauf22aefed2021-03-11 18:14:35 -0700943 const ApplyTrackbackStackAction barrier_action(prev_dep.barriers, previous_barrier);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600944 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600945 }
John Zulauf5f13a792020-03-10 07:31:21 -0600946 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600947}
948
John Zulauf4a6105a2020-11-17 15:11:05 -0700949// Non-lazy import of all accesses, WaitEvents needs this.
950void AccessContext::ResolvePreviousAccesses() {
951 ResourceAccessState default_state;
John Zulauf22aefed2021-03-11 18:14:35 -0700952 if (!prev_.size()) return; // If no previous contexts, nothing to do
953
John Zulauf4a6105a2020-11-17 15:11:05 -0700954 for (const auto address_type : kAddressTypes) {
955 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
956 }
957}
958
John Zulauf43cc7462020-12-03 12:33:12 -0700959AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
960 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -0600961}
962
John Zulauf1507ee42020-05-18 11:33:09 -0600963static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
964 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
965 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
966 return stage_access;
967}
968static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
969 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
970 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
971 return stage_access;
972}
973
John Zulauf7635de32020-05-29 17:14:15 -0600974// Caller must manage returned pointer
975static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -0700976 uint32_t subpass, const AttachmentViewGenVector &attachment_views) {
John Zulauf7635de32020-05-29 17:14:15 -0600977 auto *proxy = new AccessContext(context);
John Zulaufd0ec59f2021-03-13 14:25:08 -0700978 proxy->UpdateAttachmentResolveAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
979 proxy->UpdateAttachmentStoreAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600980 return proxy;
981}
982
John Zulaufb02c1eb2020-10-06 16:33:36 -0600983template <typename BarrierAction>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700984void AccessContext::ResolveAccessRange(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
985 BarrierAction &barrier_action, ResourceAccessRangeMap *descent_map,
986 const ResourceAccessState *infill_state) const {
987 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
988 if (!attachment_gen) return;
989
990 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
991 const AccessAddressType address_type = view_gen.GetAddressType();
992 for (; range_gen->non_empty(); ++range_gen) {
993 ResolveAccessRange(address_type, *range_gen, barrier_action, descent_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600994 }
John Zulauf62f10592020-04-03 12:20:02 -0600995}
996
John Zulauf7635de32020-05-29 17:14:15 -0600997// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf64ffe552021-02-06 10:25:07 -0700998bool AccessContext::ValidateLayoutTransitions(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600999 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001000 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001001 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -06001002 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
1003 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
1004 // those affects have not been recorded yet.
1005 //
1006 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1007 // to apply and only copy then, if this proves a hot spot.
1008 std::unique_ptr<AccessContext> proxy_for_prev;
1009 TrackBack proxy_track_back;
1010
John Zulauf355e49b2020-04-24 15:11:15 -06001011 const auto &transitions = rp_state.subpass_transitions[subpass];
1012 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -06001013 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
1014
1015 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
John Zulauf22aefed2021-03-11 18:14:35 -07001016 assert(track_back);
John Zulauf7635de32020-05-29 17:14:15 -06001017 if (prev_needs_proxy) {
1018 if (!proxy_for_prev) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001019 proxy_for_prev.reset(
1020 CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass, attachment_views));
John Zulauf7635de32020-05-29 17:14:15 -06001021 proxy_track_back = *track_back;
1022 proxy_track_back.context = proxy_for_prev.get();
1023 }
1024 track_back = &proxy_track_back;
1025 }
1026 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -06001027 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07001028 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001029 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1030 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
1031 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1032 string_VkImageLayout(transition.old_layout),
1033 string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07001034 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001035 }
1036 }
1037 return skip;
1038}
1039
John Zulauf64ffe552021-02-06 10:25:07 -07001040bool AccessContext::ValidateLoadOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001041 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001042 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001043 bool skip = false;
1044 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufa0a98292020-09-18 09:30:10 -06001045
John Zulauf1507ee42020-05-18 11:33:09 -06001046 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1047 if (subpass == rp_state.attachment_first_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001048 const auto &view_gen = attachment_views[i];
1049 if (!view_gen.IsValid()) continue;
John Zulauf1507ee42020-05-18 11:33:09 -06001050 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001051
1052 // Need check in the following way
1053 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1054 // vs. transition
1055 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1056 // for each aspect loaded.
1057
1058 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001059 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001060 const bool is_color = !(has_depth || has_stencil);
1061
1062 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001063 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001064
John Zulaufaff20662020-06-01 14:07:58 -06001065 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001066 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001067
John Zulaufb02c1eb2020-10-06 16:33:36 -06001068 bool checked_stencil = false;
1069 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001070 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea, load_index, SyncOrdering::kColorAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001071 aspect = "color";
1072 } else {
1073 if (has_depth) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001074 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_index,
1075 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001076 aspect = "depth";
1077 }
1078 if (!hazard.hazard && has_stencil) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001079 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, stencil_load_index,
1080 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001081 aspect = "stencil";
1082 checked_stencil = true;
1083 }
1084 }
1085
1086 if (hazard.hazard) {
1087 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001088 const auto &sync_state = ex_context.GetSyncState();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001089 if (hazard.tag == kCurrentCommandTag) {
1090 // Hazard vs. ILT
1091 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1092 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1093 " aspect %s during load with loadOp %s.",
1094 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1095 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06001096 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1097 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001098 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001099 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf64ffe552021-02-06 10:25:07 -07001100 ex_context.FormatUsage(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001101 }
1102 }
1103 }
1104 }
1105 return skip;
1106}
1107
John Zulaufaff20662020-06-01 14:07:58 -06001108// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1109// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1110// store is part of the same Next/End operation.
1111// The latter is handled in layout transistion validation directly
John Zulauf64ffe552021-02-06 10:25:07 -07001112bool AccessContext::ValidateStoreOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufaff20662020-06-01 14:07:58 -06001113 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001114 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001115 bool skip = false;
1116 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001117
1118 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1119 if (subpass == rp_state.attachment_last_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001120 const AttachmentViewGen &view_gen = attachment_views[i];
1121 if (!view_gen.IsValid()) continue;
John Zulaufaff20662020-06-01 14:07:58 -06001122 const auto &ci = attachment_ci[i];
1123
1124 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1125 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1126 // sake, we treat DONT_CARE as writing.
1127 const bool has_depth = FormatHasDepth(ci.format);
1128 const bool has_stencil = FormatHasStencil(ci.format);
1129 const bool is_color = !(has_depth || has_stencil);
1130 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1131 if (!has_stencil && !store_op_stores) continue;
1132
1133 HazardResult hazard;
1134 const char *aspect = nullptr;
1135 bool checked_stencil = false;
1136 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001137 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
1138 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001139 aspect = "color";
1140 } else {
1141 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
John Zulaufaff20662020-06-01 14:07:58 -06001142 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001143 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1144 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001145 aspect = "depth";
1146 }
1147 if (!hazard.hazard && has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001148 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1149 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001150 aspect = "stencil";
1151 checked_stencil = true;
1152 }
1153 }
1154
1155 if (hazard.hazard) {
1156 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1157 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001158 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001159 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1160 " %s aspect during store with %s %s. Access info %s",
1161 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect,
John Zulauf64ffe552021-02-06 10:25:07 -07001162 op_type_string, store_op_string, ex_context.FormatUsage(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001163 }
1164 }
1165 }
1166 return skip;
1167}
1168
John Zulauf64ffe552021-02-06 10:25:07 -07001169bool AccessContext::ValidateResolveOperations(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001170 const VkRect2D &render_area, const AttachmentViewGenVector &attachment_views,
1171 const char *func_name, uint32_t subpass) const {
John Zulauf64ffe552021-02-06 10:25:07 -07001172 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, ex_context, func_name);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001173 ResolveOperation(validate_action, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001174 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001175}
1176
John Zulauf3d84f1b2020-03-09 13:33:25 -06001177class HazardDetector {
1178 SyncStageAccessIndex usage_index_;
1179
1180 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001181 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001182 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1183 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001184 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001185 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001186};
1187
John Zulauf69133422020-05-20 14:55:53 -06001188class HazardDetectorWithOrdering {
1189 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001190 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001191
1192 public:
1193 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001194 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001195 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001196 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1197 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001198 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001199 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001200};
1201
John Zulauf16adfc92020-04-08 10:28:33 -06001202HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001203 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001204 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001205 const auto base_address = ResourceBaseAddress(buffer);
1206 HazardDetector detector(usage_index);
1207 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001208}
1209
John Zulauf69133422020-05-20 14:55:53 -06001210template <typename Detector>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001211HazardResult AccessContext::DetectHazard(Detector &detector, const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1212 DetectOptions options) const {
1213 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1214 if (!attachment_gen) return HazardResult();
1215
1216 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1217 const auto address_type = view_gen.GetAddressType();
1218 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf110413c2021-03-20 05:38:38 -06001219#ifdef SYNCVAL_DIAGNOSTICS
1220 sync_diagnostics.Detect(*range_gen);
1221#endif
John Zulaufd0ec59f2021-03-13 14:25:08 -07001222 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1223 if (hazard.hazard) return hazard;
1224 }
1225
1226 return HazardResult();
1227}
1228
1229template <typename Detector>
John Zulauf69133422020-05-20 14:55:53 -06001230HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1231 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1232 const VkExtent3D &extent, DetectOptions options) const {
1233 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001234 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001235 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1236 base_address);
1237 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001238 for (; range_gen->non_empty(); ++range_gen) {
John Zulaufd05c5842021-03-26 11:32:16 -06001239#ifdef SYNCVAL_DIAGNOSTICS
1240 sync_diagnostics.Detect(*range_gen);
1241#endif
John Zulauf150e5332020-12-03 08:52:52 -07001242 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001243 if (hazard.hazard) return hazard;
1244 }
1245 return HazardResult();
1246}
John Zulauf110413c2021-03-20 05:38:38 -06001247template <typename Detector>
1248HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1249 const VkImageSubresourceRange &subresource_range, DetectOptions options) const {
1250 if (!SimpleBinding(image)) return HazardResult();
1251 const auto base_address = ResourceBaseAddress(image);
1252 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1253 const auto address_type = ImageAddressType(image);
1254 for (; range_gen->non_empty(); ++range_gen) {
1255#ifdef SYNCVAL_DIAGNOSTICS
1256 sync_diagnostics.Detect(*range_gen);
1257#endif
1258 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1259 if (hazard.hazard) return hazard;
1260 }
1261 return HazardResult();
1262}
John Zulauf69133422020-05-20 14:55:53 -06001263
John Zulauf540266b2020-04-06 18:54:53 -06001264HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1265 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1266 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001267 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1268 subresource.layerCount};
John Zulauf110413c2021-03-20 05:38:38 -06001269 HazardDetector detector(current_usage);
1270 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf1507ee42020-05-18 11:33:09 -06001271}
1272
1273HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf110413c2021-03-20 05:38:38 -06001274 const VkImageSubresourceRange &subresource_range) const {
John Zulauf69133422020-05-20 14:55:53 -06001275 HazardDetector detector(current_usage);
John Zulauf110413c2021-03-20 05:38:38 -06001276 return DetectHazard(detector, image, subresource_range, DetectOptions::kDetectAll);
John Zulauf69133422020-05-20 14:55:53 -06001277}
1278
John Zulaufd0ec59f2021-03-13 14:25:08 -07001279HazardResult AccessContext::DetectHazard(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1280 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) const {
1281 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
1282 return DetectHazard(detector, view_gen, gen_type, DetectOptions::kDetectAll);
1283}
1284
John Zulauf69133422020-05-20 14:55:53 -06001285HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001286 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001287 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001288 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001289 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001290}
1291
John Zulauf3d84f1b2020-03-09 13:33:25 -06001292class BarrierHazardDetector {
1293 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001294 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001295 SyncStageAccessFlags src_access_scope)
1296 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1297
John Zulauf5f13a792020-03-10 07:31:21 -06001298 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1299 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001300 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001301 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001302 // 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 -07001303 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001304 }
1305
1306 private:
1307 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001308 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001309 SyncStageAccessFlags src_access_scope_;
1310};
1311
John Zulauf4a6105a2020-11-17 15:11:05 -07001312class EventBarrierHazardDetector {
1313 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001314 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001315 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
1316 const ResourceUsageTag &scope_tag)
1317 : usage_index_(usage_index),
1318 src_exec_scope_(src_exec_scope),
1319 src_access_scope_(src_access_scope),
1320 event_scope_(event_scope),
1321 scope_pos_(event_scope.cbegin()),
1322 scope_end_(event_scope.cend()),
1323 scope_tag_(scope_tag) {}
1324
1325 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1326 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1327 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1328 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1329 if (scope_pos_ == scope_end_) return HazardResult();
1330 if (!scope_pos_->first.intersects(pos->first)) {
1331 event_scope_.lower_bound(pos->first);
1332 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1333 }
1334
1335 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1336 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1337 }
1338 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1339 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1340 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1341 }
1342
1343 private:
1344 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001345 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001346 SyncStageAccessFlags src_access_scope_;
1347 const SyncEventState::ScopeMap &event_scope_;
1348 SyncEventState::ScopeMap::const_iterator scope_pos_;
1349 SyncEventState::ScopeMap::const_iterator scope_end_;
1350 const ResourceUsageTag &scope_tag_;
1351};
1352
Jeremy Gebben40a22942020-12-22 14:22:06 -07001353HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001354 const SyncStageAccessFlags &src_access_scope,
1355 const VkImageSubresourceRange &subresource_range,
1356 const SyncEventState &sync_event, DetectOptions options) const {
1357 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1358 // first access scope map to use, and there's no easy way to plumb it in below.
1359 const auto address_type = ImageAddressType(image);
1360 const auto &event_scope = sync_event.FirstScope(address_type);
1361
1362 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1363 event_scope, sync_event.first_scope_tag);
John Zulauf110413c2021-03-20 05:38:38 -06001364 return DetectHazard(detector, image, subresource_range, options);
John Zulauf4a6105a2020-11-17 15:11:05 -07001365}
1366
John Zulaufd0ec59f2021-03-13 14:25:08 -07001367HazardResult AccessContext::DetectImageBarrierHazard(const AttachmentViewGen &view_gen, const SyncBarrier &barrier,
1368 DetectOptions options) const {
1369 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, barrier.src_exec_scope.exec_scope,
1370 barrier.src_access_scope);
1371 return DetectHazard(detector, view_gen, AttachmentViewGen::Gen::kViewSubresource, options);
1372}
1373
Jeremy Gebben40a22942020-12-22 14:22:06 -07001374HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001375 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001376 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001377 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001378 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
John Zulauf110413c2021-03-20 05:38:38 -06001379 return DetectHazard(detector, image, subresource_range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001380}
1381
Jeremy Gebben40a22942020-12-22 14:22:06 -07001382HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001383 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001384 const VkImageMemoryBarrier &barrier) const {
1385 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1386 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1387 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1388}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001389HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07001390 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope.exec_scope,
John Zulauf110413c2021-03-20 05:38:38 -06001391 image_barrier.barrier.src_access_scope, image_barrier.range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001392}
John Zulauf355e49b2020-04-24 15:11:15 -06001393
John Zulauf9cb530d2019-09-30 14:14:10 -06001394template <typename Flags, typename Map>
1395SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1396 SyncStageAccessFlags scope = 0;
1397 for (const auto &bit_scope : map) {
1398 if (flag_mask < bit_scope.first) break;
1399
1400 if (flag_mask & bit_scope.first) {
1401 scope |= bit_scope.second;
1402 }
1403 }
1404 return scope;
1405}
1406
Jeremy Gebben40a22942020-12-22 14:22:06 -07001407SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags2KHR stages) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001408 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1409}
1410
Jeremy Gebben40a22942020-12-22 14:22:06 -07001411SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags2KHR accesses) {
1412 return AccessScopeImpl(sync_utils::ExpandAccessFlags(accesses), syncStageAccessMaskByAccessBit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001413}
1414
Jeremy Gebben40a22942020-12-22 14:22:06 -07001415// Getting from stage mask and access mask to stage/access masks is something we need to be good at...
1416SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001417 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1418 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1419 // 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 -06001420 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1421}
1422
1423template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001424void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001425 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1426 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001427 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001428 auto pos = accesses->lower_bound(range);
1429 if (pos == accesses->end() || !pos->first.intersects(range)) {
1430 // The range is empty, fill it with a default value.
1431 pos = action.Infill(accesses, pos, range);
1432 } else if (range.begin < pos->first.begin) {
1433 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001434 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001435 } else if (pos->first.begin < range.begin) {
1436 // Trim the beginning if needed
1437 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1438 ++pos;
1439 }
1440
1441 const auto the_end = accesses->end();
1442 while ((pos != the_end) && pos->first.intersects(range)) {
1443 if (pos->first.end > range.end) {
1444 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1445 }
1446
1447 pos = action(accesses, pos);
1448 if (pos == the_end) break;
1449
1450 auto next = pos;
1451 ++next;
1452 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1453 // Need to infill if next is disjoint
1454 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001455 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001456 next = action.Infill(accesses, next, new_range);
1457 }
1458 pos = next;
1459 }
1460}
John Zulaufd5115702021-01-18 12:34:33 -07001461
1462// Give a comparable interface for range generators and ranges
1463template <typename Action>
1464inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1465 assert(range);
1466 UpdateMemoryAccessState(accesses, *range, action);
1467}
1468
John Zulauf4a6105a2020-11-17 15:11:05 -07001469template <typename Action, typename RangeGen>
1470void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1471 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001472 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 -07001473 for (; range_gen->non_empty(); ++range_gen) {
1474 UpdateMemoryAccessState(accesses, *range_gen, action);
1475 }
1476}
John Zulauf9cb530d2019-09-30 14:14:10 -06001477
John Zulaufd0ec59f2021-03-13 14:25:08 -07001478template <typename Action, typename RangeGen>
1479void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, const RangeGen &range_gen_prebuilt) {
1480 RangeGen range_gen(range_gen_prebuilt); // RangeGenerators can be expensive to create from scratch... initialize from built
1481 for (; range_gen->non_empty(); ++range_gen) {
1482 UpdateMemoryAccessState(accesses, *range_gen, action);
1483 }
1484}
John Zulauf9cb530d2019-09-30 14:14:10 -06001485struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001486 using Iterator = ResourceAccessRangeMap::iterator;
1487 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001488 // this is only called on gaps, and never returns a gap.
1489 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001490 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001491 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001492 }
John Zulauf5f13a792020-03-10 07:31:21 -06001493
John Zulauf5c5e88d2019-12-26 11:22:02 -07001494 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001495 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001496 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001497 return pos;
1498 }
1499
John Zulauf43cc7462020-12-03 12:33:12 -07001500 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001501 SyncOrdering ordering_rule_, const ResourceUsageTag &tag_)
1502 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001503 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001504 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001505 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001506 const SyncOrdering ordering_rule;
John Zulauf9cb530d2019-09-30 14:14:10 -06001507 const ResourceUsageTag &tag;
1508};
1509
John Zulauf4a6105a2020-11-17 15:11:05 -07001510// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001511struct PipelineBarrierOp {
1512 SyncBarrier barrier;
1513 bool layout_transition;
1514 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1515 : barrier(barrier_), layout_transition(layout_transition_) {}
1516 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001517 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001518 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1519};
John Zulauf4a6105a2020-11-17 15:11:05 -07001520// The barrier operation for wait events
1521struct WaitEventBarrierOp {
1522 const ResourceUsageTag *scope_tag;
1523 SyncBarrier barrier;
1524 bool layout_transition;
1525 WaitEventBarrierOp(const ResourceUsageTag &scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1526 : scope_tag(&scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
1527 WaitEventBarrierOp() = default;
1528 void operator()(ResourceAccessState *access_state) const {
1529 assert(scope_tag); // Not valid to have a non-scope op executed, default construct included for std::vector support
1530 access_state->ApplyBarrier(*scope_tag, barrier, layout_transition);
1531 }
1532};
John Zulauf1e331ec2020-12-04 18:29:38 -07001533
John Zulauf4a6105a2020-11-17 15:11:05 -07001534// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1535// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1536// of a collection is known/present.
John Zulauf1e331ec2020-12-04 18:29:38 -07001537template <typename BarrierOp>
John Zulauf89311b42020-09-29 16:28:47 -06001538class ApplyBarrierOpsFunctor {
1539 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001540 using Iterator = ResourceAccessRangeMap::iterator;
1541 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001542
John Zulauf5c5e88d2019-12-26 11:22:02 -07001543 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001544 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001545 for (const auto &op : barrier_ops_) {
1546 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001547 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001548
John Zulauf89311b42020-09-29 16:28:47 -06001549 if (resolve_) {
1550 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1551 // another walk
1552 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001553 }
1554 return pos;
1555 }
1556
John Zulauf89311b42020-09-29 16:28:47 -06001557 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulaufd5115702021-01-18 12:34:33 -07001558 ApplyBarrierOpsFunctor(bool resolve, size_t size_hint, const ResourceUsageTag &tag)
1559 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1560 barrier_ops_.reserve(size_hint);
1561 }
1562 void EmplaceBack(const BarrierOp &op) { barrier_ops_.emplace_back(op); }
John Zulauf89311b42020-09-29 16:28:47 -06001563
1564 private:
1565 bool resolve_;
John Zulaufd5115702021-01-18 12:34:33 -07001566 std::vector<BarrierOp> barrier_ops_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001567 const ResourceUsageTag &tag_;
1568};
1569
John Zulauf4a6105a2020-11-17 15:11:05 -07001570// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1571// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1572template <typename BarrierOp>
1573class ApplyBarrierFunctor {
1574 public:
1575 using Iterator = ResourceAccessRangeMap::iterator;
1576 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1577
1578 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1579 auto &access_state = pos->second;
1580 barrier_op_(&access_state);
1581 return pos;
1582 }
1583
1584 ApplyBarrierFunctor(const BarrierOp &barrier_op) : barrier_op_(barrier_op) {}
1585
1586 private:
John Zulaufd5115702021-01-18 12:34:33 -07001587 BarrierOp barrier_op_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001588};
1589
John Zulauf1e331ec2020-12-04 18:29:38 -07001590// This functor resolves the pendinging state.
1591class ResolvePendingBarrierFunctor {
1592 public:
1593 using Iterator = ResourceAccessRangeMap::iterator;
1594 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1595
1596 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1597 auto &access_state = pos->second;
1598 access_state.ApplyPendingBarriers(tag_);
1599 return pos;
1600 }
1601
1602 ResolvePendingBarrierFunctor(const ResourceUsageTag &tag) : tag_(tag) {}
1603
1604 private:
John Zulauf89311b42020-09-29 16:28:47 -06001605 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001606};
1607
John Zulauf8e3c3e92021-01-06 11:19:36 -07001608void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
1609 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
1610 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001611 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001612}
1613
John Zulauf8e3c3e92021-01-06 11:19:36 -07001614void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001615 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001616 if (!SimpleBinding(buffer)) return;
1617 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001618 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001619}
John Zulauf355e49b2020-04-24 15:11:15 -06001620
John Zulauf8e3c3e92021-01-06 11:19:36 -07001621void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf110413c2021-03-20 05:38:38 -06001622 const VkImageSubresourceRange &subresource_range, const ResourceUsageTag &tag) {
1623 if (!SimpleBinding(image)) return;
1624 const auto base_address = ResourceBaseAddress(image);
1625 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1626 const auto address_type = ImageAddressType(image);
1627 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1628 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
1629}
1630void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001631 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001632 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001633 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001634 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001635 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1636 base_address);
1637 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001638 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf110413c2021-03-20 05:38:38 -06001639 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001640}
John Zulaufd0ec59f2021-03-13 14:25:08 -07001641
1642void AccessContext::UpdateAccessState(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1643 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule, const ResourceUsageTag &tag) {
1644 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1645 if (!gen) return;
1646 subresource_adapter::ImageRangeGenerator range_gen(*gen);
1647 const auto address_type = view_gen.GetAddressType();
1648 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1649 ApplyUpdateAction(address_type, action, &range_gen);
John Zulauf7635de32020-05-29 17:14:15 -06001650}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001651
John Zulauf8e3c3e92021-01-06 11:19:36 -07001652void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001653 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1654 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001655 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1656 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001657 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001658}
1659
John Zulaufd0ec59f2021-03-13 14:25:08 -07001660template <typename Action, typename RangeGen>
1661void AccessContext::ApplyUpdateAction(AccessAddressType address_type, const Action &action, RangeGen *range_gen_arg) {
1662 assert(range_gen_arg); // Old Google C++ styleguide require non-const object pass by * not &, but this isn't an optional arg.
1663 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, range_gen_arg);
John Zulauf540266b2020-04-06 18:54:53 -06001664}
1665
1666template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001667void AccessContext::ApplyUpdateAction(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, const Action &action) {
1668 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1669 if (!gen) return;
1670 UpdateMemoryAccessState(&GetAccessStateMap(view_gen.GetAddressType()), action, *gen);
John Zulauf540266b2020-04-06 18:54:53 -06001671}
1672
John Zulaufd0ec59f2021-03-13 14:25:08 -07001673void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state,
1674 const AttachmentViewGenVector &attachment_views, uint32_t subpass,
John Zulauf7635de32020-05-29 17:14:15 -06001675 const ResourceUsageTag &tag) {
1676 UpdateStateResolveAction update(*this, tag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001677 ResolveOperation(update, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001678}
1679
John Zulaufd0ec59f2021-03-13 14:25:08 -07001680void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
1681 uint32_t subpass, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001682 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001683
1684 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1685 if (rp_state.attachment_last_subpass[i] == subpass) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001686 const auto &view_gen = attachment_views[i];
1687 if (!view_gen.IsValid()) continue; // UNUSED
John Zulaufaff20662020-06-01 14:07:58 -06001688
1689 const auto &ci = attachment_ci[i];
1690 const bool has_depth = FormatHasDepth(ci.format);
1691 const bool has_stencil = FormatHasStencil(ci.format);
1692 const bool is_color = !(has_depth || has_stencil);
1693 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1694
1695 if (is_color && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001696 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
1697 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001698 } else {
John Zulaufaff20662020-06-01 14:07:58 -06001699 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001700 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1701 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001702 }
1703 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1704 if (has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001705 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1706 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001707 }
1708 }
1709 }
1710 }
1711}
1712
John Zulauf540266b2020-04-06 18:54:53 -06001713template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001714void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001715 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001716 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001717 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001718 }
1719}
1720
1721void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001722 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1723 auto &context = contexts[subpass_index];
John Zulauf22aefed2021-03-11 18:14:35 -07001724 ApplyTrackbackStackAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001725 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001726 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001727 }
1728 }
1729}
1730
John Zulauf355e49b2020-04-24 15:11:15 -06001731// Suitable only for *subpass* access contexts
John Zulaufd0ec59f2021-03-13 14:25:08 -07001732HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const AttachmentViewGen &attach_view) const {
1733 if (!attach_view.IsValid()) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001734
John Zulauf355e49b2020-04-24 15:11:15 -06001735 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001736 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001737
1738 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001739 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1740 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001741 HazardResult hazard = track_back.context->DetectImageBarrierHazard(attach_view, merged_barrier, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001742 if (!hazard.hazard) {
1743 // The Async hazard check is against the current context's async set.
John Zulaufd0ec59f2021-03-13 14:25:08 -07001744 hazard = DetectImageBarrierHazard(attach_view, merged_barrier, kDetectAsync);
John Zulauf355e49b2020-04-24 15:11:15 -06001745 }
John Zulaufa0a98292020-09-18 09:30:10 -06001746
John Zulauf355e49b2020-04-24 15:11:15 -06001747 return hazard;
1748}
1749
John Zulaufb02c1eb2020-10-06 16:33:36 -06001750void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001751 const AttachmentViewGenVector &attachment_views, const ResourceUsageTag &tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001752 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001753 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001754 for (const auto &transition : transitions) {
1755 const auto prev_pass = transition.prev_pass;
John Zulaufd0ec59f2021-03-13 14:25:08 -07001756 const auto &view_gen = attachment_views[transition.attachment];
1757 if (!view_gen.IsValid()) continue;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001758
1759 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1760 assert(trackback);
1761
1762 // Import the attachments into the current context
1763 const auto *prev_context = trackback->context;
1764 assert(prev_context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001765 const auto address_type = view_gen.GetAddressType();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001766 auto &target_map = GetAccessStateMap(address_type);
1767 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001768 prev_context->ResolveAccessRange(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action, &target_map,
1769 &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001770 }
1771
John Zulauf86356ca2020-10-19 11:46:41 -06001772 // If there were no transitions skip this global map walk
1773 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001774 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001775 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001776 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001777}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001778
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001779void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
1780 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
John Zulauf669dfd52021-01-27 17:15:28 -07001781
1782 auto *events_context = GetCurrentEventsContext();
1783 assert(events_context);
1784 for (auto &event_pair : *events_context) {
John Zulauf4a6105a2020-11-17 15:11:05 -07001785 assert(event_pair.second); // Shouldn't be storing empty
1786 auto &sync_event = *event_pair.second;
1787 // 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 -07001788 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
1789 sync_event.barriers |= dst.exec_scope;
1790 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
John Zulauf4a6105a2020-11-17 15:11:05 -07001791 }
1792 }
1793}
1794
John Zulauf355e49b2020-04-24 15:11:15 -06001795
locke-lunarg61870c22020-06-09 14:51:50 -06001796bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1797 const char *func_name) const {
1798 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001799 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001800 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001801 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1802 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001803 return skip;
1804 }
1805
1806 using DescriptorClass = cvdescriptorset::DescriptorClass;
1807 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1808 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1809 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1810 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1811
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001812 for (const auto &stage_state : pipe->stage_state) {
1813 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1814 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001815 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001816 }
locke-lunarg61870c22020-06-09 14:51:50 -06001817 for (const auto &set_binding : stage_state.descriptor_uses) {
1818 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1819 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1820 set_binding.first.second);
1821 const auto descriptor_type = binding_it.GetType();
1822 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1823 auto array_idx = 0;
1824
1825 if (binding_it.IsVariableDescriptorCount()) {
1826 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1827 }
1828 SyncStageAccessIndex sync_index =
1829 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1830
1831 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1832 uint32_t index = i - index_range.start;
1833 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1834 switch (descriptor->GetClass()) {
1835 case DescriptorClass::ImageSampler:
1836 case DescriptorClass::Image: {
1837 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001838 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001839 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001840 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1841 img_view_state = image_sampler_descriptor->GetImageViewState();
1842 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001843 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001844 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1845 img_view_state = image_descriptor->GetImageViewState();
1846 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001847 }
1848 if (!img_view_state) continue;
John Zulauf361fb532020-07-22 10:45:39 -06001849 HazardResult hazard;
John Zulauf110413c2021-03-20 05:38:38 -06001850 const IMAGE_STATE *img_state = img_view_state->image_state.get();
John Zulauf361fb532020-07-22 10:45:39 -06001851 const auto &subresource_range = img_view_state->normalized_subresource_range;
John Zulauf110413c2021-03-20 05:38:38 -06001852
1853 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1854 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1855 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
John Zulauf361fb532020-07-22 10:45:39 -06001856 // Input attachments are subject to raster ordering rules
1857 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001858 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001859 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001860 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range);
John Zulauf361fb532020-07-22 10:45:39 -06001861 }
John Zulauf110413c2021-03-20 05:38:38 -06001862
John Zulauf33fc1d52020-07-17 11:01:10 -06001863 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001864 skip |= sync_state_->LogError(
1865 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001866 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1867 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001868 func_name, string_SyncHazard(hazard.hazard),
1869 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1870 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001871 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001872 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1873 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
John Zulauffaea0ee2021-01-14 14:01:32 -07001874 set_binding.first.second, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001875 }
1876 break;
1877 }
1878 case DescriptorClass::TexelBuffer: {
1879 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1880 if (!buf_view_state) continue;
1881 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001882 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001883 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001884 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001885 skip |= sync_state_->LogError(
1886 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001887 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1888 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001889 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1890 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001891 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001892 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1893 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001894 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001895 }
1896 break;
1897 }
1898 case DescriptorClass::GeneralBuffer: {
1899 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1900 auto buf_state = buffer_descriptor->GetBufferState();
1901 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001902 const ResourceAccessRange range =
1903 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001904 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001905 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001906 skip |= sync_state_->LogError(
1907 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001908 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1909 func_name, string_SyncHazard(hazard.hazard),
1910 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001911 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001912 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001913 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1914 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001915 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001916 }
1917 break;
1918 }
1919 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1920 default:
1921 break;
1922 }
1923 }
1924 }
1925 }
1926 return skip;
1927}
1928
1929void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1930 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001931 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001932 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001933 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1934 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001935 return;
1936 }
1937
1938 using DescriptorClass = cvdescriptorset::DescriptorClass;
1939 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1940 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1941 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1942 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1943
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001944 for (const auto &stage_state : pipe->stage_state) {
1945 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1946 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001947 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001948 }
locke-lunarg61870c22020-06-09 14:51:50 -06001949 for (const auto &set_binding : stage_state.descriptor_uses) {
1950 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1951 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1952 set_binding.first.second);
1953 const auto descriptor_type = binding_it.GetType();
1954 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1955 auto array_idx = 0;
1956
1957 if (binding_it.IsVariableDescriptorCount()) {
1958 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1959 }
1960 SyncStageAccessIndex sync_index =
1961 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1962
1963 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1964 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1965 switch (descriptor->GetClass()) {
1966 case DescriptorClass::ImageSampler:
1967 case DescriptorClass::Image: {
1968 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1969 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1970 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1971 } else {
1972 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1973 }
1974 if (!img_view_state) continue;
1975 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001976 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
John Zulauf110413c2021-03-20 05:38:38 -06001977 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1978 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1979 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kRaster,
1980 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001981 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001982 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kNonAttachment,
1983 img_view_state->normalized_subresource_range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001984 }
locke-lunarg61870c22020-06-09 14:51:50 -06001985 break;
1986 }
1987 case DescriptorClass::TexelBuffer: {
1988 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1989 if (!buf_view_state) continue;
1990 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001991 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001992 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001993 break;
1994 }
1995 case DescriptorClass::GeneralBuffer: {
1996 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1997 auto buf_state = buffer_descriptor->GetBufferState();
1998 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001999 const ResourceAccessRange range =
2000 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07002001 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002002 break;
2003 }
2004 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2005 default:
2006 break;
2007 }
2008 }
2009 }
2010 }
2011}
2012
2013bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2014 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002015 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2016 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002017 return skip;
2018 }
2019
2020 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2021 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002022 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002023
2024 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002025 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002026 if (binding_description.binding < binding_buffers_size) {
2027 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002028 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002029
locke-lunarg1ae57d62020-11-18 10:49:19 -07002030 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002031 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2032 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002033 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002034 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002035 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002036 buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002037 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07002038 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002039 }
2040 }
2041 }
2042 return skip;
2043}
2044
2045void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002046 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2047 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002048 return;
2049 }
2050 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2051 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002052 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002053
2054 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002055 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002056 if (binding_description.binding < binding_buffers_size) {
2057 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002058 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002059
locke-lunarg1ae57d62020-11-18 10:49:19 -07002060 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002061 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2062 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002063 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2064 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002065 }
2066 }
2067}
2068
2069bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2070 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002071 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002072 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002073 }
locke-lunarg61870c22020-06-09 14:51:50 -06002074
locke-lunarg1ae57d62020-11-18 10:49:19 -07002075 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002076 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002077 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2078 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002079 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002080 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002081 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002082 index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002083 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07002084 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002085 }
2086
2087 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2088 // We will detect more accurate range in the future.
2089 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2090 return skip;
2091}
2092
2093void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002094 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 -06002095
locke-lunarg1ae57d62020-11-18 10:49:19 -07002096 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002097 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002098 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2099 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002100 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002101
2102 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2103 // We will detect more accurate range in the future.
2104 RecordDrawVertex(UINT32_MAX, 0, tag);
2105}
2106
2107bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002108 bool skip = false;
2109 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002110 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002111 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002112}
2113
2114void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002115 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002116 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002117 }
locke-lunarg61870c22020-06-09 14:51:50 -06002118}
2119
John Zulauf64ffe552021-02-06 10:25:07 -07002120void CommandBufferAccessContext::RecordBeginRenderPass(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2121 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2122 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002123 // Create an access context the current renderpass.
John Zulauf64ffe552021-02-06 10:25:07 -07002124 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002125 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf64ffe552021-02-06 10:25:07 -07002126 current_renderpass_context_->RecordBeginRenderPass(tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002127 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002128}
2129
John Zulauf64ffe552021-02-06 10:25:07 -07002130void CommandBufferAccessContext::RecordNextSubpass(CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002131 assert(current_renderpass_context_);
John Zulauffaea0ee2021-01-14 14:01:32 -07002132 auto prev_tag = NextCommandTag(command);
2133 auto next_tag = NextSubcommandTag(command);
John Zulauf64ffe552021-02-06 10:25:07 -07002134 current_renderpass_context_->RecordNextSubpass(prev_tag, next_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002135 current_context_ = &current_renderpass_context_->CurrentContext();
2136}
2137
John Zulauf64ffe552021-02-06 10:25:07 -07002138void CommandBufferAccessContext::RecordEndRenderPass(CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002139 assert(current_renderpass_context_);
2140 if (!current_renderpass_context_) return;
2141
John Zulauf64ffe552021-02-06 10:25:07 -07002142 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, NextCommandTag(command));
John Zulauf355e49b2020-04-24 15:11:15 -06002143 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002144 current_renderpass_context_ = nullptr;
2145}
2146
John Zulauf4a6105a2020-11-17 15:11:05 -07002147void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2148 // Erase is okay with the key not being
John Zulauf669dfd52021-01-27 17:15:28 -07002149 const auto *event_state = sync_state_->Get<EVENT_STATE>(event);
2150 if (event_state) {
2151 GetCurrentEventsContext()->Destroy(event_state);
John Zulaufd5115702021-01-18 12:34:33 -07002152 }
2153}
2154
John Zulauf64ffe552021-02-06 10:25:07 -07002155bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
John Zulauffaea0ee2021-01-14 14:01:32 -07002156 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002157 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002158 const auto &sync_state = ex_context.GetSyncState();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002159 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2160 if (!pipe ||
2161 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002162 return skip;
2163 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002164 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002165 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg37047832020-06-12 13:44:45 -06002166
John Zulauf1a224292020-06-30 14:52:13 -06002167 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002168 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002169 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2170 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002171 if (location >= subpass.colorAttachmentCount ||
2172 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002173 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002174 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002175 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2176 if (!view_gen.IsValid()) continue;
2177 HazardResult hazard =
2178 current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
2179 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment);
locke-lunarg96dc9632020-06-10 17:22:18 -06002180 if (hazard.hazard) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002181 const VkImageView view_handle = view_gen.GetViewState()->image_view;
2182 skip |= sync_state.LogError(view_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002183 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002184 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002185 sync_state.report_data->FormatHandle(view_handle).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06002186 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002187 location, ex_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002188 }
2189 }
2190 }
locke-lunarg37047832020-06-12 13:44:45 -06002191
2192 // PHASE1 TODO: Add layout based read/vs. write selection.
2193 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002194 const uint32_t depth_stencil_attachment =
2195 GetSubpassDepthStencilAttachmentIndex(pipe->graphicsPipelineCI.pDepthStencilState, subpass.pDepthStencilAttachment);
2196
2197 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2198 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2199 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002200 bool depth_write = false, stencil_write = false;
2201
2202 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002203 if (!FormatIsStencilOnly(view_state.create_info.format) && pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002204 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002205 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2206 depth_write = true;
2207 }
2208 // PHASE1 TODO: It needs to check if stencil is writable.
2209 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2210 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2211 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002212 if (!FormatIsDepthOnly(view_state.create_info.format) && pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002213 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2214 stencil_write = true;
2215 }
2216
2217 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2218 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002219 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2220 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2221 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002222 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002223 skip |= sync_state.LogError(
John Zulaufd0ec59f2021-03-13 14:25:08 -07002224 view_state.image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002225 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002226 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002227 sync_state.report_data->FormatHandle(view_state.image_view).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06002228 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002229 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002230 }
2231 }
2232 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002233 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2234 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2235 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002236 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002237 skip |= sync_state.LogError(
John Zulaufd0ec59f2021-03-13 14:25:08 -07002238 view_state.image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002239 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002240 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002241 sync_state.report_data->FormatHandle(view_state.image_view).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06002242 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002243 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002244 }
locke-lunarg61870c22020-06-09 14:51:50 -06002245 }
2246 }
2247 return skip;
2248}
2249
John Zulauf64ffe552021-02-06 10:25:07 -07002250void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002251 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2252 if (!pipe ||
2253 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002254 return;
2255 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002256 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002257 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002258
John Zulauf1a224292020-06-30 14:52:13 -06002259 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002260 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002261 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2262 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002263 if (location >= subpass.colorAttachmentCount ||
2264 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002265 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002266 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002267 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2268 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2269 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2270 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002271 }
2272 }
locke-lunarg37047832020-06-12 13:44:45 -06002273
2274 // PHASE1 TODO: Add layout based read/vs. write selection.
2275 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002276 const uint32_t depth_stencil_attachment =
2277 GetSubpassDepthStencilAttachmentIndex(pipe->graphicsPipelineCI.pDepthStencilState, subpass.pDepthStencilAttachment);
2278 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2279 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2280 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002281 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002282 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2283 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002284
2285 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002286 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002287 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2288 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002289 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2290 depth_write = true;
2291 }
2292 // PHASE1 TODO: It needs to check if stencil is writable.
2293 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2294 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2295 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002296 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002297 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002298 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2299 stencil_write = true;
2300 }
2301
John Zulaufd0ec59f2021-03-13 14:25:08 -07002302 if (depth_write || stencil_write) {
2303 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2304 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2305 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2306 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002307 }
locke-lunarg61870c22020-06-09 14:51:50 -06002308 }
2309}
2310
John Zulauf64ffe552021-02-06 10:25:07 -07002311bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002312 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002313 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002314 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002315 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002316 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002317 func_name);
2318
John Zulauf355e49b2020-04-24 15:11:15 -06002319 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002320 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002321 skip |=
2322 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002323 if (!skip) {
2324 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2325 // on a copy of the (empty) next context.
2326 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2327 AccessContext temp_context(next_context);
2328 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002329 skip |=
2330 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002331 }
John Zulauf7635de32020-05-29 17:14:15 -06002332 return skip;
2333}
John Zulauf64ffe552021-02-06 10:25:07 -07002334bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002335 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002336 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002337 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002338 current_subpass_);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002339 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_,
2340
2341 attachment_views_, func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002342 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002343 return skip;
2344}
2345
John Zulauf64ffe552021-02-06 10:25:07 -07002346AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002347 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002348}
2349
John Zulauf64ffe552021-02-06 10:25:07 -07002350bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2351 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002352 bool skip = false;
2353
John Zulauf7635de32020-05-29 17:14:15 -06002354 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2355 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2356 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2357 // to apply and only copy then, if this proves a hot spot.
2358 std::unique_ptr<AccessContext> proxy_for_current;
2359
John Zulauf355e49b2020-04-24 15:11:15 -06002360 // Validate the "finalLayout" transitions to external
2361 // Get them from where there we're hidding in the extra entry.
2362 const auto &final_transitions = rp_state_->subpass_transitions.back();
2363 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002364 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002365 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2366 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002367 auto *context = trackback.context;
2368
2369 if (transition.prev_pass == current_subpass_) {
2370 if (!proxy_for_current) {
2371 // 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 -07002372 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002373 }
2374 context = proxy_for_current.get();
2375 }
2376
John Zulaufa0a98292020-09-18 09:30:10 -06002377 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2378 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002379 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002380 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002381 skip |= ex_context.GetSyncState().LogError(
John Zulauffaea0ee2021-01-14 14:01:32 -07002382 rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2383 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2384 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2385 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2386 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002387 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002388 }
2389 }
2390 return skip;
2391}
2392
2393void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2394 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002395 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002396}
2397
John Zulauf64ffe552021-02-06 10:25:07 -07002398void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag &tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002399 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2400 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002401
2402 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2403 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002404 const AttachmentViewGen &view_gen = attachment_views_[i];
2405 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002406
2407 const auto &ci = attachment_ci[i];
2408 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002409 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002410 const bool is_color = !(has_depth || has_stencil);
2411
2412 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002413 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, ColorLoadUsage(ci.loadOp),
2414 SyncOrdering::kColorAttachment, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002415 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002416 if (has_depth) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002417 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2418 DepthStencilLoadUsage(ci.loadOp), SyncOrdering::kDepthStencilAttachment, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002419 }
2420 if (has_stencil) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002421 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2422 DepthStencilLoadUsage(ci.stencilLoadOp),
2423 SyncOrdering::kDepthStencilAttachment, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002424 }
2425 }
2426 }
2427 }
2428}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002429AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2430 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2431 AttachmentViewGenVector view_gens;
2432 VkExtent3D extent = CastTo3D(render_area.extent);
2433 VkOffset3D offset = CastTo3D(render_area.offset);
2434 view_gens.reserve(attachment_views.size());
2435 for (const auto *view : attachment_views) {
2436 view_gens.emplace_back(view, offset, extent);
2437 }
2438 return view_gens;
2439}
John Zulauf64ffe552021-02-06 10:25:07 -07002440RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2441 VkQueueFlags queue_flags,
2442 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2443 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002444 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002445 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002446 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002447 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002448 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002449 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002450 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002451}
2452void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
2453 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002454 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002455 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002456 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002457}
John Zulauf1507ee42020-05-18 11:33:09 -06002458
John Zulauf64ffe552021-02-06 10:25:07 -07002459void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag &prev_subpass_tag,
John Zulauffaea0ee2021-01-14 14:01:32 -07002460 const ResourceUsageTag &next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002461 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulaufd0ec59f2021-03-13 14:25:08 -07002462 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
2463 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002464
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002465 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2466 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002467 current_subpass_++;
2468 assert(current_subpass_ < subpass_contexts_.size());
John Zulauffaea0ee2021-01-14 14:01:32 -07002469 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2470 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002471 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002472}
2473
John Zulauf64ffe552021-02-06 10:25:07 -07002474void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002475 // Add the resolve and store accesses
John Zulaufd0ec59f2021-03-13 14:25:08 -07002476 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, tag);
2477 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002478
John Zulauf355e49b2020-04-24 15:11:15 -06002479 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002480 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002481
2482 // Add the "finalLayout" transitions to external
2483 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002484 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2485 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2486 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002487 const auto &final_transitions = rp_state_->subpass_transitions.back();
2488 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002489 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002490 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002491 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002492 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002493 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002494 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002495 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002496 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002497 }
2498}
2499
Jeremy Gebben40a22942020-12-22 14:22:06 -07002500SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002501 SyncExecScope result;
2502 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002503 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2504 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002505 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2506 return result;
2507}
2508
Jeremy Gebben40a22942020-12-22 14:22:06 -07002509SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002510 SyncExecScope result;
2511 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002512 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2513 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002514 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2515 return result;
2516}
2517
2518SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002519 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002520 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002521 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002522 dst_access_scope = 0;
2523}
2524
2525template <typename Barrier>
2526SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002527 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002528 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002529 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002530 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2531}
2532
2533SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002534 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2535 if (barrier) {
2536 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002537 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002538 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002539
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002540 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002541 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002542 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2543
2544 } else {
2545 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002546 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002547 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2548
2549 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002550 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002551 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2552 }
2553}
2554
2555template <typename Barrier>
2556SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2557 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2558 src_exec_scope = src.exec_scope;
2559 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2560
2561 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002562 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002563 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002564}
2565
John Zulaufb02c1eb2020-10-06 16:33:36 -06002566// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2567void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2568 for (const auto &barrier : barriers) {
2569 ApplyBarrier(barrier, layout_transition);
2570 }
2571}
2572
John Zulauf89311b42020-09-29 16:28:47 -06002573// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2574// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2575// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002576void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2577 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002578 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002579 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002580 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002581 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002582 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002583 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002584}
John Zulauf9cb530d2019-09-30 14:14:10 -06002585HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2586 HazardResult hazard;
2587 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002588 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002589 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002590 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002591 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002592 }
2593 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002594 // Write operation:
2595 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2596 // If reads exists -- test only against them because either:
2597 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2598 // * 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
2599 // the current write happens after the reads, so just test the write against the reades
2600 // Otherwise test against last_write
2601 //
2602 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002603 if (last_reads.size()) {
2604 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002605 if (IsReadHazard(usage_stage, read_access)) {
2606 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2607 break;
2608 }
2609 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002610 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002611 // Write-After-Write check -- if we have a previous write to test against
2612 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002613 }
2614 }
2615 return hazard;
2616}
2617
John Zulauf8e3c3e92021-01-06 11:19:36 -07002618HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering &ordering_rule) const {
2619 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06002620 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2621 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002622 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002623 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002624 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2625 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002626 if (IsRead(usage_bit)) {
2627 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2628 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2629 if (is_raw_hazard) {
2630 // NOTE: we know last_write is non-zero
2631 // See if the ordering rules save us from the simple RAW check above
2632 // First check to see if the current usage is covered by the ordering rules
2633 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2634 const bool usage_is_ordered =
2635 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2636 if (usage_is_ordered) {
2637 // Now see of the most recent write (or a subsequent read) are ordered
2638 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2639 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002640 }
2641 }
John Zulauf4285ee92020-09-23 10:20:52 -06002642 if (is_raw_hazard) {
2643 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2644 }
John Zulauf361fb532020-07-22 10:45:39 -06002645 } else {
2646 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002647 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002648 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002649 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002650 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002651 if (usage_write_is_ordered) {
2652 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2653 ordered_stages = GetOrderedStages(ordering);
2654 }
2655 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2656 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002657 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002658 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2659 if (IsReadHazard(usage_stage, read_access)) {
2660 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2661 break;
2662 }
John Zulaufd14743a2020-07-03 09:42:39 -06002663 }
2664 }
John Zulauf4285ee92020-09-23 10:20:52 -06002665 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002666 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002667 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002668 }
John Zulauf69133422020-05-20 14:55:53 -06002669 }
2670 }
2671 return hazard;
2672}
2673
John Zulauf2f952d22020-02-10 11:34:51 -07002674// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002675HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002676 HazardResult hazard;
2677 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002678 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2679 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2680 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002681 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002682 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002683 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002684 }
2685 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002686 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002687 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002688 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002689 // 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 -07002690 for (const auto &read_access : last_reads) {
2691 if (read_access.tag.index >= start_tag.index) {
2692 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002693 break;
2694 }
2695 }
John Zulauf2f952d22020-02-10 11:34:51 -07002696 }
2697 }
2698 return hazard;
2699}
2700
Jeremy Gebben40a22942020-12-22 14:22:06 -07002701HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002702 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002703 // Only supporting image layout transitions for now
2704 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2705 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002706 // only test for WAW if there no intervening read operations.
2707 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002708 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002709 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002710 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002711 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002712 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002713 break;
2714 }
2715 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002716 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2717 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2718 }
2719
2720 return hazard;
2721}
2722
Jeremy Gebben40a22942020-12-22 14:22:06 -07002723HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07002724 const SyncStageAccessFlags &src_access_scope,
2725 const ResourceUsageTag &event_tag) const {
2726 // Only supporting image layout transitions for now
2727 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2728 HazardResult hazard;
2729 // only test for WAW if there no intervening read operations.
2730 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2731
John Zulaufab7756b2020-12-29 16:10:16 -07002732 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002733 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
2734 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07002735 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002736 if (read_access.tag.IsBefore(event_tag)) {
2737 // The read is in the events first synchronization scope, so we use a barrier hazard check
2738 // If the read stage is not in the src sync scope
2739 // *AND* not execution chained with an existing sync barrier (that's the or)
2740 // then the barrier access is unsafe (R/W after R)
2741 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
2742 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2743 break;
2744 }
2745 } else {
2746 // The read not in the event first sync scope and so is a hazard vs. the layout transition
2747 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2748 }
2749 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002750 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002751 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
2752 if (write_tag.IsBefore(event_tag)) {
2753 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
2754 // So do a normal barrier hazard check
2755 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2756 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2757 }
2758 } else {
2759 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06002760 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2761 }
John Zulaufd14743a2020-07-03 09:42:39 -06002762 }
John Zulauf361fb532020-07-22 10:45:39 -06002763
John Zulauf0cb5be22020-01-23 12:18:22 -07002764 return hazard;
2765}
2766
John Zulauf5f13a792020-03-10 07:31:21 -06002767// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2768// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2769// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2770void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2771 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002772 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2773 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002774 *this = other;
2775 } else if (!other.write_tag.IsBefore(write_tag)) {
2776 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2777 // dependency chaining logic or any stage expansion)
2778 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002779 pending_write_barriers |= other.pending_write_barriers;
2780 pending_layout_transition |= other.pending_layout_transition;
2781 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002782
John Zulaufd14743a2020-07-03 09:42:39 -06002783 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07002784 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06002785 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07002786 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002787 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002788 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002789 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002790 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2791 // but we should wait on profiling data for that.
2792 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002793 auto &my_read = last_reads[my_read_index];
2794 if (other_read.stage == my_read.stage) {
2795 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002796 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002797 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002798 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002799 my_read.pending_dep_chain = other_read.pending_dep_chain;
2800 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2801 // May require tracking more than one access per stage.
2802 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002803 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06002804 // Since I'm overwriting the fragement stage read, also update the input attachment info
2805 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002806 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002807 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002808 } else if (other_read.tag.IsBefore(my_read.tag)) {
2809 // The read tags match so merge the barriers
2810 my_read.barriers |= other_read.barriers;
2811 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002812 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002813
John Zulauf5f13a792020-03-10 07:31:21 -06002814 break;
2815 }
2816 }
2817 } else {
2818 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07002819 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06002820 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002821 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002822 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002823 }
John Zulauf5f13a792020-03-10 07:31:21 -06002824 }
2825 }
John Zulauf361fb532020-07-22 10:45:39 -06002826 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002827 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2828 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07002829
2830 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
2831 // of the copy and other into this using the update first logic.
2832 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
2833 // of the other first_accesses... )
2834 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
2835 FirstAccesses firsts(std::move(first_accesses_));
2836 first_accesses_.clear();
2837 first_read_stages_ = 0U;
2838 auto a = firsts.begin();
2839 auto a_end = firsts.end();
2840 for (auto &b : other.first_accesses_) {
2841 // TODO: Determine whether "IsBefore" or "IsGloballyBefore" is needed...
2842 while (a != a_end && a->tag.IsBefore(b.tag)) {
2843 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2844 ++a;
2845 }
2846 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
2847 }
2848 for (; a != a_end; ++a) {
2849 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2850 }
2851 }
John Zulauf5f13a792020-03-10 07:31:21 -06002852}
2853
John Zulauf8e3c3e92021-01-06 11:19:36 -07002854void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag &tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002855 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2856 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002857 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002858 // Mulitple outstanding reads may be of interest and do dependency chains independently
2859 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2860 const auto usage_stage = PipelineStageBit(usage_index);
2861 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002862 for (auto &read_access : last_reads) {
2863 if (read_access.stage == usage_stage) {
2864 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002865 break;
2866 }
2867 }
2868 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07002869 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002870 last_read_stages |= usage_stage;
2871 }
John Zulauf4285ee92020-09-23 10:20:52 -06002872
2873 // 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 -07002874 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002875 // TODO Revisit re: multiple reads for a given stage
2876 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002877 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002878 } else {
2879 // Assume write
2880 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002881 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002882 }
John Zulauffaea0ee2021-01-14 14:01:32 -07002883 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06002884}
John Zulauf5f13a792020-03-10 07:31:21 -06002885
John Zulauf89311b42020-09-29 16:28:47 -06002886// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2887// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2888// We can overwrite them as *this* write is now after them.
2889//
2890// 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 -07002891void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002892 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06002893 last_read_stages = 0;
2894 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002895 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002896
2897 write_barriers = 0;
2898 write_dependency_chain = 0;
2899 write_tag = tag;
2900 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002901}
2902
John Zulauf89311b42020-09-29 16:28:47 -06002903// Apply the memory barrier without updating the existing barriers. The execution barrier
2904// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2905// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2906// replace the current write barriers or add to them, so accumulate to pending as well.
2907void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2908 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2909 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002910 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
2911 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
2912 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
2913 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07002914 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06002915 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07002916 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002917 }
John Zulauf89311b42020-09-29 16:28:47 -06002918 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2919 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002920
John Zulauf89311b42020-09-29 16:28:47 -06002921 if (!pending_layout_transition) {
2922 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2923 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002924 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06002925 // 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 -07002926 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
2927 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002928 }
2929 }
John Zulaufa0a98292020-09-18 09:30:10 -06002930 }
John Zulaufa0a98292020-09-18 09:30:10 -06002931}
2932
John Zulauf4a6105a2020-11-17 15:11:05 -07002933// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
2934// changes the "chaining" state, but to keep barriers independent. See discussion above.
2935void ResourceAccessState::ApplyBarrier(const ResourceUsageTag &scope_tag, const SyncBarrier &barrier, bool layout_transition) {
2936 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
2937 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
2938 // in order to know if it's in the excecution scope
2939 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
2940 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
2941 // errors w.r.t. "most recent" accesses.
2942 if (layout_transition || ((write_tag.IsBefore(scope_tag)) && (barrier.src_access_scope & last_write).any())) {
2943 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07002944 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002945 }
2946 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2947 pending_layout_transition |= layout_transition;
2948
2949 if (!pending_layout_transition) {
2950 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2951 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002952 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002953 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
2954 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
2955 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
2956 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
2957 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
2958 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
2959 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulaufc523bf62021-02-16 08:20:34 -07002960 if (read_access.tag.IsBefore(scope_tag) &&
2961 (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers))) {
2962 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002963 }
2964 }
2965 }
2966}
John Zulauf89311b42020-09-29 16:28:47 -06002967void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
2968 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06002969 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2970 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07002971 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf89311b42020-09-29 16:28:47 -06002972 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002973 }
John Zulauf89311b42020-09-29 16:28:47 -06002974
2975 // Apply the accumulate execution barriers (and thus update chaining information)
2976 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07002977 for (auto &read_access : last_reads) {
2978 read_access.barriers |= read_access.pending_dep_chain;
2979 read_execution_barriers |= read_access.barriers;
2980 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06002981 }
2982
2983 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2984 write_dependency_chain |= pending_write_dep_chain;
2985 write_barriers |= pending_write_barriers;
2986 pending_write_dep_chain = 0;
2987 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002988}
2989
John Zulauf59e25072020-07-17 10:55:21 -06002990// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07002991VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
2992 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002993
John Zulaufab7756b2020-12-29 16:10:16 -07002994 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002995 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06002996 barriers = read_access.barriers;
2997 break;
John Zulauf59e25072020-07-17 10:55:21 -06002998 }
2999 }
John Zulauf4285ee92020-09-23 10:20:52 -06003000
John Zulauf59e25072020-07-17 10:55:21 -06003001 return barriers;
3002}
3003
Jeremy Gebben40a22942020-12-22 14:22:06 -07003004inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003005 assert(IsRead(usage));
3006 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3007 // * the previous reads are not hazards, and thus last_write must be visible and available to
3008 // any reads that happen after.
3009 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3010 // 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 -07003011 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003012}
3013
Jeremy Gebben40a22942020-12-22 14:22:06 -07003014VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003015 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07003016 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06003017 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003018 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003019 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003020 // 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 -07003021 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06003022 }
3023
3024 return ordered_stages;
3025}
3026
John Zulauffaea0ee2021-01-14 14:01:32 -07003027void ResourceAccessState::UpdateFirst(const ResourceUsageTag &tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
3028 // Only record until we record a write.
3029 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003030 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003031 if (0 == (usage_stage & first_read_stages_)) {
3032 // If this is a read we haven't seen or a write, record.
3033 first_read_stages_ |= usage_stage;
3034 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3035 }
3036 }
3037}
3038
John Zulaufd1f85d42020-04-15 12:23:15 -06003039void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003040 auto *access_context = GetAccessContextNoInsert(command_buffer);
3041 if (access_context) {
3042 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003043 }
3044}
3045
John Zulaufd1f85d42020-04-15 12:23:15 -06003046void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3047 auto access_found = cb_access_state.find(command_buffer);
3048 if (access_found != cb_access_state.end()) {
3049 access_found->second->Reset();
3050 cb_access_state.erase(access_found);
3051 }
3052}
3053
John Zulauf9cb530d2019-09-30 14:14:10 -06003054bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3055 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3056 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003057 const auto *cb_context = GetAccessContext(commandBuffer);
3058 assert(cb_context);
3059 if (!cb_context) return skip;
3060 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003061
John Zulauf3d84f1b2020-03-09 13:33:25 -06003062 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003063 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003064 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003065
3066 for (uint32_t region = 0; region < regionCount; region++) {
3067 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003068 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003069 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003070 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003071 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003072 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003073 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003074 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003075 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003076 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003077 }
John Zulauf16adfc92020-04-08 10:28:33 -06003078 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003079 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003080 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003081 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003082 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003083 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003084 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003085 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003086 }
3087 }
3088 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003089 }
3090 return skip;
3091}
3092
3093void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3094 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003095 auto *cb_context = GetAccessContext(commandBuffer);
3096 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003097 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003098 auto *context = cb_context->GetCurrentAccessContext();
3099
John Zulauf9cb530d2019-09-30 14:14:10 -06003100 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003101 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003102
3103 for (uint32_t region = 0; region < regionCount; region++) {
3104 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003105 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003106 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003107 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003108 }
John Zulauf16adfc92020-04-08 10:28:33 -06003109 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003110 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003111 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003112 }
3113 }
3114}
3115
John Zulauf4a6105a2020-11-17 15:11:05 -07003116void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3117 // Clear out events from the command buffer contexts
3118 for (auto &cb_context : cb_access_state) {
3119 cb_context.second->RecordDestroyEvent(event);
3120 }
3121}
3122
Jeff Leger178b1e52020-10-05 12:22:23 -04003123bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3124 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3125 bool skip = false;
3126 const auto *cb_context = GetAccessContext(commandBuffer);
3127 assert(cb_context);
3128 if (!cb_context) return skip;
3129 const auto *context = cb_context->GetCurrentAccessContext();
3130
3131 // If we have no previous accesses, we have no hazards
3132 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3133 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3134
3135 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3136 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3137 if (src_buffer) {
3138 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003139 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003140 if (hazard.hazard) {
3141 // TODO -- add tag information to log msg when useful.
3142 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3143 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3144 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003145 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003146 }
3147 }
3148 if (dst_buffer && !skip) {
3149 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003150 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003151 if (hazard.hazard) {
3152 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3153 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3154 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003155 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003156 }
3157 }
3158 if (skip) break;
3159 }
3160 return skip;
3161}
3162
3163void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3164 auto *cb_context = GetAccessContext(commandBuffer);
3165 assert(cb_context);
3166 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3167 auto *context = cb_context->GetCurrentAccessContext();
3168
3169 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3170 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3171
3172 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3173 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3174 if (src_buffer) {
3175 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003176 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003177 }
3178 if (dst_buffer) {
3179 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003180 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003181 }
3182 }
3183}
3184
John Zulauf5c5e88d2019-12-26 11:22:02 -07003185bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3186 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3187 const VkImageCopy *pRegions) const {
3188 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003189 const auto *cb_access_context = GetAccessContext(commandBuffer);
3190 assert(cb_access_context);
3191 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003192
John Zulauf3d84f1b2020-03-09 13:33:25 -06003193 const auto *context = cb_access_context->GetCurrentAccessContext();
3194 assert(context);
3195 if (!context) return skip;
3196
3197 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3198 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003199 for (uint32_t region = 0; region < regionCount; region++) {
3200 const auto &copy_region = pRegions[region];
3201 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003202 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003203 copy_region.srcOffset, copy_region.extent);
3204 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003205 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003206 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003207 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003208 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003209 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003210 }
3211
3212 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003213 VkExtent3D dst_copy_extent =
3214 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003215 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003216 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003217 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003218 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003219 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003220 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003221 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003222 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003223 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003224 }
3225 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003226
John Zulauf5c5e88d2019-12-26 11:22:02 -07003227 return skip;
3228}
3229
3230void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3231 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3232 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003233 auto *cb_access_context = GetAccessContext(commandBuffer);
3234 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003235 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003236 auto *context = cb_access_context->GetCurrentAccessContext();
3237 assert(context);
3238
John Zulauf5c5e88d2019-12-26 11:22:02 -07003239 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003240 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003241
3242 for (uint32_t region = 0; region < regionCount; region++) {
3243 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003244 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003245 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003246 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003247 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003248 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003249 VkExtent3D dst_copy_extent =
3250 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003251 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003252 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003253 }
3254 }
3255}
3256
Jeff Leger178b1e52020-10-05 12:22:23 -04003257bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3258 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3259 bool skip = false;
3260 const auto *cb_access_context = GetAccessContext(commandBuffer);
3261 assert(cb_access_context);
3262 if (!cb_access_context) return skip;
3263
3264 const auto *context = cb_access_context->GetCurrentAccessContext();
3265 assert(context);
3266 if (!context) return skip;
3267
3268 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3269 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3270 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3271 const auto &copy_region = pCopyImageInfo->pRegions[region];
3272 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003273 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003274 copy_region.srcOffset, copy_region.extent);
3275 if (hazard.hazard) {
3276 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3277 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3278 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003279 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003280 }
3281 }
3282
3283 if (dst_image) {
3284 VkExtent3D dst_copy_extent =
3285 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003286 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003287 copy_region.dstOffset, dst_copy_extent);
3288 if (hazard.hazard) {
3289 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3290 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3291 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003292 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003293 }
3294 if (skip) break;
3295 }
3296 }
3297
3298 return skip;
3299}
3300
3301void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3302 auto *cb_access_context = GetAccessContext(commandBuffer);
3303 assert(cb_access_context);
3304 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3305 auto *context = cb_access_context->GetCurrentAccessContext();
3306 assert(context);
3307
3308 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3309 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3310
3311 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3312 const auto &copy_region = pCopyImageInfo->pRegions[region];
3313 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003314 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003315 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003316 }
3317 if (dst_image) {
3318 VkExtent3D dst_copy_extent =
3319 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003320 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003321 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003322 }
3323 }
3324}
3325
John Zulauf9cb530d2019-09-30 14:14:10 -06003326bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3327 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3328 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3329 uint32_t bufferMemoryBarrierCount,
3330 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3331 uint32_t imageMemoryBarrierCount,
3332 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3333 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003334 const auto *cb_access_context = GetAccessContext(commandBuffer);
3335 assert(cb_access_context);
3336 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003337
John Zulauf36ef9282021-02-02 11:47:24 -07003338 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3339 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3340 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3341 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003342 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003343 return skip;
3344}
3345
3346void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3347 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3348 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3349 uint32_t bufferMemoryBarrierCount,
3350 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3351 uint32_t imageMemoryBarrierCount,
3352 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003353 auto *cb_access_context = GetAccessContext(commandBuffer);
3354 assert(cb_access_context);
3355 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003356
John Zulauf36ef9282021-02-02 11:47:24 -07003357 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3358 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3359 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3360 pImageMemoryBarriers);
3361 pipeline_barrier.Record(cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003362}
3363
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003364bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3365 const VkDependencyInfoKHR *pDependencyInfo) const {
3366 bool skip = false;
3367 const auto *cb_access_context = GetAccessContext(commandBuffer);
3368 assert(cb_access_context);
3369 if (!cb_access_context) return skip;
3370
3371 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3372 skip = pipeline_barrier.Validate(*cb_access_context);
3373 return skip;
3374}
3375
3376void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3377 auto *cb_access_context = GetAccessContext(commandBuffer);
3378 assert(cb_access_context);
3379 if (!cb_access_context) return;
3380
3381 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3382 pipeline_barrier.Record(cb_access_context);
3383}
3384
John Zulauf9cb530d2019-09-30 14:14:10 -06003385void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3386 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3387 // The state tracker sets up the device state
3388 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3389
John Zulauf5f13a792020-03-10 07:31:21 -06003390 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3391 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003392 // TODO: Find a good way to do this hooklessly.
3393 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3394 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3395 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3396
John Zulaufd1f85d42020-04-15 12:23:15 -06003397 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3398 sync_device_state->ResetCommandBufferCallback(command_buffer);
3399 });
3400 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3401 sync_device_state->FreeCommandBufferCallback(command_buffer);
3402 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003403}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003404
John Zulauf355e49b2020-04-24 15:11:15 -06003405bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf64ffe552021-02-06 10:25:07 -07003406 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd, const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003407 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003408 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003409 if (cb_context) {
3410 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo, cmd_name);
3411 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003412 }
John Zulauf355e49b2020-04-24 15:11:15 -06003413 return skip;
3414}
3415
3416bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3417 VkSubpassContents contents) const {
3418 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003419 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003420 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003421 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003422 return skip;
3423}
3424
3425bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003426 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003427 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003428 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003429 return skip;
3430}
3431
John Zulauf64ffe552021-02-06 10:25:07 -07003432static const char *kBeginRenderPass2KhrName = "vkCmdBeginRenderPass2KHR";
John Zulauf355e49b2020-04-24 15:11:15 -06003433bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3434 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003435 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003436 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003437 skip |=
3438 ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2, kBeginRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003439 return skip;
3440}
3441
John Zulauf3d84f1b2020-03-09 13:33:25 -06003442void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3443 VkResult result) {
3444 // The state tracker sets up the command buffer state
3445 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3446
3447 // Create/initialize the structure that trackers accesses at the command buffer scope.
3448 auto cb_access_context = GetAccessContext(commandBuffer);
3449 assert(cb_access_context);
3450 cb_access_context->Reset();
3451}
3452
3453void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf64ffe552021-02-06 10:25:07 -07003454 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd, const char *cmd_name) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003455 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003456 if (cb_context) {
John Zulauf64ffe552021-02-06 10:25:07 -07003457 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo, cmd_name);
3458 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003459 }
3460}
3461
3462void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3463 VkSubpassContents contents) {
3464 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003465 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003466 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003467 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003468}
3469
3470void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3471 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3472 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003473 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003474}
3475
3476void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3477 const VkRenderPassBeginInfo *pRenderPassBegin,
3478 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3479 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003480 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2, kBeginRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003481}
3482
Mike Schuchardt2df08912020-12-15 16:28:09 -08003483bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf64ffe552021-02-06 10:25:07 -07003484 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd, const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003485 bool skip = false;
3486
3487 auto cb_context = GetAccessContext(commandBuffer);
3488 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003489 if (!cb_context) return skip;
3490 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo, cmd_name);
3491 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003492}
3493
3494bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3495 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003496 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003497 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003498 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003499 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3500 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003501 return skip;
3502}
3503
John Zulauf64ffe552021-02-06 10:25:07 -07003504static const char *kNextSubpass2KhrName = "vkCmdNextSubpass2KHR";
Mike Schuchardt2df08912020-12-15 16:28:09 -08003505bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3506 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003507 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003508 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2, kNextSubpass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003509 return skip;
3510}
3511
3512bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3513 const VkSubpassEndInfo *pSubpassEndInfo) const {
3514 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003515 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003516 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003517}
3518
3519void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf64ffe552021-02-06 10:25:07 -07003520 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd, const char *cmd_name) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003521 auto cb_context = GetAccessContext(commandBuffer);
3522 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003523 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003524
John Zulauf64ffe552021-02-06 10:25:07 -07003525 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo, cmd_name);
3526 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003527}
3528
3529void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3530 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003531 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003532 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003533 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003534}
3535
3536void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3537 const VkSubpassEndInfo *pSubpassEndInfo) {
3538 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003539 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003540}
3541
3542void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3543 const VkSubpassEndInfo *pSubpassEndInfo) {
3544 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003545 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2, kNextSubpass2KhrName);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003546}
3547
John Zulauf64ffe552021-02-06 10:25:07 -07003548bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd,
3549 const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003550 bool skip = false;
3551
3552 auto cb_context = GetAccessContext(commandBuffer);
3553 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003554 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003555
John Zulauf64ffe552021-02-06 10:25:07 -07003556 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo, cmd_name);
3557 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003558 return skip;
3559}
3560
3561bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3562 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003563 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003564 return skip;
3565}
3566
Mike Schuchardt2df08912020-12-15 16:28:09 -08003567bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003568 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003569 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003570 return skip;
3571}
3572
John Zulauf64ffe552021-02-06 10:25:07 -07003573const static char *kEndRenderPass2KhrName = "vkEndRenderPass2KHR";
John Zulauf355e49b2020-04-24 15:11:15 -06003574bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003575 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003576 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003577 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2, kEndRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003578 return skip;
3579}
3580
John Zulauf64ffe552021-02-06 10:25:07 -07003581void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd,
3582 const char *cmd_name) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003583 // Resolve the all subpass contexts to the command buffer contexts
3584 auto cb_context = GetAccessContext(commandBuffer);
3585 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003586 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003587
John Zulauf64ffe552021-02-06 10:25:07 -07003588 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo, cmd_name);
3589 sync_op.Record(cb_context);
3590 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003591}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003592
John Zulauf33fc1d52020-07-17 11:01:10 -06003593// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3594// updates to a resource which do not conflict at the byte level.
3595// TODO: Revisit this rule to see if it needs to be tighter or looser
3596// TODO: Add programatic control over suppression heuristics
3597bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3598 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3599}
3600
John Zulauf3d84f1b2020-03-09 13:33:25 -06003601void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003602 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003603 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003604}
3605
3606void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003607 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003608 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003609}
3610
3611void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf64ffe552021-02-06 10:25:07 -07003612 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2, kEndRenderPass2KhrName);
John Zulauf5a1a5382020-06-22 17:23:25 -06003613 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003614}
locke-lunarga19c71d2020-03-02 18:17:04 -07003615
Jeff Leger178b1e52020-10-05 12:22:23 -04003616template <typename BufferImageCopyRegionType>
3617bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3618 VkImageLayout dstImageLayout, uint32_t regionCount,
3619 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003620 bool skip = false;
3621 const auto *cb_access_context = GetAccessContext(commandBuffer);
3622 assert(cb_access_context);
3623 if (!cb_access_context) return skip;
3624
Jeff Leger178b1e52020-10-05 12:22:23 -04003625 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3626 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3627
locke-lunarga19c71d2020-03-02 18:17:04 -07003628 const auto *context = cb_access_context->GetCurrentAccessContext();
3629 assert(context);
3630 if (!context) return skip;
3631
3632 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003633 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3634
3635 for (uint32_t region = 0; region < regionCount; region++) {
3636 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003637 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003638 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003639 if (src_buffer) {
3640 ResourceAccessRange src_range =
3641 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003642 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07003643 if (hazard.hazard) {
3644 // PHASE1 TODO -- add tag information to log msg when useful.
3645 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3646 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3647 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003648 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003649 }
3650 }
3651
Jeremy Gebben40a22942020-12-22 14:22:06 -07003652 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07003653 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003654 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003655 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003656 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003657 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003658 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003659 }
3660 if (skip) break;
3661 }
3662 if (skip) break;
3663 }
3664 return skip;
3665}
3666
Jeff Leger178b1e52020-10-05 12:22:23 -04003667bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3668 VkImageLayout dstImageLayout, uint32_t regionCount,
3669 const VkBufferImageCopy *pRegions) const {
3670 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3671 COPY_COMMAND_VERSION_1);
3672}
3673
3674bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3675 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3676 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3677 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3678 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3679}
3680
3681template <typename BufferImageCopyRegionType>
3682void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3683 VkImageLayout dstImageLayout, uint32_t regionCount,
3684 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003685 auto *cb_access_context = GetAccessContext(commandBuffer);
3686 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003687
3688 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3689 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3690
3691 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003692 auto *context = cb_access_context->GetCurrentAccessContext();
3693 assert(context);
3694
3695 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003696 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003697
3698 for (uint32_t region = 0; region < regionCount; region++) {
3699 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07003700 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003701 if (src_buffer) {
3702 ResourceAccessRange src_range =
3703 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003704 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003705 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07003706 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003707 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003708 }
3709 }
3710}
3711
Jeff Leger178b1e52020-10-05 12:22:23 -04003712void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3713 VkImageLayout dstImageLayout, uint32_t regionCount,
3714 const VkBufferImageCopy *pRegions) {
3715 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3716 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3717}
3718
3719void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3720 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3721 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3722 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3723 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3724 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3725}
3726
3727template <typename BufferImageCopyRegionType>
3728bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3729 VkBuffer dstBuffer, uint32_t regionCount,
3730 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003731 bool skip = false;
3732 const auto *cb_access_context = GetAccessContext(commandBuffer);
3733 assert(cb_access_context);
3734 if (!cb_access_context) return skip;
3735
Jeff Leger178b1e52020-10-05 12:22:23 -04003736 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3737 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3738
locke-lunarga19c71d2020-03-02 18:17:04 -07003739 const auto *context = cb_access_context->GetCurrentAccessContext();
3740 assert(context);
3741 if (!context) return skip;
3742
3743 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3744 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3745 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3746 for (uint32_t region = 0; region < regionCount; region++) {
3747 const auto &copy_region = pRegions[region];
3748 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003749 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003750 copy_region.imageOffset, copy_region.imageExtent);
3751 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003752 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003753 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003754 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003755 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003756 }
John Zulauf477700e2021-01-06 11:41:49 -07003757 if (dst_mem) {
3758 ResourceAccessRange dst_range =
3759 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003760 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07003761 if (hazard.hazard) {
3762 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3763 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3764 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003765 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003766 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003767 }
3768 }
3769 if (skip) break;
3770 }
3771 return skip;
3772}
3773
Jeff Leger178b1e52020-10-05 12:22:23 -04003774bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3775 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3776 const VkBufferImageCopy *pRegions) const {
3777 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3778 COPY_COMMAND_VERSION_1);
3779}
3780
3781bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3782 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3783 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3784 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3785 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3786}
3787
3788template <typename BufferImageCopyRegionType>
3789void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3790 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3791 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003792 auto *cb_access_context = GetAccessContext(commandBuffer);
3793 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003794
3795 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3796 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3797
3798 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003799 auto *context = cb_access_context->GetCurrentAccessContext();
3800 assert(context);
3801
3802 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003803 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3804 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06003805 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003806
3807 for (uint32_t region = 0; region < regionCount; region++) {
3808 const auto &copy_region = pRegions[region];
3809 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003810 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003811 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003812 if (dst_buffer) {
3813 ResourceAccessRange dst_range =
3814 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003815 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003816 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003817 }
3818 }
3819}
3820
Jeff Leger178b1e52020-10-05 12:22:23 -04003821void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3822 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3823 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3824 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3825}
3826
3827void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3828 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3829 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3830 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3831 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3832 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3833}
3834
3835template <typename RegionType>
3836bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3837 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3838 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003839 bool skip = false;
3840 const auto *cb_access_context = GetAccessContext(commandBuffer);
3841 assert(cb_access_context);
3842 if (!cb_access_context) return skip;
3843
3844 const auto *context = cb_access_context->GetCurrentAccessContext();
3845 assert(context);
3846 if (!context) return skip;
3847
3848 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3849 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3850
3851 for (uint32_t region = 0; region < regionCount; region++) {
3852 const auto &blit_region = pRegions[region];
3853 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003854 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3855 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3856 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3857 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3858 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3859 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003860 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003861 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003862 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003863 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003864 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003865 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003866 }
3867 }
3868
3869 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003870 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3871 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3872 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3873 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3874 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3875 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003876 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003877 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003878 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003879 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003880 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003881 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003882 }
3883 if (skip) break;
3884 }
3885 }
3886
3887 return skip;
3888}
3889
Jeff Leger178b1e52020-10-05 12:22:23 -04003890bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3891 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3892 const VkImageBlit *pRegions, VkFilter filter) const {
3893 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3894 "vkCmdBlitImage");
3895}
3896
3897bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3898 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3899 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3900 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3901 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3902}
3903
3904template <typename RegionType>
3905void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3906 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3907 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003908 auto *cb_access_context = GetAccessContext(commandBuffer);
3909 assert(cb_access_context);
3910 auto *context = cb_access_context->GetCurrentAccessContext();
3911 assert(context);
3912
3913 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003914 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003915
3916 for (uint32_t region = 0; region < regionCount; region++) {
3917 const auto &blit_region = pRegions[region];
3918 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003919 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3920 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3921 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3922 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3923 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3924 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003925 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003926 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003927 }
3928 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003929 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3930 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3931 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3932 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3933 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3934 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003935 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003936 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003937 }
3938 }
3939}
locke-lunarg36ba2592020-04-03 09:42:04 -06003940
Jeff Leger178b1e52020-10-05 12:22:23 -04003941void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3942 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3943 const VkImageBlit *pRegions, VkFilter filter) {
3944 auto *cb_access_context = GetAccessContext(commandBuffer);
3945 assert(cb_access_context);
3946 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3947 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3948 pRegions, filter);
3949 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3950}
3951
3952void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3953 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3954 auto *cb_access_context = GetAccessContext(commandBuffer);
3955 assert(cb_access_context);
3956 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3957 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3958 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3959 pBlitImageInfo->filter, tag);
3960}
3961
John Zulauffaea0ee2021-01-14 14:01:32 -07003962bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
3963 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
3964 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
3965 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003966 bool skip = false;
3967 if (drawCount == 0) return skip;
3968
3969 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3970 VkDeviceSize size = struct_size;
3971 if (drawCount == 1 || stride == size) {
3972 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003973 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003974 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3975 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003976 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003977 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003978 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003979 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003980 }
3981 } else {
3982 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003983 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003984 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3985 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003986 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003987 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3988 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 break;
3991 }
3992 }
3993 }
3994 return skip;
3995}
3996
locke-lunarg61870c22020-06-09 14:51:50 -06003997void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3998 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3999 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06004000 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4001 VkDeviceSize size = struct_size;
4002 if (drawCount == 1 || stride == size) {
4003 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004004 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004005 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004006 } else {
4007 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004008 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004009 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4010 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004011 }
4012 }
4013}
4014
John Zulauffaea0ee2021-01-14 14:01:32 -07004015bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4016 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4017 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004018 bool skip = false;
4019
4020 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004021 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004022 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4023 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004024 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004025 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004026 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004027 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004028 }
4029 return skip;
4030}
4031
locke-lunarg61870c22020-06-09 14:51:50 -06004032void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06004033 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004034 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004035 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004036}
4037
locke-lunarg36ba2592020-04-03 09:42:04 -06004038bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004039 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004040 const auto *cb_access_context = GetAccessContext(commandBuffer);
4041 assert(cb_access_context);
4042 if (!cb_access_context) return skip;
4043
locke-lunarg61870c22020-06-09 14:51:50 -06004044 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004045 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004046}
4047
4048void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004049 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004050 auto *cb_access_context = GetAccessContext(commandBuffer);
4051 assert(cb_access_context);
4052 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004053
locke-lunarg61870c22020-06-09 14:51:50 -06004054 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004055}
locke-lunarge1a67022020-04-29 00:15:36 -06004056
4057bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004058 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004059 const auto *cb_access_context = GetAccessContext(commandBuffer);
4060 assert(cb_access_context);
4061 if (!cb_access_context) return skip;
4062
4063 const auto *context = cb_access_context->GetCurrentAccessContext();
4064 assert(context);
4065 if (!context) return skip;
4066
locke-lunarg61870c22020-06-09 14:51:50 -06004067 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004068 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4069 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004070 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004071}
4072
4073void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004074 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004075 auto *cb_access_context = GetAccessContext(commandBuffer);
4076 assert(cb_access_context);
4077 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4078 auto *context = cb_access_context->GetCurrentAccessContext();
4079 assert(context);
4080
locke-lunarg61870c22020-06-09 14:51:50 -06004081 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4082 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004083}
4084
4085bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4086 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004087 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004088 const auto *cb_access_context = GetAccessContext(commandBuffer);
4089 assert(cb_access_context);
4090 if (!cb_access_context) return skip;
4091
locke-lunarg61870c22020-06-09 14:51:50 -06004092 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4093 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4094 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004095 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004096}
4097
4098void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4099 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004100 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004101 auto *cb_access_context = GetAccessContext(commandBuffer);
4102 assert(cb_access_context);
4103 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004104
locke-lunarg61870c22020-06-09 14:51:50 -06004105 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4106 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4107 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004108}
4109
4110bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4111 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004112 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004113 const auto *cb_access_context = GetAccessContext(commandBuffer);
4114 assert(cb_access_context);
4115 if (!cb_access_context) return skip;
4116
locke-lunarg61870c22020-06-09 14:51:50 -06004117 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4118 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4119 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004120 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004121}
4122
4123void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4124 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004125 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
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_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004129
locke-lunarg61870c22020-06-09 14:51:50 -06004130 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4131 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4132 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004133}
4134
4135bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4136 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004137 bool skip = false;
4138 if (drawCount == 0) return skip;
4139
locke-lunargff255f92020-05-13 18:53:52 -06004140 const auto *cb_access_context = GetAccessContext(commandBuffer);
4141 assert(cb_access_context);
4142 if (!cb_access_context) return skip;
4143
4144 const auto *context = cb_access_context->GetCurrentAccessContext();
4145 assert(context);
4146 if (!context) return skip;
4147
locke-lunarg61870c22020-06-09 14:51:50 -06004148 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4149 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004150 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4151 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004152
4153 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4154 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4155 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004156 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004157 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004158}
4159
4160void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4161 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004162 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004163 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004164 auto *cb_access_context = GetAccessContext(commandBuffer);
4165 assert(cb_access_context);
4166 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4167 auto *context = cb_access_context->GetCurrentAccessContext();
4168 assert(context);
4169
locke-lunarg61870c22020-06-09 14:51:50 -06004170 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4171 cb_access_context->RecordDrawSubpassAttachment(tag);
4172 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004173
4174 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4175 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4176 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004177 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004178}
4179
4180bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4181 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004182 bool skip = false;
4183 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004184 const auto *cb_access_context = GetAccessContext(commandBuffer);
4185 assert(cb_access_context);
4186 if (!cb_access_context) return skip;
4187
4188 const auto *context = cb_access_context->GetCurrentAccessContext();
4189 assert(context);
4190 if (!context) return skip;
4191
locke-lunarg61870c22020-06-09 14:51:50 -06004192 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4193 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004194 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4195 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004196
4197 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4198 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4199 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004200 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004201 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004202}
4203
4204void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4205 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004206 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004207 auto *cb_access_context = GetAccessContext(commandBuffer);
4208 assert(cb_access_context);
4209 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4210 auto *context = cb_access_context->GetCurrentAccessContext();
4211 assert(context);
4212
locke-lunarg61870c22020-06-09 14:51:50 -06004213 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4214 cb_access_context->RecordDrawSubpassAttachment(tag);
4215 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004216
4217 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4218 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4219 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004220 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004221}
4222
4223bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4224 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4225 uint32_t stride, const char *function) const {
4226 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004227 const auto *cb_access_context = GetAccessContext(commandBuffer);
4228 assert(cb_access_context);
4229 if (!cb_access_context) return skip;
4230
4231 const auto *context = cb_access_context->GetCurrentAccessContext();
4232 assert(context);
4233 if (!context) return skip;
4234
locke-lunarg61870c22020-06-09 14:51:50 -06004235 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4236 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004237 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4238 maxDrawCount, stride, function);
4239 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004240
4241 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4242 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4243 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004244 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004245 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004246}
4247
4248bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4249 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4250 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004251 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4252 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004253}
4254
4255void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4256 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4257 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004258 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4259 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004260 auto *cb_access_context = GetAccessContext(commandBuffer);
4261 assert(cb_access_context);
4262 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
4263 auto *context = cb_access_context->GetCurrentAccessContext();
4264 assert(context);
4265
locke-lunarg61870c22020-06-09 14:51:50 -06004266 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4267 cb_access_context->RecordDrawSubpassAttachment(tag);
4268 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4269 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004270
4271 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4272 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4273 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004274 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004275}
4276
4277bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4278 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4279 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004280 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4281 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004282}
4283
4284void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4285 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4286 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004287 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4288 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004289 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004290}
4291
4292bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4293 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4294 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004295 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4296 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004297}
4298
4299void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4300 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4301 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004302 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4303 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004304 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4305}
4306
4307bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4308 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4309 uint32_t stride, const char *function) const {
4310 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004311 const auto *cb_access_context = GetAccessContext(commandBuffer);
4312 assert(cb_access_context);
4313 if (!cb_access_context) return skip;
4314
4315 const auto *context = cb_access_context->GetCurrentAccessContext();
4316 assert(context);
4317 if (!context) return skip;
4318
locke-lunarg61870c22020-06-09 14:51:50 -06004319 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4320 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004321 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4322 offset, maxDrawCount, stride, function);
4323 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004324
4325 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4326 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4327 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004328 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004329 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004330}
4331
4332bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4333 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4334 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004335 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4336 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004337}
4338
4339void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4340 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4341 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004342 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4343 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004344 auto *cb_access_context = GetAccessContext(commandBuffer);
4345 assert(cb_access_context);
4346 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4347 auto *context = cb_access_context->GetCurrentAccessContext();
4348 assert(context);
4349
locke-lunarg61870c22020-06-09 14:51:50 -06004350 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4351 cb_access_context->RecordDrawSubpassAttachment(tag);
4352 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4353 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004354
4355 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4356 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004357 // We will update the index and vertex buffer in SubmitQueue in the future.
4358 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004359}
4360
4361bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4362 VkDeviceSize offset, VkBuffer countBuffer,
4363 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4364 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004365 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4366 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004367}
4368
4369void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4370 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4371 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004372 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4373 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004374 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4375}
4376
4377bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4378 VkDeviceSize offset, VkBuffer countBuffer,
4379 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4380 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004381 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4382 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004383}
4384
4385void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4386 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4387 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004388 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4389 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004390 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4391}
4392
4393bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4394 const VkClearColorValue *pColor, uint32_t rangeCount,
4395 const VkImageSubresourceRange *pRanges) const {
4396 bool skip = false;
4397 const auto *cb_access_context = GetAccessContext(commandBuffer);
4398 assert(cb_access_context);
4399 if (!cb_access_context) return skip;
4400
4401 const auto *context = cb_access_context->GetCurrentAccessContext();
4402 assert(context);
4403 if (!context) return skip;
4404
4405 const auto *image_state = Get<IMAGE_STATE>(image);
4406
4407 for (uint32_t index = 0; index < rangeCount; index++) {
4408 const auto &range = pRanges[index];
4409 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004410 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004411 if (hazard.hazard) {
4412 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004413 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004414 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004415 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004416 }
4417 }
4418 }
4419 return skip;
4420}
4421
4422void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4423 const VkClearColorValue *pColor, uint32_t rangeCount,
4424 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004425 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004426 auto *cb_access_context = GetAccessContext(commandBuffer);
4427 assert(cb_access_context);
4428 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4429 auto *context = cb_access_context->GetCurrentAccessContext();
4430 assert(context);
4431
4432 const auto *image_state = Get<IMAGE_STATE>(image);
4433
4434 for (uint32_t index = 0; index < rangeCount; index++) {
4435 const auto &range = pRanges[index];
4436 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004437 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004438 }
4439 }
4440}
4441
4442bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4443 VkImageLayout imageLayout,
4444 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4445 const VkImageSubresourceRange *pRanges) const {
4446 bool skip = false;
4447 const auto *cb_access_context = GetAccessContext(commandBuffer);
4448 assert(cb_access_context);
4449 if (!cb_access_context) return skip;
4450
4451 const auto *context = cb_access_context->GetCurrentAccessContext();
4452 assert(context);
4453 if (!context) return skip;
4454
4455 const auto *image_state = Get<IMAGE_STATE>(image);
4456
4457 for (uint32_t index = 0; index < rangeCount; index++) {
4458 const auto &range = pRanges[index];
4459 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004460 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004461 if (hazard.hazard) {
4462 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004463 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004464 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004465 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004466 }
4467 }
4468 }
4469 return skip;
4470}
4471
4472void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4473 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4474 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004475 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004476 auto *cb_access_context = GetAccessContext(commandBuffer);
4477 assert(cb_access_context);
4478 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4479 auto *context = cb_access_context->GetCurrentAccessContext();
4480 assert(context);
4481
4482 const auto *image_state = Get<IMAGE_STATE>(image);
4483
4484 for (uint32_t index = 0; index < rangeCount; index++) {
4485 const auto &range = pRanges[index];
4486 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004487 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004488 }
4489 }
4490}
4491
4492bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4493 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4494 VkDeviceSize dstOffset, VkDeviceSize stride,
4495 VkQueryResultFlags flags) const {
4496 bool skip = false;
4497 const auto *cb_access_context = GetAccessContext(commandBuffer);
4498 assert(cb_access_context);
4499 if (!cb_access_context) return skip;
4500
4501 const auto *context = cb_access_context->GetCurrentAccessContext();
4502 assert(context);
4503 if (!context) return skip;
4504
4505 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4506
4507 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004508 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004509 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004510 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004511 skip |=
4512 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4513 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004514 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004515 }
4516 }
locke-lunargff255f92020-05-13 18:53:52 -06004517
4518 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004519 return skip;
4520}
4521
4522void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4523 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4524 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004525 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4526 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004527 auto *cb_access_context = GetAccessContext(commandBuffer);
4528 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004529 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004530 auto *context = cb_access_context->GetCurrentAccessContext();
4531 assert(context);
4532
4533 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4534
4535 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004536 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004537 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004538 }
locke-lunargff255f92020-05-13 18:53:52 -06004539
4540 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004541}
4542
4543bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4544 VkDeviceSize size, uint32_t data) const {
4545 bool skip = false;
4546 const auto *cb_access_context = GetAccessContext(commandBuffer);
4547 assert(cb_access_context);
4548 if (!cb_access_context) return skip;
4549
4550 const auto *context = cb_access_context->GetCurrentAccessContext();
4551 assert(context);
4552 if (!context) return skip;
4553
4554 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4555
4556 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004557 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004558 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004559 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004560 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004561 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004562 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004563 }
4564 }
4565 return skip;
4566}
4567
4568void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4569 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004570 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004571 auto *cb_access_context = GetAccessContext(commandBuffer);
4572 assert(cb_access_context);
4573 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4574 auto *context = cb_access_context->GetCurrentAccessContext();
4575 assert(context);
4576
4577 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4578
4579 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004580 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004581 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004582 }
4583}
4584
4585bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4586 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4587 const VkImageResolve *pRegions) const {
4588 bool skip = false;
4589 const auto *cb_access_context = GetAccessContext(commandBuffer);
4590 assert(cb_access_context);
4591 if (!cb_access_context) return skip;
4592
4593 const auto *context = cb_access_context->GetCurrentAccessContext();
4594 assert(context);
4595 if (!context) return skip;
4596
4597 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4598 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4599
4600 for (uint32_t region = 0; region < regionCount; region++) {
4601 const auto &resolve_region = pRegions[region];
4602 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004603 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004604 resolve_region.srcOffset, resolve_region.extent);
4605 if (hazard.hazard) {
4606 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004607 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004608 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004609 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004610 }
4611 }
4612
4613 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004614 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004615 resolve_region.dstOffset, resolve_region.extent);
4616 if (hazard.hazard) {
4617 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004618 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004619 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004620 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004621 }
4622 if (skip) break;
4623 }
4624 }
4625
4626 return skip;
4627}
4628
4629void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4630 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4631 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004632 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4633 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004634 auto *cb_access_context = GetAccessContext(commandBuffer);
4635 assert(cb_access_context);
4636 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4637 auto *context = cb_access_context->GetCurrentAccessContext();
4638 assert(context);
4639
4640 auto *src_image = Get<IMAGE_STATE>(srcImage);
4641 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4642
4643 for (uint32_t region = 0; region < regionCount; region++) {
4644 const auto &resolve_region = pRegions[region];
4645 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004646 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004647 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004648 }
4649 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004650 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004651 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004652 }
4653 }
4654}
4655
Jeff Leger178b1e52020-10-05 12:22:23 -04004656bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4657 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4658 bool skip = false;
4659 const auto *cb_access_context = GetAccessContext(commandBuffer);
4660 assert(cb_access_context);
4661 if (!cb_access_context) return skip;
4662
4663 const auto *context = cb_access_context->GetCurrentAccessContext();
4664 assert(context);
4665 if (!context) return skip;
4666
4667 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4668 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4669
4670 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4671 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4672 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004673 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004674 resolve_region.srcOffset, resolve_region.extent);
4675 if (hazard.hazard) {
4676 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4677 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4678 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004679 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004680 }
4681 }
4682
4683 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004684 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004685 resolve_region.dstOffset, resolve_region.extent);
4686 if (hazard.hazard) {
4687 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4688 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4689 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004690 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004691 }
4692 if (skip) break;
4693 }
4694 }
4695
4696 return skip;
4697}
4698
4699void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4700 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4701 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4702 auto *cb_access_context = GetAccessContext(commandBuffer);
4703 assert(cb_access_context);
4704 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4705 auto *context = cb_access_context->GetCurrentAccessContext();
4706 assert(context);
4707
4708 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4709 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4710
4711 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4712 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4713 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004714 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004715 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004716 }
4717 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004718 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004719 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004720 }
4721 }
4722}
4723
locke-lunarge1a67022020-04-29 00:15:36 -06004724bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4725 VkDeviceSize dataSize, const void *pData) const {
4726 bool skip = false;
4727 const auto *cb_access_context = GetAccessContext(commandBuffer);
4728 assert(cb_access_context);
4729 if (!cb_access_context) return skip;
4730
4731 const auto *context = cb_access_context->GetCurrentAccessContext();
4732 assert(context);
4733 if (!context) return skip;
4734
4735 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4736
4737 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004738 // VK_WHOLE_SIZE not allowed
4739 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004740 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004741 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004742 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004743 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004744 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004745 }
4746 }
4747 return skip;
4748}
4749
4750void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4751 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004752 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004753 auto *cb_access_context = GetAccessContext(commandBuffer);
4754 assert(cb_access_context);
4755 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4756 auto *context = cb_access_context->GetCurrentAccessContext();
4757 assert(context);
4758
4759 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4760
4761 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004762 // VK_WHOLE_SIZE not allowed
4763 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004764 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004765 }
4766}
locke-lunargff255f92020-05-13 18:53:52 -06004767
4768bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4769 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4770 bool skip = false;
4771 const auto *cb_access_context = GetAccessContext(commandBuffer);
4772 assert(cb_access_context);
4773 if (!cb_access_context) return skip;
4774
4775 const auto *context = cb_access_context->GetCurrentAccessContext();
4776 assert(context);
4777 if (!context) return skip;
4778
4779 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4780
4781 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004782 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004783 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06004784 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004785 skip |=
4786 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4787 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004788 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004789 }
4790 }
4791 return skip;
4792}
4793
4794void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4795 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004796 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004797 auto *cb_access_context = GetAccessContext(commandBuffer);
4798 assert(cb_access_context);
4799 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4800 auto *context = cb_access_context->GetCurrentAccessContext();
4801 assert(context);
4802
4803 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4804
4805 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004806 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004807 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004808 }
4809}
John Zulauf49beb112020-11-04 16:06:31 -07004810
4811bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
4812 bool skip = false;
4813 const auto *cb_context = GetAccessContext(commandBuffer);
4814 assert(cb_context);
4815 if (!cb_context) return skip;
4816
John Zulauf36ef9282021-02-02 11:47:24 -07004817 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004818 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004819}
4820
4821void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4822 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
4823 auto *cb_context = GetAccessContext(commandBuffer);
4824 assert(cb_context);
4825 if (!cb_context) return;
John Zulauf36ef9282021-02-02 11:47:24 -07004826 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4827 set_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004828}
4829
John Zulauf4edde622021-02-15 08:54:50 -07004830bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4831 const VkDependencyInfoKHR *pDependencyInfo) const {
4832 bool skip = false;
4833 const auto *cb_context = GetAccessContext(commandBuffer);
4834 assert(cb_context);
4835 if (!cb_context || !pDependencyInfo) return skip;
4836
4837 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4838 return set_event_op.Validate(*cb_context);
4839}
4840
4841void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4842 const VkDependencyInfoKHR *pDependencyInfo) {
4843 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
4844 auto *cb_context = GetAccessContext(commandBuffer);
4845 assert(cb_context);
4846 if (!cb_context || !pDependencyInfo) return;
4847
4848 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4849 set_event_op.Record(cb_context);
4850}
4851
John Zulauf49beb112020-11-04 16:06:31 -07004852bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
4853 VkPipelineStageFlags stageMask) const {
4854 bool skip = false;
4855 const auto *cb_context = GetAccessContext(commandBuffer);
4856 assert(cb_context);
4857 if (!cb_context) return skip;
4858
John Zulauf36ef9282021-02-02 11:47:24 -07004859 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004860 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004861}
4862
4863void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4864 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
4865 auto *cb_context = GetAccessContext(commandBuffer);
4866 assert(cb_context);
4867 if (!cb_context) return;
4868
John Zulauf36ef9282021-02-02 11:47:24 -07004869 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4870 reset_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004871}
4872
John Zulauf4edde622021-02-15 08:54:50 -07004873bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4874 VkPipelineStageFlags2KHR stageMask) const {
4875 bool skip = false;
4876 const auto *cb_context = GetAccessContext(commandBuffer);
4877 assert(cb_context);
4878 if (!cb_context) return skip;
4879
4880 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4881 return reset_event_op.Validate(*cb_context);
4882}
4883
4884void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4885 VkPipelineStageFlags2KHR stageMask) {
4886 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
4887 auto *cb_context = GetAccessContext(commandBuffer);
4888 assert(cb_context);
4889 if (!cb_context) return;
4890
4891 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4892 reset_event_op.Record(cb_context);
4893}
4894
John Zulauf49beb112020-11-04 16:06:31 -07004895bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4896 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4897 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4898 uint32_t bufferMemoryBarrierCount,
4899 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4900 uint32_t imageMemoryBarrierCount,
4901 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
4902 bool skip = false;
4903 const auto *cb_context = GetAccessContext(commandBuffer);
4904 assert(cb_context);
4905 if (!cb_context) return skip;
4906
John Zulauf36ef9282021-02-02 11:47:24 -07004907 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4908 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4909 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07004910 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004911}
4912
4913void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4914 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4915 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4916 uint32_t bufferMemoryBarrierCount,
4917 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4918 uint32_t imageMemoryBarrierCount,
4919 const VkImageMemoryBarrier *pImageMemoryBarriers) {
4920 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
4921 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
4922 imageMemoryBarrierCount, pImageMemoryBarriers);
4923
4924 auto *cb_context = GetAccessContext(commandBuffer);
4925 assert(cb_context);
4926 if (!cb_context) return;
4927
John Zulauf36ef9282021-02-02 11:47:24 -07004928 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4929 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4930 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
4931 return wait_events_op.Record(cb_context);
John Zulauf4a6105a2020-11-17 15:11:05 -07004932}
4933
John Zulauf4edde622021-02-15 08:54:50 -07004934bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4935 const VkDependencyInfoKHR *pDependencyInfos) const {
4936 bool skip = false;
4937 const auto *cb_context = GetAccessContext(commandBuffer);
4938 assert(cb_context);
4939 if (!cb_context) return skip;
4940
4941 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
4942 skip |= wait_events_op.Validate(*cb_context);
4943 return skip;
4944}
4945
4946void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4947 const VkDependencyInfoKHR *pDependencyInfos) {
4948 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
4949
4950 auto *cb_context = GetAccessContext(commandBuffer);
4951 assert(cb_context);
4952 if (!cb_context) return;
4953
4954 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
4955 wait_events_op.Record(cb_context);
4956}
4957
John Zulauf4a6105a2020-11-17 15:11:05 -07004958void SyncEventState::ResetFirstScope() {
4959 for (const auto address_type : kAddressTypes) {
4960 first_scope[static_cast<size_t>(address_type)].clear();
4961 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07004962 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07004963}
4964
4965// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07004966SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07004967 IgnoreReason reason = NotIgnored;
4968
John Zulauf4edde622021-02-15 08:54:50 -07004969 if ((CMD_WAITEVENTS2KHR == cmd) && (CMD_SETEVENT == last_command)) {
4970 reason = SetVsWait2;
4971 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
4972 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07004973 } else if (unsynchronized_set) {
4974 reason = SetRace;
4975 } else {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004976 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07004977 if (missing_bits) reason = MissingStageBits;
4978 }
4979
4980 return reason;
4981}
4982
Jeremy Gebben40a22942020-12-22 14:22:06 -07004983bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07004984 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
4985 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
4986 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07004987}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004988
John Zulauf36ef9282021-02-02 11:47:24 -07004989SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
4990 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4991 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07004992 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
4993 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
4994 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07004995 : SyncOpBase(cmd), barriers_(1) {
4996 auto &barrier_set = barriers_[0];
4997 barrier_set.dependency_flags = dependencyFlags;
4998 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
4999 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005000 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07005001 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
5002 pMemoryBarriers);
5003 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5004 bufferMemoryBarrierCount, pBufferMemoryBarriers);
5005 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5006 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005007}
5008
John Zulauf4edde622021-02-15 08:54:50 -07005009SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
5010 const VkDependencyInfoKHR *dep_infos)
5011 : SyncOpBase(cmd), barriers_(event_count) {
5012 for (uint32_t i = 0; i < event_count; i++) {
5013 const auto &dep_info = dep_infos[i];
5014 auto &barrier_set = barriers_[i];
5015 barrier_set.dependency_flags = dep_info.dependencyFlags;
5016 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5017 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5018 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5019 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5020 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5021 dep_info.pMemoryBarriers);
5022 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5023 dep_info.pBufferMemoryBarriers);
5024 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5025 dep_info.pImageMemoryBarriers);
5026 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005027}
5028
John Zulauf36ef9282021-02-02 11:47:24 -07005029SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005030 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5031 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5032 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5033 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5034 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005035 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005036 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5037
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005038SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5039 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005040 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005041
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005042bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5043 bool skip = false;
5044 const auto *context = cb_context.GetCurrentAccessContext();
5045 assert(context);
5046 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005047 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5048
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005049 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005050 const auto &barrier_set = barriers_[0];
5051 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5052 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5053 const auto *image_state = image_barrier.image.get();
5054 if (!image_state) continue;
5055 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5056 if (hazard.hazard) {
5057 // PHASE1 TODO -- add tag information to log msg when useful.
5058 const auto &sync_state = cb_context.GetSyncState();
5059 const auto image_handle = image_state->image;
5060 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5061 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5062 string_SyncHazard(hazard.hazard), image_barrier.index,
5063 sync_state.report_data->FormatHandle(image_handle).c_str(),
5064 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005065 }
5066 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005067 return skip;
5068}
5069
John Zulaufd5115702021-01-18 12:34:33 -07005070struct SyncOpPipelineBarrierFunctorFactory {
5071 using BarrierOpFunctor = PipelineBarrierOp;
5072 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5073 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5074 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5075 using BufferRange = ResourceAccessRange;
5076 using ImageRange = subresource_adapter::ImageRangeGenerator;
5077 using GlobalRange = ResourceAccessRange;
5078
5079 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5080 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5081 }
5082 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
5083 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5084 }
5085 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5086 return GlobalBarrierOpFunctor(barrier, false);
5087 }
5088
5089 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5090 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5091 const auto base_address = ResourceBaseAddress(buffer);
5092 return (range + base_address);
5093 }
John Zulauf110413c2021-03-20 05:38:38 -06005094 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005095 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005096
5097 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005098 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005099 return range_gen;
5100 }
5101 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5102};
5103
5104template <typename Barriers, typename FunctorFactory>
5105void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
5106 AccessContext *context) {
5107 for (const auto &barrier : barriers) {
5108 const auto *state = barrier.GetState();
5109 if (state) {
5110 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5111 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5112 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5113 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5114 }
5115 }
5116}
5117
5118template <typename Barriers, typename FunctorFactory>
5119void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
5120 AccessContext *access_context) {
5121 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5122 for (const auto &barrier : barriers) {
5123 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5124 }
5125 for (const auto address_type : kAddressTypes) {
5126 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5127 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5128 }
5129}
5130
John Zulauf36ef9282021-02-02 11:47:24 -07005131void SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005132 SyncOpPipelineBarrierFunctorFactory factory;
5133 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005134 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005135
John Zulauf4edde622021-02-15 08:54:50 -07005136 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5137 assert(barriers_.size() == 1);
5138 const auto &barrier_set = barriers_[0];
5139 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5140 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5141 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
5142
5143 if (barrier_set.single_exec_scope) {
5144 cb_context->ApplyGlobalBarriersToEvents(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
5145 } else {
5146 for (const auto &barrier : barrier_set.memory_barriers) {
5147 cb_context->ApplyGlobalBarriersToEvents(barrier.src_exec_scope, barrier.dst_exec_scope);
5148 }
5149 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005150}
5151
John Zulauf4edde622021-02-15 08:54:50 -07005152void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5153 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5154 const VkMemoryBarrier *barriers) {
5155 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005156 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005157 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005158 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005159 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005160 }
5161 if (0 == memory_barrier_count) {
5162 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005163 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005164 }
John Zulauf4edde622021-02-15 08:54:50 -07005165 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005166}
5167
John Zulauf4edde622021-02-15 08:54:50 -07005168void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5169 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5170 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5171 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005172 for (uint32_t index = 0; index < barrier_count; index++) {
5173 const auto &barrier = barriers[index];
5174 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5175 if (buffer) {
5176 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5177 const auto range = MakeRange(barrier.offset, barrier_size);
5178 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005179 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005180 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005181 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005182 }
5183 }
5184}
5185
John Zulauf4edde622021-02-15 08:54:50 -07005186void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
5187 uint32_t memory_barrier_count, const VkMemoryBarrier2KHR *barriers) {
5188 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005189 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005190 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005191 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5192 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5193 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005194 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005195 }
John Zulauf4edde622021-02-15 08:54:50 -07005196 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005197}
5198
John Zulauf4edde622021-02-15 08:54:50 -07005199void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5200 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5201 const VkBufferMemoryBarrier2KHR *barriers) {
5202 buffer_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 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5208 if (buffer) {
5209 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5210 const auto range = MakeRange(barrier.offset, barrier_size);
5211 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005212 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005213 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005214 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005215 }
5216 }
5217}
5218
John Zulauf4edde622021-02-15 08:54:50 -07005219void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5220 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5221 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5222 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005223 for (uint32_t index = 0; index < barrier_count; index++) {
5224 const auto &barrier = barriers[index];
5225 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5226 if (image) {
5227 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5228 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005229 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005230 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005231 image_memory_barriers.emplace_back();
5232 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005233 }
5234 }
5235}
John Zulaufd5115702021-01-18 12:34:33 -07005236
John Zulauf4edde622021-02-15 08:54:50 -07005237void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5238 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5239 const VkImageMemoryBarrier2KHR *barriers) {
5240 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005241 for (uint32_t index = 0; index < barrier_count; index++) {
5242 const auto &barrier = barriers[index];
5243 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5244 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5245 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5246 if (image) {
5247 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5248 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005249 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005250 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005251 image_memory_barriers.emplace_back();
5252 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005253 }
5254 }
5255}
5256
John Zulauf36ef9282021-02-02 11:47:24 -07005257SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005258 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5259 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5260 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5261 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005262 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005263 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5264 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005265 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005266}
5267
John Zulauf4edde622021-02-15 08:54:50 -07005268SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5269 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5270 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5271 MakeEventsList(sync_state, eventCount, pEvents);
5272 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5273}
5274
John Zulaufd5115702021-01-18 12:34:33 -07005275bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005276 const char *const ignored = "Wait operation is ignored for this event.";
5277 bool skip = false;
5278 const auto &sync_state = cb_context.GetSyncState();
5279 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer;
5280
John Zulauf4edde622021-02-15 08:54:50 -07005281 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5282 const auto &barrier_set = barriers_[barrier_set_index];
5283 if (barrier_set.single_exec_scope) {
5284 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5285 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5286 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5287 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5288 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5289 } else {
5290 const auto &barriers = barrier_set.memory_barriers;
5291 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5292 const auto &barrier = barriers[barrier_index];
5293 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5294 const std::string vuid =
5295 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5296 skip =
5297 sync_state.LogInfo(command_buffer_handle, vuid,
5298 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5299 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5300 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5301 }
5302 }
5303 }
5304 }
John Zulaufd5115702021-01-18 12:34:33 -07005305 }
5306
Jeremy Gebben40a22942020-12-22 14:22:06 -07005307 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005308 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005309 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005310 const auto *events_context = cb_context.GetCurrentEventsContext();
5311 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005312 size_t barrier_set_index = 0;
5313 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5314 for (size_t event_index = 0; event_index < events_.size(); event_index++)
5315 for (const auto &event : events_) {
5316 const auto *sync_event = events_context->Get(event.get());
5317 const auto &barrier_set = barriers_[barrier_set_index];
5318 if (!sync_event) {
5319 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5320 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5321 // new validation error... wait without previously submitted set event...
5322 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives at *record time*
5323 barrier_set_index += barrier_set_incr;
5324 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005325 }
John Zulauf4edde622021-02-15 08:54:50 -07005326 const auto event_handle = sync_event->event->event;
5327 // TODO add "destroyed" checks
5328
5329 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5330 const auto &src_exec_scope = barrier_set.src_exec_scope;
5331 event_stage_masks |= sync_event->scope.mask_param;
5332 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5333 if (ignore_reason) {
5334 switch (ignore_reason) {
5335 case SyncEventState::ResetWaitRace:
5336 case SyncEventState::Reset2WaitRace: {
5337 // Four permuations of Reset and Wait calls...
5338 const char *vuid =
5339 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5340 if (ignore_reason == SyncEventState::Reset2WaitRace) {
5341 vuid =
Jeremy Gebben476f5e22021-03-01 15:27:20 -07005342 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2KHR-event-03831" : "VUID-vkCmdResetEvent2KHR-event-03832";
John Zulauf4edde622021-02-15 08:54:50 -07005343 }
5344 const char *const message =
5345 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5346 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5347 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
5348 CommandTypeString(sync_event->last_command), ignored);
5349 break;
5350 }
5351 case SyncEventState::SetRace: {
5352 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5353 // this event
5354 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5355 const char *const message =
5356 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5357 const char *const reason = "First synchronization scope is undefined.";
5358 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5359 sync_state.report_data->FormatHandle(event_handle).c_str(),
5360 CommandTypeString(sync_event->last_command), reason, ignored);
5361 break;
5362 }
5363 case SyncEventState::MissingStageBits: {
5364 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5365 // Issue error message that event waited for is not in wait events scope
5366 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5367 const char *const message =
5368 "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5369 ". Bits missing from srcStageMask %s. %s";
5370 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5371 sync_state.report_data->FormatHandle(event_handle).c_str(),
5372 sync_event->scope.mask_param, src_exec_scope.mask_param,
5373 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), ignored);
5374 break;
5375 }
5376 case SyncEventState::SetVsWait2: {
5377 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2KHR-pEvents-03837",
5378 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5379 sync_state.report_data->FormatHandle(event_handle).c_str(),
5380 CommandTypeString(sync_event->last_command));
5381 break;
5382 }
5383 default:
5384 assert(ignore_reason == SyncEventState::NotIgnored);
5385 }
5386 } else if (barrier_set.image_memory_barriers.size()) {
5387 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5388 const auto *context = cb_context.GetCurrentAccessContext();
5389 assert(context);
5390 for (const auto &image_memory_barrier : image_memory_barriers) {
5391 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5392 const auto *image_state = image_memory_barrier.image.get();
5393 if (!image_state) continue;
John Zulauf110413c2021-03-20 05:38:38 -06005394 const auto &subresource_range = image_memory_barrier.range;
John Zulauf4edde622021-02-15 08:54:50 -07005395 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5396 const auto hazard =
5397 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5398 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5399 if (hazard.hazard) {
5400 skip |= sync_state.LogError(image_state->image, string_SyncHazardVUID(hazard.hazard),
5401 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5402 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
5403 sync_state.report_data->FormatHandle(image_state->image).c_str(),
5404 cb_context.FormatUsage(hazard).c_str());
5405 break;
5406 }
John Zulaufd5115702021-01-18 12:34:33 -07005407 }
5408 }
John Zulauf4edde622021-02-15 08:54:50 -07005409 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5410 // 03839
5411 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005412 }
John Zulaufd5115702021-01-18 12:34:33 -07005413
5414 // 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 -07005415 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 -07005416 if (extra_stage_bits) {
5417 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005418 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5419 const char *const vuid =
5420 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2KHR-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005421 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005422 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulaufd5115702021-01-18 12:34:33 -07005423 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005424 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005425 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005426 " vkCmdSetEvent may be in previously submitted command buffer.");
5427 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005428 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005429 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005430 }
5431 }
5432 return skip;
5433}
5434
5435struct SyncOpWaitEventsFunctorFactory {
5436 using BarrierOpFunctor = WaitEventBarrierOp;
5437 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5438 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5439 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5440 using BufferRange = EventSimpleRangeGenerator;
5441 using ImageRange = EventImageRangeGenerator;
5442 using GlobalRange = EventSimpleRangeGenerator;
5443
5444 // Need to restrict to only valid exec and access scope for this event
5445 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5446 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005447 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005448 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5449 return barrier;
5450 }
5451 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5452 auto barrier = RestrictToEvent(barrier_arg);
5453 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5454 }
5455 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
5456 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5457 }
5458 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5459 auto barrier = RestrictToEvent(barrier_arg);
5460 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5461 }
5462
5463 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5464 const AccessAddressType address_type = GetAccessAddressType(buffer);
5465 const auto base_address = ResourceBaseAddress(buffer);
5466 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5467 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5468 return filtered_range_gen;
5469 }
John Zulauf110413c2021-03-20 05:38:38 -06005470 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07005471 if (!SimpleBinding(image)) return ImageRange();
5472 const auto address_type = GetAccessAddressType(image);
5473 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005474 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005475 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5476
5477 return filtered_range_gen;
5478 }
5479 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5480 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5481 }
5482 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5483 SyncEventState *sync_event;
5484};
5485
John Zulauf36ef9282021-02-02 11:47:24 -07005486void SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
5487 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07005488 auto *access_context = cb_context->GetCurrentAccessContext();
5489 assert(access_context);
5490 if (!access_context) return;
John Zulauf669dfd52021-01-27 17:15:28 -07005491 auto *events_context = cb_context->GetCurrentEventsContext();
5492 assert(events_context);
5493 if (!events_context) return;
John Zulaufd5115702021-01-18 12:34:33 -07005494
5495 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
5496 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
5497 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
5498 access_context->ResolvePreviousAccesses();
5499
John Zulaufd5115702021-01-18 12:34:33 -07005500 // TODO... this needs change the SyncEventContext it's using depending on whether this is replay... the recorded
5501 // 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 -07005502 size_t barrier_set_index = 0;
5503 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5504 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07005505 for (auto &event_shared : events_) {
5506 if (!event_shared.get()) continue;
5507 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07005508
John Zulauf4edde622021-02-15 08:54:50 -07005509 sync_event->last_command = cmd_;
John Zulaufd5115702021-01-18 12:34:33 -07005510
John Zulauf4edde622021-02-15 08:54:50 -07005511 const auto &barrier_set = barriers_[barrier_set_index];
5512 const auto &dst = barrier_set.dst_exec_scope;
5513 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07005514 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
5515 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
5516 // of the barriers is maintained.
5517 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07005518 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5519 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5520 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07005521
5522 // Apply the global barrier to the event itself (for race condition tracking)
5523 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
5524 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
5525 sync_event->barriers |= dst.exec_scope;
5526 } else {
5527 // We ignored this wait, so we don't have any effective synchronization barriers for it.
5528 sync_event->barriers = 0U;
5529 }
John Zulauf4edde622021-02-15 08:54:50 -07005530 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005531 }
5532
5533 // Apply the pending barriers
5534 ResolvePendingBarrierFunctor apply_pending_action(tag);
5535 access_context->ApplyToContext(apply_pending_action);
5536}
5537
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005538bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5539 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5540 bool skip = false;
5541 const auto *cb_access_context = GetAccessContext(commandBuffer);
5542 assert(cb_access_context);
5543 if (!cb_access_context) return skip;
5544
5545 const auto *context = cb_access_context->GetCurrentAccessContext();
5546 assert(context);
5547 if (!context) return skip;
5548
5549 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5550
5551 if (dst_buffer) {
5552 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5553 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
5554 if (hazard.hazard) {
5555 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5556 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
5557 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
5558 string_UsageTag(hazard.tag).c_str());
5559 }
5560 }
5561 return skip;
5562}
5563
John Zulauf669dfd52021-01-27 17:15:28 -07005564void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07005565 events_.reserve(event_count);
5566 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
John Zulauf669dfd52021-01-27 17:15:28 -07005567 events_.emplace_back(sync_state.GetShared<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07005568 }
5569}
John Zulauf6ce24372021-01-30 05:56:25 -07005570
John Zulauf36ef9282021-02-02 11:47:24 -07005571SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005572 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005573 : SyncOpBase(cmd),
5574 event_(sync_state.GetShared<EVENT_STATE>(event)),
5575 exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005576
5577bool SyncOpResetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005578 auto *events_context = cb_context.GetCurrentEventsContext();
5579 assert(events_context);
5580 bool skip = false;
5581 if (!events_context) return skip;
5582
5583 const auto &sync_state = cb_context.GetSyncState();
5584 const auto *sync_event = events_context->Get(event_);
5585 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5586
5587 const char *const set_wait =
5588 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5589 "hazards.";
5590 const char *message = set_wait; // Only one message this call.
5591 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
5592 const char *vuid = nullptr;
5593 switch (sync_event->last_command) {
5594 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005595 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005596 // Needs a barrier between set and reset
5597 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
5598 break;
John Zulauf4edde622021-02-15 08:54:50 -07005599 case CMD_WAITEVENTS:
5600 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07005601 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
5602 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
5603 break;
5604 }
5605 default:
5606 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07005607 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
5608 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07005609 break;
5610 }
5611 if (vuid) {
John Zulauf36ef9282021-02-02 11:47:24 -07005612 skip |= sync_state.LogError(event_->event, vuid, message, CmdName(),
5613 sync_state.report_data->FormatHandle(event_->event).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005614 CommandTypeString(sync_event->last_command));
5615 }
5616 }
5617 return skip;
5618}
5619
John Zulauf36ef9282021-02-02 11:47:24 -07005620void SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005621 auto *events_context = cb_context->GetCurrentEventsContext();
5622 assert(events_context);
5623 if (!events_context) return;
5624
5625 auto *sync_event = events_context->GetFromShared(event_);
5626 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5627
5628 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07005629 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005630 sync_event->unsynchronized_set = CMD_NONE;
5631 sync_event->ResetFirstScope();
5632 sync_event->barriers = 0U;
5633}
5634
John Zulauf36ef9282021-02-02 11:47:24 -07005635SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005636 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005637 : SyncOpBase(cmd),
5638 event_(sync_state.GetShared<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07005639 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
5640 dep_info_() {}
5641
5642SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
5643 const VkDependencyInfoKHR &dep_info)
5644 : SyncOpBase(cmd),
5645 event_(sync_state.GetShared<EVENT_STATE>(event)),
5646 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
5647 dep_info_(new safe_VkDependencyInfoKHR(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005648
5649bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
5650 // 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 -07005651 bool skip = false;
5652
5653 const auto &sync_state = cb_context.GetSyncState();
5654 auto *events_context = cb_context.GetCurrentEventsContext();
5655 assert(events_context);
5656 if (!events_context) return skip;
5657
5658 const auto *sync_event = events_context->Get(event_);
5659 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5660
5661 const char *const reset_set =
5662 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5663 "hazards.";
5664 const char *const wait =
5665 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
5666
5667 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07005668 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07005669 const char *message = nullptr;
5670 switch (sync_event->last_command) {
5671 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005672 case CMD_RESETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005673 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07005674 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07005675 message = reset_set;
5676 break;
5677 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005678 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005679 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07005680 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07005681 message = reset_set;
5682 break;
5683 case CMD_WAITEVENTS:
John Zulauf4edde622021-02-15 08:54:50 -07005684 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005685 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07005686 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07005687 message = wait;
5688 break;
5689 default:
5690 // The only other valid last command that wasn't one.
5691 assert(sync_event->last_command == CMD_NONE);
5692 break;
5693 }
John Zulauf4edde622021-02-15 08:54:50 -07005694 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07005695 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07005696 std::string vuid("SYNC-");
5697 vuid.append(CmdName()).append(vuid_stem);
5698 skip |= sync_state.LogError(event_->event, vuid.c_str(), message, CmdName(),
John Zulauf36ef9282021-02-02 11:47:24 -07005699 sync_state.report_data->FormatHandle(event_->event).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005700 CommandTypeString(sync_event->last_command));
5701 }
5702 }
5703
5704 return skip;
5705}
5706
John Zulauf36ef9282021-02-02 11:47:24 -07005707void SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
5708 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07005709 auto *events_context = cb_context->GetCurrentEventsContext();
5710 auto *access_context = cb_context->GetCurrentAccessContext();
5711 assert(events_context);
5712 if (!events_context) return;
5713
5714 auto *sync_event = events_context->GetFromShared(event_);
5715 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5716
5717 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
5718 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
5719 // any issues caused by naive scope setting here.
5720
5721 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
5722 // Given:
5723 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
5724 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
5725
5726 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
5727 sync_event->unsynchronized_set = sync_event->last_command;
5728 sync_event->ResetFirstScope();
5729 } else if (sync_event->scope.exec_scope == 0) {
5730 // We only set the scope if there isn't one
5731 sync_event->scope = src_exec_scope_;
5732
5733 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
5734 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
5735 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
5736 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
5737 }
5738 };
5739 access_context->ForAll(set_scope);
5740 sync_event->unsynchronized_set = CMD_NONE;
5741 sync_event->first_scope_tag = tag;
5742 }
John Zulauf4edde622021-02-15 08:54:50 -07005743 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
5744 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005745 sync_event->barriers = 0U;
5746}
John Zulauf64ffe552021-02-06 10:25:07 -07005747
5748SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
5749 const VkRenderPassBeginInfo *pRenderPassBegin,
5750 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *cmd_name)
5751 : SyncOpBase(cmd, cmd_name) {
5752 if (pRenderPassBegin) {
5753 rp_state_ = sync_state.GetShared<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
5754 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
5755 const auto *fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
5756 if (fb_state) {
5757 shared_attachments_ = sync_state.GetSharedAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
5758 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
5759 // Note that this a safe to presist as long as shared_attachments is not cleared
5760 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08005761 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07005762 attachments_.emplace_back(attachment.get());
5763 }
5764 }
5765 if (pSubpassBeginInfo) {
5766 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
5767 }
5768 }
5769}
5770
5771bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5772 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
5773 bool skip = false;
5774
5775 assert(rp_state_.get());
5776 if (nullptr == rp_state_.get()) return skip;
5777 auto &rp_state = *rp_state_.get();
5778
5779 const uint32_t subpass = 0;
5780
5781 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
5782 // hasn't happened yet)
5783 const std::vector<AccessContext> empty_context_vector;
5784 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
5785 cb_context.GetCurrentAccessContext());
5786
5787 // Validate attachment operations
5788 if (attachments_.size() == 0) return skip;
5789 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07005790
5791 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
5792 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
5793 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
5794 // operations (though it's currently a messy approach)
5795 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
5796 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07005797
5798 // Validate load operations if there were no layout transition hazards
5799 if (!skip) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07005800 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kCurrentCommandTag);
5801 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07005802 }
5803
5804 return skip;
5805}
5806
5807void SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5808 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5809 assert(rp_state_.get());
5810 if (nullptr == rp_state_.get()) return;
5811 const auto tag = cb_context->NextCommandTag(cmd_);
5812 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
5813}
5814
5815SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
5816 const VkSubpassEndInfo *pSubpassEndInfo, const char *name_override)
5817 : SyncOpBase(cmd, name_override) {
5818 if (pSubpassBeginInfo) {
5819 subpass_begin_info_.initialize(pSubpassBeginInfo);
5820 }
5821 if (pSubpassEndInfo) {
5822 subpass_end_info_.initialize(pSubpassEndInfo);
5823 }
5824}
5825
5826bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
5827 bool skip = false;
5828 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5829 if (!renderpass_context) return skip;
5830
5831 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
5832 return skip;
5833}
5834
5835void SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
5836 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5837 cb_context->RecordNextSubpass(cmd_);
5838}
5839
5840SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo,
5841 const char *name_override)
5842 : SyncOpBase(cmd, name_override) {
5843 if (pSubpassEndInfo) {
5844 subpass_end_info_.initialize(pSubpassEndInfo);
5845 }
5846}
5847
5848bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5849 bool skip = false;
5850 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5851
5852 if (!renderpass_context) return skip;
5853 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
5854 return skip;
5855}
5856
5857void SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5858 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5859 cb_context->RecordEndRenderPass(cmd_);
5860}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005861
5862void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5863 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
5864 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
5865 auto *cb_access_context = GetAccessContext(commandBuffer);
5866 assert(cb_access_context);
5867 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5868 auto *context = cb_access_context->GetCurrentAccessContext();
5869 assert(context);
5870
5871 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5872
5873 if (dst_buffer) {
5874 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5875 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
5876 }
5877}
John Zulaufd05c5842021-03-26 11:32:16 -06005878
5879#ifdef SYNCVAL_DIAGNOSTICS
5880bool SyncValidator::PreCallValidateDestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) const {
5881 sync_diagnostics.InstanceDump(instance);
5882 ImageRangeGen::diag_.Report();
5883 return StateTracker::PreCallValidateDestroyInstance(instance, pAllocator);
5884}
5885#endif
John Zulaufd0ec59f2021-03-13 14:25:08 -07005886
5887AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
5888 : view_(view), view_mask_(), gen_store_() {
5889 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
5890 const IMAGE_STATE &image_state = *view_->image_state.get();
5891 const auto base_address = ResourceBaseAddress(image_state);
5892 const auto *encoder = image_state.fragment_encoder.get();
5893 if (!encoder) return;
5894 const VkOffset3D zero_offset = {0, 0, 0};
5895 const VkExtent3D &image_extent = image_state.createInfo.extent;
5896 // Intentional copy
5897 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
5898 view_mask_ = subres_range.aspectMask;
5899 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
5900 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5901
5902 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
5903 if (depth && (depth != view_mask_)) {
5904 subres_range.aspectMask = depth;
5905 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5906 }
5907 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
5908 if (stencil && (stencil != view_mask_)) {
5909 subres_range.aspectMask = stencil;
5910 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5911 }
5912}
5913
5914const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
5915 const ImageRangeGen *got = nullptr;
5916 switch (gen_type) {
5917 case kViewSubresource:
5918 got = &gen_store_[kViewSubresource];
5919 break;
5920 case kRenderArea:
5921 got = &gen_store_[kRenderArea];
5922 break;
5923 case kDepthOnlyRenderArea:
5924 got =
5925 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
5926 break;
5927 case kStencilOnlyRenderArea:
5928 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
5929 : &gen_store_[Gen::kStencilOnlyRenderArea];
5930 break;
5931 default:
5932 assert(got);
5933 }
5934 return got;
5935}
5936
5937AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
5938 assert(IsValid());
5939 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
5940 if (depth_op) {
5941 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
5942 if (stencil_op) {
5943 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
5944 return kRenderArea;
5945 }
5946 return kDepthOnlyRenderArea;
5947 }
5948 if (stencil_op) {
5949 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
5950 return kStencilOnlyRenderArea;
5951 }
5952
5953 assert(depth_op || stencil_op);
5954 return kRenderArea;
5955}
5956
5957AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }