blob: 5f49a7d65b278acb1cc222568157c0df88e8a955 [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>
552void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
553 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
554 VkExtent3D extent = CastTo3D(render_area.extent);
555 VkOffset3D offset = CastTo3D(render_area.offset);
556 const auto &rp_ci = rp_state.createInfo;
557 const auto *attachment_ci = rp_ci.pAttachments;
558 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
559
560 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
561 const auto *color_attachments = subpass_ci.pColorAttachments;
562 const auto *color_resolve = subpass_ci.pResolveAttachments;
563 if (color_resolve && color_attachments) {
564 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
565 const auto &color_attach = color_attachments[i].attachment;
566 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
567 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
568 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulauf8e3c3e92021-01-06 11:19:36 -0700569 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kColorAttachment, offset, extent, 0);
John Zulauf7635de32020-05-29 17:14:15 -0600570 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulauf8e3c3e92021-01-06 11:19:36 -0700571 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment, offset, extent, 0);
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;
587 VkImageAspectFlags aspect_mask = 0u;
588
589 // Figure out which aspects are actually touched during resolve operations
590 const char *aspect_string = nullptr;
591 if (resolve_depth && resolve_stencil) {
592 // Validate all aspects together
593 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
594 aspect_string = "depth/stencil";
595 } else if (resolve_depth) {
596 // Validate depth only
597 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
598 aspect_string = "depth";
599 } else if (resolve_stencil) {
600 // Validate all stencil only
601 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
602 aspect_string = "stencil";
603 }
604
605 if (aspect_mask) {
606 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
John Zulauf8e3c3e92021-01-06 11:19:36 -0700607 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster, offset, extent, aspect_mask);
John Zulauf7635de32020-05-29 17:14:15 -0600608 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
John Zulauf8e3c3e92021-01-06 11:19:36 -0700609 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, offset, extent, aspect_mask);
John Zulauf7635de32020-05-29 17:14:15 -0600610 }
611 }
612}
613
614// Action for validating resolve operations
615class ValidateResolveAction {
616 public:
John Zulauffaea0ee2021-01-14 14:01:32 -0700617 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context,
John Zulauf64ffe552021-02-06 10:25:07 -0700618 const CommandExecutionContext &ex_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600619 : render_pass_(render_pass),
620 subpass_(subpass),
621 context_(context),
John Zulauf64ffe552021-02-06 10:25:07 -0700622 ex_context_(ex_context),
John Zulauf7635de32020-05-29 17:14:15 -0600623 func_name_(func_name),
624 skip_(false) {}
625 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
John Zulauf8e3c3e92021-01-06 11:19:36 -0700626 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf7635de32020-05-29 17:14:15 -0600627 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
628 HazardResult hazard;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700629 hazard = context_.DetectHazard(view, current_usage, ordering_rule, offset, extent, aspect_mask);
John Zulauf7635de32020-05-29 17:14:15 -0600630 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -0700631 skip_ |=
John Zulauf64ffe552021-02-06 10:25:07 -0700632 ex_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700633 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
634 " to resolve attachment %" PRIu32 ". Access info %s.",
635 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name,
John Zulauf64ffe552021-02-06 10:25:07 -0700636 attachment_name, src_at, dst_at, ex_context_.FormatUsage(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600637 }
638 }
639 // Providing a mechanism for the constructing caller to get the result of the validation
640 bool GetSkip() const { return skip_; }
641
642 private:
643 VkRenderPass render_pass_;
644 const uint32_t subpass_;
645 const AccessContext &context_;
John Zulauf64ffe552021-02-06 10:25:07 -0700646 const CommandExecutionContext &ex_context_;
John Zulauf7635de32020-05-29 17:14:15 -0600647 const char *func_name_;
648 bool skip_;
649};
650
651// Update action for resolve operations
652class UpdateStateResolveAction {
653 public:
654 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
655 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
John Zulauf8e3c3e92021-01-06 11:19:36 -0700656 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf7635de32020-05-29 17:14:15 -0600657 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
658 // Ignores validation only arguments...
John Zulauf8e3c3e92021-01-06 11:19:36 -0700659 context_.UpdateAccessState(view, current_usage, ordering_rule, offset, extent, aspect_mask, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600660 }
661
662 private:
663 AccessContext &context_;
664 const ResourceUsageTag &tag_;
665};
666
John Zulauf59e25072020-07-17 10:55:21 -0600667void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700668 const SyncStageAccessFlags &prior_, const ResourceUsageTag &tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600669 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
670 usage_index = usage_index_;
671 hazard = hazard_;
672 prior_access = prior_;
673 tag = tag_;
674}
675
John Zulauf540266b2020-04-06 18:54:53 -0600676AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
677 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600678 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600679 Reset();
680 const auto &subpass_dep = dependencies[subpass];
John Zulauf22aefed2021-03-11 18:14:35 -0700681 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
682 prev_.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
John Zulauf355e49b2020-04-24 15:11:15 -0600683 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600684 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600685 const auto prev_pass = prev_dep.first->pass;
686 const auto &prev_barriers = prev_dep.second;
687 assert(prev_dep.second.size());
688 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
689 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700690 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600691
692 async_.reserve(subpass_dep.async.size());
693 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700694 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600695 }
John Zulauf22aefed2021-03-11 18:14:35 -0700696 if (has_barrier_from_external) {
697 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
698 prev_.emplace_back(external_context, queue_flags, subpass_dep.barrier_from_external);
699 src_external_ = &prev_.back();
John Zulaufe5da6e52020-03-18 15:32:18 -0600700 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600701 if (subpass_dep.barrier_to_external.size()) {
702 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600703 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700704}
705
John Zulauf5f13a792020-03-10 07:31:21 -0600706template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700707HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600708 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600709 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600710 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600711
712 HazardResult hazard;
713 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
714 hazard = detector.Detect(prev);
715 }
716 return hazard;
717}
718
John Zulauf4a6105a2020-11-17 15:11:05 -0700719template <typename Action>
720void AccessContext::ForAll(Action &&action) {
721 for (const auto address_type : kAddressTypes) {
722 auto &accesses = GetAccessStateMap(address_type);
723 for (const auto &access : accesses) {
724 action(address_type, access);
725 }
726 }
727}
728
John Zulauf3d84f1b2020-03-09 13:33:25 -0600729// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
730// the DAG of the contexts (for example subpasses)
731template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700732HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600733 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600734 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600735
John Zulauf1a224292020-06-30 14:52:13 -0600736 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600737 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
738 // so we'll check these first
739 for (const auto &async_context : async_) {
740 hazard = async_context->DetectAsyncHazard(type, detector, range);
741 if (hazard.hazard) return hazard;
742 }
John Zulauf5f13a792020-03-10 07:31:21 -0600743 }
744
John Zulauf1a224292020-06-30 14:52:13 -0600745 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600746
John Zulauf69133422020-05-20 14:55:53 -0600747 const auto &accesses = GetAccessStateMap(type);
748 const auto from = accesses.lower_bound(range);
749 const auto to = accesses.upper_bound(range);
750 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600751
John Zulauf69133422020-05-20 14:55:53 -0600752 for (auto pos = from; pos != to; ++pos) {
753 // Cover any leading gap, or gap between entries
754 if (detect_prev) {
755 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
756 // Cover any leading gap, or gap between entries
757 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600758 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600759 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600760 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600761 if (hazard.hazard) return hazard;
762 }
John Zulauf69133422020-05-20 14:55:53 -0600763 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
764 gap.begin = pos->first.end;
765 }
766
767 hazard = detector.Detect(pos);
768 if (hazard.hazard) return hazard;
769 }
770
771 if (detect_prev) {
772 // Detect in the trailing empty as needed
773 gap.end = range.end;
774 if (gap.non_empty()) {
775 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600776 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600777 }
778
779 return hazard;
780}
781
782// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
783template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700784HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
785 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600786 auto &accesses = GetAccessStateMap(type);
787 const auto from = accesses.lower_bound(range);
788 const auto to = accesses.upper_bound(range);
789
John Zulauf3d84f1b2020-03-09 13:33:25 -0600790 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600791 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700792 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600793 }
John Zulauf16adfc92020-04-08 10:28:33 -0600794
John Zulauf3d84f1b2020-03-09 13:33:25 -0600795 return hazard;
796}
797
John Zulaufb02c1eb2020-10-06 16:33:36 -0600798struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700799 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600800 void operator()(ResourceAccessState *access) const {
801 assert(access);
802 access->ApplyBarriers(barriers, true);
803 }
804 const std::vector<SyncBarrier> &barriers;
805};
806
John Zulauf22aefed2021-03-11 18:14:35 -0700807struct ApplyTrackbackStackAction {
808 explicit ApplyTrackbackStackAction(const std::vector<SyncBarrier> &barriers_,
809 const ResourceAccessStateFunction *previous_barrier_ = nullptr)
810 : barriers(barriers_), previous_barrier(previous_barrier_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600811 void operator()(ResourceAccessState *access) const {
812 assert(access);
813 assert(!access->HasPendingState());
814 access->ApplyBarriers(barriers, false);
815 access->ApplyPendingBarriers(kCurrentCommandTag);
John Zulauf22aefed2021-03-11 18:14:35 -0700816 if (previous_barrier) {
817 assert(bool(*previous_barrier));
818 (*previous_barrier)(access);
819 }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600820 }
821 const std::vector<SyncBarrier> &barriers;
John Zulauf22aefed2021-03-11 18:14:35 -0700822 const ResourceAccessStateFunction *previous_barrier;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600823};
824
825// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
826// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
827// *different* map from dest.
828// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
829// range [first, last)
830template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600831static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
832 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600833 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600834 auto at = entry;
835 for (auto pos = first; pos != last; ++pos) {
836 // Every member of the input iterator range must fit within the remaining portion of entry
837 assert(at->first.includes(pos->first));
838 assert(at != dest->end());
839 // Trim up at to the same size as the entry to resolve
840 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600841 auto access = pos->second; // intentional copy
842 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600843 at->second.Resolve(access);
844 ++at; // Go to the remaining unused section of entry
845 }
846}
847
John Zulaufa0a98292020-09-18 09:30:10 -0600848static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
849 SyncBarrier merged = {};
850 for (const auto &barrier : barriers) {
851 merged.Merge(barrier);
852 }
853 return merged;
854}
855
John Zulaufb02c1eb2020-10-06 16:33:36 -0600856template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700857void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600858 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
859 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600860 if (!range.non_empty()) return;
861
John Zulauf355e49b2020-04-24 15:11:15 -0600862 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
863 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600864 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600865 if (current->pos_B->valid) {
866 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600867 auto access = src_pos->second; // intentional copy
868 barrier_action(&access);
869
John Zulauf16adfc92020-04-08 10:28:33 -0600870 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600871 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
872 trimmed->second.Resolve(access);
873 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600874 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600875 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600876 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600877 }
John Zulauf16adfc92020-04-08 10:28:33 -0600878 } else {
879 // we have to descend to fill this gap
880 if (recur_to_infill) {
John Zulauf22aefed2021-03-11 18:14:35 -0700881 ResourceAccessRange recurrence_range = current_range;
882 // The current context is empty for the current range, so recur to fill the gap.
883 // Since we will be recurring back up the DAG, expand the gap descent to cover the full range for which B
884 // is not valid, to minimize that recurrence
885 if (current->pos_B.at_end()) {
886 // Do the remainder here....
887 recurrence_range.end = range.end;
John Zulauf355e49b2020-04-24 15:11:15 -0600888 } else {
John Zulauf22aefed2021-03-11 18:14:35 -0700889 // Recur only over the range until B becomes valid (within the limits of range).
890 recurrence_range.end = std::min(range.end, current->pos_B->lower_bound->first.begin);
John Zulauf355e49b2020-04-24 15:11:15 -0600891 }
John Zulauf22aefed2021-03-11 18:14:35 -0700892 ResolvePreviousAccessStack(type, recurrence_range, resolve_map, infill_state, barrier_action);
893
John Zulauf355e49b2020-04-24 15:11:15 -0600894 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
895 // iterator of the outer while.
896
897 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
898 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
899 // we stepped on the dest map
John Zulauf22aefed2021-03-11 18:14:35 -0700900 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 -0600901 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600902 current.seek(seek_to);
903 } else if (!current->pos_A->valid && infill_state) {
904 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
905 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
906 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600907 }
John Zulauf5f13a792020-03-10 07:31:21 -0600908 }
John Zulauf16adfc92020-04-08 10:28:33 -0600909 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600910 }
John Zulauf1a224292020-06-30 14:52:13 -0600911
912 // Infill if range goes passed both the current and resolve map prior contents
913 if (recur_to_infill && (current->range.end < range.end)) {
914 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
John Zulauf22aefed2021-03-11 18:14:35 -0700915 ResolvePreviousAccessStack<BarrierAction>(type, trailing_fill_range, resolve_map, infill_state, barrier_action);
John Zulauf1a224292020-06-30 14:52:13 -0600916 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600917}
918
John Zulauf22aefed2021-03-11 18:14:35 -0700919template <typename BarrierAction>
920void AccessContext::ResolvePreviousAccessStack(AccessAddressType type, const ResourceAccessRange &range,
921 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
922 const BarrierAction &previous_barrier) const {
923 ResourceAccessStateFunction stacked_barrier(std::ref(previous_barrier));
924 ResolvePreviousAccess(type, range, descent_map, infill_state, &stacked_barrier);
925}
926
John Zulauf43cc7462020-12-03 12:33:12 -0700927void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
John Zulauf22aefed2021-03-11 18:14:35 -0700928 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
929 const ResourceAccessStateFunction *previous_barrier) const {
930 if (prev_.size() == 0) {
John Zulauf5f13a792020-03-10 07:31:21 -0600931 if (range.non_empty() && infill_state) {
John Zulauf22aefed2021-03-11 18:14:35 -0700932 // Fill the empty poritions of descent_map with the default_state with the barrier function applied (iff present)
933 ResourceAccessState state_copy;
934 if (previous_barrier) {
935 assert(bool(*previous_barrier));
936 state_copy = *infill_state;
937 (*previous_barrier)(&state_copy);
938 infill_state = &state_copy;
939 }
940 sparse_container::update_range_value(*descent_map, range, *infill_state,
941 sparse_container::value_precedence::prefer_dest);
John Zulauf5f13a792020-03-10 07:31:21 -0600942 }
943 } else {
944 // Look for something to fill the gap further along.
945 for (const auto &prev_dep : prev_) {
John Zulauf22aefed2021-03-11 18:14:35 -0700946 const ApplyTrackbackStackAction barrier_action(prev_dep.barriers, previous_barrier);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600947 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600948 }
John Zulauf5f13a792020-03-10 07:31:21 -0600949 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600950}
951
John Zulauf4a6105a2020-11-17 15:11:05 -0700952// Non-lazy import of all accesses, WaitEvents needs this.
953void AccessContext::ResolvePreviousAccesses() {
954 ResourceAccessState default_state;
John Zulauf22aefed2021-03-11 18:14:35 -0700955 if (!prev_.size()) return; // If no previous contexts, nothing to do
956
John Zulauf4a6105a2020-11-17 15:11:05 -0700957 for (const auto address_type : kAddressTypes) {
958 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
959 }
960}
961
John Zulauf43cc7462020-12-03 12:33:12 -0700962AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
963 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -0600964}
965
John Zulauf1507ee42020-05-18 11:33:09 -0600966static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
967 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
968 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
969 return stage_access;
970}
971static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
972 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
973 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
974 return stage_access;
975}
976
John Zulauf7635de32020-05-29 17:14:15 -0600977// Caller must manage returned pointer
978static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
979 uint32_t subpass, const VkRect2D &render_area,
980 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
981 auto *proxy = new AccessContext(context);
982 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600983 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600984 return proxy;
985}
986
John Zulaufb02c1eb2020-10-06 16:33:36 -0600987template <typename BarrierAction>
John Zulauf52446eb2020-10-22 16:40:08 -0600988class ResolveAccessRangeFunctor {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600989 public:
John Zulauf43cc7462020-12-03 12:33:12 -0700990 ResolveAccessRangeFunctor(const AccessContext &context, AccessAddressType address_type, ResourceAccessRangeMap *descent_map,
991 const ResourceAccessState *infill_state, BarrierAction &barrier_action)
John Zulauf52446eb2020-10-22 16:40:08 -0600992 : context_(context),
993 address_type_(address_type),
994 descent_map_(descent_map),
995 infill_state_(infill_state),
996 barrier_action_(barrier_action) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600997 ResolveAccessRangeFunctor() = delete;
998 void operator()(const ResourceAccessRange &range) const {
999 context_.ResolveAccessRange(address_type_, range, barrier_action_, descent_map_, infill_state_);
1000 }
1001
1002 private:
John Zulauf52446eb2020-10-22 16:40:08 -06001003 const AccessContext &context_;
John Zulauf43cc7462020-12-03 12:33:12 -07001004 const AccessAddressType address_type_;
John Zulauf52446eb2020-10-22 16:40:08 -06001005 ResourceAccessRangeMap *const descent_map_;
1006 const ResourceAccessState *infill_state_;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001007 BarrierAction &barrier_action_;
1008};
1009
John Zulaufb02c1eb2020-10-06 16:33:36 -06001010template <typename BarrierAction>
1011void AccessContext::ResolveAccessRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001012 BarrierAction &barrier_action, AccessAddressType address_type,
1013 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001014 const ResolveAccessRangeFunctor<BarrierAction> action(*this, address_type, descent_map, infill_state, barrier_action);
1015 ApplyOverImageRange(image_state, subresource_range, action);
John Zulauf62f10592020-04-03 12:20:02 -06001016}
1017
John Zulauf7635de32020-05-29 17:14:15 -06001018// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf64ffe552021-02-06 10:25:07 -07001019bool AccessContext::ValidateLayoutTransitions(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001020 const VkRect2D &render_area, uint32_t subpass,
1021 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1022 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001023 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -06001024 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
1025 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
1026 // those affects have not been recorded yet.
1027 //
1028 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1029 // to apply and only copy then, if this proves a hot spot.
1030 std::unique_ptr<AccessContext> proxy_for_prev;
1031 TrackBack proxy_track_back;
1032
John Zulauf355e49b2020-04-24 15:11:15 -06001033 const auto &transitions = rp_state.subpass_transitions[subpass];
1034 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -06001035 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
1036
1037 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
John Zulauf22aefed2021-03-11 18:14:35 -07001038 assert(track_back);
John Zulauf7635de32020-05-29 17:14:15 -06001039 if (prev_needs_proxy) {
1040 if (!proxy_for_prev) {
1041 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
1042 render_area, attachment_views));
1043 proxy_track_back = *track_back;
1044 proxy_track_back.context = proxy_for_prev.get();
1045 }
1046 track_back = &proxy_track_back;
1047 }
1048 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -06001049 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07001050 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001051 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1052 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
1053 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1054 string_VkImageLayout(transition.old_layout),
1055 string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07001056 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001057 }
1058 }
1059 return skip;
1060}
1061
John Zulauf64ffe552021-02-06 10:25:07 -07001062bool AccessContext::ValidateLoadOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001063 const VkRect2D &render_area, uint32_t subpass,
1064 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1065 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001066 bool skip = false;
1067 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1068 VkExtent3D extent = CastTo3D(render_area.extent);
1069 VkOffset3D offset = CastTo3D(render_area.offset);
John Zulaufa0a98292020-09-18 09:30:10 -06001070
John Zulauf1507ee42020-05-18 11:33:09 -06001071 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1072 if (subpass == rp_state.attachment_first_subpass[i]) {
1073 if (attachment_views[i] == nullptr) continue;
1074 const IMAGE_VIEW_STATE &view = *attachment_views[i];
1075 const IMAGE_STATE *image = view.image_state.get();
1076 if (image == nullptr) continue;
1077 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001078
1079 // Need check in the following way
1080 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1081 // vs. transition
1082 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1083 // for each aspect loaded.
1084
1085 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001086 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001087 const bool is_color = !(has_depth || has_stencil);
1088
1089 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001090 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001091
John Zulaufaff20662020-06-01 14:07:58 -06001092 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001093 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001094
John Zulaufb02c1eb2020-10-06 16:33:36 -06001095 auto hazard_range = view.normalized_subresource_range;
1096 bool checked_stencil = false;
1097 if (is_color) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001098 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, SyncOrdering::kColorAttachment, offset,
John Zulauf859089b2020-10-29 17:37:03 -06001099 extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001100 aspect = "color";
1101 } else {
1102 if (has_depth) {
1103 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001104 hazard = DetectHazard(*image, load_index, hazard_range, SyncOrdering::kDepthStencilAttachment, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001105 aspect = "depth";
1106 }
1107 if (!hazard.hazard && has_stencil) {
1108 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001109 hazard = DetectHazard(*image, stencil_load_index, hazard_range, SyncOrdering::kDepthStencilAttachment, offset,
1110 extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001111 aspect = "stencil";
1112 checked_stencil = true;
1113 }
1114 }
1115
1116 if (hazard.hazard) {
1117 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001118 const auto &sync_state = ex_context.GetSyncState();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001119 if (hazard.tag == kCurrentCommandTag) {
1120 // Hazard vs. ILT
1121 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1122 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1123 " aspect %s during load with loadOp %s.",
1124 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1125 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06001126 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1127 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001128 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001129 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf64ffe552021-02-06 10:25:07 -07001130 ex_context.FormatUsage(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001131 }
1132 }
1133 }
1134 }
1135 return skip;
1136}
1137
John Zulaufaff20662020-06-01 14:07:58 -06001138// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1139// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1140// store is part of the same Next/End operation.
1141// The latter is handled in layout transistion validation directly
John Zulauf64ffe552021-02-06 10:25:07 -07001142bool AccessContext::ValidateStoreOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufaff20662020-06-01 14:07:58 -06001143 const VkRect2D &render_area, uint32_t subpass,
1144 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1145 const char *func_name) const {
1146 bool skip = false;
1147 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1148 VkExtent3D extent = CastTo3D(render_area.extent);
1149 VkOffset3D offset = CastTo3D(render_area.offset);
1150
1151 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1152 if (subpass == rp_state.attachment_last_subpass[i]) {
1153 if (attachment_views[i] == nullptr) continue;
1154 const IMAGE_VIEW_STATE &view = *attachment_views[i];
1155 const IMAGE_STATE *image = view.image_state.get();
1156 if (image == nullptr) continue;
1157 const auto &ci = attachment_ci[i];
1158
1159 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1160 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1161 // sake, we treat DONT_CARE as writing.
1162 const bool has_depth = FormatHasDepth(ci.format);
1163 const bool has_stencil = FormatHasStencil(ci.format);
1164 const bool is_color = !(has_depth || has_stencil);
1165 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1166 if (!has_stencil && !store_op_stores) continue;
1167
1168 HazardResult hazard;
1169 const char *aspect = nullptr;
1170 bool checked_stencil = false;
1171 if (is_color) {
1172 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001173 view.normalized_subresource_range, SyncOrdering::kRaster, offset, extent);
John Zulaufaff20662020-06-01 14:07:58 -06001174 aspect = "color";
1175 } else {
1176 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1177 auto hazard_range = view.normalized_subresource_range;
1178 if (has_depth && store_op_stores) {
1179 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1180 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001181 SyncOrdering::kRaster, offset, extent);
John Zulaufaff20662020-06-01 14:07:58 -06001182 aspect = "depth";
1183 }
1184 if (!hazard.hazard && has_stencil && stencil_op_stores) {
1185 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1186 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001187 SyncOrdering::kRaster, offset, extent);
John Zulaufaff20662020-06-01 14:07:58 -06001188 aspect = "stencil";
1189 checked_stencil = true;
1190 }
1191 }
1192
1193 if (hazard.hazard) {
1194 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1195 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001196 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001197 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1198 " %s aspect during store with %s %s. Access info %s",
1199 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect,
John Zulauf64ffe552021-02-06 10:25:07 -07001200 op_type_string, store_op_string, ex_context.FormatUsage(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001201 }
1202 }
1203 }
1204 return skip;
1205}
1206
John Zulauf64ffe552021-02-06 10:25:07 -07001207bool AccessContext::ValidateResolveOperations(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufb027cdb2020-05-21 14:25:22 -06001208 const VkRect2D &render_area,
1209 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
1210 uint32_t subpass) const {
John Zulauf64ffe552021-02-06 10:25:07 -07001211 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, ex_context, func_name);
John Zulauf7635de32020-05-29 17:14:15 -06001212 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
1213 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001214}
1215
John Zulauf3d84f1b2020-03-09 13:33:25 -06001216class HazardDetector {
1217 SyncStageAccessIndex usage_index_;
1218
1219 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001220 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001221 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1222 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001223 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001224 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001225};
1226
John Zulauf69133422020-05-20 14:55:53 -06001227class HazardDetectorWithOrdering {
1228 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001229 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001230
1231 public:
1232 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001233 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001234 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001235 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1236 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001237 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001238 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001239};
1240
John Zulauf16adfc92020-04-08 10:28:33 -06001241HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001242 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001243 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001244 const auto base_address = ResourceBaseAddress(buffer);
1245 HazardDetector detector(usage_index);
1246 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001247}
1248
John Zulauf69133422020-05-20 14:55:53 -06001249template <typename Detector>
1250HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1251 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1252 const VkExtent3D &extent, DetectOptions options) const {
1253 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001254 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001255 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1256 base_address);
1257 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001258 for (; range_gen->non_empty(); ++range_gen) {
John Zulaufd05c5842021-03-26 11:32:16 -06001259#ifdef SYNCVAL_DIAGNOSTICS
1260 sync_diagnostics.Detect(*range_gen);
1261#endif
John Zulauf150e5332020-12-03 08:52:52 -07001262 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001263 if (hazard.hazard) return hazard;
1264 }
1265 return HazardResult();
1266}
1267
John Zulauf540266b2020-04-06 18:54:53 -06001268HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1269 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1270 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001271 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1272 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -06001273 return DetectHazard(image, current_usage, subresource_range, offset, extent);
1274}
1275
1276HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1277 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1278 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001279 HazardDetector detector(current_usage);
1280 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1281}
1282
1283HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001284 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001285 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001286 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001287 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001288}
1289
John Zulaufb027cdb2020-05-21 14:25:22 -06001290// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1291// should have reported the issue regarding an invalid attachment entry
1292HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001293 SyncOrdering ordering_rule, const VkOffset3D &offset, const VkExtent3D &extent,
John Zulaufb027cdb2020-05-21 14:25:22 -06001294 VkImageAspectFlags aspect_mask) const {
1295 if (view != nullptr) {
1296 const IMAGE_STATE *image = view->image_state.get();
1297 if (image != nullptr) {
1298 auto *detect_range = &view->normalized_subresource_range;
1299 VkImageSubresourceRange masked_range;
1300 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1301 masked_range = view->normalized_subresource_range;
1302 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1303 detect_range = &masked_range;
1304 }
1305
1306 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1307 if (detect_range->aspectMask) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001308 return DetectHazard(*image, current_usage, *detect_range, ordering_rule, offset, extent);
John Zulaufb027cdb2020-05-21 14:25:22 -06001309 }
1310 }
1311 }
1312 return HazardResult();
1313}
John Zulauf43cc7462020-12-03 12:33:12 -07001314
John Zulauf3d84f1b2020-03-09 13:33:25 -06001315class BarrierHazardDetector {
1316 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001317 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001318 SyncStageAccessFlags src_access_scope)
1319 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1320
John Zulauf5f13a792020-03-10 07:31:21 -06001321 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1322 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001323 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001324 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001325 // 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 -07001326 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001327 }
1328
1329 private:
1330 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001331 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001332 SyncStageAccessFlags src_access_scope_;
1333};
1334
John Zulauf4a6105a2020-11-17 15:11:05 -07001335class EventBarrierHazardDetector {
1336 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001337 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001338 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
1339 const ResourceUsageTag &scope_tag)
1340 : usage_index_(usage_index),
1341 src_exec_scope_(src_exec_scope),
1342 src_access_scope_(src_access_scope),
1343 event_scope_(event_scope),
1344 scope_pos_(event_scope.cbegin()),
1345 scope_end_(event_scope.cend()),
1346 scope_tag_(scope_tag) {}
1347
1348 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1349 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1350 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1351 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1352 if (scope_pos_ == scope_end_) return HazardResult();
1353 if (!scope_pos_->first.intersects(pos->first)) {
1354 event_scope_.lower_bound(pos->first);
1355 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1356 }
1357
1358 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1359 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1360 }
1361 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1362 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1363 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1364 }
1365
1366 private:
1367 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001368 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001369 SyncStageAccessFlags src_access_scope_;
1370 const SyncEventState::ScopeMap &event_scope_;
1371 SyncEventState::ScopeMap::const_iterator scope_pos_;
1372 SyncEventState::ScopeMap::const_iterator scope_end_;
1373 const ResourceUsageTag &scope_tag_;
1374};
1375
Jeremy Gebben40a22942020-12-22 14:22:06 -07001376HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001377 const SyncStageAccessFlags &src_access_scope,
1378 const VkImageSubresourceRange &subresource_range,
1379 const SyncEventState &sync_event, DetectOptions options) const {
1380 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1381 // first access scope map to use, and there's no easy way to plumb it in below.
1382 const auto address_type = ImageAddressType(image);
1383 const auto &event_scope = sync_event.FirstScope(address_type);
1384
1385 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1386 event_scope, sync_event.first_scope_tag);
1387 VkOffset3D zero_offset = {0, 0, 0};
1388 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
1389}
1390
Jeremy Gebben40a22942020-12-22 14:22:06 -07001391HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001392 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001393 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001394 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001395 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1396 VkOffset3D zero_offset = {0, 0, 0};
1397 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001398}
1399
Jeremy Gebben40a22942020-12-22 14:22:06 -07001400HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001401 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001402 const VkImageMemoryBarrier &barrier) const {
1403 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1404 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1405 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1406}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001407HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07001408 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope.exec_scope,
John Zulaufd5115702021-01-18 12:34:33 -07001409 image_barrier.barrier.src_access_scope, image_barrier.range.subresource_range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001410}
John Zulauf355e49b2020-04-24 15:11:15 -06001411
John Zulauf9cb530d2019-09-30 14:14:10 -06001412template <typename Flags, typename Map>
1413SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1414 SyncStageAccessFlags scope = 0;
1415 for (const auto &bit_scope : map) {
1416 if (flag_mask < bit_scope.first) break;
1417
1418 if (flag_mask & bit_scope.first) {
1419 scope |= bit_scope.second;
1420 }
1421 }
1422 return scope;
1423}
1424
Jeremy Gebben40a22942020-12-22 14:22:06 -07001425SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags2KHR stages) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001426 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1427}
1428
Jeremy Gebben40a22942020-12-22 14:22:06 -07001429SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags2KHR accesses) {
1430 return AccessScopeImpl(sync_utils::ExpandAccessFlags(accesses), syncStageAccessMaskByAccessBit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001431}
1432
Jeremy Gebben40a22942020-12-22 14:22:06 -07001433// Getting from stage mask and access mask to stage/access masks is something we need to be good at...
1434SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001435 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1436 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1437 // 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 -06001438 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1439}
1440
1441template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001442void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001443 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1444 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001445 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001446 auto pos = accesses->lower_bound(range);
1447 if (pos == accesses->end() || !pos->first.intersects(range)) {
1448 // The range is empty, fill it with a default value.
1449 pos = action.Infill(accesses, pos, range);
1450 } else if (range.begin < pos->first.begin) {
1451 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001452 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001453 } else if (pos->first.begin < range.begin) {
1454 // Trim the beginning if needed
1455 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1456 ++pos;
1457 }
1458
1459 const auto the_end = accesses->end();
1460 while ((pos != the_end) && pos->first.intersects(range)) {
1461 if (pos->first.end > range.end) {
1462 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1463 }
1464
1465 pos = action(accesses, pos);
1466 if (pos == the_end) break;
1467
1468 auto next = pos;
1469 ++next;
1470 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1471 // Need to infill if next is disjoint
1472 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001473 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001474 next = action.Infill(accesses, next, new_range);
1475 }
1476 pos = next;
1477 }
1478}
John Zulaufd5115702021-01-18 12:34:33 -07001479
1480// Give a comparable interface for range generators and ranges
1481template <typename Action>
1482inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1483 assert(range);
1484 UpdateMemoryAccessState(accesses, *range, action);
1485}
1486
John Zulauf4a6105a2020-11-17 15:11:05 -07001487template <typename Action, typename RangeGen>
1488void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1489 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001490 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 -07001491 for (; range_gen->non_empty(); ++range_gen) {
1492 UpdateMemoryAccessState(accesses, *range_gen, action);
1493 }
1494}
John Zulauf9cb530d2019-09-30 14:14:10 -06001495
1496struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001497 using Iterator = ResourceAccessRangeMap::iterator;
1498 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001499 // this is only called on gaps, and never returns a gap.
1500 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001501 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001502 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001503 }
John Zulauf5f13a792020-03-10 07:31:21 -06001504
John Zulauf5c5e88d2019-12-26 11:22:02 -07001505 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001506 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001507 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001508 return pos;
1509 }
1510
John Zulauf43cc7462020-12-03 12:33:12 -07001511 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001512 SyncOrdering ordering_rule_, const ResourceUsageTag &tag_)
1513 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001514 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001515 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001516 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001517 const SyncOrdering ordering_rule;
John Zulauf9cb530d2019-09-30 14:14:10 -06001518 const ResourceUsageTag &tag;
1519};
1520
John Zulauf4a6105a2020-11-17 15:11:05 -07001521// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001522struct PipelineBarrierOp {
1523 SyncBarrier barrier;
1524 bool layout_transition;
1525 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1526 : barrier(barrier_), layout_transition(layout_transition_) {}
1527 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001528 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001529 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1530};
John Zulauf4a6105a2020-11-17 15:11:05 -07001531// The barrier operation for wait events
1532struct WaitEventBarrierOp {
1533 const ResourceUsageTag *scope_tag;
1534 SyncBarrier barrier;
1535 bool layout_transition;
1536 WaitEventBarrierOp(const ResourceUsageTag &scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1537 : scope_tag(&scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
1538 WaitEventBarrierOp() = default;
1539 void operator()(ResourceAccessState *access_state) const {
1540 assert(scope_tag); // Not valid to have a non-scope op executed, default construct included for std::vector support
1541 access_state->ApplyBarrier(*scope_tag, barrier, layout_transition);
1542 }
1543};
John Zulauf1e331ec2020-12-04 18:29:38 -07001544
John Zulauf4a6105a2020-11-17 15:11:05 -07001545// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1546// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1547// of a collection is known/present.
John Zulauf1e331ec2020-12-04 18:29:38 -07001548template <typename BarrierOp>
John Zulauf89311b42020-09-29 16:28:47 -06001549class ApplyBarrierOpsFunctor {
1550 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001551 using Iterator = ResourceAccessRangeMap::iterator;
1552 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001553
John Zulauf5c5e88d2019-12-26 11:22:02 -07001554 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001555 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001556 for (const auto &op : barrier_ops_) {
1557 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001558 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001559
John Zulauf89311b42020-09-29 16:28:47 -06001560 if (resolve_) {
1561 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1562 // another walk
1563 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001564 }
1565 return pos;
1566 }
1567
John Zulauf89311b42020-09-29 16:28:47 -06001568 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulaufd5115702021-01-18 12:34:33 -07001569 ApplyBarrierOpsFunctor(bool resolve, size_t size_hint, const ResourceUsageTag &tag)
1570 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1571 barrier_ops_.reserve(size_hint);
1572 }
1573 void EmplaceBack(const BarrierOp &op) { barrier_ops_.emplace_back(op); }
John Zulauf89311b42020-09-29 16:28:47 -06001574
1575 private:
1576 bool resolve_;
John Zulaufd5115702021-01-18 12:34:33 -07001577 std::vector<BarrierOp> barrier_ops_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001578 const ResourceUsageTag &tag_;
1579};
1580
John Zulauf4a6105a2020-11-17 15:11:05 -07001581// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1582// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1583template <typename BarrierOp>
1584class ApplyBarrierFunctor {
1585 public:
1586 using Iterator = ResourceAccessRangeMap::iterator;
1587 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1588
1589 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1590 auto &access_state = pos->second;
1591 barrier_op_(&access_state);
1592 return pos;
1593 }
1594
1595 ApplyBarrierFunctor(const BarrierOp &barrier_op) : barrier_op_(barrier_op) {}
1596
1597 private:
John Zulaufd5115702021-01-18 12:34:33 -07001598 BarrierOp barrier_op_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001599};
1600
John Zulauf1e331ec2020-12-04 18:29:38 -07001601// This functor resolves the pendinging state.
1602class ResolvePendingBarrierFunctor {
1603 public:
1604 using Iterator = ResourceAccessRangeMap::iterator;
1605 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1606
1607 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1608 auto &access_state = pos->second;
1609 access_state.ApplyPendingBarriers(tag_);
1610 return pos;
1611 }
1612
1613 ResolvePendingBarrierFunctor(const ResourceUsageTag &tag) : tag_(tag) {}
1614
1615 private:
John Zulauf89311b42020-09-29 16:28:47 -06001616 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001617};
1618
John Zulauf8e3c3e92021-01-06 11:19:36 -07001619void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
1620 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
1621 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001622 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001623}
1624
John Zulauf8e3c3e92021-01-06 11:19:36 -07001625void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001626 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001627 if (!SimpleBinding(buffer)) return;
1628 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001629 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001630}
John Zulauf355e49b2020-04-24 15:11:15 -06001631
John Zulauf8e3c3e92021-01-06 11:19:36 -07001632void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001633 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001634 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001635 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001636 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001637 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1638 base_address);
1639 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001640 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001641 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001642 UpdateMemoryAccessState(&GetAccessStateMap(address_type), *range_gen, action);
John Zulauf5f13a792020-03-10 07:31:21 -06001643 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001644}
John Zulauf8e3c3e92021-01-06 11:19:36 -07001645void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
1646 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask,
1647 const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001648 if (view != nullptr) {
1649 const IMAGE_STATE *image = view->image_state.get();
1650 if (image != nullptr) {
1651 auto *update_range = &view->normalized_subresource_range;
1652 VkImageSubresourceRange masked_range;
1653 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1654 masked_range = view->normalized_subresource_range;
1655 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1656 update_range = &masked_range;
1657 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001658 UpdateAccessState(*image, current_usage, ordering_rule, *update_range, offset, extent, tag);
John Zulauf7635de32020-05-29 17:14:15 -06001659 }
1660 }
1661}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001662
John Zulauf8e3c3e92021-01-06 11:19:36 -07001663void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001664 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1665 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001666 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1667 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001668 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001669}
1670
John Zulauf540266b2020-04-06 18:54:53 -06001671template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001672void AccessContext::UpdateResourceAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001673 if (!SimpleBinding(buffer)) return;
1674 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf43cc7462020-12-03 12:33:12 -07001675 UpdateMemoryAccessState(&GetAccessStateMap(AccessAddressType::kLinear), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001676}
1677
1678template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001679void AccessContext::UpdateResourceAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1680 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001681 if (!SimpleBinding(image)) return;
1682 const auto address_type = ImageAddressType(image);
1683 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001684
John Zulauf16adfc92020-04-08 10:28:33 -06001685 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001686 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
1687 image.createInfo.extent, base_address);
1688
John Zulauf540266b2020-04-06 18:54:53 -06001689 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001690 UpdateMemoryAccessState(accesses, *range_gen, action);
John Zulauf540266b2020-04-06 18:54:53 -06001691 }
1692}
1693
John Zulauf7635de32020-05-29 17:14:15 -06001694void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1695 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1696 const ResourceUsageTag &tag) {
1697 UpdateStateResolveAction update(*this, tag);
1698 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1699}
1700
John Zulaufaff20662020-06-01 14:07:58 -06001701void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1702 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1703 const ResourceUsageTag &tag) {
1704 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1705 VkExtent3D extent = CastTo3D(render_area.extent);
1706 VkOffset3D offset = CastTo3D(render_area.offset);
1707
1708 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1709 if (rp_state.attachment_last_subpass[i] == subpass) {
1710 if (attachment_views[i] == nullptr) continue; // UNUSED
1711 const auto &view = *attachment_views[i];
1712 const IMAGE_STATE *image = view.image_state.get();
1713 if (image == nullptr) continue;
1714
1715 const auto &ci = attachment_ci[i];
1716 const bool has_depth = FormatHasDepth(ci.format);
1717 const bool has_stencil = FormatHasStencil(ci.format);
1718 const bool is_color = !(has_depth || has_stencil);
1719 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1720
1721 if (is_color && store_op_stores) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001722 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster,
1723 view.normalized_subresource_range, offset, extent, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001724 } else {
1725 auto update_range = view.normalized_subresource_range;
1726 if (has_depth && store_op_stores) {
1727 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001728 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster,
1729 update_range, offset, extent, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001730 }
1731 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1732 if (has_stencil && stencil_op_stores) {
1733 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001734 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster,
1735 update_range, offset, extent, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001736 }
1737 }
1738 }
1739 }
1740}
1741
John Zulauf540266b2020-04-06 18:54:53 -06001742template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001743void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001744 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001745 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001746 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001747 }
1748}
1749
1750void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001751 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1752 auto &context = contexts[subpass_index];
John Zulauf22aefed2021-03-11 18:14:35 -07001753 ApplyTrackbackStackAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001754 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001755 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001756 }
1757 }
1758}
1759
John Zulauf355e49b2020-04-24 15:11:15 -06001760// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001761HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001762 if (!attach_view) return HazardResult();
1763 const auto image_state = attach_view->image_state.get();
1764 if (!image_state) return HazardResult();
1765
John Zulauf355e49b2020-04-24 15:11:15 -06001766 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001767 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001768
1769 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001770 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1771 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufc523bf62021-02-16 08:20:34 -07001772 HazardResult hazard = track_back.context->DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope.exec_scope,
1773 merged_barrier.src_access_scope,
1774 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001775 if (!hazard.hazard) {
1776 // The Async hazard check is against the current context's async set.
John Zulaufc523bf62021-02-16 08:20:34 -07001777 hazard = DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope.exec_scope, merged_barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001778 attach_view->normalized_subresource_range, kDetectAsync);
1779 }
John Zulaufa0a98292020-09-18 09:30:10 -06001780
John Zulauf355e49b2020-04-24 15:11:15 -06001781 return hazard;
1782}
1783
John Zulaufb02c1eb2020-10-06 16:33:36 -06001784void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
1785 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1786 const ResourceUsageTag &tag) {
1787 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001788 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001789 for (const auto &transition : transitions) {
1790 const auto prev_pass = transition.prev_pass;
1791 const auto attachment_view = attachment_views[transition.attachment];
1792 if (!attachment_view) continue;
1793 const auto *image = attachment_view->image_state.get();
1794 if (!image) continue;
1795 if (!SimpleBinding(*image)) continue;
1796
1797 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1798 assert(trackback);
1799
1800 // Import the attachments into the current context
1801 const auto *prev_context = trackback->context;
1802 assert(prev_context);
1803 const auto address_type = ImageAddressType(*image);
1804 auto &target_map = GetAccessStateMap(address_type);
1805 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
1806 prev_context->ResolveAccessRange(*image, attachment_view->normalized_subresource_range, barrier_action, address_type,
John Zulauf646cc292020-10-23 09:16:45 -06001807 &target_map, &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001808 }
1809
John Zulauf86356ca2020-10-19 11:46:41 -06001810 // If there were no transitions skip this global map walk
1811 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001812 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001813 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001814 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001815}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001816
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001817void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
1818 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
John Zulauf669dfd52021-01-27 17:15:28 -07001819
1820 auto *events_context = GetCurrentEventsContext();
1821 assert(events_context);
1822 for (auto &event_pair : *events_context) {
John Zulauf4a6105a2020-11-17 15:11:05 -07001823 assert(event_pair.second); // Shouldn't be storing empty
1824 auto &sync_event = *event_pair.second;
1825 // 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 -07001826 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
1827 sync_event.barriers |= dst.exec_scope;
1828 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
John Zulauf4a6105a2020-11-17 15:11:05 -07001829 }
1830 }
1831}
1832
John Zulauf355e49b2020-04-24 15:11:15 -06001833
locke-lunarg61870c22020-06-09 14:51:50 -06001834bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1835 const char *func_name) const {
1836 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001837 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001838 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001839 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1840 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001841 return skip;
1842 }
1843
1844 using DescriptorClass = cvdescriptorset::DescriptorClass;
1845 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1846 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1847 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1848 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1849
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001850 for (const auto &stage_state : pipe->stage_state) {
1851 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1852 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001853 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001854 }
locke-lunarg61870c22020-06-09 14:51:50 -06001855 for (const auto &set_binding : stage_state.descriptor_uses) {
1856 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1857 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1858 set_binding.first.second);
1859 const auto descriptor_type = binding_it.GetType();
1860 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1861 auto array_idx = 0;
1862
1863 if (binding_it.IsVariableDescriptorCount()) {
1864 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1865 }
1866 SyncStageAccessIndex sync_index =
1867 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1868
1869 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1870 uint32_t index = i - index_range.start;
1871 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1872 switch (descriptor->GetClass()) {
1873 case DescriptorClass::ImageSampler:
1874 case DescriptorClass::Image: {
1875 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001876 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001877 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001878 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1879 img_view_state = image_sampler_descriptor->GetImageViewState();
1880 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001881 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001882 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1883 img_view_state = image_descriptor->GetImageViewState();
1884 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001885 }
1886 if (!img_view_state) continue;
1887 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1888 VkExtent3D extent = {};
1889 VkOffset3D offset = {};
1890 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1891 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1892 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1893 } else {
1894 extent = img_state->createInfo.extent;
1895 }
John Zulauf361fb532020-07-22 10:45:39 -06001896 HazardResult hazard;
1897 const auto &subresource_range = img_view_state->normalized_subresource_range;
1898 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1899 // Input attachments are subject to raster ordering rules
1900 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001901 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001902 } else {
1903 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1904 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001905 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001906 skip |= sync_state_->LogError(
1907 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001908 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1909 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001910 func_name, string_SyncHazard(hazard.hazard),
1911 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1912 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001913 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001914 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1915 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
John Zulauffaea0ee2021-01-14 14:01:32 -07001916 set_binding.first.second, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001917 }
1918 break;
1919 }
1920 case DescriptorClass::TexelBuffer: {
1921 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1922 if (!buf_view_state) continue;
1923 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001924 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001925 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001926 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001927 skip |= sync_state_->LogError(
1928 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001929 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1930 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001931 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1932 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001933 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001934 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1935 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001936 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001937 }
1938 break;
1939 }
1940 case DescriptorClass::GeneralBuffer: {
1941 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1942 auto buf_state = buffer_descriptor->GetBufferState();
1943 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001944 const ResourceAccessRange range =
1945 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001946 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001947 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001948 skip |= sync_state_->LogError(
1949 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001950 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1951 func_name, string_SyncHazard(hazard.hazard),
1952 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001953 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001954 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001955 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1956 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001957 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001958 }
1959 break;
1960 }
1961 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1962 default:
1963 break;
1964 }
1965 }
1966 }
1967 }
1968 return skip;
1969}
1970
1971void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1972 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001973 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001974 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001975 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1976 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001977 return;
1978 }
1979
1980 using DescriptorClass = cvdescriptorset::DescriptorClass;
1981 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1982 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1983 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1984 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1985
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001986 for (const auto &stage_state : pipe->stage_state) {
1987 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1988 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001989 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001990 }
locke-lunarg61870c22020-06-09 14:51:50 -06001991 for (const auto &set_binding : stage_state.descriptor_uses) {
1992 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1993 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1994 set_binding.first.second);
1995 const auto descriptor_type = binding_it.GetType();
1996 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1997 auto array_idx = 0;
1998
1999 if (binding_it.IsVariableDescriptorCount()) {
2000 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2001 }
2002 SyncStageAccessIndex sync_index =
2003 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
2004
2005 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2006 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2007 switch (descriptor->GetClass()) {
2008 case DescriptorClass::ImageSampler:
2009 case DescriptorClass::Image: {
2010 const IMAGE_VIEW_STATE *img_view_state = nullptr;
2011 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
2012 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
2013 } else {
2014 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
2015 }
2016 if (!img_view_state) continue;
2017 const IMAGE_STATE *img_state = img_view_state->image_state.get();
2018 VkExtent3D extent = {};
2019 VkOffset3D offset = {};
2020 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
2021 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
2022 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
2023 } else {
2024 extent = img_state->createInfo.extent;
2025 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07002026 SyncOrdering ordering_rule = (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)
2027 ? SyncOrdering::kRaster
2028 : SyncOrdering::kNonAttachment;
2029 current_context_->UpdateAccessState(*img_state, sync_index, ordering_rule,
2030 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002031 break;
2032 }
2033 case DescriptorClass::TexelBuffer: {
2034 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
2035 if (!buf_view_state) continue;
2036 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002037 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07002038 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002039 break;
2040 }
2041 case DescriptorClass::GeneralBuffer: {
2042 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
2043 auto buf_state = buffer_descriptor->GetBufferState();
2044 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002045 const ResourceAccessRange range =
2046 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07002047 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002048 break;
2049 }
2050 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2051 default:
2052 break;
2053 }
2054 }
2055 }
2056 }
2057}
2058
2059bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2060 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002061 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2062 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002063 return skip;
2064 }
2065
2066 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2067 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002068 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002069
2070 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002071 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002072 if (binding_description.binding < binding_buffers_size) {
2073 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002074 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002075
locke-lunarg1ae57d62020-11-18 10:49:19 -07002076 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002077 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2078 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002079 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_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 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 -06002083 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(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 }
2088 return skip;
2089}
2090
2091void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002092 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2093 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002094 return;
2095 }
2096 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2097 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002098 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002099
2100 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002101 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002102 if (binding_description.binding < binding_buffers_size) {
2103 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002104 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002105
locke-lunarg1ae57d62020-11-18 10:49:19 -07002106 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002107 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2108 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002109 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2110 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002111 }
2112 }
2113}
2114
2115bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2116 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002117 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002118 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002119 }
locke-lunarg61870c22020-06-09 14:51:50 -06002120
locke-lunarg1ae57d62020-11-18 10:49:19 -07002121 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002122 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002123 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2124 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002125 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002126 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002127 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002128 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 -06002129 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07002130 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002131 }
2132
2133 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2134 // We will detect more accurate range in the future.
2135 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2136 return skip;
2137}
2138
2139void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002140 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 -06002141
locke-lunarg1ae57d62020-11-18 10:49:19 -07002142 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002143 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002144 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2145 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002146 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002147
2148 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2149 // We will detect more accurate range in the future.
2150 RecordDrawVertex(UINT32_MAX, 0, tag);
2151}
2152
2153bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002154 bool skip = false;
2155 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002156 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002157 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002158}
2159
2160void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002161 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002162 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002163 }
locke-lunarg61870c22020-06-09 14:51:50 -06002164}
2165
John Zulauf64ffe552021-02-06 10:25:07 -07002166void CommandBufferAccessContext::RecordBeginRenderPass(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2167 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2168 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002169 // Create an access context the current renderpass.
John Zulauf64ffe552021-02-06 10:25:07 -07002170 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002171 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf64ffe552021-02-06 10:25:07 -07002172 current_renderpass_context_->RecordBeginRenderPass(tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002173 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002174}
2175
John Zulauf64ffe552021-02-06 10:25:07 -07002176void CommandBufferAccessContext::RecordNextSubpass(CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002177 assert(current_renderpass_context_);
John Zulauffaea0ee2021-01-14 14:01:32 -07002178 auto prev_tag = NextCommandTag(command);
2179 auto next_tag = NextSubcommandTag(command);
John Zulauf64ffe552021-02-06 10:25:07 -07002180 current_renderpass_context_->RecordNextSubpass(prev_tag, next_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002181 current_context_ = &current_renderpass_context_->CurrentContext();
2182}
2183
John Zulauf64ffe552021-02-06 10:25:07 -07002184void CommandBufferAccessContext::RecordEndRenderPass(CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002185 assert(current_renderpass_context_);
2186 if (!current_renderpass_context_) return;
2187
John Zulauf64ffe552021-02-06 10:25:07 -07002188 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, NextCommandTag(command));
John Zulauf355e49b2020-04-24 15:11:15 -06002189 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002190 current_renderpass_context_ = nullptr;
2191}
2192
John Zulauf4a6105a2020-11-17 15:11:05 -07002193void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2194 // Erase is okay with the key not being
John Zulauf669dfd52021-01-27 17:15:28 -07002195 const auto *event_state = sync_state_->Get<EVENT_STATE>(event);
2196 if (event_state) {
2197 GetCurrentEventsContext()->Destroy(event_state);
John Zulaufd5115702021-01-18 12:34:33 -07002198 }
2199}
2200
John Zulauf64ffe552021-02-06 10:25:07 -07002201bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
John Zulauffaea0ee2021-01-14 14:01:32 -07002202 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002203 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002204 const auto &sync_state = ex_context.GetSyncState();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002205 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2206 if (!pipe ||
2207 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002208 return skip;
2209 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002210 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002211 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
John Zulauf64ffe552021-02-06 10:25:07 -07002212 VkExtent3D extent = CastTo3D(render_area_.extent);
2213 VkOffset3D offset = CastTo3D(render_area_.offset);
locke-lunarg37047832020-06-12 13:44:45 -06002214
John Zulauf1a224292020-06-30 14:52:13 -06002215 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002216 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002217 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2218 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002219 if (location >= subpass.colorAttachmentCount ||
2220 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002221 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002222 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002223 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002224 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07002225 SyncOrdering::kColorAttachment, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06002226 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002227 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002228 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002229 func_name, string_SyncHazard(hazard.hazard),
2230 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2231 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002232 location, ex_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002233 }
2234 }
2235 }
locke-lunarg37047832020-06-12 13:44:45 -06002236
2237 // PHASE1 TODO: Add layout based read/vs. write selection.
2238 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002239 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002240 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002241 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002242 bool depth_write = false, stencil_write = false;
2243
2244 // PHASE1 TODO: These validation should be in core_checks.
2245 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002246 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2247 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002248 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2249 depth_write = true;
2250 }
2251 // PHASE1 TODO: It needs to check if stencil is writable.
2252 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2253 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2254 // PHASE1 TODO: These validation should be in core_checks.
2255 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002256 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002257 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2258 stencil_write = true;
2259 }
2260
2261 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2262 if (depth_write) {
2263 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002264 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07002265 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002266 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002267 skip |= sync_state.LogError(
2268 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002269 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002270 func_name, string_SyncHazard(hazard.hazard),
2271 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2272 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002273 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002274 }
2275 }
2276 if (stencil_write) {
2277 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002278 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07002279 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002280 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002281 skip |= sync_state.LogError(
2282 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002283 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002284 func_name, string_SyncHazard(hazard.hazard),
2285 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2286 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002287 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002288 }
locke-lunarg61870c22020-06-09 14:51:50 -06002289 }
2290 }
2291 return skip;
2292}
2293
John Zulauf64ffe552021-02-06 10:25:07 -07002294void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002295 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2296 if (!pipe ||
2297 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002298 return;
2299 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002300 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002301 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
John Zulauf64ffe552021-02-06 10:25:07 -07002302 VkExtent3D extent = CastTo3D(render_area_.extent);
2303 VkOffset3D offset = CastTo3D(render_area_.offset);
locke-lunarg61870c22020-06-09 14:51:50 -06002304
John Zulauf1a224292020-06-30 14:52:13 -06002305 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002306 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002307 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2308 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002309 if (location >= subpass.colorAttachmentCount ||
2310 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002311 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002312 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002313 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf8e3c3e92021-01-06 11:19:36 -07002314 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
2315 SyncOrdering::kColorAttachment, offset, extent, 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002316 }
2317 }
locke-lunarg37047832020-06-12 13:44:45 -06002318
2319 // PHASE1 TODO: Add layout based read/vs. write selection.
2320 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002321 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002322 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002323 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002324 bool depth_write = false, stencil_write = false;
2325
2326 // PHASE1 TODO: These validation should be in core_checks.
2327 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002328 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2329 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002330 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2331 depth_write = true;
2332 }
2333 // PHASE1 TODO: It needs to check if stencil is writable.
2334 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2335 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2336 // PHASE1 TODO: These validation should be in core_checks.
2337 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002338 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002339 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2340 stencil_write = true;
2341 }
2342
2343 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2344 if (depth_write) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002345 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2346 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT,
2347 tag);
locke-lunarg37047832020-06-12 13:44:45 -06002348 }
2349 if (stencil_write) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002350 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2351 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT,
2352 tag);
locke-lunarg37047832020-06-12 13:44:45 -06002353 }
locke-lunarg61870c22020-06-09 14:51:50 -06002354 }
2355}
2356
John Zulauf64ffe552021-02-06 10:25:07 -07002357bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002358 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002359 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002360 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002361 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002362 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002363 func_name);
2364
John Zulauf355e49b2020-04-24 15:11:15 -06002365 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002366 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002367 skip |=
2368 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002369 if (!skip) {
2370 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2371 // on a copy of the (empty) next context.
2372 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2373 AccessContext temp_context(next_context);
2374 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002375 skip |=
2376 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002377 }
John Zulauf7635de32020-05-29 17:14:15 -06002378 return skip;
2379}
John Zulauf64ffe552021-02-06 10:25:07 -07002380bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002381 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002382 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002383 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002384 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002385 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002386 func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002387 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002388 return skip;
2389}
2390
John Zulauf64ffe552021-02-06 10:25:07 -07002391AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
2392 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002393}
2394
John Zulauf64ffe552021-02-06 10:25:07 -07002395bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2396 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002397 bool skip = false;
2398
John Zulauf7635de32020-05-29 17:14:15 -06002399 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2400 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2401 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2402 // to apply and only copy then, if this proves a hot spot.
2403 std::unique_ptr<AccessContext> proxy_for_current;
2404
John Zulauf355e49b2020-04-24 15:11:15 -06002405 // Validate the "finalLayout" transitions to external
2406 // Get them from where there we're hidding in the extra entry.
2407 const auto &final_transitions = rp_state_->subpass_transitions.back();
2408 for (const auto &transition : final_transitions) {
2409 const auto &attach_view = attachment_views_[transition.attachment];
2410 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2411 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002412 auto *context = trackback.context;
2413
2414 if (transition.prev_pass == current_subpass_) {
2415 if (!proxy_for_current) {
2416 // 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 -07002417 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002418 }
2419 context = proxy_for_current.get();
2420 }
2421
John Zulaufa0a98292020-09-18 09:30:10 -06002422 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2423 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufc523bf62021-02-16 08:20:34 -07002424 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope.exec_scope,
John Zulaufa0a98292020-09-18 09:30:10 -06002425 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2426 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002427 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002428 skip |= ex_context.GetSyncState().LogError(
John Zulauffaea0ee2021-01-14 14:01:32 -07002429 rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2430 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2431 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2432 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2433 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002434 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002435 }
2436 }
2437 return skip;
2438}
2439
2440void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2441 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002442 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002443}
2444
John Zulauf64ffe552021-02-06 10:25:07 -07002445void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag &tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002446 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2447 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf64ffe552021-02-06 10:25:07 -07002448 VkExtent3D extent = CastTo3D(render_area_.extent);
2449 VkOffset3D offset = CastTo3D(render_area_.offset);
John Zulauf1507ee42020-05-18 11:33:09 -06002450
2451 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2452 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2453 if (attachment_views_[i] == nullptr) continue; // UNUSED
2454 const auto &view = *attachment_views_[i];
2455 const IMAGE_STATE *image = view.image_state.get();
2456 if (image == nullptr) continue;
2457
2458 const auto &ci = attachment_ci[i];
2459 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002460 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002461 const bool is_color = !(has_depth || has_stencil);
2462
2463 if (is_color) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002464 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), SyncOrdering::kColorAttachment,
2465 view.normalized_subresource_range, offset, extent, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002466 } else {
2467 auto update_range = view.normalized_subresource_range;
2468 if (has_depth) {
2469 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07002470 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp),
2471 SyncOrdering::kDepthStencilAttachment, update_range, offset, extent, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002472 }
2473 if (has_stencil) {
2474 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07002475 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp),
2476 SyncOrdering::kDepthStencilAttachment, update_range, offset, extent, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002477 }
2478 }
2479 }
2480 }
2481}
John Zulauf64ffe552021-02-06 10:25:07 -07002482RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2483 VkQueueFlags queue_flags,
2484 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2485 const AccessContext *external_context)
2486 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_(attachment_views) {
John Zulauf355e49b2020-04-24 15:11:15 -06002487 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002488 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002489 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002490 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002491 }
John Zulauf64ffe552021-02-06 10:25:07 -07002492}
2493void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
2494 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002495 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002496 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002497 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002498}
John Zulauf1507ee42020-05-18 11:33:09 -06002499
John Zulauf64ffe552021-02-06 10:25:07 -07002500void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag &prev_subpass_tag,
John Zulauffaea0ee2021-01-14 14:01:32 -07002501 const ResourceUsageTag &next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002502 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulauf64ffe552021-02-06 10:25:07 -07002503 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area_, attachment_views_, current_subpass_, prev_subpass_tag);
2504 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002505
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002506 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2507 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002508 current_subpass_++;
2509 assert(current_subpass_ < subpass_contexts_.size());
John Zulauffaea0ee2021-01-14 14:01:32 -07002510 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2511 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002512 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002513}
2514
John Zulauf64ffe552021-02-06 10:25:07 -07002515void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002516 // Add the resolve and store accesses
John Zulauf64ffe552021-02-06 10:25:07 -07002517 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area_, attachment_views_, current_subpass_, tag);
2518 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002519
John Zulauf355e49b2020-04-24 15:11:15 -06002520 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002521 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002522
2523 // Add the "finalLayout" transitions to external
2524 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002525 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2526 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2527 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002528 const auto &final_transitions = rp_state_->subpass_transitions.back();
2529 for (const auto &transition : final_transitions) {
2530 const auto &attachment = attachment_views_[transition.attachment];
2531 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002532 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002533 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002534 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002535 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002536 }
John Zulauf1e331ec2020-12-04 18:29:38 -07002537 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002538 }
2539}
2540
Jeremy Gebben40a22942020-12-22 14:22:06 -07002541SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002542 SyncExecScope result;
2543 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002544 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2545 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002546 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2547 return result;
2548}
2549
Jeremy Gebben40a22942020-12-22 14:22:06 -07002550SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002551 SyncExecScope result;
2552 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002553 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2554 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002555 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2556 return result;
2557}
2558
2559SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002560 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002561 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002562 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002563 dst_access_scope = 0;
2564}
2565
2566template <typename Barrier>
2567SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002568 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002569 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002570 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002571 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2572}
2573
2574SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002575 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2576 if (barrier) {
2577 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002578 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002579 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002580
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002581 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002582 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002583 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2584
2585 } else {
2586 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002587 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002588 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2589
2590 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002591 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002592 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2593 }
2594}
2595
2596template <typename Barrier>
2597SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2598 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2599 src_exec_scope = src.exec_scope;
2600 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2601
2602 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002603 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002604 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002605}
2606
John Zulaufb02c1eb2020-10-06 16:33:36 -06002607// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2608void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2609 for (const auto &barrier : barriers) {
2610 ApplyBarrier(barrier, layout_transition);
2611 }
2612}
2613
John Zulauf89311b42020-09-29 16:28:47 -06002614// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2615// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2616// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002617void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2618 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002619 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002620 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002621 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002622 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002623 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002624 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002625}
John Zulauf9cb530d2019-09-30 14:14:10 -06002626HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2627 HazardResult hazard;
2628 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002629 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002630 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002631 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002632 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002633 }
2634 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002635 // Write operation:
2636 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2637 // If reads exists -- test only against them because either:
2638 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2639 // * 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
2640 // the current write happens after the reads, so just test the write against the reades
2641 // Otherwise test against last_write
2642 //
2643 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002644 if (last_reads.size()) {
2645 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002646 if (IsReadHazard(usage_stage, read_access)) {
2647 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2648 break;
2649 }
2650 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002651 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002652 // Write-After-Write check -- if we have a previous write to test against
2653 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002654 }
2655 }
2656 return hazard;
2657}
2658
John Zulauf8e3c3e92021-01-06 11:19:36 -07002659HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering &ordering_rule) const {
2660 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06002661 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2662 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002663 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002664 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002665 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2666 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002667 if (IsRead(usage_bit)) {
2668 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2669 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2670 if (is_raw_hazard) {
2671 // NOTE: we know last_write is non-zero
2672 // See if the ordering rules save us from the simple RAW check above
2673 // First check to see if the current usage is covered by the ordering rules
2674 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2675 const bool usage_is_ordered =
2676 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2677 if (usage_is_ordered) {
2678 // Now see of the most recent write (or a subsequent read) are ordered
2679 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2680 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002681 }
2682 }
John Zulauf4285ee92020-09-23 10:20:52 -06002683 if (is_raw_hazard) {
2684 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2685 }
John Zulauf361fb532020-07-22 10:45:39 -06002686 } else {
2687 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002688 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002689 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002690 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002691 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002692 if (usage_write_is_ordered) {
2693 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2694 ordered_stages = GetOrderedStages(ordering);
2695 }
2696 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2697 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002698 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002699 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2700 if (IsReadHazard(usage_stage, read_access)) {
2701 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2702 break;
2703 }
John Zulaufd14743a2020-07-03 09:42:39 -06002704 }
2705 }
John Zulauf4285ee92020-09-23 10:20:52 -06002706 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002707 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002708 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002709 }
John Zulauf69133422020-05-20 14:55:53 -06002710 }
2711 }
2712 return hazard;
2713}
2714
John Zulauf2f952d22020-02-10 11:34:51 -07002715// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002716HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002717 HazardResult hazard;
2718 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002719 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2720 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2721 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002722 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002723 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002724 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002725 }
2726 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002727 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002728 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002729 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002730 // 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 -07002731 for (const auto &read_access : last_reads) {
2732 if (read_access.tag.index >= start_tag.index) {
2733 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002734 break;
2735 }
2736 }
John Zulauf2f952d22020-02-10 11:34:51 -07002737 }
2738 }
2739 return hazard;
2740}
2741
Jeremy Gebben40a22942020-12-22 14:22:06 -07002742HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002743 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002744 // Only supporting image layout transitions for now
2745 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2746 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002747 // only test for WAW if there no intervening read operations.
2748 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002749 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002750 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002751 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002752 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002753 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002754 break;
2755 }
2756 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002757 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2758 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2759 }
2760
2761 return hazard;
2762}
2763
Jeremy Gebben40a22942020-12-22 14:22:06 -07002764HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07002765 const SyncStageAccessFlags &src_access_scope,
2766 const ResourceUsageTag &event_tag) const {
2767 // Only supporting image layout transitions for now
2768 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2769 HazardResult hazard;
2770 // only test for WAW if there no intervening read operations.
2771 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2772
John Zulaufab7756b2020-12-29 16:10:16 -07002773 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002774 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
2775 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07002776 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002777 if (read_access.tag.IsBefore(event_tag)) {
2778 // The read is in the events first synchronization scope, so we use a barrier hazard check
2779 // If the read stage is not in the src sync scope
2780 // *AND* not execution chained with an existing sync barrier (that's the or)
2781 // then the barrier access is unsafe (R/W after R)
2782 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
2783 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2784 break;
2785 }
2786 } else {
2787 // The read not in the event first sync scope and so is a hazard vs. the layout transition
2788 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2789 }
2790 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002791 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002792 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
2793 if (write_tag.IsBefore(event_tag)) {
2794 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
2795 // So do a normal barrier hazard check
2796 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2797 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2798 }
2799 } else {
2800 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06002801 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2802 }
John Zulaufd14743a2020-07-03 09:42:39 -06002803 }
John Zulauf361fb532020-07-22 10:45:39 -06002804
John Zulauf0cb5be22020-01-23 12:18:22 -07002805 return hazard;
2806}
2807
John Zulauf5f13a792020-03-10 07:31:21 -06002808// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2809// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2810// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2811void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2812 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002813 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2814 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002815 *this = other;
2816 } else if (!other.write_tag.IsBefore(write_tag)) {
2817 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2818 // dependency chaining logic or any stage expansion)
2819 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002820 pending_write_barriers |= other.pending_write_barriers;
2821 pending_layout_transition |= other.pending_layout_transition;
2822 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002823
John Zulaufd14743a2020-07-03 09:42:39 -06002824 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07002825 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06002826 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07002827 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002828 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002829 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002830 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002831 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2832 // but we should wait on profiling data for that.
2833 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002834 auto &my_read = last_reads[my_read_index];
2835 if (other_read.stage == my_read.stage) {
2836 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002837 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002838 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002839 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002840 my_read.pending_dep_chain = other_read.pending_dep_chain;
2841 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2842 // May require tracking more than one access per stage.
2843 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002844 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06002845 // Since I'm overwriting the fragement stage read, also update the input attachment info
2846 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002847 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002848 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002849 } else if (other_read.tag.IsBefore(my_read.tag)) {
2850 // The read tags match so merge the barriers
2851 my_read.barriers |= other_read.barriers;
2852 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002853 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002854
John Zulauf5f13a792020-03-10 07:31:21 -06002855 break;
2856 }
2857 }
2858 } else {
2859 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07002860 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06002861 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002862 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002863 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002864 }
John Zulauf5f13a792020-03-10 07:31:21 -06002865 }
2866 }
John Zulauf361fb532020-07-22 10:45:39 -06002867 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002868 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2869 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07002870
2871 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
2872 // of the copy and other into this using the update first logic.
2873 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
2874 // of the other first_accesses... )
2875 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
2876 FirstAccesses firsts(std::move(first_accesses_));
2877 first_accesses_.clear();
2878 first_read_stages_ = 0U;
2879 auto a = firsts.begin();
2880 auto a_end = firsts.end();
2881 for (auto &b : other.first_accesses_) {
2882 // TODO: Determine whether "IsBefore" or "IsGloballyBefore" is needed...
2883 while (a != a_end && a->tag.IsBefore(b.tag)) {
2884 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2885 ++a;
2886 }
2887 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
2888 }
2889 for (; a != a_end; ++a) {
2890 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2891 }
2892 }
John Zulauf5f13a792020-03-10 07:31:21 -06002893}
2894
John Zulauf8e3c3e92021-01-06 11:19:36 -07002895void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag &tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002896 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2897 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002898 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002899 // Mulitple outstanding reads may be of interest and do dependency chains independently
2900 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2901 const auto usage_stage = PipelineStageBit(usage_index);
2902 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002903 for (auto &read_access : last_reads) {
2904 if (read_access.stage == usage_stage) {
2905 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002906 break;
2907 }
2908 }
2909 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07002910 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002911 last_read_stages |= usage_stage;
2912 }
John Zulauf4285ee92020-09-23 10:20:52 -06002913
2914 // 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 -07002915 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002916 // TODO Revisit re: multiple reads for a given stage
2917 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002918 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002919 } else {
2920 // Assume write
2921 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002922 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002923 }
John Zulauffaea0ee2021-01-14 14:01:32 -07002924 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06002925}
John Zulauf5f13a792020-03-10 07:31:21 -06002926
John Zulauf89311b42020-09-29 16:28:47 -06002927// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2928// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2929// We can overwrite them as *this* write is now after them.
2930//
2931// 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 -07002932void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002933 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06002934 last_read_stages = 0;
2935 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002936 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002937
2938 write_barriers = 0;
2939 write_dependency_chain = 0;
2940 write_tag = tag;
2941 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002942}
2943
John Zulauf89311b42020-09-29 16:28:47 -06002944// Apply the memory barrier without updating the existing barriers. The execution barrier
2945// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2946// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2947// replace the current write barriers or add to them, so accumulate to pending as well.
2948void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2949 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2950 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002951 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
2952 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
2953 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
2954 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07002955 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06002956 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07002957 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002958 }
John Zulauf89311b42020-09-29 16:28:47 -06002959 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2960 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002961
John Zulauf89311b42020-09-29 16:28:47 -06002962 if (!pending_layout_transition) {
2963 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2964 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002965 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06002966 // 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 -07002967 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
2968 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002969 }
2970 }
John Zulaufa0a98292020-09-18 09:30:10 -06002971 }
John Zulaufa0a98292020-09-18 09:30:10 -06002972}
2973
John Zulauf4a6105a2020-11-17 15:11:05 -07002974// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
2975// changes the "chaining" state, but to keep barriers independent. See discussion above.
2976void ResourceAccessState::ApplyBarrier(const ResourceUsageTag &scope_tag, const SyncBarrier &barrier, bool layout_transition) {
2977 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
2978 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
2979 // in order to know if it's in the excecution scope
2980 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
2981 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
2982 // errors w.r.t. "most recent" accesses.
2983 if (layout_transition || ((write_tag.IsBefore(scope_tag)) && (barrier.src_access_scope & last_write).any())) {
2984 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07002985 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002986 }
2987 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2988 pending_layout_transition |= layout_transition;
2989
2990 if (!pending_layout_transition) {
2991 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2992 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002993 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002994 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
2995 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
2996 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
2997 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
2998 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
2999 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3000 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulaufc523bf62021-02-16 08:20:34 -07003001 if (read_access.tag.IsBefore(scope_tag) &&
3002 (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers))) {
3003 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003004 }
3005 }
3006 }
3007}
John Zulauf89311b42020-09-29 16:28:47 -06003008void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
3009 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06003010 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
3011 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07003012 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf89311b42020-09-29 16:28:47 -06003013 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003014 }
John Zulauf89311b42020-09-29 16:28:47 -06003015
3016 // Apply the accumulate execution barriers (and thus update chaining information)
3017 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003018 for (auto &read_access : last_reads) {
3019 read_access.barriers |= read_access.pending_dep_chain;
3020 read_execution_barriers |= read_access.barriers;
3021 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003022 }
3023
3024 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3025 write_dependency_chain |= pending_write_dep_chain;
3026 write_barriers |= pending_write_barriers;
3027 pending_write_dep_chain = 0;
3028 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003029}
3030
John Zulauf59e25072020-07-17 10:55:21 -06003031// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07003032VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
3033 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003034
John Zulaufab7756b2020-12-29 16:10:16 -07003035 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003036 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003037 barriers = read_access.barriers;
3038 break;
John Zulauf59e25072020-07-17 10:55:21 -06003039 }
3040 }
John Zulauf4285ee92020-09-23 10:20:52 -06003041
John Zulauf59e25072020-07-17 10:55:21 -06003042 return barriers;
3043}
3044
Jeremy Gebben40a22942020-12-22 14:22:06 -07003045inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003046 assert(IsRead(usage));
3047 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3048 // * the previous reads are not hazards, and thus last_write must be visible and available to
3049 // any reads that happen after.
3050 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3051 // 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 -07003052 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003053}
3054
Jeremy Gebben40a22942020-12-22 14:22:06 -07003055VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003056 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07003057 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06003058 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003059 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003060 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003061 // 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 -07003062 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06003063 }
3064
3065 return ordered_stages;
3066}
3067
John Zulauffaea0ee2021-01-14 14:01:32 -07003068void ResourceAccessState::UpdateFirst(const ResourceUsageTag &tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
3069 // Only record until we record a write.
3070 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003071 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003072 if (0 == (usage_stage & first_read_stages_)) {
3073 // If this is a read we haven't seen or a write, record.
3074 first_read_stages_ |= usage_stage;
3075 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3076 }
3077 }
3078}
3079
John Zulaufd1f85d42020-04-15 12:23:15 -06003080void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003081 auto *access_context = GetAccessContextNoInsert(command_buffer);
3082 if (access_context) {
3083 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003084 }
3085}
3086
John Zulaufd1f85d42020-04-15 12:23:15 -06003087void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3088 auto access_found = cb_access_state.find(command_buffer);
3089 if (access_found != cb_access_state.end()) {
3090 access_found->second->Reset();
3091 cb_access_state.erase(access_found);
3092 }
3093}
3094
John Zulauf9cb530d2019-09-30 14:14:10 -06003095bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3096 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3097 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003098 const auto *cb_context = GetAccessContext(commandBuffer);
3099 assert(cb_context);
3100 if (!cb_context) return skip;
3101 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003102
John Zulauf3d84f1b2020-03-09 13:33:25 -06003103 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003104 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003105 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003106
3107 for (uint32_t region = 0; region < regionCount; region++) {
3108 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003109 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003110 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003111 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003112 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003113 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003114 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003115 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003116 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003117 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003118 }
John Zulauf16adfc92020-04-08 10:28:33 -06003119 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003120 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003121 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003122 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003123 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003124 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003125 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003126 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003127 }
3128 }
3129 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003130 }
3131 return skip;
3132}
3133
3134void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3135 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003136 auto *cb_context = GetAccessContext(commandBuffer);
3137 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003138 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003139 auto *context = cb_context->GetCurrentAccessContext();
3140
John Zulauf9cb530d2019-09-30 14:14:10 -06003141 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003142 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003143
3144 for (uint32_t region = 0; region < regionCount; region++) {
3145 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003146 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003147 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003148 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003149 }
John Zulauf16adfc92020-04-08 10:28:33 -06003150 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003151 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003152 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003153 }
3154 }
3155}
3156
John Zulauf4a6105a2020-11-17 15:11:05 -07003157void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3158 // Clear out events from the command buffer contexts
3159 for (auto &cb_context : cb_access_state) {
3160 cb_context.second->RecordDestroyEvent(event);
3161 }
3162}
3163
Jeff Leger178b1e52020-10-05 12:22:23 -04003164bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3165 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3166 bool skip = false;
3167 const auto *cb_context = GetAccessContext(commandBuffer);
3168 assert(cb_context);
3169 if (!cb_context) return skip;
3170 const auto *context = cb_context->GetCurrentAccessContext();
3171
3172 // If we have no previous accesses, we have no hazards
3173 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3174 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3175
3176 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3177 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3178 if (src_buffer) {
3179 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003180 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003181 if (hazard.hazard) {
3182 // TODO -- add tag information to log msg when useful.
3183 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3184 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3185 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003186 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003187 }
3188 }
3189 if (dst_buffer && !skip) {
3190 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003191 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003192 if (hazard.hazard) {
3193 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3194 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3195 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003196 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003197 }
3198 }
3199 if (skip) break;
3200 }
3201 return skip;
3202}
3203
3204void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3205 auto *cb_context = GetAccessContext(commandBuffer);
3206 assert(cb_context);
3207 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3208 auto *context = cb_context->GetCurrentAccessContext();
3209
3210 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3211 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3212
3213 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3214 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3215 if (src_buffer) {
3216 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003217 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003218 }
3219 if (dst_buffer) {
3220 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003221 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003222 }
3223 }
3224}
3225
John Zulauf5c5e88d2019-12-26 11:22:02 -07003226bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3227 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3228 const VkImageCopy *pRegions) const {
3229 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003230 const auto *cb_access_context = GetAccessContext(commandBuffer);
3231 assert(cb_access_context);
3232 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003233
John Zulauf3d84f1b2020-03-09 13:33:25 -06003234 const auto *context = cb_access_context->GetCurrentAccessContext();
3235 assert(context);
3236 if (!context) return skip;
3237
3238 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3239 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003240 for (uint32_t region = 0; region < regionCount; region++) {
3241 const auto &copy_region = pRegions[region];
3242 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003243 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003244 copy_region.srcOffset, copy_region.extent);
3245 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003246 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003247 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003248 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003249 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003250 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003251 }
3252
3253 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003254 VkExtent3D dst_copy_extent =
3255 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003256 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003257 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003258 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003259 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003260 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003261 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003262 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003263 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003264 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003265 }
3266 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003267
John Zulauf5c5e88d2019-12-26 11:22:02 -07003268 return skip;
3269}
3270
3271void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3272 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3273 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003274 auto *cb_access_context = GetAccessContext(commandBuffer);
3275 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003276 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003277 auto *context = cb_access_context->GetCurrentAccessContext();
3278 assert(context);
3279
John Zulauf5c5e88d2019-12-26 11:22:02 -07003280 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003281 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003282
3283 for (uint32_t region = 0; region < regionCount; region++) {
3284 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003285 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003286 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003287 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003288 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003289 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003290 VkExtent3D dst_copy_extent =
3291 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003292 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003293 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003294 }
3295 }
3296}
3297
Jeff Leger178b1e52020-10-05 12:22:23 -04003298bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3299 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3300 bool skip = false;
3301 const auto *cb_access_context = GetAccessContext(commandBuffer);
3302 assert(cb_access_context);
3303 if (!cb_access_context) return skip;
3304
3305 const auto *context = cb_access_context->GetCurrentAccessContext();
3306 assert(context);
3307 if (!context) return skip;
3308
3309 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3310 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
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 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003315 copy_region.srcOffset, copy_region.extent);
3316 if (hazard.hazard) {
3317 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3318 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3319 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003320 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003321 }
3322 }
3323
3324 if (dst_image) {
3325 VkExtent3D dst_copy_extent =
3326 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003327 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003328 copy_region.dstOffset, dst_copy_extent);
3329 if (hazard.hazard) {
3330 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3331 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3332 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003333 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003334 }
3335 if (skip) break;
3336 }
3337 }
3338
3339 return skip;
3340}
3341
3342void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3343 auto *cb_access_context = GetAccessContext(commandBuffer);
3344 assert(cb_access_context);
3345 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3346 auto *context = cb_access_context->GetCurrentAccessContext();
3347 assert(context);
3348
3349 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3350 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3351
3352 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3353 const auto &copy_region = pCopyImageInfo->pRegions[region];
3354 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003355 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003356 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003357 }
3358 if (dst_image) {
3359 VkExtent3D dst_copy_extent =
3360 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003361 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003362 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003363 }
3364 }
3365}
3366
John Zulauf9cb530d2019-09-30 14:14:10 -06003367bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3368 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3369 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3370 uint32_t bufferMemoryBarrierCount,
3371 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3372 uint32_t imageMemoryBarrierCount,
3373 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3374 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003375 const auto *cb_access_context = GetAccessContext(commandBuffer);
3376 assert(cb_access_context);
3377 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003378
John Zulauf36ef9282021-02-02 11:47:24 -07003379 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3380 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3381 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3382 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003383 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003384 return skip;
3385}
3386
3387void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3388 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3389 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3390 uint32_t bufferMemoryBarrierCount,
3391 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3392 uint32_t imageMemoryBarrierCount,
3393 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003394 auto *cb_access_context = GetAccessContext(commandBuffer);
3395 assert(cb_access_context);
3396 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003397
John Zulauf36ef9282021-02-02 11:47:24 -07003398 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3399 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3400 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3401 pImageMemoryBarriers);
3402 pipeline_barrier.Record(cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003403}
3404
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003405bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3406 const VkDependencyInfoKHR *pDependencyInfo) const {
3407 bool skip = false;
3408 const auto *cb_access_context = GetAccessContext(commandBuffer);
3409 assert(cb_access_context);
3410 if (!cb_access_context) return skip;
3411
3412 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3413 skip = pipeline_barrier.Validate(*cb_access_context);
3414 return skip;
3415}
3416
3417void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3418 auto *cb_access_context = GetAccessContext(commandBuffer);
3419 assert(cb_access_context);
3420 if (!cb_access_context) return;
3421
3422 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3423 pipeline_barrier.Record(cb_access_context);
3424}
3425
John Zulauf9cb530d2019-09-30 14:14:10 -06003426void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3427 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3428 // The state tracker sets up the device state
3429 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3430
John Zulauf5f13a792020-03-10 07:31:21 -06003431 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3432 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003433 // TODO: Find a good way to do this hooklessly.
3434 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3435 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3436 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3437
John Zulaufd1f85d42020-04-15 12:23:15 -06003438 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3439 sync_device_state->ResetCommandBufferCallback(command_buffer);
3440 });
3441 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3442 sync_device_state->FreeCommandBufferCallback(command_buffer);
3443 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003444}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003445
John Zulauf355e49b2020-04-24 15:11:15 -06003446bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf64ffe552021-02-06 10:25:07 -07003447 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd, const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003448 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003449 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003450 if (cb_context) {
3451 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo, cmd_name);
3452 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003453 }
John Zulauf355e49b2020-04-24 15:11:15 -06003454 return skip;
3455}
3456
3457bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3458 VkSubpassContents contents) const {
3459 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003460 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003461 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003462 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003463 return skip;
3464}
3465
3466bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003467 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003468 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003469 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003470 return skip;
3471}
3472
John Zulauf64ffe552021-02-06 10:25:07 -07003473static const char *kBeginRenderPass2KhrName = "vkCmdBeginRenderPass2KHR";
John Zulauf355e49b2020-04-24 15:11:15 -06003474bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3475 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003476 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003477 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003478 skip |=
3479 ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2, kBeginRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003480 return skip;
3481}
3482
John Zulauf3d84f1b2020-03-09 13:33:25 -06003483void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3484 VkResult result) {
3485 // The state tracker sets up the command buffer state
3486 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3487
3488 // Create/initialize the structure that trackers accesses at the command buffer scope.
3489 auto cb_access_context = GetAccessContext(commandBuffer);
3490 assert(cb_access_context);
3491 cb_access_context->Reset();
3492}
3493
3494void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf64ffe552021-02-06 10:25:07 -07003495 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd, const char *cmd_name) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003496 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003497 if (cb_context) {
John Zulauf64ffe552021-02-06 10:25:07 -07003498 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo, cmd_name);
3499 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003500 }
3501}
3502
3503void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3504 VkSubpassContents contents) {
3505 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003506 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003507 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003508 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003509}
3510
3511void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3512 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3513 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003514 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003515}
3516
3517void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3518 const VkRenderPassBeginInfo *pRenderPassBegin,
3519 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3520 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003521 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2, kBeginRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003522}
3523
Mike Schuchardt2df08912020-12-15 16:28:09 -08003524bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf64ffe552021-02-06 10:25:07 -07003525 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd, const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003526 bool skip = false;
3527
3528 auto cb_context = GetAccessContext(commandBuffer);
3529 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003530 if (!cb_context) return skip;
3531 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo, cmd_name);
3532 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003533}
3534
3535bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3536 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003537 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003538 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003539 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003540 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3541 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003542 return skip;
3543}
3544
John Zulauf64ffe552021-02-06 10:25:07 -07003545static const char *kNextSubpass2KhrName = "vkCmdNextSubpass2KHR";
Mike Schuchardt2df08912020-12-15 16:28:09 -08003546bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3547 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003548 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003549 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2, kNextSubpass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003550 return skip;
3551}
3552
3553bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3554 const VkSubpassEndInfo *pSubpassEndInfo) const {
3555 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003556 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003557 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003558}
3559
3560void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf64ffe552021-02-06 10:25:07 -07003561 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd, const char *cmd_name) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003562 auto cb_context = GetAccessContext(commandBuffer);
3563 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003564 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003565
John Zulauf64ffe552021-02-06 10:25:07 -07003566 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo, cmd_name);
3567 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003568}
3569
3570void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3571 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003572 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003573 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003574 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003575}
3576
3577void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3578 const VkSubpassEndInfo *pSubpassEndInfo) {
3579 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003580 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003581}
3582
3583void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3584 const VkSubpassEndInfo *pSubpassEndInfo) {
3585 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003586 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2, kNextSubpass2KhrName);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003587}
3588
John Zulauf64ffe552021-02-06 10:25:07 -07003589bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd,
3590 const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003591 bool skip = false;
3592
3593 auto cb_context = GetAccessContext(commandBuffer);
3594 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003595 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003596
John Zulauf64ffe552021-02-06 10:25:07 -07003597 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo, cmd_name);
3598 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003599 return skip;
3600}
3601
3602bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3603 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003604 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003605 return skip;
3606}
3607
Mike Schuchardt2df08912020-12-15 16:28:09 -08003608bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003609 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003610 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003611 return skip;
3612}
3613
John Zulauf64ffe552021-02-06 10:25:07 -07003614const static char *kEndRenderPass2KhrName = "vkEndRenderPass2KHR";
John Zulauf355e49b2020-04-24 15:11:15 -06003615bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003616 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003617 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003618 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2, kEndRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003619 return skip;
3620}
3621
John Zulauf64ffe552021-02-06 10:25:07 -07003622void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd,
3623 const char *cmd_name) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003624 // Resolve the all subpass contexts to the command buffer contexts
3625 auto cb_context = GetAccessContext(commandBuffer);
3626 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003627 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003628
John Zulauf64ffe552021-02-06 10:25:07 -07003629 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo, cmd_name);
3630 sync_op.Record(cb_context);
3631 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003632}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003633
John Zulauf33fc1d52020-07-17 11:01:10 -06003634// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3635// updates to a resource which do not conflict at the byte level.
3636// TODO: Revisit this rule to see if it needs to be tighter or looser
3637// TODO: Add programatic control over suppression heuristics
3638bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3639 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3640}
3641
John Zulauf3d84f1b2020-03-09 13:33:25 -06003642void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003643 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003644 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003645}
3646
3647void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003648 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003649 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003650}
3651
3652void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf64ffe552021-02-06 10:25:07 -07003653 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2, kEndRenderPass2KhrName);
John Zulauf5a1a5382020-06-22 17:23:25 -06003654 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003655}
locke-lunarga19c71d2020-03-02 18:17:04 -07003656
Jeff Leger178b1e52020-10-05 12:22:23 -04003657template <typename BufferImageCopyRegionType>
3658bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3659 VkImageLayout dstImageLayout, uint32_t regionCount,
3660 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003661 bool skip = false;
3662 const auto *cb_access_context = GetAccessContext(commandBuffer);
3663 assert(cb_access_context);
3664 if (!cb_access_context) return skip;
3665
Jeff Leger178b1e52020-10-05 12:22:23 -04003666 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3667 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3668
locke-lunarga19c71d2020-03-02 18:17:04 -07003669 const auto *context = cb_access_context->GetCurrentAccessContext();
3670 assert(context);
3671 if (!context) return skip;
3672
3673 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003674 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3675
3676 for (uint32_t region = 0; region < regionCount; region++) {
3677 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003678 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003679 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003680 if (src_buffer) {
3681 ResourceAccessRange src_range =
3682 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003683 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07003684 if (hazard.hazard) {
3685 // PHASE1 TODO -- add tag information to log msg when useful.
3686 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3687 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3688 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003689 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003690 }
3691 }
3692
Jeremy Gebben40a22942020-12-22 14:22:06 -07003693 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07003694 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003695 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003696 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003697 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003698 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003699 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003700 }
3701 if (skip) break;
3702 }
3703 if (skip) break;
3704 }
3705 return skip;
3706}
3707
Jeff Leger178b1e52020-10-05 12:22:23 -04003708bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3709 VkImageLayout dstImageLayout, uint32_t regionCount,
3710 const VkBufferImageCopy *pRegions) const {
3711 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3712 COPY_COMMAND_VERSION_1);
3713}
3714
3715bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3716 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3717 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3718 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3719 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3720}
3721
3722template <typename BufferImageCopyRegionType>
3723void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3724 VkImageLayout dstImageLayout, uint32_t regionCount,
3725 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003726 auto *cb_access_context = GetAccessContext(commandBuffer);
3727 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003728
3729 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3730 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3731
3732 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003733 auto *context = cb_access_context->GetCurrentAccessContext();
3734 assert(context);
3735
3736 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003737 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003738
3739 for (uint32_t region = 0; region < regionCount; region++) {
3740 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07003741 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003742 if (src_buffer) {
3743 ResourceAccessRange src_range =
3744 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003745 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003746 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07003747 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003748 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003749 }
3750 }
3751}
3752
Jeff Leger178b1e52020-10-05 12:22:23 -04003753void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3754 VkImageLayout dstImageLayout, uint32_t regionCount,
3755 const VkBufferImageCopy *pRegions) {
3756 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3757 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3758}
3759
3760void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3761 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3762 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3763 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3764 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3765 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3766}
3767
3768template <typename BufferImageCopyRegionType>
3769bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3770 VkBuffer dstBuffer, uint32_t regionCount,
3771 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003772 bool skip = false;
3773 const auto *cb_access_context = GetAccessContext(commandBuffer);
3774 assert(cb_access_context);
3775 if (!cb_access_context) return skip;
3776
Jeff Leger178b1e52020-10-05 12:22:23 -04003777 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3778 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3779
locke-lunarga19c71d2020-03-02 18:17:04 -07003780 const auto *context = cb_access_context->GetCurrentAccessContext();
3781 assert(context);
3782 if (!context) return skip;
3783
3784 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3785 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3786 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3787 for (uint32_t region = 0; region < regionCount; region++) {
3788 const auto &copy_region = pRegions[region];
3789 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003790 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003791 copy_region.imageOffset, copy_region.imageExtent);
3792 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003793 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003794 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003795 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003796 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003797 }
John Zulauf477700e2021-01-06 11:41:49 -07003798 if (dst_mem) {
3799 ResourceAccessRange dst_range =
3800 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003801 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07003802 if (hazard.hazard) {
3803 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3804 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3805 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003806 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003807 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003808 }
3809 }
3810 if (skip) break;
3811 }
3812 return skip;
3813}
3814
Jeff Leger178b1e52020-10-05 12:22:23 -04003815bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3816 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3817 const VkBufferImageCopy *pRegions) const {
3818 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3819 COPY_COMMAND_VERSION_1);
3820}
3821
3822bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3823 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3824 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3825 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3826 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3827}
3828
3829template <typename BufferImageCopyRegionType>
3830void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3831 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3832 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003833 auto *cb_access_context = GetAccessContext(commandBuffer);
3834 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003835
3836 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3837 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3838
3839 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003840 auto *context = cb_access_context->GetCurrentAccessContext();
3841 assert(context);
3842
3843 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003844 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3845 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 -06003846 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003847
3848 for (uint32_t region = 0; region < regionCount; region++) {
3849 const auto &copy_region = pRegions[region];
3850 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003851 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003852 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003853 if (dst_buffer) {
3854 ResourceAccessRange dst_range =
3855 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003856 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003857 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003858 }
3859 }
3860}
3861
Jeff Leger178b1e52020-10-05 12:22:23 -04003862void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3863 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3864 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3865 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3866}
3867
3868void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3869 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3870 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3871 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3872 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3873 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3874}
3875
3876template <typename RegionType>
3877bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3878 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3879 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003880 bool skip = false;
3881 const auto *cb_access_context = GetAccessContext(commandBuffer);
3882 assert(cb_access_context);
3883 if (!cb_access_context) return skip;
3884
3885 const auto *context = cb_access_context->GetCurrentAccessContext();
3886 assert(context);
3887 if (!context) return skip;
3888
3889 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3890 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3891
3892 for (uint32_t region = 0; region < regionCount; region++) {
3893 const auto &blit_region = pRegions[region];
3894 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003895 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3896 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3897 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3898 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3899 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3900 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003901 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003902 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003903 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003904 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003905 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003906 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003907 }
3908 }
3909
3910 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003911 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3912 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3913 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3914 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3915 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3916 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003917 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003918 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003919 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003920 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003921 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003922 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003923 }
3924 if (skip) break;
3925 }
3926 }
3927
3928 return skip;
3929}
3930
Jeff Leger178b1e52020-10-05 12:22:23 -04003931bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3932 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3933 const VkImageBlit *pRegions, VkFilter filter) const {
3934 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3935 "vkCmdBlitImage");
3936}
3937
3938bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3939 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3940 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3941 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3942 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3943}
3944
3945template <typename RegionType>
3946void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3947 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3948 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003949 auto *cb_access_context = GetAccessContext(commandBuffer);
3950 assert(cb_access_context);
3951 auto *context = cb_access_context->GetCurrentAccessContext();
3952 assert(context);
3953
3954 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003955 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003956
3957 for (uint32_t region = 0; region < regionCount; region++) {
3958 const auto &blit_region = pRegions[region];
3959 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003960 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3961 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3962 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3963 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3964 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3965 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003966 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003967 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003968 }
3969 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003970 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3971 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3972 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3973 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3974 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3975 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003976 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003977 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003978 }
3979 }
3980}
locke-lunarg36ba2592020-04-03 09:42:04 -06003981
Jeff Leger178b1e52020-10-05 12:22:23 -04003982void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3983 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3984 const VkImageBlit *pRegions, VkFilter filter) {
3985 auto *cb_access_context = GetAccessContext(commandBuffer);
3986 assert(cb_access_context);
3987 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3988 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3989 pRegions, filter);
3990 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3991}
3992
3993void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3994 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3995 auto *cb_access_context = GetAccessContext(commandBuffer);
3996 assert(cb_access_context);
3997 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3998 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3999 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4000 pBlitImageInfo->filter, tag);
4001}
4002
John Zulauffaea0ee2021-01-14 14:01:32 -07004003bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4004 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
4005 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
4006 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004007 bool skip = false;
4008 if (drawCount == 0) return skip;
4009
4010 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4011 VkDeviceSize size = struct_size;
4012 if (drawCount == 1 || stride == size) {
4013 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004014 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004015 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4016 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004017 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004018 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004019 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004020 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004021 }
4022 } else {
4023 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004024 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004025 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4026 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004027 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004028 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4029 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004030 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004031 break;
4032 }
4033 }
4034 }
4035 return skip;
4036}
4037
locke-lunarg61870c22020-06-09 14:51:50 -06004038void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
4039 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4040 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06004041 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4042 VkDeviceSize size = struct_size;
4043 if (drawCount == 1 || stride == size) {
4044 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004045 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004046 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004047 } else {
4048 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004049 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004050 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4051 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004052 }
4053 }
4054}
4055
John Zulauffaea0ee2021-01-14 14:01:32 -07004056bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4057 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4058 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004059 bool skip = false;
4060
4061 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004062 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004063 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4064 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004065 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004066 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004067 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004068 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004069 }
4070 return skip;
4071}
4072
locke-lunarg61870c22020-06-09 14:51:50 -06004073void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06004074 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004075 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004076 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004077}
4078
locke-lunarg36ba2592020-04-03 09:42:04 -06004079bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004080 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004081 const auto *cb_access_context = GetAccessContext(commandBuffer);
4082 assert(cb_access_context);
4083 if (!cb_access_context) return skip;
4084
locke-lunarg61870c22020-06-09 14:51:50 -06004085 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004086 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004087}
4088
4089void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004090 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004091 auto *cb_access_context = GetAccessContext(commandBuffer);
4092 assert(cb_access_context);
4093 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004094
locke-lunarg61870c22020-06-09 14:51:50 -06004095 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004096}
locke-lunarge1a67022020-04-29 00:15:36 -06004097
4098bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004099 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004100 const auto *cb_access_context = GetAccessContext(commandBuffer);
4101 assert(cb_access_context);
4102 if (!cb_access_context) return skip;
4103
4104 const auto *context = cb_access_context->GetCurrentAccessContext();
4105 assert(context);
4106 if (!context) return skip;
4107
locke-lunarg61870c22020-06-09 14:51:50 -06004108 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004109 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4110 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004111 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004112}
4113
4114void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004115 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004116 auto *cb_access_context = GetAccessContext(commandBuffer);
4117 assert(cb_access_context);
4118 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4119 auto *context = cb_access_context->GetCurrentAccessContext();
4120 assert(context);
4121
locke-lunarg61870c22020-06-09 14:51:50 -06004122 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4123 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004124}
4125
4126bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4127 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004128 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004129 const auto *cb_access_context = GetAccessContext(commandBuffer);
4130 assert(cb_access_context);
4131 if (!cb_access_context) return skip;
4132
locke-lunarg61870c22020-06-09 14:51:50 -06004133 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4134 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4135 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004136 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004137}
4138
4139void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4140 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004141 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004142 auto *cb_access_context = GetAccessContext(commandBuffer);
4143 assert(cb_access_context);
4144 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004145
locke-lunarg61870c22020-06-09 14:51:50 -06004146 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4147 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4148 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004149}
4150
4151bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4152 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004153 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004154 const auto *cb_access_context = GetAccessContext(commandBuffer);
4155 assert(cb_access_context);
4156 if (!cb_access_context) return skip;
4157
locke-lunarg61870c22020-06-09 14:51:50 -06004158 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4159 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4160 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004161 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004162}
4163
4164void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4165 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004166 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004167 auto *cb_access_context = GetAccessContext(commandBuffer);
4168 assert(cb_access_context);
4169 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004170
locke-lunarg61870c22020-06-09 14:51:50 -06004171 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4172 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4173 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004174}
4175
4176bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4177 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004178 bool skip = false;
4179 if (drawCount == 0) return skip;
4180
locke-lunargff255f92020-05-13 18:53:52 -06004181 const auto *cb_access_context = GetAccessContext(commandBuffer);
4182 assert(cb_access_context);
4183 if (!cb_access_context) return skip;
4184
4185 const auto *context = cb_access_context->GetCurrentAccessContext();
4186 assert(context);
4187 if (!context) return skip;
4188
locke-lunarg61870c22020-06-09 14:51:50 -06004189 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4190 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004191 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4192 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004193
4194 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4195 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4196 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004197 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004198 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004199}
4200
4201void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4202 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004203 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004204 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004205 auto *cb_access_context = GetAccessContext(commandBuffer);
4206 assert(cb_access_context);
4207 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4208 auto *context = cb_access_context->GetCurrentAccessContext();
4209 assert(context);
4210
locke-lunarg61870c22020-06-09 14:51:50 -06004211 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4212 cb_access_context->RecordDrawSubpassAttachment(tag);
4213 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004214
4215 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4216 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4217 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004218 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004219}
4220
4221bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4222 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004223 bool skip = false;
4224 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004225 const auto *cb_access_context = GetAccessContext(commandBuffer);
4226 assert(cb_access_context);
4227 if (!cb_access_context) return skip;
4228
4229 const auto *context = cb_access_context->GetCurrentAccessContext();
4230 assert(context);
4231 if (!context) return skip;
4232
locke-lunarg61870c22020-06-09 14:51:50 -06004233 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4234 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004235 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4236 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004237
4238 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4239 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4240 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004241 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004242 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004243}
4244
4245void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4246 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004247 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004248 auto *cb_access_context = GetAccessContext(commandBuffer);
4249 assert(cb_access_context);
4250 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4251 auto *context = cb_access_context->GetCurrentAccessContext();
4252 assert(context);
4253
locke-lunarg61870c22020-06-09 14:51:50 -06004254 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4255 cb_access_context->RecordDrawSubpassAttachment(tag);
4256 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004257
4258 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4259 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4260 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004261 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004262}
4263
4264bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4265 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4266 uint32_t stride, const char *function) const {
4267 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004268 const auto *cb_access_context = GetAccessContext(commandBuffer);
4269 assert(cb_access_context);
4270 if (!cb_access_context) return skip;
4271
4272 const auto *context = cb_access_context->GetCurrentAccessContext();
4273 assert(context);
4274 if (!context) return skip;
4275
locke-lunarg61870c22020-06-09 14:51:50 -06004276 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4277 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004278 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4279 maxDrawCount, stride, function);
4280 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004281
4282 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4283 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4284 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004285 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004286 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004287}
4288
4289bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4290 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4291 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004292 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4293 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004294}
4295
4296void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4297 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4298 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004299 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4300 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004301 auto *cb_access_context = GetAccessContext(commandBuffer);
4302 assert(cb_access_context);
4303 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
4304 auto *context = cb_access_context->GetCurrentAccessContext();
4305 assert(context);
4306
locke-lunarg61870c22020-06-09 14:51:50 -06004307 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4308 cb_access_context->RecordDrawSubpassAttachment(tag);
4309 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4310 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004311
4312 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4313 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4314 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004315 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004316}
4317
4318bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4319 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4320 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004321 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4322 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004323}
4324
4325void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4326 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4327 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004328 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4329 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004330 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004331}
4332
4333bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4334 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4335 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004336 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4337 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004338}
4339
4340void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4341 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4342 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004343 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4344 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004345 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4346}
4347
4348bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4349 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4350 uint32_t stride, const char *function) const {
4351 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004352 const auto *cb_access_context = GetAccessContext(commandBuffer);
4353 assert(cb_access_context);
4354 if (!cb_access_context) return skip;
4355
4356 const auto *context = cb_access_context->GetCurrentAccessContext();
4357 assert(context);
4358 if (!context) return skip;
4359
locke-lunarg61870c22020-06-09 14:51:50 -06004360 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4361 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004362 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4363 offset, maxDrawCount, stride, function);
4364 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004365
4366 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4367 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4368 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004369 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004370 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004371}
4372
4373bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4374 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4375 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004376 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4377 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004378}
4379
4380void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4381 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4382 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004383 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4384 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004385 auto *cb_access_context = GetAccessContext(commandBuffer);
4386 assert(cb_access_context);
4387 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4388 auto *context = cb_access_context->GetCurrentAccessContext();
4389 assert(context);
4390
locke-lunarg61870c22020-06-09 14:51:50 -06004391 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4392 cb_access_context->RecordDrawSubpassAttachment(tag);
4393 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4394 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004395
4396 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4397 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004398 // We will update the index and vertex buffer in SubmitQueue in the future.
4399 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004400}
4401
4402bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4403 VkDeviceSize offset, VkBuffer countBuffer,
4404 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4405 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004406 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4407 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004408}
4409
4410void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4411 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4412 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004413 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4414 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004415 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4416}
4417
4418bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4419 VkDeviceSize offset, VkBuffer countBuffer,
4420 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4421 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004422 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4423 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004424}
4425
4426void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4427 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4428 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004429 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4430 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004431 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4432}
4433
4434bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4435 const VkClearColorValue *pColor, uint32_t rangeCount,
4436 const VkImageSubresourceRange *pRanges) const {
4437 bool skip = false;
4438 const auto *cb_access_context = GetAccessContext(commandBuffer);
4439 assert(cb_access_context);
4440 if (!cb_access_context) return skip;
4441
4442 const auto *context = cb_access_context->GetCurrentAccessContext();
4443 assert(context);
4444 if (!context) return skip;
4445
4446 const auto *image_state = Get<IMAGE_STATE>(image);
4447
4448 for (uint32_t index = 0; index < rangeCount; index++) {
4449 const auto &range = pRanges[index];
4450 if (image_state) {
4451 auto hazard =
Jeremy Gebben40a22942020-12-22 14:22:06 -07004452 context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
locke-lunarge1a67022020-04-29 00:15:36 -06004453 if (hazard.hazard) {
4454 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004455 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004456 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004457 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004458 }
4459 }
4460 }
4461 return skip;
4462}
4463
4464void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4465 const VkClearColorValue *pColor, uint32_t rangeCount,
4466 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004467 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004468 auto *cb_access_context = GetAccessContext(commandBuffer);
4469 assert(cb_access_context);
4470 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4471 auto *context = cb_access_context->GetCurrentAccessContext();
4472 assert(context);
4473
4474 const auto *image_state = Get<IMAGE_STATE>(image);
4475
4476 for (uint32_t index = 0; index < rangeCount; index++) {
4477 const auto &range = pRanges[index];
4478 if (image_state) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004479 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, {0, 0, 0},
John Zulauf8e3c3e92021-01-06 11:19:36 -07004480 image_state->createInfo.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004481 }
4482 }
4483}
4484
4485bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4486 VkImageLayout imageLayout,
4487 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4488 const VkImageSubresourceRange *pRanges) const {
4489 bool skip = false;
4490 const auto *cb_access_context = GetAccessContext(commandBuffer);
4491 assert(cb_access_context);
4492 if (!cb_access_context) return skip;
4493
4494 const auto *context = cb_access_context->GetCurrentAccessContext();
4495 assert(context);
4496 if (!context) return skip;
4497
4498 const auto *image_state = Get<IMAGE_STATE>(image);
4499
4500 for (uint32_t index = 0; index < rangeCount; index++) {
4501 const auto &range = pRanges[index];
4502 if (image_state) {
4503 auto hazard =
Jeremy Gebben40a22942020-12-22 14:22:06 -07004504 context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
locke-lunarge1a67022020-04-29 00:15:36 -06004505 if (hazard.hazard) {
4506 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004507 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004508 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004509 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004510 }
4511 }
4512 }
4513 return skip;
4514}
4515
4516void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4517 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4518 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004519 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004520 auto *cb_access_context = GetAccessContext(commandBuffer);
4521 assert(cb_access_context);
4522 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4523 auto *context = cb_access_context->GetCurrentAccessContext();
4524 assert(context);
4525
4526 const auto *image_state = Get<IMAGE_STATE>(image);
4527
4528 for (uint32_t index = 0; index < rangeCount; index++) {
4529 const auto &range = pRanges[index];
4530 if (image_state) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004531 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, {0, 0, 0},
John Zulauf8e3c3e92021-01-06 11:19:36 -07004532 image_state->createInfo.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004533 }
4534 }
4535}
4536
4537bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4538 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4539 VkDeviceSize dstOffset, VkDeviceSize stride,
4540 VkQueryResultFlags flags) const {
4541 bool skip = false;
4542 const auto *cb_access_context = GetAccessContext(commandBuffer);
4543 assert(cb_access_context);
4544 if (!cb_access_context) return skip;
4545
4546 const auto *context = cb_access_context->GetCurrentAccessContext();
4547 assert(context);
4548 if (!context) return skip;
4549
4550 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4551
4552 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004553 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004554 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004555 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004556 skip |=
4557 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4558 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004559 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004560 }
4561 }
locke-lunargff255f92020-05-13 18:53:52 -06004562
4563 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004564 return skip;
4565}
4566
4567void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4568 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4569 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004570 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4571 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004572 auto *cb_access_context = GetAccessContext(commandBuffer);
4573 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004574 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004575 auto *context = cb_access_context->GetCurrentAccessContext();
4576 assert(context);
4577
4578 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4579
4580 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004581 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004582 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004583 }
locke-lunargff255f92020-05-13 18:53:52 -06004584
4585 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004586}
4587
4588bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4589 VkDeviceSize size, uint32_t data) const {
4590 bool skip = false;
4591 const auto *cb_access_context = GetAccessContext(commandBuffer);
4592 assert(cb_access_context);
4593 if (!cb_access_context) return skip;
4594
4595 const auto *context = cb_access_context->GetCurrentAccessContext();
4596 assert(context);
4597 if (!context) return skip;
4598
4599 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4600
4601 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004602 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004603 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004604 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004605 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004606 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004607 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004608 }
4609 }
4610 return skip;
4611}
4612
4613void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4614 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004615 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004616 auto *cb_access_context = GetAccessContext(commandBuffer);
4617 assert(cb_access_context);
4618 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4619 auto *context = cb_access_context->GetCurrentAccessContext();
4620 assert(context);
4621
4622 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4623
4624 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004625 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004626 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004627 }
4628}
4629
4630bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4631 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4632 const VkImageResolve *pRegions) const {
4633 bool skip = false;
4634 const auto *cb_access_context = GetAccessContext(commandBuffer);
4635 assert(cb_access_context);
4636 if (!cb_access_context) return skip;
4637
4638 const auto *context = cb_access_context->GetCurrentAccessContext();
4639 assert(context);
4640 if (!context) return skip;
4641
4642 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4643 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4644
4645 for (uint32_t region = 0; region < regionCount; region++) {
4646 const auto &resolve_region = pRegions[region];
4647 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004648 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004649 resolve_region.srcOffset, resolve_region.extent);
4650 if (hazard.hazard) {
4651 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004652 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004653 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004654 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004655 }
4656 }
4657
4658 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004659 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004660 resolve_region.dstOffset, resolve_region.extent);
4661 if (hazard.hazard) {
4662 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004663 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004664 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004665 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004666 }
4667 if (skip) break;
4668 }
4669 }
4670
4671 return skip;
4672}
4673
4674void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4675 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4676 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004677 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4678 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004679 auto *cb_access_context = GetAccessContext(commandBuffer);
4680 assert(cb_access_context);
4681 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4682 auto *context = cb_access_context->GetCurrentAccessContext();
4683 assert(context);
4684
4685 auto *src_image = Get<IMAGE_STATE>(srcImage);
4686 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4687
4688 for (uint32_t region = 0; region < regionCount; region++) {
4689 const auto &resolve_region = pRegions[region];
4690 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004691 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004692 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004693 }
4694 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004695 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004696 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004697 }
4698 }
4699}
4700
Jeff Leger178b1e52020-10-05 12:22:23 -04004701bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4702 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4703 bool skip = false;
4704 const auto *cb_access_context = GetAccessContext(commandBuffer);
4705 assert(cb_access_context);
4706 if (!cb_access_context) return skip;
4707
4708 const auto *context = cb_access_context->GetCurrentAccessContext();
4709 assert(context);
4710 if (!context) return skip;
4711
4712 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4713 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4714
4715 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4716 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4717 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004718 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004719 resolve_region.srcOffset, resolve_region.extent);
4720 if (hazard.hazard) {
4721 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4722 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4723 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004724 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004725 }
4726 }
4727
4728 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004729 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004730 resolve_region.dstOffset, resolve_region.extent);
4731 if (hazard.hazard) {
4732 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4733 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4734 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004735 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004736 }
4737 if (skip) break;
4738 }
4739 }
4740
4741 return skip;
4742}
4743
4744void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4745 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4746 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4747 auto *cb_access_context = GetAccessContext(commandBuffer);
4748 assert(cb_access_context);
4749 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4750 auto *context = cb_access_context->GetCurrentAccessContext();
4751 assert(context);
4752
4753 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4754 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4755
4756 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4757 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4758 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004759 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004760 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004761 }
4762 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004763 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004764 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004765 }
4766 }
4767}
4768
locke-lunarge1a67022020-04-29 00:15:36 -06004769bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4770 VkDeviceSize dataSize, const void *pData) const {
4771 bool skip = false;
4772 const auto *cb_access_context = GetAccessContext(commandBuffer);
4773 assert(cb_access_context);
4774 if (!cb_access_context) return skip;
4775
4776 const auto *context = cb_access_context->GetCurrentAccessContext();
4777 assert(context);
4778 if (!context) return skip;
4779
4780 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4781
4782 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004783 // VK_WHOLE_SIZE not allowed
4784 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004785 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004786 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004787 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004788 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004789 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004790 }
4791 }
4792 return skip;
4793}
4794
4795void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4796 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004797 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004798 auto *cb_access_context = GetAccessContext(commandBuffer);
4799 assert(cb_access_context);
4800 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4801 auto *context = cb_access_context->GetCurrentAccessContext();
4802 assert(context);
4803
4804 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4805
4806 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004807 // VK_WHOLE_SIZE not allowed
4808 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004809 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004810 }
4811}
locke-lunargff255f92020-05-13 18:53:52 -06004812
4813bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4814 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4815 bool skip = false;
4816 const auto *cb_access_context = GetAccessContext(commandBuffer);
4817 assert(cb_access_context);
4818 if (!cb_access_context) return skip;
4819
4820 const auto *context = cb_access_context->GetCurrentAccessContext();
4821 assert(context);
4822 if (!context) return skip;
4823
4824 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4825
4826 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004827 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004828 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06004829 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004830 skip |=
4831 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4832 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004833 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004834 }
4835 }
4836 return skip;
4837}
4838
4839void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4840 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004841 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004842 auto *cb_access_context = GetAccessContext(commandBuffer);
4843 assert(cb_access_context);
4844 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4845 auto *context = cb_access_context->GetCurrentAccessContext();
4846 assert(context);
4847
4848 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4849
4850 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004851 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004852 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004853 }
4854}
John Zulauf49beb112020-11-04 16:06:31 -07004855
4856bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
4857 bool skip = false;
4858 const auto *cb_context = GetAccessContext(commandBuffer);
4859 assert(cb_context);
4860 if (!cb_context) return skip;
4861
John Zulauf36ef9282021-02-02 11:47:24 -07004862 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004863 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004864}
4865
4866void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4867 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
4868 auto *cb_context = GetAccessContext(commandBuffer);
4869 assert(cb_context);
4870 if (!cb_context) return;
John Zulauf36ef9282021-02-02 11:47:24 -07004871 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4872 set_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004873}
4874
John Zulauf4edde622021-02-15 08:54:50 -07004875bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4876 const VkDependencyInfoKHR *pDependencyInfo) const {
4877 bool skip = false;
4878 const auto *cb_context = GetAccessContext(commandBuffer);
4879 assert(cb_context);
4880 if (!cb_context || !pDependencyInfo) return skip;
4881
4882 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4883 return set_event_op.Validate(*cb_context);
4884}
4885
4886void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4887 const VkDependencyInfoKHR *pDependencyInfo) {
4888 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
4889 auto *cb_context = GetAccessContext(commandBuffer);
4890 assert(cb_context);
4891 if (!cb_context || !pDependencyInfo) return;
4892
4893 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4894 set_event_op.Record(cb_context);
4895}
4896
John Zulauf49beb112020-11-04 16:06:31 -07004897bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
4898 VkPipelineStageFlags stageMask) const {
4899 bool skip = false;
4900 const auto *cb_context = GetAccessContext(commandBuffer);
4901 assert(cb_context);
4902 if (!cb_context) return skip;
4903
John Zulauf36ef9282021-02-02 11:47:24 -07004904 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004905 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004906}
4907
4908void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4909 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
4910 auto *cb_context = GetAccessContext(commandBuffer);
4911 assert(cb_context);
4912 if (!cb_context) return;
4913
John Zulauf36ef9282021-02-02 11:47:24 -07004914 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4915 reset_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004916}
4917
John Zulauf4edde622021-02-15 08:54:50 -07004918bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4919 VkPipelineStageFlags2KHR stageMask) const {
4920 bool skip = false;
4921 const auto *cb_context = GetAccessContext(commandBuffer);
4922 assert(cb_context);
4923 if (!cb_context) return skip;
4924
4925 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4926 return reset_event_op.Validate(*cb_context);
4927}
4928
4929void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4930 VkPipelineStageFlags2KHR stageMask) {
4931 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
4932 auto *cb_context = GetAccessContext(commandBuffer);
4933 assert(cb_context);
4934 if (!cb_context) return;
4935
4936 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4937 reset_event_op.Record(cb_context);
4938}
4939
John Zulauf49beb112020-11-04 16:06:31 -07004940bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4941 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4942 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4943 uint32_t bufferMemoryBarrierCount,
4944 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4945 uint32_t imageMemoryBarrierCount,
4946 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
4947 bool skip = false;
4948 const auto *cb_context = GetAccessContext(commandBuffer);
4949 assert(cb_context);
4950 if (!cb_context) return skip;
4951
John Zulauf36ef9282021-02-02 11:47:24 -07004952 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4953 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4954 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07004955 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004956}
4957
4958void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4959 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4960 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4961 uint32_t bufferMemoryBarrierCount,
4962 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4963 uint32_t imageMemoryBarrierCount,
4964 const VkImageMemoryBarrier *pImageMemoryBarriers) {
4965 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
4966 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
4967 imageMemoryBarrierCount, pImageMemoryBarriers);
4968
4969 auto *cb_context = GetAccessContext(commandBuffer);
4970 assert(cb_context);
4971 if (!cb_context) return;
4972
John Zulauf36ef9282021-02-02 11:47:24 -07004973 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4974 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4975 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
4976 return wait_events_op.Record(cb_context);
John Zulauf4a6105a2020-11-17 15:11:05 -07004977}
4978
John Zulauf4edde622021-02-15 08:54:50 -07004979bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4980 const VkDependencyInfoKHR *pDependencyInfos) const {
4981 bool skip = false;
4982 const auto *cb_context = GetAccessContext(commandBuffer);
4983 assert(cb_context);
4984 if (!cb_context) return skip;
4985
4986 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
4987 skip |= wait_events_op.Validate(*cb_context);
4988 return skip;
4989}
4990
4991void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4992 const VkDependencyInfoKHR *pDependencyInfos) {
4993 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
4994
4995 auto *cb_context = GetAccessContext(commandBuffer);
4996 assert(cb_context);
4997 if (!cb_context) return;
4998
4999 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5000 wait_events_op.Record(cb_context);
5001}
5002
John Zulauf4a6105a2020-11-17 15:11:05 -07005003void SyncEventState::ResetFirstScope() {
5004 for (const auto address_type : kAddressTypes) {
5005 first_scope[static_cast<size_t>(address_type)].clear();
5006 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005007 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07005008}
5009
5010// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07005011SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005012 IgnoreReason reason = NotIgnored;
5013
John Zulauf4edde622021-02-15 08:54:50 -07005014 if ((CMD_WAITEVENTS2KHR == cmd) && (CMD_SETEVENT == last_command)) {
5015 reason = SetVsWait2;
5016 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
5017 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07005018 } else if (unsynchronized_set) {
5019 reason = SetRace;
5020 } else {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005021 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005022 if (missing_bits) reason = MissingStageBits;
5023 }
5024
5025 return reason;
5026}
5027
Jeremy Gebben40a22942020-12-22 14:22:06 -07005028bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005029 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5030 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5031 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005032}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005033
John Zulauf36ef9282021-02-02 11:47:24 -07005034SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5035 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5036 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005037 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5038 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5039 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07005040 : SyncOpBase(cmd), barriers_(1) {
5041 auto &barrier_set = barriers_[0];
5042 barrier_set.dependency_flags = dependencyFlags;
5043 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
5044 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005045 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07005046 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
5047 pMemoryBarriers);
5048 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5049 bufferMemoryBarrierCount, pBufferMemoryBarriers);
5050 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5051 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005052}
5053
John Zulauf4edde622021-02-15 08:54:50 -07005054SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
5055 const VkDependencyInfoKHR *dep_infos)
5056 : SyncOpBase(cmd), barriers_(event_count) {
5057 for (uint32_t i = 0; i < event_count; i++) {
5058 const auto &dep_info = dep_infos[i];
5059 auto &barrier_set = barriers_[i];
5060 barrier_set.dependency_flags = dep_info.dependencyFlags;
5061 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5062 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5063 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5064 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5065 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5066 dep_info.pMemoryBarriers);
5067 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5068 dep_info.pBufferMemoryBarriers);
5069 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5070 dep_info.pImageMemoryBarriers);
5071 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005072}
5073
John Zulauf36ef9282021-02-02 11:47:24 -07005074SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005075 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5076 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5077 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5078 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5079 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005080 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005081 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5082
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005083SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5084 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005085 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005086
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005087bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5088 bool skip = false;
5089 const auto *context = cb_context.GetCurrentAccessContext();
5090 assert(context);
5091 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005092 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5093
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005094 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005095 const auto &barrier_set = barriers_[0];
5096 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5097 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5098 const auto *image_state = image_barrier.image.get();
5099 if (!image_state) continue;
5100 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5101 if (hazard.hazard) {
5102 // PHASE1 TODO -- add tag information to log msg when useful.
5103 const auto &sync_state = cb_context.GetSyncState();
5104 const auto image_handle = image_state->image;
5105 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5106 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5107 string_SyncHazard(hazard.hazard), image_barrier.index,
5108 sync_state.report_data->FormatHandle(image_handle).c_str(),
5109 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005110 }
5111 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005112 return skip;
5113}
5114
John Zulaufd5115702021-01-18 12:34:33 -07005115struct SyncOpPipelineBarrierFunctorFactory {
5116 using BarrierOpFunctor = PipelineBarrierOp;
5117 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5118 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5119 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5120 using BufferRange = ResourceAccessRange;
5121 using ImageRange = subresource_adapter::ImageRangeGenerator;
5122 using GlobalRange = ResourceAccessRange;
5123
5124 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5125 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5126 }
5127 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
5128 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5129 }
5130 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5131 return GlobalBarrierOpFunctor(barrier, false);
5132 }
5133
5134 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5135 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5136 const auto base_address = ResourceBaseAddress(buffer);
5137 return (range + base_address);
5138 }
5139 ImageRange MakeRangeGen(const IMAGE_STATE &image, const SyncImageMemoryBarrier::SubImageRange &range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005140 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005141
5142 const auto base_address = ResourceBaseAddress(image);
5143 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), range.subresource_range, range.offset,
5144 range.extent, base_address);
5145 return range_gen;
5146 }
5147 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5148};
5149
5150template <typename Barriers, typename FunctorFactory>
5151void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
5152 AccessContext *context) {
5153 for (const auto &barrier : barriers) {
5154 const auto *state = barrier.GetState();
5155 if (state) {
5156 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5157 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5158 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5159 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5160 }
5161 }
5162}
5163
5164template <typename Barriers, typename FunctorFactory>
5165void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
5166 AccessContext *access_context) {
5167 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5168 for (const auto &barrier : barriers) {
5169 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5170 }
5171 for (const auto address_type : kAddressTypes) {
5172 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5173 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5174 }
5175}
5176
John Zulauf36ef9282021-02-02 11:47:24 -07005177void SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005178 SyncOpPipelineBarrierFunctorFactory factory;
5179 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005180 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005181
John Zulauf4edde622021-02-15 08:54:50 -07005182 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5183 assert(barriers_.size() == 1);
5184 const auto &barrier_set = barriers_[0];
5185 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5186 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5187 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
5188
5189 if (barrier_set.single_exec_scope) {
5190 cb_context->ApplyGlobalBarriersToEvents(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
5191 } else {
5192 for (const auto &barrier : barrier_set.memory_barriers) {
5193 cb_context->ApplyGlobalBarriersToEvents(barrier.src_exec_scope, barrier.dst_exec_scope);
5194 }
5195 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005196}
5197
John Zulauf4edde622021-02-15 08:54:50 -07005198void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5199 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5200 const VkMemoryBarrier *barriers) {
5201 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005202 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005203 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005204 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005205 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005206 }
5207 if (0 == memory_barrier_count) {
5208 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005209 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005210 }
John Zulauf4edde622021-02-15 08:54:50 -07005211 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005212}
5213
John Zulauf4edde622021-02-15 08:54:50 -07005214void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5215 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5216 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5217 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005218 for (uint32_t index = 0; index < barrier_count; index++) {
5219 const auto &barrier = barriers[index];
5220 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5221 if (buffer) {
5222 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5223 const auto range = MakeRange(barrier.offset, barrier_size);
5224 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005225 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005226 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005227 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005228 }
5229 }
5230}
5231
John Zulauf4edde622021-02-15 08:54:50 -07005232void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
5233 uint32_t memory_barrier_count, const VkMemoryBarrier2KHR *barriers) {
5234 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005235 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005236 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005237 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5238 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5239 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005240 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005241 }
John Zulauf4edde622021-02-15 08:54:50 -07005242 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005243}
5244
John Zulauf4edde622021-02-15 08:54:50 -07005245void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5246 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5247 const VkBufferMemoryBarrier2KHR *barriers) {
5248 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005249 for (uint32_t index = 0; index < barrier_count; index++) {
5250 const auto &barrier = barriers[index];
5251 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5252 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5253 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5254 if (buffer) {
5255 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5256 const auto range = MakeRange(barrier.offset, barrier_size);
5257 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005258 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005259 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005260 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005261 }
5262 }
5263}
5264
John Zulauf4edde622021-02-15 08:54:50 -07005265void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5266 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5267 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5268 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005269 for (uint32_t index = 0; index < barrier_count; index++) {
5270 const auto &barrier = barriers[index];
5271 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5272 if (image) {
5273 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5274 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005275 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005276 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005277 image_memory_barriers.emplace_back();
5278 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005279 }
5280 }
5281}
John Zulaufd5115702021-01-18 12:34:33 -07005282
John Zulauf4edde622021-02-15 08:54:50 -07005283void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5284 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5285 const VkImageMemoryBarrier2KHR *barriers) {
5286 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005287 for (uint32_t index = 0; index < barrier_count; index++) {
5288 const auto &barrier = barriers[index];
5289 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5290 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5291 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5292 if (image) {
5293 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5294 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005295 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005296 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005297 image_memory_barriers.emplace_back();
5298 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005299 }
5300 }
5301}
5302
John Zulauf36ef9282021-02-02 11:47:24 -07005303SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005304 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5305 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5306 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5307 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005308 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005309 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5310 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005311 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005312}
5313
John Zulauf4edde622021-02-15 08:54:50 -07005314SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5315 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5316 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5317 MakeEventsList(sync_state, eventCount, pEvents);
5318 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5319}
5320
John Zulaufd5115702021-01-18 12:34:33 -07005321bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005322 const char *const ignored = "Wait operation is ignored for this event.";
5323 bool skip = false;
5324 const auto &sync_state = cb_context.GetSyncState();
5325 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer;
5326
John Zulauf4edde622021-02-15 08:54:50 -07005327 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5328 const auto &barrier_set = barriers_[barrier_set_index];
5329 if (barrier_set.single_exec_scope) {
5330 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5331 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5332 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5333 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5334 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5335 } else {
5336 const auto &barriers = barrier_set.memory_barriers;
5337 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5338 const auto &barrier = barriers[barrier_index];
5339 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5340 const std::string vuid =
5341 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5342 skip =
5343 sync_state.LogInfo(command_buffer_handle, vuid,
5344 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5345 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5346 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5347 }
5348 }
5349 }
5350 }
John Zulaufd5115702021-01-18 12:34:33 -07005351 }
5352
Jeremy Gebben40a22942020-12-22 14:22:06 -07005353 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005354 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005355 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005356 const auto *events_context = cb_context.GetCurrentEventsContext();
5357 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005358 size_t barrier_set_index = 0;
5359 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5360 for (size_t event_index = 0; event_index < events_.size(); event_index++)
5361 for (const auto &event : events_) {
5362 const auto *sync_event = events_context->Get(event.get());
5363 const auto &barrier_set = barriers_[barrier_set_index];
5364 if (!sync_event) {
5365 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5366 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5367 // new validation error... wait without previously submitted set event...
5368 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives at *record time*
5369 barrier_set_index += barrier_set_incr;
5370 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005371 }
John Zulauf4edde622021-02-15 08:54:50 -07005372 const auto event_handle = sync_event->event->event;
5373 // TODO add "destroyed" checks
5374
5375 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5376 const auto &src_exec_scope = barrier_set.src_exec_scope;
5377 event_stage_masks |= sync_event->scope.mask_param;
5378 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5379 if (ignore_reason) {
5380 switch (ignore_reason) {
5381 case SyncEventState::ResetWaitRace:
5382 case SyncEventState::Reset2WaitRace: {
5383 // Four permuations of Reset and Wait calls...
5384 const char *vuid =
5385 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5386 if (ignore_reason == SyncEventState::Reset2WaitRace) {
5387 vuid =
Jeremy Gebben476f5e22021-03-01 15:27:20 -07005388 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2KHR-event-03831" : "VUID-vkCmdResetEvent2KHR-event-03832";
John Zulauf4edde622021-02-15 08:54:50 -07005389 }
5390 const char *const message =
5391 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5392 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5393 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
5394 CommandTypeString(sync_event->last_command), ignored);
5395 break;
5396 }
5397 case SyncEventState::SetRace: {
5398 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5399 // this event
5400 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5401 const char *const message =
5402 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5403 const char *const reason = "First synchronization scope is undefined.";
5404 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5405 sync_state.report_data->FormatHandle(event_handle).c_str(),
5406 CommandTypeString(sync_event->last_command), reason, ignored);
5407 break;
5408 }
5409 case SyncEventState::MissingStageBits: {
5410 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5411 // Issue error message that event waited for is not in wait events scope
5412 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5413 const char *const message =
5414 "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5415 ". Bits missing from srcStageMask %s. %s";
5416 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5417 sync_state.report_data->FormatHandle(event_handle).c_str(),
5418 sync_event->scope.mask_param, src_exec_scope.mask_param,
5419 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), ignored);
5420 break;
5421 }
5422 case SyncEventState::SetVsWait2: {
5423 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2KHR-pEvents-03837",
5424 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5425 sync_state.report_data->FormatHandle(event_handle).c_str(),
5426 CommandTypeString(sync_event->last_command));
5427 break;
5428 }
5429 default:
5430 assert(ignore_reason == SyncEventState::NotIgnored);
5431 }
5432 } else if (barrier_set.image_memory_barriers.size()) {
5433 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5434 const auto *context = cb_context.GetCurrentAccessContext();
5435 assert(context);
5436 for (const auto &image_memory_barrier : image_memory_barriers) {
5437 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5438 const auto *image_state = image_memory_barrier.image.get();
5439 if (!image_state) continue;
5440 const auto &subresource_range = image_memory_barrier.range.subresource_range;
5441 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5442 const auto hazard =
5443 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5444 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5445 if (hazard.hazard) {
5446 skip |= sync_state.LogError(image_state->image, string_SyncHazardVUID(hazard.hazard),
5447 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5448 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
5449 sync_state.report_data->FormatHandle(image_state->image).c_str(),
5450 cb_context.FormatUsage(hazard).c_str());
5451 break;
5452 }
John Zulaufd5115702021-01-18 12:34:33 -07005453 }
5454 }
John Zulauf4edde622021-02-15 08:54:50 -07005455 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5456 // 03839
5457 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005458 }
John Zulaufd5115702021-01-18 12:34:33 -07005459
5460 // 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 -07005461 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 -07005462 if (extra_stage_bits) {
5463 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005464 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5465 const char *const vuid =
5466 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2KHR-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005467 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005468 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulaufd5115702021-01-18 12:34:33 -07005469 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005470 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005471 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005472 " vkCmdSetEvent may be in previously submitted command buffer.");
5473 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005474 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005475 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005476 }
5477 }
5478 return skip;
5479}
5480
5481struct SyncOpWaitEventsFunctorFactory {
5482 using BarrierOpFunctor = WaitEventBarrierOp;
5483 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5484 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5485 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5486 using BufferRange = EventSimpleRangeGenerator;
5487 using ImageRange = EventImageRangeGenerator;
5488 using GlobalRange = EventSimpleRangeGenerator;
5489
5490 // Need to restrict to only valid exec and access scope for this event
5491 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5492 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005493 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005494 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5495 return barrier;
5496 }
5497 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5498 auto barrier = RestrictToEvent(barrier_arg);
5499 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5500 }
5501 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
5502 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5503 }
5504 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5505 auto barrier = RestrictToEvent(barrier_arg);
5506 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5507 }
5508
5509 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5510 const AccessAddressType address_type = GetAccessAddressType(buffer);
5511 const auto base_address = ResourceBaseAddress(buffer);
5512 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5513 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5514 return filtered_range_gen;
5515 }
5516 ImageRange MakeRangeGen(const IMAGE_STATE &image, const SyncImageMemoryBarrier::SubImageRange &range) const {
5517 if (!SimpleBinding(image)) return ImageRange();
5518 const auto address_type = GetAccessAddressType(image);
5519 const auto base_address = ResourceBaseAddress(image);
5520 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), range.subresource_range,
5521 range.offset, range.extent, base_address);
5522 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5523
5524 return filtered_range_gen;
5525 }
5526 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5527 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5528 }
5529 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5530 SyncEventState *sync_event;
5531};
5532
John Zulauf36ef9282021-02-02 11:47:24 -07005533void SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
5534 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07005535 auto *access_context = cb_context->GetCurrentAccessContext();
5536 assert(access_context);
5537 if (!access_context) return;
John Zulauf669dfd52021-01-27 17:15:28 -07005538 auto *events_context = cb_context->GetCurrentEventsContext();
5539 assert(events_context);
5540 if (!events_context) return;
John Zulaufd5115702021-01-18 12:34:33 -07005541
5542 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
5543 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
5544 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
5545 access_context->ResolvePreviousAccesses();
5546
John Zulaufd5115702021-01-18 12:34:33 -07005547 // TODO... this needs change the SyncEventContext it's using depending on whether this is replay... the recorded
5548 // 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 -07005549 size_t barrier_set_index = 0;
5550 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5551 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07005552 for (auto &event_shared : events_) {
5553 if (!event_shared.get()) continue;
5554 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07005555
John Zulauf4edde622021-02-15 08:54:50 -07005556 sync_event->last_command = cmd_;
John Zulaufd5115702021-01-18 12:34:33 -07005557
John Zulauf4edde622021-02-15 08:54:50 -07005558 const auto &barrier_set = barriers_[barrier_set_index];
5559 const auto &dst = barrier_set.dst_exec_scope;
5560 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07005561 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
5562 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
5563 // of the barriers is maintained.
5564 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07005565 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5566 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5567 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07005568
5569 // Apply the global barrier to the event itself (for race condition tracking)
5570 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
5571 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
5572 sync_event->barriers |= dst.exec_scope;
5573 } else {
5574 // We ignored this wait, so we don't have any effective synchronization barriers for it.
5575 sync_event->barriers = 0U;
5576 }
John Zulauf4edde622021-02-15 08:54:50 -07005577 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005578 }
5579
5580 // Apply the pending barriers
5581 ResolvePendingBarrierFunctor apply_pending_action(tag);
5582 access_context->ApplyToContext(apply_pending_action);
5583}
5584
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005585bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5586 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5587 bool skip = false;
5588 const auto *cb_access_context = GetAccessContext(commandBuffer);
5589 assert(cb_access_context);
5590 if (!cb_access_context) return skip;
5591
5592 const auto *context = cb_access_context->GetCurrentAccessContext();
5593 assert(context);
5594 if (!context) return skip;
5595
5596 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5597
5598 if (dst_buffer) {
5599 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5600 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
5601 if (hazard.hazard) {
5602 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5603 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
5604 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
5605 string_UsageTag(hazard.tag).c_str());
5606 }
5607 }
5608 return skip;
5609}
5610
John Zulauf669dfd52021-01-27 17:15:28 -07005611void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07005612 events_.reserve(event_count);
5613 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
John Zulauf669dfd52021-01-27 17:15:28 -07005614 events_.emplace_back(sync_state.GetShared<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07005615 }
5616}
John Zulauf6ce24372021-01-30 05:56:25 -07005617
John Zulauf36ef9282021-02-02 11:47:24 -07005618SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005619 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005620 : SyncOpBase(cmd),
5621 event_(sync_state.GetShared<EVENT_STATE>(event)),
5622 exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005623
5624bool SyncOpResetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005625 auto *events_context = cb_context.GetCurrentEventsContext();
5626 assert(events_context);
5627 bool skip = false;
5628 if (!events_context) return skip;
5629
5630 const auto &sync_state = cb_context.GetSyncState();
5631 const auto *sync_event = events_context->Get(event_);
5632 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5633
5634 const char *const set_wait =
5635 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5636 "hazards.";
5637 const char *message = set_wait; // Only one message this call.
5638 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
5639 const char *vuid = nullptr;
5640 switch (sync_event->last_command) {
5641 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005642 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005643 // Needs a barrier between set and reset
5644 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
5645 break;
John Zulauf4edde622021-02-15 08:54:50 -07005646 case CMD_WAITEVENTS:
5647 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07005648 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
5649 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
5650 break;
5651 }
5652 default:
5653 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07005654 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
5655 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07005656 break;
5657 }
5658 if (vuid) {
John Zulauf36ef9282021-02-02 11:47:24 -07005659 skip |= sync_state.LogError(event_->event, vuid, message, CmdName(),
5660 sync_state.report_data->FormatHandle(event_->event).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005661 CommandTypeString(sync_event->last_command));
5662 }
5663 }
5664 return skip;
5665}
5666
John Zulauf36ef9282021-02-02 11:47:24 -07005667void SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005668 auto *events_context = cb_context->GetCurrentEventsContext();
5669 assert(events_context);
5670 if (!events_context) return;
5671
5672 auto *sync_event = events_context->GetFromShared(event_);
5673 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5674
5675 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07005676 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005677 sync_event->unsynchronized_set = CMD_NONE;
5678 sync_event->ResetFirstScope();
5679 sync_event->barriers = 0U;
5680}
5681
John Zulauf36ef9282021-02-02 11:47:24 -07005682SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005683 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005684 : SyncOpBase(cmd),
5685 event_(sync_state.GetShared<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07005686 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
5687 dep_info_() {}
5688
5689SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
5690 const VkDependencyInfoKHR &dep_info)
5691 : SyncOpBase(cmd),
5692 event_(sync_state.GetShared<EVENT_STATE>(event)),
5693 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
5694 dep_info_(new safe_VkDependencyInfoKHR(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005695
5696bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
5697 // 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 -07005698 bool skip = false;
5699
5700 const auto &sync_state = cb_context.GetSyncState();
5701 auto *events_context = cb_context.GetCurrentEventsContext();
5702 assert(events_context);
5703 if (!events_context) return skip;
5704
5705 const auto *sync_event = events_context->Get(event_);
5706 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5707
5708 const char *const reset_set =
5709 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5710 "hazards.";
5711 const char *const wait =
5712 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
5713
5714 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07005715 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07005716 const char *message = nullptr;
5717 switch (sync_event->last_command) {
5718 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005719 case CMD_RESETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005720 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07005721 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07005722 message = reset_set;
5723 break;
5724 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005725 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005726 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07005727 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07005728 message = reset_set;
5729 break;
5730 case CMD_WAITEVENTS:
John Zulauf4edde622021-02-15 08:54:50 -07005731 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005732 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07005733 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07005734 message = wait;
5735 break;
5736 default:
5737 // The only other valid last command that wasn't one.
5738 assert(sync_event->last_command == CMD_NONE);
5739 break;
5740 }
John Zulauf4edde622021-02-15 08:54:50 -07005741 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07005742 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07005743 std::string vuid("SYNC-");
5744 vuid.append(CmdName()).append(vuid_stem);
5745 skip |= sync_state.LogError(event_->event, vuid.c_str(), message, CmdName(),
John Zulauf36ef9282021-02-02 11:47:24 -07005746 sync_state.report_data->FormatHandle(event_->event).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005747 CommandTypeString(sync_event->last_command));
5748 }
5749 }
5750
5751 return skip;
5752}
5753
John Zulauf36ef9282021-02-02 11:47:24 -07005754void SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
5755 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07005756 auto *events_context = cb_context->GetCurrentEventsContext();
5757 auto *access_context = cb_context->GetCurrentAccessContext();
5758 assert(events_context);
5759 if (!events_context) return;
5760
5761 auto *sync_event = events_context->GetFromShared(event_);
5762 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5763
5764 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
5765 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
5766 // any issues caused by naive scope setting here.
5767
5768 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
5769 // Given:
5770 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
5771 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
5772
5773 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
5774 sync_event->unsynchronized_set = sync_event->last_command;
5775 sync_event->ResetFirstScope();
5776 } else if (sync_event->scope.exec_scope == 0) {
5777 // We only set the scope if there isn't one
5778 sync_event->scope = src_exec_scope_;
5779
5780 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
5781 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
5782 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
5783 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
5784 }
5785 };
5786 access_context->ForAll(set_scope);
5787 sync_event->unsynchronized_set = CMD_NONE;
5788 sync_event->first_scope_tag = tag;
5789 }
John Zulauf4edde622021-02-15 08:54:50 -07005790 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
5791 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005792 sync_event->barriers = 0U;
5793}
John Zulauf64ffe552021-02-06 10:25:07 -07005794
5795SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
5796 const VkRenderPassBeginInfo *pRenderPassBegin,
5797 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *cmd_name)
5798 : SyncOpBase(cmd, cmd_name) {
5799 if (pRenderPassBegin) {
5800 rp_state_ = sync_state.GetShared<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
5801 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
5802 const auto *fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
5803 if (fb_state) {
5804 shared_attachments_ = sync_state.GetSharedAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
5805 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
5806 // Note that this a safe to presist as long as shared_attachments is not cleared
5807 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08005808 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07005809 attachments_.emplace_back(attachment.get());
5810 }
5811 }
5812 if (pSubpassBeginInfo) {
5813 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
5814 }
5815 }
5816}
5817
5818bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5819 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
5820 bool skip = false;
5821
5822 assert(rp_state_.get());
5823 if (nullptr == rp_state_.get()) return skip;
5824 auto &rp_state = *rp_state_.get();
5825
5826 const uint32_t subpass = 0;
5827
5828 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
5829 // hasn't happened yet)
5830 const std::vector<AccessContext> empty_context_vector;
5831 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
5832 cb_context.GetCurrentAccessContext());
5833
5834 // Validate attachment operations
5835 if (attachments_.size() == 0) return skip;
5836 const auto &render_area = renderpass_begin_info_.renderArea;
5837 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, attachments_, CmdName());
5838
5839 // Validate load operations if there were no layout transition hazards
5840 if (!skip) {
5841 temp_context.RecordLayoutTransitions(rp_state, subpass, attachments_, kCurrentCommandTag);
5842 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, attachments_, CmdName());
5843 }
5844
5845 return skip;
5846}
5847
5848void SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5849 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5850 assert(rp_state_.get());
5851 if (nullptr == rp_state_.get()) return;
5852 const auto tag = cb_context->NextCommandTag(cmd_);
5853 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
5854}
5855
5856SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
5857 const VkSubpassEndInfo *pSubpassEndInfo, const char *name_override)
5858 : SyncOpBase(cmd, name_override) {
5859 if (pSubpassBeginInfo) {
5860 subpass_begin_info_.initialize(pSubpassBeginInfo);
5861 }
5862 if (pSubpassEndInfo) {
5863 subpass_end_info_.initialize(pSubpassEndInfo);
5864 }
5865}
5866
5867bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
5868 bool skip = false;
5869 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5870 if (!renderpass_context) return skip;
5871
5872 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
5873 return skip;
5874}
5875
5876void SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
5877 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5878 cb_context->RecordNextSubpass(cmd_);
5879}
5880
5881SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo,
5882 const char *name_override)
5883 : SyncOpBase(cmd, name_override) {
5884 if (pSubpassEndInfo) {
5885 subpass_end_info_.initialize(pSubpassEndInfo);
5886 }
5887}
5888
5889bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5890 bool skip = false;
5891 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5892
5893 if (!renderpass_context) return skip;
5894 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
5895 return skip;
5896}
5897
5898void SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5899 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5900 cb_context->RecordEndRenderPass(cmd_);
5901}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005902
5903void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5904 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
5905 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
5906 auto *cb_access_context = GetAccessContext(commandBuffer);
5907 assert(cb_access_context);
5908 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5909 auto *context = cb_access_context->GetCurrentAccessContext();
5910 assert(context);
5911
5912 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5913
5914 if (dst_buffer) {
5915 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5916 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
5917 }
5918}
John Zulaufd05c5842021-03-26 11:32:16 -06005919
5920#ifdef SYNCVAL_DIAGNOSTICS
5921bool SyncValidator::PreCallValidateDestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) const {
5922 sync_diagnostics.InstanceDump(instance);
5923 ImageRangeGen::diag_.Report();
5924 return StateTracker::PreCallValidateDestroyInstance(instance, pAllocator);
5925}
5926#endif