blob: 6aec4aa67d1f74be17783ade5e8431d1feef2642 [file] [log] [blame]
John Zulaufab7756b2020-12-29 16:10:16 -07001/* Copyright (c) 2019-2021 The Khronos Group Inc.
2 * Copyright (c) 2019-2021 Valve Corporation
3 * Copyright (c) 2019-2021 LunarG, Inc.
John Zulauf9cb530d2019-09-30 14:14:10 -06004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: John Zulauf <jzulauf@lunarg.com>
John Zulaufab7756b2020-12-29 16:10:16 -070018 * Author: Locke Lin <locke@lunarg.com>
19 * Author: Jeremy Gebben <jeremyg@lunarg.com>
John Zulauf9cb530d2019-09-30 14:14:10 -060020 */
21
22#include <limits>
23#include <vector>
locke-lunarg296a3c92020-03-25 01:04:29 -060024#include <memory>
25#include <bitset>
John Zulauf9cb530d2019-09-30 14:14:10 -060026#include "synchronization_validation.h"
Jeremy Gebben5f585ae2021-02-02 09:03:06 -070027#include "sync_utils.h"
John Zulauf9cb530d2019-09-30 14:14:10 -060028
John Zulaufd05c5842021-03-26 11:32:16 -060029#ifdef SYNCVAL_DIAGNOSTICS
30struct SyncDiagnostics {
31 void DebugAction() const {
32#if defined(_WIN32)
33 __debugbreak();
34#endif
35 }
36 void Detect(const ResourceAccessRange &range) {
37 std::lock_guard<std::mutex> lock(diag_mutex);
38 if (range.distance() == kConditionValue) {
39 ++condition;
40 DebugAction();
41 }
42 detect_histogram[range.distance()] += 1;
43 }
44 void InstanceDump(VkInstance instance) {
45 std::cout << "# instance handle\n" << instance << "\n";
46 std::cout << "# condition count\n" << condition << "\n";
47 std::cout << "# Detection Size Histogram\n";
48 for (const auto &entry : detect_histogram) {
49 std::cout << "{ " << entry.first << ", " << entry.second << "}\n";
50 }
51 std::cout << std::endl;
52 detect_histogram.clear();
53 }
54 std::map<ResourceAccessRange::index_type, size_t> detect_histogram;
55 uint64_t condition;
56 std::mutex diag_mutex;
57 static const ResourceAccessRangeIndex kConditionValue = ~ResourceAccessRangeIndex(0);
58};
59static SyncDiagnostics sync_diagnostics;
60#endif
61
John Zulauf264cce02021-02-05 14:40:47 -070062static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
63
John Zulauf29d00532021-03-04 13:28:54 -070064static bool SimpleBinding(const IMAGE_STATE &image_state) {
65 bool simple = SimpleBinding(static_cast<const BINDABLE &>(image_state)) || image_state.is_swapchain_image ||
66 (VK_NULL_HANDLE != image_state.bind_swapchain);
67
68 // If it's not simple we must have an encoder.
69 assert(!simple || image_state.fragment_encoder.get());
70 return simple;
71}
72
John Zulauf43cc7462020-12-03 12:33:12 -070073const static std::array<AccessAddressType, static_cast<size_t>(AccessAddressType::kTypeCount)> kAddressTypes = {
74 AccessAddressType::kLinear, AccessAddressType::kIdealized};
75
John Zulaufd5115702021-01-18 12:34:33 -070076static constexpr AccessAddressType GetAccessAddressType(const BUFFER_STATE &) { return AccessAddressType::kLinear; };
John Zulauf264cce02021-02-05 14:40:47 -070077static AccessAddressType GetAccessAddressType(const IMAGE_STATE &image) {
78 return SimpleBinding(image) ? AccessContext::ImageAddressType(image) : AccessAddressType::kIdealized;
79}
John Zulaufd5115702021-01-18 12:34:33 -070080
John Zulauf9cb530d2019-09-30 14:14:10 -060081static const char *string_SyncHazardVUID(SyncHazard hazard) {
82 switch (hazard) {
83 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070084 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060085 break;
86 case SyncHazard::READ_AFTER_WRITE:
87 return "SYNC-HAZARD-READ_AFTER_WRITE";
88 break;
89 case SyncHazard::WRITE_AFTER_READ:
90 return "SYNC-HAZARD-WRITE_AFTER_READ";
91 break;
92 case SyncHazard::WRITE_AFTER_WRITE:
93 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
94 break;
John Zulauf2f952d22020-02-10 11:34:51 -070095 case SyncHazard::READ_RACING_WRITE:
96 return "SYNC-HAZARD-READ-RACING-WRITE";
97 break;
98 case SyncHazard::WRITE_RACING_WRITE:
99 return "SYNC-HAZARD-WRITE-RACING-WRITE";
100 break;
101 case SyncHazard::WRITE_RACING_READ:
102 return "SYNC-HAZARD-WRITE-RACING-READ";
103 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600104 default:
105 assert(0);
106 }
107 return "SYNC-HAZARD-INVALID";
108}
109
John Zulauf59e25072020-07-17 10:55:21 -0600110static bool IsHazardVsRead(SyncHazard hazard) {
111 switch (hazard) {
112 case SyncHazard::NONE:
113 return false;
114 break;
115 case SyncHazard::READ_AFTER_WRITE:
116 return false;
117 break;
118 case SyncHazard::WRITE_AFTER_READ:
119 return true;
120 break;
121 case SyncHazard::WRITE_AFTER_WRITE:
122 return false;
123 break;
124 case SyncHazard::READ_RACING_WRITE:
125 return false;
126 break;
127 case SyncHazard::WRITE_RACING_WRITE:
128 return false;
129 break;
130 case SyncHazard::WRITE_RACING_READ:
131 return true;
132 break;
133 default:
134 assert(0);
135 }
136 return false;
137}
138
John Zulauf9cb530d2019-09-30 14:14:10 -0600139static const char *string_SyncHazard(SyncHazard hazard) {
140 switch (hazard) {
141 case SyncHazard::NONE:
142 return "NONR";
143 break;
144 case SyncHazard::READ_AFTER_WRITE:
145 return "READ_AFTER_WRITE";
146 break;
147 case SyncHazard::WRITE_AFTER_READ:
148 return "WRITE_AFTER_READ";
149 break;
150 case SyncHazard::WRITE_AFTER_WRITE:
151 return "WRITE_AFTER_WRITE";
152 break;
John Zulauf2f952d22020-02-10 11:34:51 -0700153 case SyncHazard::READ_RACING_WRITE:
154 return "READ_RACING_WRITE";
155 break;
156 case SyncHazard::WRITE_RACING_WRITE:
157 return "WRITE_RACING_WRITE";
158 break;
159 case SyncHazard::WRITE_RACING_READ:
160 return "WRITE_RACING_READ";
161 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600162 default:
163 assert(0);
164 }
165 return "INVALID HAZARD";
166}
167
John Zulauf37ceaed2020-07-03 16:18:15 -0600168static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
169 // Return the info for the first bit found
170 const SyncStageAccessInfoType *info = nullptr;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700171 for (size_t i = 0; i < flags.size(); i++) {
172 if (flags.test(i)) {
173 info = &syncStageAccessInfoByStageAccessIndex[i];
174 break;
John Zulauf37ceaed2020-07-03 16:18:15 -0600175 }
176 }
177 return info;
178}
179
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700180static std::string string_SyncStageAccessFlags(const SyncStageAccessFlags &flags, const char *sep = "|") {
John Zulauf59e25072020-07-17 10:55:21 -0600181 std::string out_str;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700182 if (flags.none()) {
John Zulauf389c34b2020-07-28 11:19:35 -0600183 out_str = "0";
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700184 } else {
185 for (size_t i = 0; i < syncStageAccessInfoByStageAccessIndex.size(); i++) {
186 const auto &info = syncStageAccessInfoByStageAccessIndex[i];
187 if ((flags & info.stage_access_bit).any()) {
188 if (!out_str.empty()) {
189 out_str.append(sep);
190 }
191 out_str.append(info.name);
John Zulauf59e25072020-07-17 10:55:21 -0600192 }
John Zulauf59e25072020-07-17 10:55:21 -0600193 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700194 if (out_str.length() == 0) {
195 out_str.append("Unhandled SyncStageAccess");
196 }
John Zulauf59e25072020-07-17 10:55:21 -0600197 }
198 return out_str;
199}
200
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700201static std::string string_UsageTag(const ResourceUsageTag &tag) {
202 std::stringstream out;
203
John Zulauffaea0ee2021-01-14 14:01:32 -0700204 out << "command: " << CommandTypeString(tag.command);
205 out << ", seq_no: " << tag.seq_num;
206 if (tag.sub_command != 0) {
207 out << ", subcmd: " << tag.sub_command;
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700208 }
209 return out.str();
210}
211
John Zulauffaea0ee2021-01-14 14:01:32 -0700212std::string CommandBufferAccessContext::FormatUsage(const HazardResult &hazard) const {
John Zulauf37ceaed2020-07-03 16:18:15 -0600213 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600214 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
215 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600216 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600217 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
218 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf59e25072020-07-17 10:55:21 -0600219 out << "(usage: " << usage_info.name << ", prior_usage: " << stage_access_name;
220 if (IsHazardVsRead(hazard.hazard)) {
221 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
Jeremy Gebben40a22942020-12-22 14:22:06 -0700222 out << ", read_barriers: " << string_VkPipelineStageFlags2KHR(barriers);
John Zulauf59e25072020-07-17 10:55:21 -0600223 } else {
224 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
225 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
226 }
227
John Zulauffaea0ee2021-01-14 14:01:32 -0700228 // PHASE2 TODO -- add comand buffer and reset from secondary if applicable
ZaOniRinku56b86472021-03-23 20:25:05 +0100229 out << ", " << string_UsageTag(tag) << ", reset_no: " << reset_count_ << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600230 return out.str();
231}
232
John Zulaufd14743a2020-07-03 09:42:39 -0600233// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
234// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
235// also reflects this special case for read hazard detection (using access instead of exec scope)
Jeremy Gebben40a22942020-12-22 14:22:06 -0700236static constexpr VkPipelineStageFlags2KHR kColorAttachmentExecScope = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700237static const SyncStageAccessFlags kColorAttachmentAccessScope =
238 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
239 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
240 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
241 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebben40a22942020-12-22 14:22:06 -0700242static constexpr VkPipelineStageFlags2KHR kDepthStencilAttachmentExecScope =
243 VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT_KHR | VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700244static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
245 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
246 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
247 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -0700248static constexpr VkPipelineStageFlags2KHR kRasterAttachmentExecScope = kDepthStencilAttachmentExecScope | kColorAttachmentExecScope;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700249static const SyncStageAccessFlags kRasterAttachmentAccessScope = kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope;
John Zulaufb027cdb2020-05-21 14:25:22 -0600250
John Zulauf8e3c3e92021-01-06 11:19:36 -0700251ResourceAccessState::OrderingBarriers ResourceAccessState::kOrderingRules = {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700252 {{VK_PIPELINE_STAGE_2_NONE_KHR, SyncStageAccessFlags()},
John Zulauf8e3c3e92021-01-06 11:19:36 -0700253 {kColorAttachmentExecScope, kColorAttachmentAccessScope},
254 {kDepthStencilAttachmentExecScope, kDepthStencilAttachmentAccessScope},
255 {kRasterAttachmentExecScope, kRasterAttachmentAccessScope}}};
256
John Zulauf7635de32020-05-29 17:14:15 -0600257// Sometimes we have an internal access conflict, and we using the kCurrentCommandTag to set and detect in temporary/proxy contexts
John Zulauffaea0ee2021-01-14 14:01:32 -0700258static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, ResourceUsageTag::kMaxCount,
259 ResourceUsageTag::kMaxCount, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600260
John Zulaufb02c1eb2020-10-06 16:33:36 -0600261static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) {
262 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
263}
John Zulauf29d00532021-03-04 13:28:54 -0700264static VkDeviceSize ResourceBaseAddress(const IMAGE_STATE &image_state) {
265 VkDeviceSize base_address;
266 if (image_state.is_swapchain_image || (VK_NULL_HANDLE != image_state.bind_swapchain)) {
267 base_address = image_state.swapchain_fake_address;
268 } else {
269 base_address = ResourceBaseAddress(static_cast<const BINDABLE &>(image_state));
270 }
271 return base_address;
272}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600273
locke-lunarg3c038002020-04-30 23:08:08 -0600274inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
275 if (size == VK_WHOLE_SIZE) {
276 return (whole_size - offset);
277 }
278 return size;
279}
280
John Zulauf3e86bf02020-09-12 10:47:57 -0600281static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
282 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
283}
284
John Zulauf16adfc92020-04-08 10:28:33 -0600285template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600286static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600287 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
288}
289
John Zulauf355e49b2020-04-24 15:11:15 -0600290static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600291
John Zulauf3e86bf02020-09-12 10:47:57 -0600292static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
293 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
294}
295
296static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
297 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
298}
299
John Zulauf4a6105a2020-11-17 15:11:05 -0700300// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
301//
John Zulauf10f1f522020-12-18 12:00:35 -0700302// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
303//
John Zulauf4a6105a2020-11-17 15:11:05 -0700304// Usage:
305// Constructor() -- initializes the generator to point to the begin of the space declared.
306// * -- the current range of the generator empty signfies end
307// ++ -- advance to the next non-empty range (or end)
308
309// A wrapper for a single range with the same semantics as the actual generators below
310template <typename KeyType>
311class SingleRangeGenerator {
312 public:
313 SingleRangeGenerator(const KeyType &range) : current_(range) {}
John Zulaufd5115702021-01-18 12:34:33 -0700314 const KeyType &operator*() const { return current_; }
315 const KeyType *operator->() const { return &current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700316 SingleRangeGenerator &operator++() {
317 current_ = KeyType(); // just one real range
318 return *this;
319 }
320
321 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
322
323 private:
324 SingleRangeGenerator() = default;
325 const KeyType range_;
326 KeyType current_;
327};
328
329// Generate the ranges that are the intersection of range and the entries in the FilterMap
330template <typename FilterMap, typename KeyType = typename FilterMap::key_type>
331class FilteredRangeGenerator {
332 public:
John Zulaufd5115702021-01-18 12:34:33 -0700333 // Default constructed is safe to dereference for "empty" test, but for no other operation.
334 FilteredRangeGenerator() : range_(), filter_(nullptr), filter_pos_(), current_() {
335 // Default construction for KeyType *must* be empty range
336 assert(current_.empty());
337 }
John Zulauf4a6105a2020-11-17 15:11:05 -0700338 FilteredRangeGenerator(const FilterMap &filter, const KeyType &range)
339 : range_(range), filter_(&filter), filter_pos_(), current_() {
340 SeekBegin();
341 }
John Zulaufd5115702021-01-18 12:34:33 -0700342 FilteredRangeGenerator(const FilteredRangeGenerator &from) = default;
343
John Zulauf4a6105a2020-11-17 15:11:05 -0700344 const KeyType &operator*() const { return current_; }
345 const KeyType *operator->() const { return &current_; }
346 FilteredRangeGenerator &operator++() {
347 ++filter_pos_;
348 UpdateCurrent();
349 return *this;
350 }
351
352 bool operator==(const FilteredRangeGenerator &other) const { return current_ == other.current_; }
353
354 private:
John Zulauf4a6105a2020-11-17 15:11:05 -0700355 void UpdateCurrent() {
356 if (filter_pos_ != filter_->cend()) {
357 current_ = range_ & filter_pos_->first;
358 } else {
359 current_ = KeyType();
360 }
361 }
362 void SeekBegin() {
363 filter_pos_ = filter_->lower_bound(range_);
364 UpdateCurrent();
365 }
366 const KeyType range_;
367 const FilterMap *filter_;
368 typename FilterMap::const_iterator filter_pos_;
369 KeyType current_;
370};
John Zulaufd5115702021-01-18 12:34:33 -0700371using SingleAccessRangeGenerator = SingleRangeGenerator<ResourceAccessRange>;
John Zulauf4a6105a2020-11-17 15:11:05 -0700372using EventSimpleRangeGenerator = FilteredRangeGenerator<SyncEventState::ScopeMap>;
373
374// Templated to allow for different Range generators or map sources...
375
376// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulauf4a6105a2020-11-17 15:11:05 -0700377template <typename FilterMap, typename RangeGen, typename KeyType = typename FilterMap::key_type>
378class FilteredGeneratorGenerator {
379 public:
John Zulaufd5115702021-01-18 12:34:33 -0700380 // Default constructed is safe to dereference for "empty" test, but for no other operation.
381 FilteredGeneratorGenerator() : filter_(nullptr), gen_(), filter_pos_(), current_() {
382 // Default construction for KeyType *must* be empty range
383 assert(current_.empty());
384 }
385 FilteredGeneratorGenerator(const FilterMap &filter, RangeGen &gen) : filter_(&filter), gen_(gen), filter_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700386 SeekBegin();
387 }
John Zulaufd5115702021-01-18 12:34:33 -0700388 FilteredGeneratorGenerator(const FilteredGeneratorGenerator &from) = default;
John Zulauf4a6105a2020-11-17 15:11:05 -0700389 const KeyType &operator*() const { return current_; }
390 const KeyType *operator->() const { return &current_; }
391 FilteredGeneratorGenerator &operator++() {
392 KeyType gen_range = GenRange();
393 KeyType filter_range = FilterRange();
394 current_ = KeyType();
395 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
396 if (gen_range.end > filter_range.end) {
397 // if the generated range is beyond the filter_range, advance the filter range
398 filter_range = AdvanceFilter();
399 } else {
400 gen_range = AdvanceGen();
401 }
402 current_ = gen_range & filter_range;
403 }
404 return *this;
405 }
406
407 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
408
409 private:
410 KeyType AdvanceFilter() {
411 ++filter_pos_;
412 auto filter_range = FilterRange();
413 if (filter_range.valid()) {
414 FastForwardGen(filter_range);
415 }
416 return filter_range;
417 }
418 KeyType AdvanceGen() {
John Zulaufd5115702021-01-18 12:34:33 -0700419 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700420 auto gen_range = GenRange();
421 if (gen_range.valid()) {
422 FastForwardFilter(gen_range);
423 }
424 return gen_range;
425 }
426
427 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
John Zulaufd5115702021-01-18 12:34:33 -0700428 KeyType GenRange() const { return *gen_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700429
430 KeyType FastForwardFilter(const KeyType &range) {
431 auto filter_range = FilterRange();
432 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700433 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700434 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
435 if (retry_count < kRetryLimit) {
436 ++filter_pos_;
437 filter_range = FilterRange();
438 retry_count++;
439 } else {
440 // Okay we've tried walking, do a seek.
441 filter_pos_ = filter_->lower_bound(range);
442 break;
443 }
444 }
445 return FilterRange();
446 }
447
448 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
449 // faster.
450 KeyType FastForwardGen(const KeyType &range) {
451 auto gen_range = GenRange();
452 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
John Zulaufd5115702021-01-18 12:34:33 -0700453 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700454 gen_range = GenRange();
455 }
456 return gen_range;
457 }
458
459 void SeekBegin() {
460 auto gen_range = GenRange();
461 if (gen_range.empty()) {
462 current_ = KeyType();
463 filter_pos_ = filter_->cend();
464 } else {
465 filter_pos_ = filter_->lower_bound(gen_range);
466 current_ = gen_range & FilterRange();
467 }
468 }
469
John Zulauf4a6105a2020-11-17 15:11:05 -0700470 const FilterMap *filter_;
John Zulaufd5115702021-01-18 12:34:33 -0700471 RangeGen gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700472 typename FilterMap::const_iterator filter_pos_;
473 KeyType current_;
474};
475
476using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
477
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700478static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700479
John Zulauf3e86bf02020-09-12 10:47:57 -0600480ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
481 VkDeviceSize stride) {
482 VkDeviceSize range_start = offset + first_index * stride;
483 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600484 if (count == UINT32_MAX) {
485 range_size = buf_whole_size - range_start;
486 } else {
487 range_size = count * stride;
488 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600489 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600490}
491
locke-lunarg654e3692020-06-04 17:19:15 -0600492SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
493 VkShaderStageFlagBits stage_flag) {
494 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
495 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
496 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
497 }
498 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
499 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
500 assert(0);
501 }
502 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
503 return stage_access->second.uniform_read;
504 }
505
506 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
507 // Because if write hazard happens, read hazard might or might not happen.
508 // But if write hazard doesn't happen, read hazard is impossible to happen.
509 if (descriptor_data.is_writable) {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700510 return stage_access->second.storage_write;
locke-lunarg654e3692020-06-04 17:19:15 -0600511 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700512 // TODO: sampled_read
513 return stage_access->second.storage_read;
locke-lunarg654e3692020-06-04 17:19:15 -0600514}
515
locke-lunarg37047832020-06-12 13:44:45 -0600516bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
517 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
518 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
519 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
520 ? true
521 : false;
522}
523
524bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
525 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
526 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
527 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
528 ? true
529 : false;
530}
531
John Zulauf355e49b2020-04-24 15:11:15 -0600532// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600533template <typename Action>
534static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
535 Action &action) {
536 // At this point the "apply over range" logic only supports a single memory binding
537 if (!SimpleBinding(image_state)) return;
538 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600539 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700540 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
541 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600542 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700543 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600544 }
545}
546
John Zulauf7635de32020-05-29 17:14:15 -0600547// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
548// Used by both validation and record operations
549//
550// The signature for Action() reflect the needs of both uses.
551template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700552void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
553 uint32_t subpass) {
John Zulauf7635de32020-05-29 17:14:15 -0600554 const auto &rp_ci = rp_state.createInfo;
555 const auto *attachment_ci = rp_ci.pAttachments;
556 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
557
558 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
559 const auto *color_attachments = subpass_ci.pColorAttachments;
560 const auto *color_resolve = subpass_ci.pResolveAttachments;
561 if (color_resolve && color_attachments) {
562 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
563 const auto &color_attach = color_attachments[i].attachment;
564 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
565 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
566 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700567 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ,
568 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600569 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700570 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
571 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600572 }
573 }
574 }
575
576 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700577 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600578 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
579 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
580 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
581 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
582 const auto src_ci = attachment_ci[src_at];
583 // The formats are required to match so we can pick either
584 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
585 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
586 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
John Zulauf7635de32020-05-29 17:14:15 -0600587
588 // Figure out which aspects are actually touched during resolve operations
589 const char *aspect_string = nullptr;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700590 AttachmentViewGen::Gen gen_type = AttachmentViewGen::Gen::kRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600591 if (resolve_depth && resolve_stencil) {
John Zulauf7635de32020-05-29 17:14:15 -0600592 aspect_string = "depth/stencil";
593 } else if (resolve_depth) {
594 // Validate depth only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700595 gen_type = AttachmentViewGen::Gen::kDepthOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600596 aspect_string = "depth";
597 } else if (resolve_stencil) {
598 // Validate all stencil only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700599 gen_type = AttachmentViewGen::Gen::kStencilOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600600 aspect_string = "stencil";
601 }
602
John Zulaufd0ec59f2021-03-13 14:25:08 -0700603 if (aspect_string) {
604 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at], gen_type,
605 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster);
606 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at], gen_type,
607 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulauf7635de32020-05-29 17:14:15 -0600608 }
609 }
610}
611
612// Action for validating resolve operations
613class ValidateResolveAction {
614 public:
John Zulauffaea0ee2021-01-14 14:01:32 -0700615 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context,
John Zulauf64ffe552021-02-06 10:25:07 -0700616 const CommandExecutionContext &ex_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600617 : render_pass_(render_pass),
618 subpass_(subpass),
619 context_(context),
John Zulauf64ffe552021-02-06 10:25:07 -0700620 ex_context_(ex_context),
John Zulauf7635de32020-05-29 17:14:15 -0600621 func_name_(func_name),
622 skip_(false) {}
623 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
John Zulaufd0ec59f2021-03-13 14:25:08 -0700624 const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage,
625 SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600626 HazardResult hazard;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700627 hazard = context_.DetectHazard(view_gen, gen_type, current_usage, ordering_rule);
John Zulauf7635de32020-05-29 17:14:15 -0600628 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -0700629 skip_ |=
John Zulauf64ffe552021-02-06 10:25:07 -0700630 ex_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700631 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
632 " to resolve attachment %" PRIu32 ". Access info %s.",
633 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name,
John Zulauf64ffe552021-02-06 10:25:07 -0700634 attachment_name, src_at, dst_at, ex_context_.FormatUsage(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600635 }
636 }
637 // Providing a mechanism for the constructing caller to get the result of the validation
638 bool GetSkip() const { return skip_; }
639
640 private:
641 VkRenderPass render_pass_;
642 const uint32_t subpass_;
643 const AccessContext &context_;
John Zulauf64ffe552021-02-06 10:25:07 -0700644 const CommandExecutionContext &ex_context_;
John Zulauf7635de32020-05-29 17:14:15 -0600645 const char *func_name_;
646 bool skip_;
647};
648
649// Update action for resolve operations
650class UpdateStateResolveAction {
651 public:
652 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
John Zulaufd0ec59f2021-03-13 14:25:08 -0700653 void operator()(const char *, const char *, uint32_t, uint32_t, const AttachmentViewGen &view_gen,
654 AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600655 // Ignores validation only arguments...
John Zulaufd0ec59f2021-03-13 14:25:08 -0700656 context_.UpdateAccessState(view_gen, gen_type, current_usage, ordering_rule, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600657 }
658
659 private:
660 AccessContext &context_;
661 const ResourceUsageTag &tag_;
662};
663
John Zulauf59e25072020-07-17 10:55:21 -0600664void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700665 const SyncStageAccessFlags &prior_, const ResourceUsageTag &tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600666 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
667 usage_index = usage_index_;
668 hazard = hazard_;
669 prior_access = prior_;
670 tag = tag_;
671}
672
John Zulauf540266b2020-04-06 18:54:53 -0600673AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
674 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600675 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600676 Reset();
677 const auto &subpass_dep = dependencies[subpass];
John Zulauf22aefed2021-03-11 18:14:35 -0700678 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
679 prev_.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
John Zulauf355e49b2020-04-24 15:11:15 -0600680 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600681 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600682 const auto prev_pass = prev_dep.first->pass;
683 const auto &prev_barriers = prev_dep.second;
684 assert(prev_dep.second.size());
685 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
686 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700687 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600688
689 async_.reserve(subpass_dep.async.size());
690 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700691 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600692 }
John Zulauf22aefed2021-03-11 18:14:35 -0700693 if (has_barrier_from_external) {
694 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
695 prev_.emplace_back(external_context, queue_flags, subpass_dep.barrier_from_external);
696 src_external_ = &prev_.back();
John Zulaufe5da6e52020-03-18 15:32:18 -0600697 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600698 if (subpass_dep.barrier_to_external.size()) {
699 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600700 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700701}
702
John Zulauf5f13a792020-03-10 07:31:21 -0600703template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700704HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600705 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600706 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600707 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600708
709 HazardResult hazard;
710 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
711 hazard = detector.Detect(prev);
712 }
713 return hazard;
714}
715
John Zulauf4a6105a2020-11-17 15:11:05 -0700716template <typename Action>
717void AccessContext::ForAll(Action &&action) {
718 for (const auto address_type : kAddressTypes) {
719 auto &accesses = GetAccessStateMap(address_type);
720 for (const auto &access : accesses) {
721 action(address_type, access);
722 }
723 }
724}
725
John Zulauf3d84f1b2020-03-09 13:33:25 -0600726// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
727// the DAG of the contexts (for example subpasses)
728template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700729HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600730 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600731 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600732
John Zulauf1a224292020-06-30 14:52:13 -0600733 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600734 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
735 // so we'll check these first
736 for (const auto &async_context : async_) {
737 hazard = async_context->DetectAsyncHazard(type, detector, range);
738 if (hazard.hazard) return hazard;
739 }
John Zulauf5f13a792020-03-10 07:31:21 -0600740 }
741
John Zulauf1a224292020-06-30 14:52:13 -0600742 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600743
John Zulauf69133422020-05-20 14:55:53 -0600744 const auto &accesses = GetAccessStateMap(type);
745 const auto from = accesses.lower_bound(range);
746 const auto to = accesses.upper_bound(range);
747 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600748
John Zulauf69133422020-05-20 14:55:53 -0600749 for (auto pos = from; pos != to; ++pos) {
750 // Cover any leading gap, or gap between entries
751 if (detect_prev) {
752 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
753 // Cover any leading gap, or gap between entries
754 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600755 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600756 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600757 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600758 if (hazard.hazard) return hazard;
759 }
John Zulauf69133422020-05-20 14:55:53 -0600760 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
761 gap.begin = pos->first.end;
762 }
763
764 hazard = detector.Detect(pos);
765 if (hazard.hazard) return hazard;
766 }
767
768 if (detect_prev) {
769 // Detect in the trailing empty as needed
770 gap.end = range.end;
771 if (gap.non_empty()) {
772 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600773 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600774 }
775
776 return hazard;
777}
778
779// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
780template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700781HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
782 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600783 auto &accesses = GetAccessStateMap(type);
784 const auto from = accesses.lower_bound(range);
785 const auto to = accesses.upper_bound(range);
786
John Zulauf3d84f1b2020-03-09 13:33:25 -0600787 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600788 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700789 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600790 }
John Zulauf16adfc92020-04-08 10:28:33 -0600791
John Zulauf3d84f1b2020-03-09 13:33:25 -0600792 return hazard;
793}
794
John Zulaufb02c1eb2020-10-06 16:33:36 -0600795struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700796 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600797 void operator()(ResourceAccessState *access) const {
798 assert(access);
799 access->ApplyBarriers(barriers, true);
800 }
801 const std::vector<SyncBarrier> &barriers;
802};
803
John Zulauf22aefed2021-03-11 18:14:35 -0700804struct ApplyTrackbackStackAction {
805 explicit ApplyTrackbackStackAction(const std::vector<SyncBarrier> &barriers_,
806 const ResourceAccessStateFunction *previous_barrier_ = nullptr)
807 : barriers(barriers_), previous_barrier(previous_barrier_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600808 void operator()(ResourceAccessState *access) const {
809 assert(access);
810 assert(!access->HasPendingState());
811 access->ApplyBarriers(barriers, false);
812 access->ApplyPendingBarriers(kCurrentCommandTag);
John Zulauf22aefed2021-03-11 18:14:35 -0700813 if (previous_barrier) {
814 assert(bool(*previous_barrier));
815 (*previous_barrier)(access);
816 }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600817 }
818 const std::vector<SyncBarrier> &barriers;
John Zulauf22aefed2021-03-11 18:14:35 -0700819 const ResourceAccessStateFunction *previous_barrier;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600820};
821
822// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
823// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
824// *different* map from dest.
825// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
826// range [first, last)
827template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600828static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
829 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600830 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600831 auto at = entry;
832 for (auto pos = first; pos != last; ++pos) {
833 // Every member of the input iterator range must fit within the remaining portion of entry
834 assert(at->first.includes(pos->first));
835 assert(at != dest->end());
836 // Trim up at to the same size as the entry to resolve
837 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600838 auto access = pos->second; // intentional copy
839 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600840 at->second.Resolve(access);
841 ++at; // Go to the remaining unused section of entry
842 }
843}
844
John Zulaufa0a98292020-09-18 09:30:10 -0600845static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
846 SyncBarrier merged = {};
847 for (const auto &barrier : barriers) {
848 merged.Merge(barrier);
849 }
850 return merged;
851}
852
John Zulaufb02c1eb2020-10-06 16:33:36 -0600853template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700854void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600855 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
856 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600857 if (!range.non_empty()) return;
858
John Zulauf355e49b2020-04-24 15:11:15 -0600859 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
860 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600861 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600862 if (current->pos_B->valid) {
863 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600864 auto access = src_pos->second; // intentional copy
865 barrier_action(&access);
866
John Zulauf16adfc92020-04-08 10:28:33 -0600867 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600868 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
869 trimmed->second.Resolve(access);
870 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600871 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600872 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600873 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600874 }
John Zulauf16adfc92020-04-08 10:28:33 -0600875 } else {
876 // we have to descend to fill this gap
877 if (recur_to_infill) {
John Zulauf22aefed2021-03-11 18:14:35 -0700878 ResourceAccessRange recurrence_range = current_range;
879 // The current context is empty for the current range, so recur to fill the gap.
880 // Since we will be recurring back up the DAG, expand the gap descent to cover the full range for which B
881 // is not valid, to minimize that recurrence
882 if (current->pos_B.at_end()) {
883 // Do the remainder here....
884 recurrence_range.end = range.end;
John Zulauf355e49b2020-04-24 15:11:15 -0600885 } else {
John Zulauf22aefed2021-03-11 18:14:35 -0700886 // Recur only over the range until B becomes valid (within the limits of range).
887 recurrence_range.end = std::min(range.end, current->pos_B->lower_bound->first.begin);
John Zulauf355e49b2020-04-24 15:11:15 -0600888 }
John Zulauf22aefed2021-03-11 18:14:35 -0700889 ResolvePreviousAccessStack(type, recurrence_range, resolve_map, infill_state, barrier_action);
890
John Zulauf355e49b2020-04-24 15:11:15 -0600891 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
892 // iterator of the outer while.
893
894 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
895 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
896 // we stepped on the dest map
John Zulauf22aefed2021-03-11 18:14:35 -0700897 const auto seek_to = recurrence_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
locke-lunarg88dbb542020-06-23 22:05:42 -0600898 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600899 current.seek(seek_to);
900 } else if (!current->pos_A->valid && infill_state) {
901 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
902 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
903 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600904 }
John Zulauf5f13a792020-03-10 07:31:21 -0600905 }
John Zulauf16adfc92020-04-08 10:28:33 -0600906 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600907 }
John Zulauf1a224292020-06-30 14:52:13 -0600908
909 // Infill if range goes passed both the current and resolve map prior contents
910 if (recur_to_infill && (current->range.end < range.end)) {
911 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
John Zulauf22aefed2021-03-11 18:14:35 -0700912 ResolvePreviousAccessStack<BarrierAction>(type, trailing_fill_range, resolve_map, infill_state, barrier_action);
John Zulauf1a224292020-06-30 14:52:13 -0600913 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600914}
915
John Zulauf22aefed2021-03-11 18:14:35 -0700916template <typename BarrierAction>
917void AccessContext::ResolvePreviousAccessStack(AccessAddressType type, const ResourceAccessRange &range,
918 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
919 const BarrierAction &previous_barrier) const {
920 ResourceAccessStateFunction stacked_barrier(std::ref(previous_barrier));
921 ResolvePreviousAccess(type, range, descent_map, infill_state, &stacked_barrier);
922}
923
John Zulauf43cc7462020-12-03 12:33:12 -0700924void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
John Zulauf22aefed2021-03-11 18:14:35 -0700925 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
926 const ResourceAccessStateFunction *previous_barrier) const {
927 if (prev_.size() == 0) {
John Zulauf5f13a792020-03-10 07:31:21 -0600928 if (range.non_empty() && infill_state) {
John Zulauf22aefed2021-03-11 18:14:35 -0700929 // Fill the empty poritions of descent_map with the default_state with the barrier function applied (iff present)
930 ResourceAccessState state_copy;
931 if (previous_barrier) {
932 assert(bool(*previous_barrier));
933 state_copy = *infill_state;
934 (*previous_barrier)(&state_copy);
935 infill_state = &state_copy;
936 }
937 sparse_container::update_range_value(*descent_map, range, *infill_state,
938 sparse_container::value_precedence::prefer_dest);
John Zulauf5f13a792020-03-10 07:31:21 -0600939 }
940 } else {
941 // Look for something to fill the gap further along.
942 for (const auto &prev_dep : prev_) {
John Zulauf22aefed2021-03-11 18:14:35 -0700943 const ApplyTrackbackStackAction barrier_action(prev_dep.barriers, previous_barrier);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600944 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600945 }
John Zulauf5f13a792020-03-10 07:31:21 -0600946 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600947}
948
John Zulauf4a6105a2020-11-17 15:11:05 -0700949// Non-lazy import of all accesses, WaitEvents needs this.
950void AccessContext::ResolvePreviousAccesses() {
951 ResourceAccessState default_state;
John Zulauf22aefed2021-03-11 18:14:35 -0700952 if (!prev_.size()) return; // If no previous contexts, nothing to do
953
John Zulauf4a6105a2020-11-17 15:11:05 -0700954 for (const auto address_type : kAddressTypes) {
955 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
956 }
957}
958
John Zulauf43cc7462020-12-03 12:33:12 -0700959AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
960 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -0600961}
962
John Zulauf1507ee42020-05-18 11:33:09 -0600963static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
964 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
965 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
966 return stage_access;
967}
968static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
969 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
970 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
971 return stage_access;
972}
973
John Zulauf7635de32020-05-29 17:14:15 -0600974// Caller must manage returned pointer
975static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -0700976 uint32_t subpass, const AttachmentViewGenVector &attachment_views) {
John Zulauf7635de32020-05-29 17:14:15 -0600977 auto *proxy = new AccessContext(context);
John Zulaufd0ec59f2021-03-13 14:25:08 -0700978 proxy->UpdateAttachmentResolveAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
979 proxy->UpdateAttachmentStoreAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600980 return proxy;
981}
982
John Zulaufb02c1eb2020-10-06 16:33:36 -0600983template <typename BarrierAction>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700984void AccessContext::ResolveAccessRange(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
985 BarrierAction &barrier_action, ResourceAccessRangeMap *descent_map,
986 const ResourceAccessState *infill_state) const {
987 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
988 if (!attachment_gen) return;
989
990 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
991 const AccessAddressType address_type = view_gen.GetAddressType();
992 for (; range_gen->non_empty(); ++range_gen) {
993 ResolveAccessRange(address_type, *range_gen, barrier_action, descent_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600994 }
John Zulauf62f10592020-04-03 12:20:02 -0600995}
996
John Zulauf7635de32020-05-29 17:14:15 -0600997// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf64ffe552021-02-06 10:25:07 -0700998bool AccessContext::ValidateLayoutTransitions(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600999 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001000 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001001 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -06001002 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
1003 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
1004 // those affects have not been recorded yet.
1005 //
1006 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1007 // to apply and only copy then, if this proves a hot spot.
1008 std::unique_ptr<AccessContext> proxy_for_prev;
1009 TrackBack proxy_track_back;
1010
John Zulauf355e49b2020-04-24 15:11:15 -06001011 const auto &transitions = rp_state.subpass_transitions[subpass];
1012 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -06001013 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
1014
1015 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
John Zulauf22aefed2021-03-11 18:14:35 -07001016 assert(track_back);
John Zulauf7635de32020-05-29 17:14:15 -06001017 if (prev_needs_proxy) {
1018 if (!proxy_for_prev) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001019 proxy_for_prev.reset(
1020 CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass, attachment_views));
John Zulauf7635de32020-05-29 17:14:15 -06001021 proxy_track_back = *track_back;
1022 proxy_track_back.context = proxy_for_prev.get();
1023 }
1024 track_back = &proxy_track_back;
1025 }
1026 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -06001027 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07001028 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001029 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1030 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
1031 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1032 string_VkImageLayout(transition.old_layout),
1033 string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07001034 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001035 }
1036 }
1037 return skip;
1038}
1039
John Zulauf64ffe552021-02-06 10:25:07 -07001040bool AccessContext::ValidateLoadOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001041 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001042 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001043 bool skip = false;
1044 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufa0a98292020-09-18 09:30:10 -06001045
John Zulauf1507ee42020-05-18 11:33:09 -06001046 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1047 if (subpass == rp_state.attachment_first_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001048 const auto &view_gen = attachment_views[i];
1049 if (!view_gen.IsValid()) continue;
John Zulauf1507ee42020-05-18 11:33:09 -06001050 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001051
1052 // Need check in the following way
1053 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1054 // vs. transition
1055 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1056 // for each aspect loaded.
1057
1058 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001059 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001060 const bool is_color = !(has_depth || has_stencil);
1061
1062 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001063 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001064
John Zulaufaff20662020-06-01 14:07:58 -06001065 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001066 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001067
John Zulaufb02c1eb2020-10-06 16:33:36 -06001068 bool checked_stencil = false;
1069 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001070 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea, load_index, SyncOrdering::kColorAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001071 aspect = "color";
1072 } else {
1073 if (has_depth) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001074 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_index,
1075 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001076 aspect = "depth";
1077 }
1078 if (!hazard.hazard && has_stencil) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001079 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, stencil_load_index,
1080 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001081 aspect = "stencil";
1082 checked_stencil = true;
1083 }
1084 }
1085
1086 if (hazard.hazard) {
1087 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001088 const auto &sync_state = ex_context.GetSyncState();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001089 if (hazard.tag == kCurrentCommandTag) {
1090 // Hazard vs. ILT
1091 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1092 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1093 " aspect %s during load with loadOp %s.",
1094 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1095 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06001096 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1097 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001098 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001099 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf64ffe552021-02-06 10:25:07 -07001100 ex_context.FormatUsage(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001101 }
1102 }
1103 }
1104 }
1105 return skip;
1106}
1107
John Zulaufaff20662020-06-01 14:07:58 -06001108// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1109// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1110// store is part of the same Next/End operation.
1111// The latter is handled in layout transistion validation directly
John Zulauf64ffe552021-02-06 10:25:07 -07001112bool AccessContext::ValidateStoreOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufaff20662020-06-01 14:07:58 -06001113 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001114 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001115 bool skip = false;
1116 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001117
1118 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1119 if (subpass == rp_state.attachment_last_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001120 const AttachmentViewGen &view_gen = attachment_views[i];
1121 if (!view_gen.IsValid()) continue;
John Zulaufaff20662020-06-01 14:07:58 -06001122 const auto &ci = attachment_ci[i];
1123
1124 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1125 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1126 // sake, we treat DONT_CARE as writing.
1127 const bool has_depth = FormatHasDepth(ci.format);
1128 const bool has_stencil = FormatHasStencil(ci.format);
1129 const bool is_color = !(has_depth || has_stencil);
1130 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1131 if (!has_stencil && !store_op_stores) continue;
1132
1133 HazardResult hazard;
1134 const char *aspect = nullptr;
1135 bool checked_stencil = false;
1136 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001137 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
1138 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001139 aspect = "color";
1140 } else {
1141 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
John Zulaufaff20662020-06-01 14:07:58 -06001142 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001143 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1144 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001145 aspect = "depth";
1146 }
1147 if (!hazard.hazard && has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001148 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1149 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001150 aspect = "stencil";
1151 checked_stencil = true;
1152 }
1153 }
1154
1155 if (hazard.hazard) {
1156 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1157 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001158 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001159 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1160 " %s aspect during store with %s %s. Access info %s",
1161 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect,
John Zulauf64ffe552021-02-06 10:25:07 -07001162 op_type_string, store_op_string, ex_context.FormatUsage(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001163 }
1164 }
1165 }
1166 return skip;
1167}
1168
John Zulauf64ffe552021-02-06 10:25:07 -07001169bool AccessContext::ValidateResolveOperations(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001170 const VkRect2D &render_area, const AttachmentViewGenVector &attachment_views,
1171 const char *func_name, uint32_t subpass) const {
John Zulauf64ffe552021-02-06 10:25:07 -07001172 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, ex_context, func_name);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001173 ResolveOperation(validate_action, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001174 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001175}
1176
John Zulauf3d84f1b2020-03-09 13:33:25 -06001177class HazardDetector {
1178 SyncStageAccessIndex usage_index_;
1179
1180 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001181 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001182 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1183 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001184 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001185 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001186};
1187
John Zulauf69133422020-05-20 14:55:53 -06001188class HazardDetectorWithOrdering {
1189 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001190 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001191
1192 public:
1193 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001194 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001195 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001196 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1197 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001198 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001199 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001200};
1201
John Zulauf16adfc92020-04-08 10:28:33 -06001202HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001203 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001204 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001205 const auto base_address = ResourceBaseAddress(buffer);
1206 HazardDetector detector(usage_index);
1207 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001208}
1209
John Zulauf69133422020-05-20 14:55:53 -06001210template <typename Detector>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001211HazardResult AccessContext::DetectHazard(Detector &detector, const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1212 DetectOptions options) const {
1213 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1214 if (!attachment_gen) return HazardResult();
1215
1216 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1217 const auto address_type = view_gen.GetAddressType();
1218 for (; range_gen->non_empty(); ++range_gen) {
1219 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1220 if (hazard.hazard) return hazard;
1221 }
1222
1223 return HazardResult();
1224}
1225
1226template <typename Detector>
John Zulauf69133422020-05-20 14:55:53 -06001227HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1228 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1229 const VkExtent3D &extent, DetectOptions options) const {
1230 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001231 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001232 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1233 base_address);
1234 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001235 for (; range_gen->non_empty(); ++range_gen) {
John Zulaufd05c5842021-03-26 11:32:16 -06001236#ifdef SYNCVAL_DIAGNOSTICS
1237 sync_diagnostics.Detect(*range_gen);
1238#endif
John Zulauf150e5332020-12-03 08:52:52 -07001239 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001240 if (hazard.hazard) return hazard;
1241 }
1242 return HazardResult();
1243}
1244
John Zulauf540266b2020-04-06 18:54:53 -06001245HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1246 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1247 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001248 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1249 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -06001250 return DetectHazard(image, current_usage, subresource_range, offset, extent);
1251}
1252
1253HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1254 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1255 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001256 HazardDetector detector(current_usage);
1257 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1258}
1259
John Zulaufd0ec59f2021-03-13 14:25:08 -07001260HazardResult AccessContext::DetectHazard(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1261 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) const {
1262 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
1263 return DetectHazard(detector, view_gen, gen_type, DetectOptions::kDetectAll);
1264}
1265
John Zulauf69133422020-05-20 14:55:53 -06001266HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001267 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001268 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001269 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001270 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001271}
1272
John Zulaufb027cdb2020-05-21 14:25:22 -06001273// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1274// should have reported the issue regarding an invalid attachment entry
1275HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001276 SyncOrdering ordering_rule, const VkOffset3D &offset, const VkExtent3D &extent,
John Zulaufb027cdb2020-05-21 14:25:22 -06001277 VkImageAspectFlags aspect_mask) const {
1278 if (view != nullptr) {
1279 const IMAGE_STATE *image = view->image_state.get();
1280 if (image != nullptr) {
1281 auto *detect_range = &view->normalized_subresource_range;
1282 VkImageSubresourceRange masked_range;
1283 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1284 masked_range = view->normalized_subresource_range;
1285 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1286 detect_range = &masked_range;
1287 }
1288
1289 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1290 if (detect_range->aspectMask) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001291 return DetectHazard(*image, current_usage, *detect_range, ordering_rule, offset, extent);
John Zulaufb027cdb2020-05-21 14:25:22 -06001292 }
1293 }
1294 }
1295 return HazardResult();
1296}
John Zulauf43cc7462020-12-03 12:33:12 -07001297
John Zulauf3d84f1b2020-03-09 13:33:25 -06001298class BarrierHazardDetector {
1299 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001300 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001301 SyncStageAccessFlags src_access_scope)
1302 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1303
John Zulauf5f13a792020-03-10 07:31:21 -06001304 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1305 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001306 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001307 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001308 // 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 -07001309 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001310 }
1311
1312 private:
1313 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001314 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001315 SyncStageAccessFlags src_access_scope_;
1316};
1317
John Zulauf4a6105a2020-11-17 15:11:05 -07001318class EventBarrierHazardDetector {
1319 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001320 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001321 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
1322 const ResourceUsageTag &scope_tag)
1323 : usage_index_(usage_index),
1324 src_exec_scope_(src_exec_scope),
1325 src_access_scope_(src_access_scope),
1326 event_scope_(event_scope),
1327 scope_pos_(event_scope.cbegin()),
1328 scope_end_(event_scope.cend()),
1329 scope_tag_(scope_tag) {}
1330
1331 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1332 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1333 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1334 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1335 if (scope_pos_ == scope_end_) return HazardResult();
1336 if (!scope_pos_->first.intersects(pos->first)) {
1337 event_scope_.lower_bound(pos->first);
1338 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1339 }
1340
1341 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1342 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1343 }
1344 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1345 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1346 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1347 }
1348
1349 private:
1350 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001351 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001352 SyncStageAccessFlags src_access_scope_;
1353 const SyncEventState::ScopeMap &event_scope_;
1354 SyncEventState::ScopeMap::const_iterator scope_pos_;
1355 SyncEventState::ScopeMap::const_iterator scope_end_;
1356 const ResourceUsageTag &scope_tag_;
1357};
1358
Jeremy Gebben40a22942020-12-22 14:22:06 -07001359HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001360 const SyncStageAccessFlags &src_access_scope,
1361 const VkImageSubresourceRange &subresource_range,
1362 const SyncEventState &sync_event, DetectOptions options) const {
1363 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1364 // first access scope map to use, and there's no easy way to plumb it in below.
1365 const auto address_type = ImageAddressType(image);
1366 const auto &event_scope = sync_event.FirstScope(address_type);
1367
1368 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1369 event_scope, sync_event.first_scope_tag);
1370 VkOffset3D zero_offset = {0, 0, 0};
1371 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
1372}
1373
John Zulaufd0ec59f2021-03-13 14:25:08 -07001374HazardResult AccessContext::DetectImageBarrierHazard(const AttachmentViewGen &view_gen, const SyncBarrier &barrier,
1375 DetectOptions options) const {
1376 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, barrier.src_exec_scope.exec_scope,
1377 barrier.src_access_scope);
1378 return DetectHazard(detector, view_gen, AttachmentViewGen::Gen::kViewSubresource, options);
1379}
1380
Jeremy Gebben40a22942020-12-22 14:22:06 -07001381HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001382 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001383 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001384 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001385 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1386 VkOffset3D zero_offset = {0, 0, 0};
1387 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001388}
1389
Jeremy Gebben40a22942020-12-22 14:22:06 -07001390HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001391 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001392 const VkImageMemoryBarrier &barrier) const {
1393 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1394 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1395 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1396}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001397HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07001398 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope.exec_scope,
John Zulaufd5115702021-01-18 12:34:33 -07001399 image_barrier.barrier.src_access_scope, image_barrier.range.subresource_range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001400}
John Zulauf355e49b2020-04-24 15:11:15 -06001401
John Zulauf9cb530d2019-09-30 14:14:10 -06001402template <typename Flags, typename Map>
1403SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1404 SyncStageAccessFlags scope = 0;
1405 for (const auto &bit_scope : map) {
1406 if (flag_mask < bit_scope.first) break;
1407
1408 if (flag_mask & bit_scope.first) {
1409 scope |= bit_scope.second;
1410 }
1411 }
1412 return scope;
1413}
1414
Jeremy Gebben40a22942020-12-22 14:22:06 -07001415SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags2KHR stages) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001416 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1417}
1418
Jeremy Gebben40a22942020-12-22 14:22:06 -07001419SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags2KHR accesses) {
1420 return AccessScopeImpl(sync_utils::ExpandAccessFlags(accesses), syncStageAccessMaskByAccessBit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001421}
1422
Jeremy Gebben40a22942020-12-22 14:22:06 -07001423// Getting from stage mask and access mask to stage/access masks is something we need to be good at...
1424SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001425 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1426 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1427 // 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 -06001428 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1429}
1430
1431template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001432void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001433 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1434 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001435 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001436 auto pos = accesses->lower_bound(range);
1437 if (pos == accesses->end() || !pos->first.intersects(range)) {
1438 // The range is empty, fill it with a default value.
1439 pos = action.Infill(accesses, pos, range);
1440 } else if (range.begin < pos->first.begin) {
1441 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001442 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001443 } else if (pos->first.begin < range.begin) {
1444 // Trim the beginning if needed
1445 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1446 ++pos;
1447 }
1448
1449 const auto the_end = accesses->end();
1450 while ((pos != the_end) && pos->first.intersects(range)) {
1451 if (pos->first.end > range.end) {
1452 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1453 }
1454
1455 pos = action(accesses, pos);
1456 if (pos == the_end) break;
1457
1458 auto next = pos;
1459 ++next;
1460 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1461 // Need to infill if next is disjoint
1462 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001463 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001464 next = action.Infill(accesses, next, new_range);
1465 }
1466 pos = next;
1467 }
1468}
John Zulaufd5115702021-01-18 12:34:33 -07001469
1470// Give a comparable interface for range generators and ranges
1471template <typename Action>
1472inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1473 assert(range);
1474 UpdateMemoryAccessState(accesses, *range, action);
1475}
1476
John Zulauf4a6105a2020-11-17 15:11:05 -07001477template <typename Action, typename RangeGen>
1478void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1479 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001480 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 -07001481 for (; range_gen->non_empty(); ++range_gen) {
1482 UpdateMemoryAccessState(accesses, *range_gen, action);
1483 }
1484}
John Zulauf9cb530d2019-09-30 14:14:10 -06001485
John Zulaufd0ec59f2021-03-13 14:25:08 -07001486template <typename Action, typename RangeGen>
1487void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, const RangeGen &range_gen_prebuilt) {
1488 RangeGen range_gen(range_gen_prebuilt); // RangeGenerators can be expensive to create from scratch... initialize from built
1489 for (; range_gen->non_empty(); ++range_gen) {
1490 UpdateMemoryAccessState(accesses, *range_gen, action);
1491 }
1492}
John Zulauf9cb530d2019-09-30 14:14:10 -06001493struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001494 using Iterator = ResourceAccessRangeMap::iterator;
1495 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001496 // this is only called on gaps, and never returns a gap.
1497 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001498 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001499 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001500 }
John Zulauf5f13a792020-03-10 07:31:21 -06001501
John Zulauf5c5e88d2019-12-26 11:22:02 -07001502 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001503 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001504 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001505 return pos;
1506 }
1507
John Zulauf43cc7462020-12-03 12:33:12 -07001508 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001509 SyncOrdering ordering_rule_, const ResourceUsageTag &tag_)
1510 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001511 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001512 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001513 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001514 const SyncOrdering ordering_rule;
John Zulauf9cb530d2019-09-30 14:14:10 -06001515 const ResourceUsageTag &tag;
1516};
1517
John Zulauf4a6105a2020-11-17 15:11:05 -07001518// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001519struct PipelineBarrierOp {
1520 SyncBarrier barrier;
1521 bool layout_transition;
1522 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1523 : barrier(barrier_), layout_transition(layout_transition_) {}
1524 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001525 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001526 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1527};
John Zulauf4a6105a2020-11-17 15:11:05 -07001528// The barrier operation for wait events
1529struct WaitEventBarrierOp {
1530 const ResourceUsageTag *scope_tag;
1531 SyncBarrier barrier;
1532 bool layout_transition;
1533 WaitEventBarrierOp(const ResourceUsageTag &scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1534 : scope_tag(&scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
1535 WaitEventBarrierOp() = default;
1536 void operator()(ResourceAccessState *access_state) const {
1537 assert(scope_tag); // Not valid to have a non-scope op executed, default construct included for std::vector support
1538 access_state->ApplyBarrier(*scope_tag, barrier, layout_transition);
1539 }
1540};
John Zulauf1e331ec2020-12-04 18:29:38 -07001541
John Zulauf4a6105a2020-11-17 15:11:05 -07001542// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1543// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1544// of a collection is known/present.
John Zulauf1e331ec2020-12-04 18:29:38 -07001545template <typename BarrierOp>
John Zulauf89311b42020-09-29 16:28:47 -06001546class ApplyBarrierOpsFunctor {
1547 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001548 using Iterator = ResourceAccessRangeMap::iterator;
1549 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001550
John Zulauf5c5e88d2019-12-26 11:22:02 -07001551 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001552 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001553 for (const auto &op : barrier_ops_) {
1554 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001555 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001556
John Zulauf89311b42020-09-29 16:28:47 -06001557 if (resolve_) {
1558 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1559 // another walk
1560 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001561 }
1562 return pos;
1563 }
1564
John Zulauf89311b42020-09-29 16:28:47 -06001565 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulaufd5115702021-01-18 12:34:33 -07001566 ApplyBarrierOpsFunctor(bool resolve, size_t size_hint, const ResourceUsageTag &tag)
1567 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1568 barrier_ops_.reserve(size_hint);
1569 }
1570 void EmplaceBack(const BarrierOp &op) { barrier_ops_.emplace_back(op); }
John Zulauf89311b42020-09-29 16:28:47 -06001571
1572 private:
1573 bool resolve_;
John Zulaufd5115702021-01-18 12:34:33 -07001574 std::vector<BarrierOp> barrier_ops_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001575 const ResourceUsageTag &tag_;
1576};
1577
John Zulauf4a6105a2020-11-17 15:11:05 -07001578// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1579// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1580template <typename BarrierOp>
1581class ApplyBarrierFunctor {
1582 public:
1583 using Iterator = ResourceAccessRangeMap::iterator;
1584 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1585
1586 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1587 auto &access_state = pos->second;
1588 barrier_op_(&access_state);
1589 return pos;
1590 }
1591
1592 ApplyBarrierFunctor(const BarrierOp &barrier_op) : barrier_op_(barrier_op) {}
1593
1594 private:
John Zulaufd5115702021-01-18 12:34:33 -07001595 BarrierOp barrier_op_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001596};
1597
John Zulauf1e331ec2020-12-04 18:29:38 -07001598// This functor resolves the pendinging state.
1599class ResolvePendingBarrierFunctor {
1600 public:
1601 using Iterator = ResourceAccessRangeMap::iterator;
1602 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1603
1604 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1605 auto &access_state = pos->second;
1606 access_state.ApplyPendingBarriers(tag_);
1607 return pos;
1608 }
1609
1610 ResolvePendingBarrierFunctor(const ResourceUsageTag &tag) : tag_(tag) {}
1611
1612 private:
John Zulauf89311b42020-09-29 16:28:47 -06001613 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001614};
1615
John Zulauf8e3c3e92021-01-06 11:19:36 -07001616void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
1617 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
1618 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001619 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001620}
1621
John Zulauf8e3c3e92021-01-06 11:19:36 -07001622void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001623 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001624 if (!SimpleBinding(buffer)) return;
1625 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001626 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001627}
John Zulauf355e49b2020-04-24 15:11:15 -06001628
John Zulauf8e3c3e92021-01-06 11:19:36 -07001629void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001630 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001631 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001632 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001633 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001634 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1635 base_address);
1636 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001637 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001638 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001639 UpdateMemoryAccessState(&GetAccessStateMap(address_type), *range_gen, action);
John Zulauf5f13a792020-03-10 07:31:21 -06001640 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001641}
John Zulaufd0ec59f2021-03-13 14:25:08 -07001642
1643void AccessContext::UpdateAccessState(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1644 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule, const ResourceUsageTag &tag) {
1645 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1646 if (!gen) return;
1647 subresource_adapter::ImageRangeGenerator range_gen(*gen);
1648 const auto address_type = view_gen.GetAddressType();
1649 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1650 ApplyUpdateAction(address_type, action, &range_gen);
John Zulauf7635de32020-05-29 17:14:15 -06001651}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001652
John Zulauf8e3c3e92021-01-06 11:19:36 -07001653void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001654 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1655 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001656 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1657 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001658 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001659}
1660
John Zulaufd0ec59f2021-03-13 14:25:08 -07001661template <typename Action, typename RangeGen>
1662void AccessContext::ApplyUpdateAction(AccessAddressType address_type, const Action &action, RangeGen *range_gen_arg) {
1663 assert(range_gen_arg); // Old Google C++ styleguide require non-const object pass by * not &, but this isn't an optional arg.
1664 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, range_gen_arg);
John Zulauf540266b2020-04-06 18:54:53 -06001665}
1666
1667template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001668void AccessContext::ApplyUpdateAction(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, const Action &action) {
1669 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1670 if (!gen) return;
1671 UpdateMemoryAccessState(&GetAccessStateMap(view_gen.GetAddressType()), action, *gen);
John Zulauf540266b2020-04-06 18:54:53 -06001672}
1673
John Zulaufd0ec59f2021-03-13 14:25:08 -07001674void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state,
1675 const AttachmentViewGenVector &attachment_views, uint32_t subpass,
John Zulauf7635de32020-05-29 17:14:15 -06001676 const ResourceUsageTag &tag) {
1677 UpdateStateResolveAction update(*this, tag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001678 ResolveOperation(update, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001679}
1680
John Zulaufd0ec59f2021-03-13 14:25:08 -07001681void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
1682 uint32_t subpass, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001683 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001684
1685 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1686 if (rp_state.attachment_last_subpass[i] == subpass) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001687 const auto &view_gen = attachment_views[i];
1688 if (!view_gen.IsValid()) continue; // UNUSED
John Zulaufaff20662020-06-01 14:07:58 -06001689
1690 const auto &ci = attachment_ci[i];
1691 const bool has_depth = FormatHasDepth(ci.format);
1692 const bool has_stencil = FormatHasStencil(ci.format);
1693 const bool is_color = !(has_depth || has_stencil);
1694 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1695
1696 if (is_color && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001697 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
1698 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001699 } else {
John Zulaufaff20662020-06-01 14:07:58 -06001700 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001701 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1702 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001703 }
1704 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1705 if (has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001706 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1707 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001708 }
1709 }
1710 }
1711 }
1712}
1713
John Zulauf540266b2020-04-06 18:54:53 -06001714template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001715void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001716 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001717 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001718 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001719 }
1720}
1721
1722void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001723 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1724 auto &context = contexts[subpass_index];
John Zulauf22aefed2021-03-11 18:14:35 -07001725 ApplyTrackbackStackAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001726 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001727 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001728 }
1729 }
1730}
1731
John Zulauf355e49b2020-04-24 15:11:15 -06001732// Suitable only for *subpass* access contexts
John Zulaufd0ec59f2021-03-13 14:25:08 -07001733HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const AttachmentViewGen &attach_view) const {
1734 if (!attach_view.IsValid()) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001735
John Zulauf355e49b2020-04-24 15:11:15 -06001736 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001737 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001738
1739 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001740 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1741 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001742 HazardResult hazard = track_back.context->DetectImageBarrierHazard(attach_view, merged_barrier, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001743 if (!hazard.hazard) {
1744 // The Async hazard check is against the current context's async set.
John Zulaufd0ec59f2021-03-13 14:25:08 -07001745 hazard = DetectImageBarrierHazard(attach_view, merged_barrier, kDetectAsync);
John Zulauf355e49b2020-04-24 15:11:15 -06001746 }
John Zulaufa0a98292020-09-18 09:30:10 -06001747
John Zulauf355e49b2020-04-24 15:11:15 -06001748 return hazard;
1749}
1750
John Zulaufb02c1eb2020-10-06 16:33:36 -06001751void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001752 const AttachmentViewGenVector &attachment_views, const ResourceUsageTag &tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001753 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001754 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001755 for (const auto &transition : transitions) {
1756 const auto prev_pass = transition.prev_pass;
John Zulaufd0ec59f2021-03-13 14:25:08 -07001757 const auto &view_gen = attachment_views[transition.attachment];
1758 if (!view_gen.IsValid()) continue;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001759
1760 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1761 assert(trackback);
1762
1763 // Import the attachments into the current context
1764 const auto *prev_context = trackback->context;
1765 assert(prev_context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001766 const auto address_type = view_gen.GetAddressType();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001767 auto &target_map = GetAccessStateMap(address_type);
1768 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001769 prev_context->ResolveAccessRange(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action, &target_map,
1770 &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001771 }
1772
John Zulauf86356ca2020-10-19 11:46:41 -06001773 // If there were no transitions skip this global map walk
1774 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001775 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001776 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001777 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001778}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001779
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001780void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
1781 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
John Zulauf669dfd52021-01-27 17:15:28 -07001782
1783 auto *events_context = GetCurrentEventsContext();
1784 assert(events_context);
1785 for (auto &event_pair : *events_context) {
John Zulauf4a6105a2020-11-17 15:11:05 -07001786 assert(event_pair.second); // Shouldn't be storing empty
1787 auto &sync_event = *event_pair.second;
1788 // 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 -07001789 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
1790 sync_event.barriers |= dst.exec_scope;
1791 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
John Zulauf4a6105a2020-11-17 15:11:05 -07001792 }
1793 }
1794}
1795
John Zulauf355e49b2020-04-24 15:11:15 -06001796
locke-lunarg61870c22020-06-09 14:51:50 -06001797bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1798 const char *func_name) const {
1799 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001800 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001801 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001802 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1803 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001804 return skip;
1805 }
1806
1807 using DescriptorClass = cvdescriptorset::DescriptorClass;
1808 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1809 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1810 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1811 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1812
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001813 for (const auto &stage_state : pipe->stage_state) {
1814 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1815 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001816 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001817 }
locke-lunarg61870c22020-06-09 14:51:50 -06001818 for (const auto &set_binding : stage_state.descriptor_uses) {
1819 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1820 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1821 set_binding.first.second);
1822 const auto descriptor_type = binding_it.GetType();
1823 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1824 auto array_idx = 0;
1825
1826 if (binding_it.IsVariableDescriptorCount()) {
1827 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1828 }
1829 SyncStageAccessIndex sync_index =
1830 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1831
1832 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1833 uint32_t index = i - index_range.start;
1834 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1835 switch (descriptor->GetClass()) {
1836 case DescriptorClass::ImageSampler:
1837 case DescriptorClass::Image: {
1838 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001839 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001840 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001841 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1842 img_view_state = image_sampler_descriptor->GetImageViewState();
1843 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001844 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001845 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1846 img_view_state = image_descriptor->GetImageViewState();
1847 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001848 }
1849 if (!img_view_state) continue;
1850 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1851 VkExtent3D extent = {};
1852 VkOffset3D offset = {};
1853 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1854 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1855 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1856 } else {
1857 extent = img_state->createInfo.extent;
1858 }
John Zulauf361fb532020-07-22 10:45:39 -06001859 HazardResult hazard;
1860 const auto &subresource_range = img_view_state->normalized_subresource_range;
1861 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1862 // Input attachments are subject to raster ordering rules
1863 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001864 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001865 } else {
1866 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1867 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001868 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001869 skip |= sync_state_->LogError(
1870 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001871 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1872 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001873 func_name, string_SyncHazard(hazard.hazard),
1874 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1875 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001876 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001877 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1878 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
John Zulauffaea0ee2021-01-14 14:01:32 -07001879 set_binding.first.second, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001880 }
1881 break;
1882 }
1883 case DescriptorClass::TexelBuffer: {
1884 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1885 if (!buf_view_state) continue;
1886 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001887 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001888 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001889 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001890 skip |= sync_state_->LogError(
1891 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001892 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1893 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001894 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1895 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001896 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001897 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1898 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001899 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001900 }
1901 break;
1902 }
1903 case DescriptorClass::GeneralBuffer: {
1904 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1905 auto buf_state = buffer_descriptor->GetBufferState();
1906 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001907 const ResourceAccessRange range =
1908 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001909 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001910 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001911 skip |= sync_state_->LogError(
1912 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001913 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1914 func_name, string_SyncHazard(hazard.hazard),
1915 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001916 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001917 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001918 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1919 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001920 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001921 }
1922 break;
1923 }
1924 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1925 default:
1926 break;
1927 }
1928 }
1929 }
1930 }
1931 return skip;
1932}
1933
1934void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1935 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001936 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001937 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001938 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1939 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001940 return;
1941 }
1942
1943 using DescriptorClass = cvdescriptorset::DescriptorClass;
1944 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1945 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1946 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1947 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1948
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001949 for (const auto &stage_state : pipe->stage_state) {
1950 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1951 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001952 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001953 }
locke-lunarg61870c22020-06-09 14:51:50 -06001954 for (const auto &set_binding : stage_state.descriptor_uses) {
1955 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1956 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1957 set_binding.first.second);
1958 const auto descriptor_type = binding_it.GetType();
1959 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1960 auto array_idx = 0;
1961
1962 if (binding_it.IsVariableDescriptorCount()) {
1963 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1964 }
1965 SyncStageAccessIndex sync_index =
1966 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1967
1968 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1969 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1970 switch (descriptor->GetClass()) {
1971 case DescriptorClass::ImageSampler:
1972 case DescriptorClass::Image: {
1973 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1974 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1975 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1976 } else {
1977 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1978 }
1979 if (!img_view_state) continue;
1980 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1981 VkExtent3D extent = {};
1982 VkOffset3D offset = {};
1983 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1984 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1985 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1986 } else {
1987 extent = img_state->createInfo.extent;
1988 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001989 SyncOrdering ordering_rule = (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)
1990 ? SyncOrdering::kRaster
1991 : SyncOrdering::kNonAttachment;
1992 current_context_->UpdateAccessState(*img_state, sync_index, ordering_rule,
1993 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001994 break;
1995 }
1996 case DescriptorClass::TexelBuffer: {
1997 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1998 if (!buf_view_state) continue;
1999 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002000 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07002001 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002002 break;
2003 }
2004 case DescriptorClass::GeneralBuffer: {
2005 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
2006 auto buf_state = buffer_descriptor->GetBufferState();
2007 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002008 const ResourceAccessRange range =
2009 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07002010 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002011 break;
2012 }
2013 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2014 default:
2015 break;
2016 }
2017 }
2018 }
2019 }
2020}
2021
2022bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2023 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002024 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2025 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002026 return skip;
2027 }
2028
2029 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2030 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002031 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002032
2033 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002034 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002035 if (binding_description.binding < binding_buffers_size) {
2036 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002037 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002038
locke-lunarg1ae57d62020-11-18 10:49:19 -07002039 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002040 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2041 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002042 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002043 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002044 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002045 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 -06002046 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07002047 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002048 }
2049 }
2050 }
2051 return skip;
2052}
2053
2054void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002055 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2056 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002057 return;
2058 }
2059 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2060 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002061 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002062
2063 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002064 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002065 if (binding_description.binding < binding_buffers_size) {
2066 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002067 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002068
locke-lunarg1ae57d62020-11-18 10:49:19 -07002069 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002070 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2071 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002072 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2073 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002074 }
2075 }
2076}
2077
2078bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2079 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002080 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002081 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002082 }
locke-lunarg61870c22020-06-09 14:51:50 -06002083
locke-lunarg1ae57d62020-11-18 10:49:19 -07002084 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002085 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002086 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2087 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002088 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002089 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002090 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002091 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 -06002092 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07002093 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002094 }
2095
2096 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2097 // We will detect more accurate range in the future.
2098 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2099 return skip;
2100}
2101
2102void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002103 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 -06002104
locke-lunarg1ae57d62020-11-18 10:49:19 -07002105 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002106 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002107 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2108 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002109 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002110
2111 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2112 // We will detect more accurate range in the future.
2113 RecordDrawVertex(UINT32_MAX, 0, tag);
2114}
2115
2116bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002117 bool skip = false;
2118 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002119 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002120 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002121}
2122
2123void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002124 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002125 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002126 }
locke-lunarg61870c22020-06-09 14:51:50 -06002127}
2128
John Zulauf64ffe552021-02-06 10:25:07 -07002129void CommandBufferAccessContext::RecordBeginRenderPass(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2130 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2131 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002132 // Create an access context the current renderpass.
John Zulauf64ffe552021-02-06 10:25:07 -07002133 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002134 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf64ffe552021-02-06 10:25:07 -07002135 current_renderpass_context_->RecordBeginRenderPass(tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002136 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002137}
2138
John Zulauf64ffe552021-02-06 10:25:07 -07002139void CommandBufferAccessContext::RecordNextSubpass(CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002140 assert(current_renderpass_context_);
John Zulauffaea0ee2021-01-14 14:01:32 -07002141 auto prev_tag = NextCommandTag(command);
2142 auto next_tag = NextSubcommandTag(command);
John Zulauf64ffe552021-02-06 10:25:07 -07002143 current_renderpass_context_->RecordNextSubpass(prev_tag, next_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002144 current_context_ = &current_renderpass_context_->CurrentContext();
2145}
2146
John Zulauf64ffe552021-02-06 10:25:07 -07002147void CommandBufferAccessContext::RecordEndRenderPass(CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002148 assert(current_renderpass_context_);
2149 if (!current_renderpass_context_) return;
2150
John Zulauf64ffe552021-02-06 10:25:07 -07002151 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, NextCommandTag(command));
John Zulauf355e49b2020-04-24 15:11:15 -06002152 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002153 current_renderpass_context_ = nullptr;
2154}
2155
John Zulauf4a6105a2020-11-17 15:11:05 -07002156void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2157 // Erase is okay with the key not being
John Zulauf669dfd52021-01-27 17:15:28 -07002158 const auto *event_state = sync_state_->Get<EVENT_STATE>(event);
2159 if (event_state) {
2160 GetCurrentEventsContext()->Destroy(event_state);
John Zulaufd5115702021-01-18 12:34:33 -07002161 }
2162}
2163
John Zulauf64ffe552021-02-06 10:25:07 -07002164bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
John Zulauffaea0ee2021-01-14 14:01:32 -07002165 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002166 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002167 const auto &sync_state = ex_context.GetSyncState();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002168 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2169 if (!pipe ||
2170 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002171 return skip;
2172 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002173 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002174 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg37047832020-06-12 13:44:45 -06002175
John Zulauf1a224292020-06-30 14:52:13 -06002176 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002177 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002178 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2179 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002180 if (location >= subpass.colorAttachmentCount ||
2181 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002182 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002183 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002184 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2185 if (!view_gen.IsValid()) continue;
2186 HazardResult hazard =
2187 current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
2188 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment);
locke-lunarg96dc9632020-06-10 17:22:18 -06002189 if (hazard.hazard) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002190 const VkImageView view_handle = view_gen.GetViewState()->image_view;
2191 skip |= sync_state.LogError(view_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002192 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002193 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002194 sync_state.report_data->FormatHandle(view_handle).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06002195 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002196 location, ex_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002197 }
2198 }
2199 }
locke-lunarg37047832020-06-12 13:44:45 -06002200
2201 // PHASE1 TODO: Add layout based read/vs. write selection.
2202 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002203 const uint32_t depth_stencil_attachment =
2204 GetSubpassDepthStencilAttachmentIndex(pipe->graphicsPipelineCI.pDepthStencilState, subpass.pDepthStencilAttachment);
2205
2206 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2207 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2208 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002209 bool depth_write = false, stencil_write = false;
2210
2211 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002212 if (!FormatIsStencilOnly(view_state.create_info.format) && pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002213 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002214 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2215 depth_write = true;
2216 }
2217 // PHASE1 TODO: It needs to check if stencil is writable.
2218 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2219 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2220 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002221 if (!FormatIsDepthOnly(view_state.create_info.format) && pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002222 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2223 stencil_write = true;
2224 }
2225
2226 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2227 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002228 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2229 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2230 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002231 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002232 skip |= sync_state.LogError(
John Zulaufd0ec59f2021-03-13 14:25:08 -07002233 view_state.image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002234 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002235 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002236 sync_state.report_data->FormatHandle(view_state.image_view).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06002237 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002238 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002239 }
2240 }
2241 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002242 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2243 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2244 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002245 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002246 skip |= sync_state.LogError(
John Zulaufd0ec59f2021-03-13 14:25:08 -07002247 view_state.image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002248 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002249 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002250 sync_state.report_data->FormatHandle(view_state.image_view).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06002251 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002252 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002253 }
locke-lunarg61870c22020-06-09 14:51:50 -06002254 }
2255 }
2256 return skip;
2257}
2258
John Zulauf64ffe552021-02-06 10:25:07 -07002259void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002260 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2261 if (!pipe ||
2262 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002263 return;
2264 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002265 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002266 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002267
John Zulauf1a224292020-06-30 14:52:13 -06002268 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002269 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002270 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2271 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002272 if (location >= subpass.colorAttachmentCount ||
2273 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002274 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002275 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002276 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2277 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2278 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2279 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002280 }
2281 }
locke-lunarg37047832020-06-12 13:44:45 -06002282
2283 // PHASE1 TODO: Add layout based read/vs. write selection.
2284 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002285 const uint32_t depth_stencil_attachment =
2286 GetSubpassDepthStencilAttachmentIndex(pipe->graphicsPipelineCI.pDepthStencilState, subpass.pDepthStencilAttachment);
2287 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2288 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2289 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002290 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002291 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2292 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002293
2294 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002295 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002296 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2297 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002298 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2299 depth_write = true;
2300 }
2301 // PHASE1 TODO: It needs to check if stencil is writable.
2302 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2303 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2304 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002305 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002306 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002307 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2308 stencil_write = true;
2309 }
2310
John Zulaufd0ec59f2021-03-13 14:25:08 -07002311 if (depth_write || stencil_write) {
2312 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2313 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2314 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2315 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002316 }
locke-lunarg61870c22020-06-09 14:51:50 -06002317 }
2318}
2319
John Zulauf64ffe552021-02-06 10:25:07 -07002320bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002321 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002322 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002323 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002324 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002325 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002326 func_name);
2327
John Zulauf355e49b2020-04-24 15:11:15 -06002328 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002329 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002330 skip |=
2331 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002332 if (!skip) {
2333 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2334 // on a copy of the (empty) next context.
2335 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2336 AccessContext temp_context(next_context);
2337 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002338 skip |=
2339 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002340 }
John Zulauf7635de32020-05-29 17:14:15 -06002341 return skip;
2342}
John Zulauf64ffe552021-02-06 10:25:07 -07002343bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002344 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002345 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002346 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002347 current_subpass_);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002348 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_,
2349
2350 attachment_views_, func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002351 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002352 return skip;
2353}
2354
John Zulauf64ffe552021-02-06 10:25:07 -07002355AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002356 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002357}
2358
John Zulauf64ffe552021-02-06 10:25:07 -07002359bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2360 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002361 bool skip = false;
2362
John Zulauf7635de32020-05-29 17:14:15 -06002363 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2364 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2365 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2366 // to apply and only copy then, if this proves a hot spot.
2367 std::unique_ptr<AccessContext> proxy_for_current;
2368
John Zulauf355e49b2020-04-24 15:11:15 -06002369 // Validate the "finalLayout" transitions to external
2370 // Get them from where there we're hidding in the extra entry.
2371 const auto &final_transitions = rp_state_->subpass_transitions.back();
2372 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002373 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002374 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2375 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002376 auto *context = trackback.context;
2377
2378 if (transition.prev_pass == current_subpass_) {
2379 if (!proxy_for_current) {
2380 // 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 -07002381 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002382 }
2383 context = proxy_for_current.get();
2384 }
2385
John Zulaufa0a98292020-09-18 09:30:10 -06002386 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2387 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002388 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002389 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002390 skip |= ex_context.GetSyncState().LogError(
John Zulauffaea0ee2021-01-14 14:01:32 -07002391 rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2392 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2393 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2394 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2395 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002396 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002397 }
2398 }
2399 return skip;
2400}
2401
2402void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2403 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002404 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002405}
2406
John Zulauf64ffe552021-02-06 10:25:07 -07002407void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag &tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002408 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2409 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002410
2411 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2412 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002413 const AttachmentViewGen &view_gen = attachment_views_[i];
2414 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002415
2416 const auto &ci = attachment_ci[i];
2417 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002418 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002419 const bool is_color = !(has_depth || has_stencil);
2420
2421 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002422 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, ColorLoadUsage(ci.loadOp),
2423 SyncOrdering::kColorAttachment, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002424 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002425 if (has_depth) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002426 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2427 DepthStencilLoadUsage(ci.loadOp), SyncOrdering::kDepthStencilAttachment, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002428 }
2429 if (has_stencil) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002430 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2431 DepthStencilLoadUsage(ci.stencilLoadOp),
2432 SyncOrdering::kDepthStencilAttachment, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002433 }
2434 }
2435 }
2436 }
2437}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002438AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2439 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2440 AttachmentViewGenVector view_gens;
2441 VkExtent3D extent = CastTo3D(render_area.extent);
2442 VkOffset3D offset = CastTo3D(render_area.offset);
2443 view_gens.reserve(attachment_views.size());
2444 for (const auto *view : attachment_views) {
2445 view_gens.emplace_back(view, offset, extent);
2446 }
2447 return view_gens;
2448}
John Zulauf64ffe552021-02-06 10:25:07 -07002449RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2450 VkQueueFlags queue_flags,
2451 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2452 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002453 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002454 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002455 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002456 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002457 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002458 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002459 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002460}
2461void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
2462 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002463 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002464 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002465 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002466}
John Zulauf1507ee42020-05-18 11:33:09 -06002467
John Zulauf64ffe552021-02-06 10:25:07 -07002468void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag &prev_subpass_tag,
John Zulauffaea0ee2021-01-14 14:01:32 -07002469 const ResourceUsageTag &next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002470 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulaufd0ec59f2021-03-13 14:25:08 -07002471 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
2472 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002473
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002474 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2475 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002476 current_subpass_++;
2477 assert(current_subpass_ < subpass_contexts_.size());
John Zulauffaea0ee2021-01-14 14:01:32 -07002478 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2479 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002480 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002481}
2482
John Zulauf64ffe552021-02-06 10:25:07 -07002483void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002484 // Add the resolve and store accesses
John Zulaufd0ec59f2021-03-13 14:25:08 -07002485 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, tag);
2486 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002487
John Zulauf355e49b2020-04-24 15:11:15 -06002488 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002489 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002490
2491 // Add the "finalLayout" transitions to external
2492 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002493 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2494 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2495 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002496 const auto &final_transitions = rp_state_->subpass_transitions.back();
2497 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002498 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002499 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002500 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002501 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002502 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002503 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002504 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002505 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002506 }
2507}
2508
Jeremy Gebben40a22942020-12-22 14:22:06 -07002509SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002510 SyncExecScope result;
2511 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002512 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2513 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002514 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2515 return result;
2516}
2517
Jeremy Gebben40a22942020-12-22 14:22:06 -07002518SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002519 SyncExecScope result;
2520 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002521 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2522 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002523 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2524 return result;
2525}
2526
2527SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002528 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002529 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002530 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002531 dst_access_scope = 0;
2532}
2533
2534template <typename Barrier>
2535SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002536 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002537 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002538 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002539 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2540}
2541
2542SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002543 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2544 if (barrier) {
2545 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002546 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002547 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002548
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002549 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002550 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002551 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2552
2553 } else {
2554 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002555 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002556 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2557
2558 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002559 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002560 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2561 }
2562}
2563
2564template <typename Barrier>
2565SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2566 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2567 src_exec_scope = src.exec_scope;
2568 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2569
2570 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002571 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002572 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002573}
2574
John Zulaufb02c1eb2020-10-06 16:33:36 -06002575// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2576void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2577 for (const auto &barrier : barriers) {
2578 ApplyBarrier(barrier, layout_transition);
2579 }
2580}
2581
John Zulauf89311b42020-09-29 16:28:47 -06002582// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2583// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2584// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002585void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2586 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002587 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002588 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002589 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002590 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002591 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002592 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002593}
John Zulauf9cb530d2019-09-30 14:14:10 -06002594HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2595 HazardResult hazard;
2596 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002597 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002598 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002599 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002600 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002601 }
2602 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002603 // Write operation:
2604 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2605 // If reads exists -- test only against them because either:
2606 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2607 // * 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
2608 // the current write happens after the reads, so just test the write against the reades
2609 // Otherwise test against last_write
2610 //
2611 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002612 if (last_reads.size()) {
2613 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002614 if (IsReadHazard(usage_stage, read_access)) {
2615 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2616 break;
2617 }
2618 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002619 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002620 // Write-After-Write check -- if we have a previous write to test against
2621 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002622 }
2623 }
2624 return hazard;
2625}
2626
John Zulauf8e3c3e92021-01-06 11:19:36 -07002627HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering &ordering_rule) const {
2628 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06002629 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2630 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002631 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002632 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002633 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2634 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002635 if (IsRead(usage_bit)) {
2636 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2637 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2638 if (is_raw_hazard) {
2639 // NOTE: we know last_write is non-zero
2640 // See if the ordering rules save us from the simple RAW check above
2641 // First check to see if the current usage is covered by the ordering rules
2642 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2643 const bool usage_is_ordered =
2644 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2645 if (usage_is_ordered) {
2646 // Now see of the most recent write (or a subsequent read) are ordered
2647 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2648 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002649 }
2650 }
John Zulauf4285ee92020-09-23 10:20:52 -06002651 if (is_raw_hazard) {
2652 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2653 }
John Zulauf361fb532020-07-22 10:45:39 -06002654 } else {
2655 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002656 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002657 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002658 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002659 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002660 if (usage_write_is_ordered) {
2661 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2662 ordered_stages = GetOrderedStages(ordering);
2663 }
2664 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2665 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002666 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002667 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2668 if (IsReadHazard(usage_stage, read_access)) {
2669 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2670 break;
2671 }
John Zulaufd14743a2020-07-03 09:42:39 -06002672 }
2673 }
John Zulauf4285ee92020-09-23 10:20:52 -06002674 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002675 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002676 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002677 }
John Zulauf69133422020-05-20 14:55:53 -06002678 }
2679 }
2680 return hazard;
2681}
2682
John Zulauf2f952d22020-02-10 11:34:51 -07002683// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002684HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002685 HazardResult hazard;
2686 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002687 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2688 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2689 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002690 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002691 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002692 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002693 }
2694 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002695 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002696 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002697 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002698 // 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 -07002699 for (const auto &read_access : last_reads) {
2700 if (read_access.tag.index >= start_tag.index) {
2701 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002702 break;
2703 }
2704 }
John Zulauf2f952d22020-02-10 11:34:51 -07002705 }
2706 }
2707 return hazard;
2708}
2709
Jeremy Gebben40a22942020-12-22 14:22:06 -07002710HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002711 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002712 // Only supporting image layout transitions for now
2713 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2714 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002715 // only test for WAW if there no intervening read operations.
2716 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002717 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002718 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002719 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002720 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002721 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002722 break;
2723 }
2724 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002725 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2726 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2727 }
2728
2729 return hazard;
2730}
2731
Jeremy Gebben40a22942020-12-22 14:22:06 -07002732HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07002733 const SyncStageAccessFlags &src_access_scope,
2734 const ResourceUsageTag &event_tag) const {
2735 // Only supporting image layout transitions for now
2736 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2737 HazardResult hazard;
2738 // only test for WAW if there no intervening read operations.
2739 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2740
John Zulaufab7756b2020-12-29 16:10:16 -07002741 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002742 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
2743 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07002744 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002745 if (read_access.tag.IsBefore(event_tag)) {
2746 // The read is in the events first synchronization scope, so we use a barrier hazard check
2747 // If the read stage is not in the src sync scope
2748 // *AND* not execution chained with an existing sync barrier (that's the or)
2749 // then the barrier access is unsafe (R/W after R)
2750 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
2751 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2752 break;
2753 }
2754 } else {
2755 // The read not in the event first sync scope and so is a hazard vs. the layout transition
2756 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2757 }
2758 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002759 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002760 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
2761 if (write_tag.IsBefore(event_tag)) {
2762 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
2763 // So do a normal barrier hazard check
2764 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2765 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2766 }
2767 } else {
2768 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06002769 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2770 }
John Zulaufd14743a2020-07-03 09:42:39 -06002771 }
John Zulauf361fb532020-07-22 10:45:39 -06002772
John Zulauf0cb5be22020-01-23 12:18:22 -07002773 return hazard;
2774}
2775
John Zulauf5f13a792020-03-10 07:31:21 -06002776// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2777// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2778// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2779void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2780 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002781 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2782 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002783 *this = other;
2784 } else if (!other.write_tag.IsBefore(write_tag)) {
2785 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2786 // dependency chaining logic or any stage expansion)
2787 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002788 pending_write_barriers |= other.pending_write_barriers;
2789 pending_layout_transition |= other.pending_layout_transition;
2790 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002791
John Zulaufd14743a2020-07-03 09:42:39 -06002792 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07002793 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06002794 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07002795 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002796 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002797 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002798 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002799 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2800 // but we should wait on profiling data for that.
2801 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002802 auto &my_read = last_reads[my_read_index];
2803 if (other_read.stage == my_read.stage) {
2804 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002805 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002806 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002807 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002808 my_read.pending_dep_chain = other_read.pending_dep_chain;
2809 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2810 // May require tracking more than one access per stage.
2811 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002812 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06002813 // Since I'm overwriting the fragement stage read, also update the input attachment info
2814 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002815 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002816 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002817 } else if (other_read.tag.IsBefore(my_read.tag)) {
2818 // The read tags match so merge the barriers
2819 my_read.barriers |= other_read.barriers;
2820 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002821 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002822
John Zulauf5f13a792020-03-10 07:31:21 -06002823 break;
2824 }
2825 }
2826 } else {
2827 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07002828 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06002829 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002830 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002831 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002832 }
John Zulauf5f13a792020-03-10 07:31:21 -06002833 }
2834 }
John Zulauf361fb532020-07-22 10:45:39 -06002835 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002836 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2837 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07002838
2839 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
2840 // of the copy and other into this using the update first logic.
2841 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
2842 // of the other first_accesses... )
2843 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
2844 FirstAccesses firsts(std::move(first_accesses_));
2845 first_accesses_.clear();
2846 first_read_stages_ = 0U;
2847 auto a = firsts.begin();
2848 auto a_end = firsts.end();
2849 for (auto &b : other.first_accesses_) {
2850 // TODO: Determine whether "IsBefore" or "IsGloballyBefore" is needed...
2851 while (a != a_end && a->tag.IsBefore(b.tag)) {
2852 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2853 ++a;
2854 }
2855 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
2856 }
2857 for (; a != a_end; ++a) {
2858 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2859 }
2860 }
John Zulauf5f13a792020-03-10 07:31:21 -06002861}
2862
John Zulauf8e3c3e92021-01-06 11:19:36 -07002863void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag &tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002864 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2865 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002866 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002867 // Mulitple outstanding reads may be of interest and do dependency chains independently
2868 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2869 const auto usage_stage = PipelineStageBit(usage_index);
2870 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002871 for (auto &read_access : last_reads) {
2872 if (read_access.stage == usage_stage) {
2873 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002874 break;
2875 }
2876 }
2877 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07002878 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002879 last_read_stages |= usage_stage;
2880 }
John Zulauf4285ee92020-09-23 10:20:52 -06002881
2882 // 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 -07002883 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002884 // TODO Revisit re: multiple reads for a given stage
2885 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002886 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002887 } else {
2888 // Assume write
2889 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002890 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002891 }
John Zulauffaea0ee2021-01-14 14:01:32 -07002892 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06002893}
John Zulauf5f13a792020-03-10 07:31:21 -06002894
John Zulauf89311b42020-09-29 16:28:47 -06002895// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2896// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2897// We can overwrite them as *this* write is now after them.
2898//
2899// 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 -07002900void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002901 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06002902 last_read_stages = 0;
2903 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002904 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002905
2906 write_barriers = 0;
2907 write_dependency_chain = 0;
2908 write_tag = tag;
2909 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002910}
2911
John Zulauf89311b42020-09-29 16:28:47 -06002912// Apply the memory barrier without updating the existing barriers. The execution barrier
2913// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2914// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2915// replace the current write barriers or add to them, so accumulate to pending as well.
2916void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2917 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2918 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002919 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
2920 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
2921 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
2922 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07002923 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06002924 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07002925 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002926 }
John Zulauf89311b42020-09-29 16:28:47 -06002927 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2928 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002929
John Zulauf89311b42020-09-29 16:28:47 -06002930 if (!pending_layout_transition) {
2931 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2932 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002933 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06002934 // 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 -07002935 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
2936 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002937 }
2938 }
John Zulaufa0a98292020-09-18 09:30:10 -06002939 }
John Zulaufa0a98292020-09-18 09:30:10 -06002940}
2941
John Zulauf4a6105a2020-11-17 15:11:05 -07002942// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
2943// changes the "chaining" state, but to keep barriers independent. See discussion above.
2944void ResourceAccessState::ApplyBarrier(const ResourceUsageTag &scope_tag, const SyncBarrier &barrier, bool layout_transition) {
2945 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
2946 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
2947 // in order to know if it's in the excecution scope
2948 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
2949 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
2950 // errors w.r.t. "most recent" accesses.
2951 if (layout_transition || ((write_tag.IsBefore(scope_tag)) && (barrier.src_access_scope & last_write).any())) {
2952 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07002953 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002954 }
2955 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2956 pending_layout_transition |= layout_transition;
2957
2958 if (!pending_layout_transition) {
2959 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2960 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002961 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002962 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
2963 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
2964 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
2965 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
2966 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
2967 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
2968 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulaufc523bf62021-02-16 08:20:34 -07002969 if (read_access.tag.IsBefore(scope_tag) &&
2970 (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers))) {
2971 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002972 }
2973 }
2974 }
2975}
John Zulauf89311b42020-09-29 16:28:47 -06002976void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
2977 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06002978 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2979 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07002980 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf89311b42020-09-29 16:28:47 -06002981 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002982 }
John Zulauf89311b42020-09-29 16:28:47 -06002983
2984 // Apply the accumulate execution barriers (and thus update chaining information)
2985 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07002986 for (auto &read_access : last_reads) {
2987 read_access.barriers |= read_access.pending_dep_chain;
2988 read_execution_barriers |= read_access.barriers;
2989 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06002990 }
2991
2992 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2993 write_dependency_chain |= pending_write_dep_chain;
2994 write_barriers |= pending_write_barriers;
2995 pending_write_dep_chain = 0;
2996 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002997}
2998
John Zulauf59e25072020-07-17 10:55:21 -06002999// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07003000VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
3001 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003002
John Zulaufab7756b2020-12-29 16:10:16 -07003003 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003004 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003005 barriers = read_access.barriers;
3006 break;
John Zulauf59e25072020-07-17 10:55:21 -06003007 }
3008 }
John Zulauf4285ee92020-09-23 10:20:52 -06003009
John Zulauf59e25072020-07-17 10:55:21 -06003010 return barriers;
3011}
3012
Jeremy Gebben40a22942020-12-22 14:22:06 -07003013inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003014 assert(IsRead(usage));
3015 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3016 // * the previous reads are not hazards, and thus last_write must be visible and available to
3017 // any reads that happen after.
3018 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3019 // 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 -07003020 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003021}
3022
Jeremy Gebben40a22942020-12-22 14:22:06 -07003023VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003024 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07003025 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06003026 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003027 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003028 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003029 // 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 -07003030 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06003031 }
3032
3033 return ordered_stages;
3034}
3035
John Zulauffaea0ee2021-01-14 14:01:32 -07003036void ResourceAccessState::UpdateFirst(const ResourceUsageTag &tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
3037 // Only record until we record a write.
3038 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003039 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003040 if (0 == (usage_stage & first_read_stages_)) {
3041 // If this is a read we haven't seen or a write, record.
3042 first_read_stages_ |= usage_stage;
3043 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3044 }
3045 }
3046}
3047
John Zulaufd1f85d42020-04-15 12:23:15 -06003048void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003049 auto *access_context = GetAccessContextNoInsert(command_buffer);
3050 if (access_context) {
3051 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003052 }
3053}
3054
John Zulaufd1f85d42020-04-15 12:23:15 -06003055void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3056 auto access_found = cb_access_state.find(command_buffer);
3057 if (access_found != cb_access_state.end()) {
3058 access_found->second->Reset();
3059 cb_access_state.erase(access_found);
3060 }
3061}
3062
John Zulauf9cb530d2019-09-30 14:14:10 -06003063bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3064 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3065 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003066 const auto *cb_context = GetAccessContext(commandBuffer);
3067 assert(cb_context);
3068 if (!cb_context) return skip;
3069 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003070
John Zulauf3d84f1b2020-03-09 13:33:25 -06003071 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003072 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003073 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003074
3075 for (uint32_t region = 0; region < regionCount; region++) {
3076 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003077 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003078 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003079 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003080 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003081 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003082 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003083 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003084 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003085 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003086 }
John Zulauf16adfc92020-04-08 10:28:33 -06003087 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003088 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003089 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003090 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003091 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003092 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003093 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003094 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003095 }
3096 }
3097 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003098 }
3099 return skip;
3100}
3101
3102void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3103 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003104 auto *cb_context = GetAccessContext(commandBuffer);
3105 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003106 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003107 auto *context = cb_context->GetCurrentAccessContext();
3108
John Zulauf9cb530d2019-09-30 14:14:10 -06003109 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003110 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003111
3112 for (uint32_t region = 0; region < regionCount; region++) {
3113 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003114 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003115 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003116 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003117 }
John Zulauf16adfc92020-04-08 10:28:33 -06003118 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003119 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003120 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003121 }
3122 }
3123}
3124
John Zulauf4a6105a2020-11-17 15:11:05 -07003125void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3126 // Clear out events from the command buffer contexts
3127 for (auto &cb_context : cb_access_state) {
3128 cb_context.second->RecordDestroyEvent(event);
3129 }
3130}
3131
Jeff Leger178b1e52020-10-05 12:22:23 -04003132bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3133 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3134 bool skip = false;
3135 const auto *cb_context = GetAccessContext(commandBuffer);
3136 assert(cb_context);
3137 if (!cb_context) return skip;
3138 const auto *context = cb_context->GetCurrentAccessContext();
3139
3140 // If we have no previous accesses, we have no hazards
3141 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3142 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3143
3144 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3145 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3146 if (src_buffer) {
3147 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003148 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003149 if (hazard.hazard) {
3150 // TODO -- add tag information to log msg when useful.
3151 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3152 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3153 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003154 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003155 }
3156 }
3157 if (dst_buffer && !skip) {
3158 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003159 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003160 if (hazard.hazard) {
3161 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3162 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3163 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003164 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003165 }
3166 }
3167 if (skip) break;
3168 }
3169 return skip;
3170}
3171
3172void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3173 auto *cb_context = GetAccessContext(commandBuffer);
3174 assert(cb_context);
3175 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3176 auto *context = cb_context->GetCurrentAccessContext();
3177
3178 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3179 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3180
3181 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3182 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3183 if (src_buffer) {
3184 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003185 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003186 }
3187 if (dst_buffer) {
3188 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003189 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003190 }
3191 }
3192}
3193
John Zulauf5c5e88d2019-12-26 11:22:02 -07003194bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3195 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3196 const VkImageCopy *pRegions) const {
3197 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003198 const auto *cb_access_context = GetAccessContext(commandBuffer);
3199 assert(cb_access_context);
3200 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003201
John Zulauf3d84f1b2020-03-09 13:33:25 -06003202 const auto *context = cb_access_context->GetCurrentAccessContext();
3203 assert(context);
3204 if (!context) return skip;
3205
3206 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3207 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003208 for (uint32_t region = 0; region < regionCount; region++) {
3209 const auto &copy_region = pRegions[region];
3210 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003211 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003212 copy_region.srcOffset, copy_region.extent);
3213 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003214 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003215 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003216 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003217 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003218 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003219 }
3220
3221 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003222 VkExtent3D dst_copy_extent =
3223 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003224 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003225 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003226 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003227 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003228 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003229 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003230 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003231 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003232 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003233 }
3234 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003235
John Zulauf5c5e88d2019-12-26 11:22:02 -07003236 return skip;
3237}
3238
3239void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3240 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3241 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003242 auto *cb_access_context = GetAccessContext(commandBuffer);
3243 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003244 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003245 auto *context = cb_access_context->GetCurrentAccessContext();
3246 assert(context);
3247
John Zulauf5c5e88d2019-12-26 11:22:02 -07003248 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003249 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003250
3251 for (uint32_t region = 0; region < regionCount; region++) {
3252 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003253 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003254 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003255 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003256 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003257 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003258 VkExtent3D dst_copy_extent =
3259 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003260 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003261 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003262 }
3263 }
3264}
3265
Jeff Leger178b1e52020-10-05 12:22:23 -04003266bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3267 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3268 bool skip = false;
3269 const auto *cb_access_context = GetAccessContext(commandBuffer);
3270 assert(cb_access_context);
3271 if (!cb_access_context) return skip;
3272
3273 const auto *context = cb_access_context->GetCurrentAccessContext();
3274 assert(context);
3275 if (!context) return skip;
3276
3277 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3278 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3279 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3280 const auto &copy_region = pCopyImageInfo->pRegions[region];
3281 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003282 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003283 copy_region.srcOffset, copy_region.extent);
3284 if (hazard.hazard) {
3285 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3286 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3287 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003288 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003289 }
3290 }
3291
3292 if (dst_image) {
3293 VkExtent3D dst_copy_extent =
3294 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003295 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003296 copy_region.dstOffset, dst_copy_extent);
3297 if (hazard.hazard) {
3298 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3299 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3300 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003301 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003302 }
3303 if (skip) break;
3304 }
3305 }
3306
3307 return skip;
3308}
3309
3310void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3311 auto *cb_access_context = GetAccessContext(commandBuffer);
3312 assert(cb_access_context);
3313 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3314 auto *context = cb_access_context->GetCurrentAccessContext();
3315 assert(context);
3316
3317 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3318 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3319
3320 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3321 const auto &copy_region = pCopyImageInfo->pRegions[region];
3322 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003323 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003324 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003325 }
3326 if (dst_image) {
3327 VkExtent3D dst_copy_extent =
3328 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003329 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003330 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003331 }
3332 }
3333}
3334
John Zulauf9cb530d2019-09-30 14:14:10 -06003335bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3336 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3337 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3338 uint32_t bufferMemoryBarrierCount,
3339 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3340 uint32_t imageMemoryBarrierCount,
3341 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3342 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003343 const auto *cb_access_context = GetAccessContext(commandBuffer);
3344 assert(cb_access_context);
3345 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003346
John Zulauf36ef9282021-02-02 11:47:24 -07003347 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3348 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3349 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3350 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003351 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003352 return skip;
3353}
3354
3355void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3356 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3357 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3358 uint32_t bufferMemoryBarrierCount,
3359 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3360 uint32_t imageMemoryBarrierCount,
3361 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003362 auto *cb_access_context = GetAccessContext(commandBuffer);
3363 assert(cb_access_context);
3364 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003365
John Zulauf36ef9282021-02-02 11:47:24 -07003366 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3367 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3368 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3369 pImageMemoryBarriers);
3370 pipeline_barrier.Record(cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003371}
3372
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003373bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3374 const VkDependencyInfoKHR *pDependencyInfo) const {
3375 bool skip = false;
3376 const auto *cb_access_context = GetAccessContext(commandBuffer);
3377 assert(cb_access_context);
3378 if (!cb_access_context) return skip;
3379
3380 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3381 skip = pipeline_barrier.Validate(*cb_access_context);
3382 return skip;
3383}
3384
3385void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3386 auto *cb_access_context = GetAccessContext(commandBuffer);
3387 assert(cb_access_context);
3388 if (!cb_access_context) return;
3389
3390 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3391 pipeline_barrier.Record(cb_access_context);
3392}
3393
John Zulauf9cb530d2019-09-30 14:14:10 -06003394void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3395 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3396 // The state tracker sets up the device state
3397 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3398
John Zulauf5f13a792020-03-10 07:31:21 -06003399 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3400 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003401 // TODO: Find a good way to do this hooklessly.
3402 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3403 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3404 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3405
John Zulaufd1f85d42020-04-15 12:23:15 -06003406 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3407 sync_device_state->ResetCommandBufferCallback(command_buffer);
3408 });
3409 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3410 sync_device_state->FreeCommandBufferCallback(command_buffer);
3411 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003412}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003413
John Zulauf355e49b2020-04-24 15:11:15 -06003414bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf64ffe552021-02-06 10:25:07 -07003415 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd, const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003416 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003417 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003418 if (cb_context) {
3419 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo, cmd_name);
3420 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003421 }
John Zulauf355e49b2020-04-24 15:11:15 -06003422 return skip;
3423}
3424
3425bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3426 VkSubpassContents contents) const {
3427 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003428 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003429 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003430 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003431 return skip;
3432}
3433
3434bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003435 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003436 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003437 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003438 return skip;
3439}
3440
John Zulauf64ffe552021-02-06 10:25:07 -07003441static const char *kBeginRenderPass2KhrName = "vkCmdBeginRenderPass2KHR";
John Zulauf355e49b2020-04-24 15:11:15 -06003442bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3443 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003444 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003445 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003446 skip |=
3447 ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2, kBeginRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003448 return skip;
3449}
3450
John Zulauf3d84f1b2020-03-09 13:33:25 -06003451void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3452 VkResult result) {
3453 // The state tracker sets up the command buffer state
3454 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3455
3456 // Create/initialize the structure that trackers accesses at the command buffer scope.
3457 auto cb_access_context = GetAccessContext(commandBuffer);
3458 assert(cb_access_context);
3459 cb_access_context->Reset();
3460}
3461
3462void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf64ffe552021-02-06 10:25:07 -07003463 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd, const char *cmd_name) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003464 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003465 if (cb_context) {
John Zulauf64ffe552021-02-06 10:25:07 -07003466 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo, cmd_name);
3467 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003468 }
3469}
3470
3471void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3472 VkSubpassContents contents) {
3473 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003474 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003475 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003476 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003477}
3478
3479void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3480 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3481 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003482 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003483}
3484
3485void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3486 const VkRenderPassBeginInfo *pRenderPassBegin,
3487 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3488 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003489 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2, kBeginRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003490}
3491
Mike Schuchardt2df08912020-12-15 16:28:09 -08003492bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf64ffe552021-02-06 10:25:07 -07003493 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd, const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003494 bool skip = false;
3495
3496 auto cb_context = GetAccessContext(commandBuffer);
3497 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003498 if (!cb_context) return skip;
3499 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo, cmd_name);
3500 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003501}
3502
3503bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3504 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003505 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003506 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003507 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003508 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3509 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003510 return skip;
3511}
3512
John Zulauf64ffe552021-02-06 10:25:07 -07003513static const char *kNextSubpass2KhrName = "vkCmdNextSubpass2KHR";
Mike Schuchardt2df08912020-12-15 16:28:09 -08003514bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3515 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003516 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003517 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2, kNextSubpass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003518 return skip;
3519}
3520
3521bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3522 const VkSubpassEndInfo *pSubpassEndInfo) const {
3523 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003524 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003525 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003526}
3527
3528void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf64ffe552021-02-06 10:25:07 -07003529 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd, const char *cmd_name) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003530 auto cb_context = GetAccessContext(commandBuffer);
3531 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003532 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003533
John Zulauf64ffe552021-02-06 10:25:07 -07003534 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo, cmd_name);
3535 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003536}
3537
3538void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3539 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003540 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003541 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003542 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003543}
3544
3545void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3546 const VkSubpassEndInfo *pSubpassEndInfo) {
3547 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003548 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003549}
3550
3551void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3552 const VkSubpassEndInfo *pSubpassEndInfo) {
3553 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003554 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2, kNextSubpass2KhrName);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003555}
3556
John Zulauf64ffe552021-02-06 10:25:07 -07003557bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd,
3558 const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003559 bool skip = false;
3560
3561 auto cb_context = GetAccessContext(commandBuffer);
3562 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003563 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003564
John Zulauf64ffe552021-02-06 10:25:07 -07003565 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo, cmd_name);
3566 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003567 return skip;
3568}
3569
3570bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3571 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003572 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003573 return skip;
3574}
3575
Mike Schuchardt2df08912020-12-15 16:28:09 -08003576bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003577 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003578 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003579 return skip;
3580}
3581
John Zulauf64ffe552021-02-06 10:25:07 -07003582const static char *kEndRenderPass2KhrName = "vkEndRenderPass2KHR";
John Zulauf355e49b2020-04-24 15:11:15 -06003583bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003584 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003585 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003586 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2, kEndRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003587 return skip;
3588}
3589
John Zulauf64ffe552021-02-06 10:25:07 -07003590void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd,
3591 const char *cmd_name) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003592 // Resolve the all subpass contexts to the command buffer contexts
3593 auto cb_context = GetAccessContext(commandBuffer);
3594 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003595 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003596
John Zulauf64ffe552021-02-06 10:25:07 -07003597 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo, cmd_name);
3598 sync_op.Record(cb_context);
3599 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003600}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003601
John Zulauf33fc1d52020-07-17 11:01:10 -06003602// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3603// updates to a resource which do not conflict at the byte level.
3604// TODO: Revisit this rule to see if it needs to be tighter or looser
3605// TODO: Add programatic control over suppression heuristics
3606bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3607 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3608}
3609
John Zulauf3d84f1b2020-03-09 13:33:25 -06003610void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003611 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003612 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003613}
3614
3615void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003616 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003617 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003618}
3619
3620void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf64ffe552021-02-06 10:25:07 -07003621 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2, kEndRenderPass2KhrName);
John Zulauf5a1a5382020-06-22 17:23:25 -06003622 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003623}
locke-lunarga19c71d2020-03-02 18:17:04 -07003624
Jeff Leger178b1e52020-10-05 12:22:23 -04003625template <typename BufferImageCopyRegionType>
3626bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3627 VkImageLayout dstImageLayout, uint32_t regionCount,
3628 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003629 bool skip = false;
3630 const auto *cb_access_context = GetAccessContext(commandBuffer);
3631 assert(cb_access_context);
3632 if (!cb_access_context) return skip;
3633
Jeff Leger178b1e52020-10-05 12:22:23 -04003634 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3635 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3636
locke-lunarga19c71d2020-03-02 18:17:04 -07003637 const auto *context = cb_access_context->GetCurrentAccessContext();
3638 assert(context);
3639 if (!context) return skip;
3640
3641 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003642 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3643
3644 for (uint32_t region = 0; region < regionCount; region++) {
3645 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003646 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003647 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003648 if (src_buffer) {
3649 ResourceAccessRange src_range =
3650 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003651 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07003652 if (hazard.hazard) {
3653 // PHASE1 TODO -- add tag information to log msg when useful.
3654 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3655 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3656 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003657 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003658 }
3659 }
3660
Jeremy Gebben40a22942020-12-22 14:22:06 -07003661 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07003662 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003663 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003664 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003665 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003666 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003667 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003668 }
3669 if (skip) break;
3670 }
3671 if (skip) break;
3672 }
3673 return skip;
3674}
3675
Jeff Leger178b1e52020-10-05 12:22:23 -04003676bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3677 VkImageLayout dstImageLayout, uint32_t regionCount,
3678 const VkBufferImageCopy *pRegions) const {
3679 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3680 COPY_COMMAND_VERSION_1);
3681}
3682
3683bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3684 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3685 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3686 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3687 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3688}
3689
3690template <typename BufferImageCopyRegionType>
3691void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3692 VkImageLayout dstImageLayout, uint32_t regionCount,
3693 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003694 auto *cb_access_context = GetAccessContext(commandBuffer);
3695 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003696
3697 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3698 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3699
3700 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003701 auto *context = cb_access_context->GetCurrentAccessContext();
3702 assert(context);
3703
3704 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003705 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003706
3707 for (uint32_t region = 0; region < regionCount; region++) {
3708 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07003709 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003710 if (src_buffer) {
3711 ResourceAccessRange src_range =
3712 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003713 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003714 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07003715 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003716 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003717 }
3718 }
3719}
3720
Jeff Leger178b1e52020-10-05 12:22:23 -04003721void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3722 VkImageLayout dstImageLayout, uint32_t regionCount,
3723 const VkBufferImageCopy *pRegions) {
3724 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3725 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3726}
3727
3728void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3729 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3730 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3731 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3732 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3733 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3734}
3735
3736template <typename BufferImageCopyRegionType>
3737bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3738 VkBuffer dstBuffer, uint32_t regionCount,
3739 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003740 bool skip = false;
3741 const auto *cb_access_context = GetAccessContext(commandBuffer);
3742 assert(cb_access_context);
3743 if (!cb_access_context) return skip;
3744
Jeff Leger178b1e52020-10-05 12:22:23 -04003745 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3746 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3747
locke-lunarga19c71d2020-03-02 18:17:04 -07003748 const auto *context = cb_access_context->GetCurrentAccessContext();
3749 assert(context);
3750 if (!context) return skip;
3751
3752 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3753 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3754 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3755 for (uint32_t region = 0; region < regionCount; region++) {
3756 const auto &copy_region = pRegions[region];
3757 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003758 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003759 copy_region.imageOffset, copy_region.imageExtent);
3760 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003761 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003762 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003763 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003764 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003765 }
John Zulauf477700e2021-01-06 11:41:49 -07003766 if (dst_mem) {
3767 ResourceAccessRange dst_range =
3768 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003769 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07003770 if (hazard.hazard) {
3771 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3772 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3773 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003774 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003775 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003776 }
3777 }
3778 if (skip) break;
3779 }
3780 return skip;
3781}
3782
Jeff Leger178b1e52020-10-05 12:22:23 -04003783bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3784 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3785 const VkBufferImageCopy *pRegions) const {
3786 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3787 COPY_COMMAND_VERSION_1);
3788}
3789
3790bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3791 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3792 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3793 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3794 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3795}
3796
3797template <typename BufferImageCopyRegionType>
3798void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3799 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3800 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003801 auto *cb_access_context = GetAccessContext(commandBuffer);
3802 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003803
3804 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3805 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3806
3807 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003808 auto *context = cb_access_context->GetCurrentAccessContext();
3809 assert(context);
3810
3811 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003812 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3813 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 -06003814 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003815
3816 for (uint32_t region = 0; region < regionCount; region++) {
3817 const auto &copy_region = pRegions[region];
3818 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003819 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003820 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003821 if (dst_buffer) {
3822 ResourceAccessRange dst_range =
3823 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003824 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003825 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003826 }
3827 }
3828}
3829
Jeff Leger178b1e52020-10-05 12:22:23 -04003830void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3831 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3832 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3833 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3834}
3835
3836void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3837 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3838 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3839 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3840 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3841 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3842}
3843
3844template <typename RegionType>
3845bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3846 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3847 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003848 bool skip = false;
3849 const auto *cb_access_context = GetAccessContext(commandBuffer);
3850 assert(cb_access_context);
3851 if (!cb_access_context) return skip;
3852
3853 const auto *context = cb_access_context->GetCurrentAccessContext();
3854 assert(context);
3855 if (!context) return skip;
3856
3857 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3858 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3859
3860 for (uint32_t region = 0; region < regionCount; region++) {
3861 const auto &blit_region = pRegions[region];
3862 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003863 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3864 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3865 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3866 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3867 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3868 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003869 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003870 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003871 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003872 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003873 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003874 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003875 }
3876 }
3877
3878 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003879 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3880 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3881 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3882 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3883 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3884 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003885 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003886 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003887 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003888 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003889 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003890 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003891 }
3892 if (skip) break;
3893 }
3894 }
3895
3896 return skip;
3897}
3898
Jeff Leger178b1e52020-10-05 12:22:23 -04003899bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3900 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3901 const VkImageBlit *pRegions, VkFilter filter) const {
3902 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3903 "vkCmdBlitImage");
3904}
3905
3906bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3907 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3908 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3909 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3910 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3911}
3912
3913template <typename RegionType>
3914void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3915 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3916 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003917 auto *cb_access_context = GetAccessContext(commandBuffer);
3918 assert(cb_access_context);
3919 auto *context = cb_access_context->GetCurrentAccessContext();
3920 assert(context);
3921
3922 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003923 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003924
3925 for (uint32_t region = 0; region < regionCount; region++) {
3926 const auto &blit_region = pRegions[region];
3927 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003928 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3929 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3930 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3931 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3932 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3933 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003934 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003935 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003936 }
3937 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003938 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3939 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3940 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3941 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3942 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3943 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003944 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003945 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003946 }
3947 }
3948}
locke-lunarg36ba2592020-04-03 09:42:04 -06003949
Jeff Leger178b1e52020-10-05 12:22:23 -04003950void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3951 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3952 const VkImageBlit *pRegions, VkFilter filter) {
3953 auto *cb_access_context = GetAccessContext(commandBuffer);
3954 assert(cb_access_context);
3955 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3956 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3957 pRegions, filter);
3958 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3959}
3960
3961void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3962 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3963 auto *cb_access_context = GetAccessContext(commandBuffer);
3964 assert(cb_access_context);
3965 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3966 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3967 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3968 pBlitImageInfo->filter, tag);
3969}
3970
John Zulauffaea0ee2021-01-14 14:01:32 -07003971bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
3972 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
3973 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
3974 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003975 bool skip = false;
3976 if (drawCount == 0) return skip;
3977
3978 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3979 VkDeviceSize size = struct_size;
3980 if (drawCount == 1 || stride == size) {
3981 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003982 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003983 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3984 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003985 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003986 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003987 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003988 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003989 }
3990 } else {
3991 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003992 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003993 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3994 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003995 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003996 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3997 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003998 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003999 break;
4000 }
4001 }
4002 }
4003 return skip;
4004}
4005
locke-lunarg61870c22020-06-09 14:51:50 -06004006void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
4007 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4008 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06004009 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4010 VkDeviceSize size = struct_size;
4011 if (drawCount == 1 || stride == size) {
4012 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004013 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004014 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004015 } else {
4016 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004017 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004018 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4019 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004020 }
4021 }
4022}
4023
John Zulauffaea0ee2021-01-14 14:01:32 -07004024bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4025 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4026 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004027 bool skip = false;
4028
4029 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004030 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004031 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4032 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004033 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004034 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004035 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004036 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004037 }
4038 return skip;
4039}
4040
locke-lunarg61870c22020-06-09 14:51:50 -06004041void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06004042 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004043 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004044 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004045}
4046
locke-lunarg36ba2592020-04-03 09:42:04 -06004047bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004048 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004049 const auto *cb_access_context = GetAccessContext(commandBuffer);
4050 assert(cb_access_context);
4051 if (!cb_access_context) return skip;
4052
locke-lunarg61870c22020-06-09 14:51:50 -06004053 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004054 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004055}
4056
4057void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004058 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004059 auto *cb_access_context = GetAccessContext(commandBuffer);
4060 assert(cb_access_context);
4061 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004062
locke-lunarg61870c22020-06-09 14:51:50 -06004063 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004064}
locke-lunarge1a67022020-04-29 00:15:36 -06004065
4066bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004067 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004068 const auto *cb_access_context = GetAccessContext(commandBuffer);
4069 assert(cb_access_context);
4070 if (!cb_access_context) return skip;
4071
4072 const auto *context = cb_access_context->GetCurrentAccessContext();
4073 assert(context);
4074 if (!context) return skip;
4075
locke-lunarg61870c22020-06-09 14:51:50 -06004076 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004077 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4078 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004079 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004080}
4081
4082void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004083 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004084 auto *cb_access_context = GetAccessContext(commandBuffer);
4085 assert(cb_access_context);
4086 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4087 auto *context = cb_access_context->GetCurrentAccessContext();
4088 assert(context);
4089
locke-lunarg61870c22020-06-09 14:51:50 -06004090 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4091 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004092}
4093
4094bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4095 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004096 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004097 const auto *cb_access_context = GetAccessContext(commandBuffer);
4098 assert(cb_access_context);
4099 if (!cb_access_context) return skip;
4100
locke-lunarg61870c22020-06-09 14:51:50 -06004101 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4102 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4103 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004104 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004105}
4106
4107void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4108 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004109 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004110 auto *cb_access_context = GetAccessContext(commandBuffer);
4111 assert(cb_access_context);
4112 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004113
locke-lunarg61870c22020-06-09 14:51:50 -06004114 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4115 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4116 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004117}
4118
4119bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4120 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004121 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004122 const auto *cb_access_context = GetAccessContext(commandBuffer);
4123 assert(cb_access_context);
4124 if (!cb_access_context) return skip;
4125
locke-lunarg61870c22020-06-09 14:51:50 -06004126 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4127 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4128 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004129 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004130}
4131
4132void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4133 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004134 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004135 auto *cb_access_context = GetAccessContext(commandBuffer);
4136 assert(cb_access_context);
4137 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004138
locke-lunarg61870c22020-06-09 14:51:50 -06004139 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4140 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4141 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004142}
4143
4144bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4145 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004146 bool skip = false;
4147 if (drawCount == 0) return skip;
4148
locke-lunargff255f92020-05-13 18:53:52 -06004149 const auto *cb_access_context = GetAccessContext(commandBuffer);
4150 assert(cb_access_context);
4151 if (!cb_access_context) return skip;
4152
4153 const auto *context = cb_access_context->GetCurrentAccessContext();
4154 assert(context);
4155 if (!context) return skip;
4156
locke-lunarg61870c22020-06-09 14:51:50 -06004157 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4158 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004159 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4160 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004161
4162 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4163 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4164 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004165 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004166 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004167}
4168
4169void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4170 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004171 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004172 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004173 auto *cb_access_context = GetAccessContext(commandBuffer);
4174 assert(cb_access_context);
4175 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4176 auto *context = cb_access_context->GetCurrentAccessContext();
4177 assert(context);
4178
locke-lunarg61870c22020-06-09 14:51:50 -06004179 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4180 cb_access_context->RecordDrawSubpassAttachment(tag);
4181 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004182
4183 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4184 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4185 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004186 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004187}
4188
4189bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4190 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004191 bool skip = false;
4192 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004193 const auto *cb_access_context = GetAccessContext(commandBuffer);
4194 assert(cb_access_context);
4195 if (!cb_access_context) return skip;
4196
4197 const auto *context = cb_access_context->GetCurrentAccessContext();
4198 assert(context);
4199 if (!context) return skip;
4200
locke-lunarg61870c22020-06-09 14:51:50 -06004201 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4202 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004203 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4204 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004205
4206 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4207 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4208 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004209 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004210 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004211}
4212
4213void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4214 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004215 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004216 auto *cb_access_context = GetAccessContext(commandBuffer);
4217 assert(cb_access_context);
4218 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4219 auto *context = cb_access_context->GetCurrentAccessContext();
4220 assert(context);
4221
locke-lunarg61870c22020-06-09 14:51:50 -06004222 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4223 cb_access_context->RecordDrawSubpassAttachment(tag);
4224 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004225
4226 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4227 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4228 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004229 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004230}
4231
4232bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4233 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4234 uint32_t stride, const char *function) const {
4235 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004236 const auto *cb_access_context = GetAccessContext(commandBuffer);
4237 assert(cb_access_context);
4238 if (!cb_access_context) return skip;
4239
4240 const auto *context = cb_access_context->GetCurrentAccessContext();
4241 assert(context);
4242 if (!context) return skip;
4243
locke-lunarg61870c22020-06-09 14:51:50 -06004244 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4245 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004246 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4247 maxDrawCount, stride, function);
4248 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004249
4250 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4251 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4252 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004253 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004254 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004255}
4256
4257bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4258 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4259 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004260 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4261 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004262}
4263
4264void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4265 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4266 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004267 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4268 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004269 auto *cb_access_context = GetAccessContext(commandBuffer);
4270 assert(cb_access_context);
4271 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
4272 auto *context = cb_access_context->GetCurrentAccessContext();
4273 assert(context);
4274
locke-lunarg61870c22020-06-09 14:51:50 -06004275 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4276 cb_access_context->RecordDrawSubpassAttachment(tag);
4277 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4278 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004279
4280 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4281 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4282 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004283 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004284}
4285
4286bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4287 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4288 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004289 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4290 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004291}
4292
4293void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4294 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4295 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004296 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4297 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004298 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004299}
4300
4301bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4302 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4303 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004304 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4305 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004306}
4307
4308void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4309 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4310 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004311 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4312 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004313 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4314}
4315
4316bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4317 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4318 uint32_t stride, const char *function) const {
4319 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004320 const auto *cb_access_context = GetAccessContext(commandBuffer);
4321 assert(cb_access_context);
4322 if (!cb_access_context) return skip;
4323
4324 const auto *context = cb_access_context->GetCurrentAccessContext();
4325 assert(context);
4326 if (!context) return skip;
4327
locke-lunarg61870c22020-06-09 14:51:50 -06004328 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4329 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004330 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4331 offset, maxDrawCount, stride, function);
4332 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004333
4334 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4335 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4336 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004337 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004338 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004339}
4340
4341bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4342 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4343 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004344 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4345 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004346}
4347
4348void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4349 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4350 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004351 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4352 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004353 auto *cb_access_context = GetAccessContext(commandBuffer);
4354 assert(cb_access_context);
4355 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4356 auto *context = cb_access_context->GetCurrentAccessContext();
4357 assert(context);
4358
locke-lunarg61870c22020-06-09 14:51:50 -06004359 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4360 cb_access_context->RecordDrawSubpassAttachment(tag);
4361 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4362 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004363
4364 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4365 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004366 // We will update the index and vertex buffer in SubmitQueue in the future.
4367 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004368}
4369
4370bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4371 VkDeviceSize offset, VkBuffer countBuffer,
4372 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4373 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004374 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4375 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004376}
4377
4378void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4379 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4380 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004381 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4382 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004383 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4384}
4385
4386bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4387 VkDeviceSize offset, VkBuffer countBuffer,
4388 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4389 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004390 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4391 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004392}
4393
4394void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4395 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4396 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004397 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4398 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004399 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4400}
4401
4402bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4403 const VkClearColorValue *pColor, uint32_t rangeCount,
4404 const VkImageSubresourceRange *pRanges) const {
4405 bool skip = false;
4406 const auto *cb_access_context = GetAccessContext(commandBuffer);
4407 assert(cb_access_context);
4408 if (!cb_access_context) return skip;
4409
4410 const auto *context = cb_access_context->GetCurrentAccessContext();
4411 assert(context);
4412 if (!context) return skip;
4413
4414 const auto *image_state = Get<IMAGE_STATE>(image);
4415
4416 for (uint32_t index = 0; index < rangeCount; index++) {
4417 const auto &range = pRanges[index];
4418 if (image_state) {
4419 auto hazard =
Jeremy Gebben40a22942020-12-22 14:22:06 -07004420 context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
locke-lunarge1a67022020-04-29 00:15:36 -06004421 if (hazard.hazard) {
4422 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004423 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004424 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004425 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004426 }
4427 }
4428 }
4429 return skip;
4430}
4431
4432void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4433 const VkClearColorValue *pColor, uint32_t rangeCount,
4434 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004435 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004436 auto *cb_access_context = GetAccessContext(commandBuffer);
4437 assert(cb_access_context);
4438 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4439 auto *context = cb_access_context->GetCurrentAccessContext();
4440 assert(context);
4441
4442 const auto *image_state = Get<IMAGE_STATE>(image);
4443
4444 for (uint32_t index = 0; index < rangeCount; index++) {
4445 const auto &range = pRanges[index];
4446 if (image_state) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004447 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, {0, 0, 0},
John Zulauf8e3c3e92021-01-06 11:19:36 -07004448 image_state->createInfo.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004449 }
4450 }
4451}
4452
4453bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4454 VkImageLayout imageLayout,
4455 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4456 const VkImageSubresourceRange *pRanges) const {
4457 bool skip = false;
4458 const auto *cb_access_context = GetAccessContext(commandBuffer);
4459 assert(cb_access_context);
4460 if (!cb_access_context) return skip;
4461
4462 const auto *context = cb_access_context->GetCurrentAccessContext();
4463 assert(context);
4464 if (!context) return skip;
4465
4466 const auto *image_state = Get<IMAGE_STATE>(image);
4467
4468 for (uint32_t index = 0; index < rangeCount; index++) {
4469 const auto &range = pRanges[index];
4470 if (image_state) {
4471 auto hazard =
Jeremy Gebben40a22942020-12-22 14:22:06 -07004472 context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
locke-lunarge1a67022020-04-29 00:15:36 -06004473 if (hazard.hazard) {
4474 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004475 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004476 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004477 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004478 }
4479 }
4480 }
4481 return skip;
4482}
4483
4484void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4485 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4486 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004487 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004488 auto *cb_access_context = GetAccessContext(commandBuffer);
4489 assert(cb_access_context);
4490 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4491 auto *context = cb_access_context->GetCurrentAccessContext();
4492 assert(context);
4493
4494 const auto *image_state = Get<IMAGE_STATE>(image);
4495
4496 for (uint32_t index = 0; index < rangeCount; index++) {
4497 const auto &range = pRanges[index];
4498 if (image_state) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004499 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, {0, 0, 0},
John Zulauf8e3c3e92021-01-06 11:19:36 -07004500 image_state->createInfo.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004501 }
4502 }
4503}
4504
4505bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4506 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4507 VkDeviceSize dstOffset, VkDeviceSize stride,
4508 VkQueryResultFlags flags) const {
4509 bool skip = false;
4510 const auto *cb_access_context = GetAccessContext(commandBuffer);
4511 assert(cb_access_context);
4512 if (!cb_access_context) return skip;
4513
4514 const auto *context = cb_access_context->GetCurrentAccessContext();
4515 assert(context);
4516 if (!context) return skip;
4517
4518 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4519
4520 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004521 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004522 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004523 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004524 skip |=
4525 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4526 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004527 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004528 }
4529 }
locke-lunargff255f92020-05-13 18:53:52 -06004530
4531 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004532 return skip;
4533}
4534
4535void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4536 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4537 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004538 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4539 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004540 auto *cb_access_context = GetAccessContext(commandBuffer);
4541 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004542 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004543 auto *context = cb_access_context->GetCurrentAccessContext();
4544 assert(context);
4545
4546 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4547
4548 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004549 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004550 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004551 }
locke-lunargff255f92020-05-13 18:53:52 -06004552
4553 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004554}
4555
4556bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4557 VkDeviceSize size, uint32_t data) const {
4558 bool skip = false;
4559 const auto *cb_access_context = GetAccessContext(commandBuffer);
4560 assert(cb_access_context);
4561 if (!cb_access_context) return skip;
4562
4563 const auto *context = cb_access_context->GetCurrentAccessContext();
4564 assert(context);
4565 if (!context) return skip;
4566
4567 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4568
4569 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004570 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004571 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004572 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004573 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004574 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004575 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004576 }
4577 }
4578 return skip;
4579}
4580
4581void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4582 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004583 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004584 auto *cb_access_context = GetAccessContext(commandBuffer);
4585 assert(cb_access_context);
4586 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4587 auto *context = cb_access_context->GetCurrentAccessContext();
4588 assert(context);
4589
4590 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4591
4592 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004593 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004594 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004595 }
4596}
4597
4598bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4599 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4600 const VkImageResolve *pRegions) const {
4601 bool skip = false;
4602 const auto *cb_access_context = GetAccessContext(commandBuffer);
4603 assert(cb_access_context);
4604 if (!cb_access_context) return skip;
4605
4606 const auto *context = cb_access_context->GetCurrentAccessContext();
4607 assert(context);
4608 if (!context) return skip;
4609
4610 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4611 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4612
4613 for (uint32_t region = 0; region < regionCount; region++) {
4614 const auto &resolve_region = pRegions[region];
4615 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004616 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004617 resolve_region.srcOffset, resolve_region.extent);
4618 if (hazard.hazard) {
4619 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004620 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004621 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004622 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004623 }
4624 }
4625
4626 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004627 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004628 resolve_region.dstOffset, resolve_region.extent);
4629 if (hazard.hazard) {
4630 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004631 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004632 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004633 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004634 }
4635 if (skip) break;
4636 }
4637 }
4638
4639 return skip;
4640}
4641
4642void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4643 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4644 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004645 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4646 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004647 auto *cb_access_context = GetAccessContext(commandBuffer);
4648 assert(cb_access_context);
4649 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4650 auto *context = cb_access_context->GetCurrentAccessContext();
4651 assert(context);
4652
4653 auto *src_image = Get<IMAGE_STATE>(srcImage);
4654 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4655
4656 for (uint32_t region = 0; region < regionCount; region++) {
4657 const auto &resolve_region = pRegions[region];
4658 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004659 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004660 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004661 }
4662 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004663 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004664 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004665 }
4666 }
4667}
4668
Jeff Leger178b1e52020-10-05 12:22:23 -04004669bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4670 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4671 bool skip = false;
4672 const auto *cb_access_context = GetAccessContext(commandBuffer);
4673 assert(cb_access_context);
4674 if (!cb_access_context) return skip;
4675
4676 const auto *context = cb_access_context->GetCurrentAccessContext();
4677 assert(context);
4678 if (!context) return skip;
4679
4680 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4681 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4682
4683 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4684 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4685 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004686 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004687 resolve_region.srcOffset, resolve_region.extent);
4688 if (hazard.hazard) {
4689 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4690 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4691 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004692 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004693 }
4694 }
4695
4696 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004697 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004698 resolve_region.dstOffset, resolve_region.extent);
4699 if (hazard.hazard) {
4700 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4701 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4702 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004703 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004704 }
4705 if (skip) break;
4706 }
4707 }
4708
4709 return skip;
4710}
4711
4712void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4713 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4714 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4715 auto *cb_access_context = GetAccessContext(commandBuffer);
4716 assert(cb_access_context);
4717 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4718 auto *context = cb_access_context->GetCurrentAccessContext();
4719 assert(context);
4720
4721 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4722 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4723
4724 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4725 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4726 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004727 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004728 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004729 }
4730 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004731 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004732 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004733 }
4734 }
4735}
4736
locke-lunarge1a67022020-04-29 00:15:36 -06004737bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4738 VkDeviceSize dataSize, const void *pData) const {
4739 bool skip = false;
4740 const auto *cb_access_context = GetAccessContext(commandBuffer);
4741 assert(cb_access_context);
4742 if (!cb_access_context) return skip;
4743
4744 const auto *context = cb_access_context->GetCurrentAccessContext();
4745 assert(context);
4746 if (!context) return skip;
4747
4748 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4749
4750 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004751 // VK_WHOLE_SIZE not allowed
4752 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004753 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004754 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004755 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004756 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004757 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004758 }
4759 }
4760 return skip;
4761}
4762
4763void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4764 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004765 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004766 auto *cb_access_context = GetAccessContext(commandBuffer);
4767 assert(cb_access_context);
4768 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4769 auto *context = cb_access_context->GetCurrentAccessContext();
4770 assert(context);
4771
4772 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4773
4774 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004775 // VK_WHOLE_SIZE not allowed
4776 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004777 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004778 }
4779}
locke-lunargff255f92020-05-13 18:53:52 -06004780
4781bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4782 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4783 bool skip = false;
4784 const auto *cb_access_context = GetAccessContext(commandBuffer);
4785 assert(cb_access_context);
4786 if (!cb_access_context) return skip;
4787
4788 const auto *context = cb_access_context->GetCurrentAccessContext();
4789 assert(context);
4790 if (!context) return skip;
4791
4792 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4793
4794 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004795 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004796 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06004797 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004798 skip |=
4799 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4800 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004801 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004802 }
4803 }
4804 return skip;
4805}
4806
4807void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4808 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004809 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004810 auto *cb_access_context = GetAccessContext(commandBuffer);
4811 assert(cb_access_context);
4812 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4813 auto *context = cb_access_context->GetCurrentAccessContext();
4814 assert(context);
4815
4816 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4817
4818 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004819 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004820 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004821 }
4822}
John Zulauf49beb112020-11-04 16:06:31 -07004823
4824bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
4825 bool skip = false;
4826 const auto *cb_context = GetAccessContext(commandBuffer);
4827 assert(cb_context);
4828 if (!cb_context) return skip;
4829
John Zulauf36ef9282021-02-02 11:47:24 -07004830 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004831 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004832}
4833
4834void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4835 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
4836 auto *cb_context = GetAccessContext(commandBuffer);
4837 assert(cb_context);
4838 if (!cb_context) return;
John Zulauf36ef9282021-02-02 11:47:24 -07004839 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4840 set_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004841}
4842
John Zulauf4edde622021-02-15 08:54:50 -07004843bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4844 const VkDependencyInfoKHR *pDependencyInfo) const {
4845 bool skip = false;
4846 const auto *cb_context = GetAccessContext(commandBuffer);
4847 assert(cb_context);
4848 if (!cb_context || !pDependencyInfo) return skip;
4849
4850 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4851 return set_event_op.Validate(*cb_context);
4852}
4853
4854void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4855 const VkDependencyInfoKHR *pDependencyInfo) {
4856 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
4857 auto *cb_context = GetAccessContext(commandBuffer);
4858 assert(cb_context);
4859 if (!cb_context || !pDependencyInfo) return;
4860
4861 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4862 set_event_op.Record(cb_context);
4863}
4864
John Zulauf49beb112020-11-04 16:06:31 -07004865bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
4866 VkPipelineStageFlags stageMask) const {
4867 bool skip = false;
4868 const auto *cb_context = GetAccessContext(commandBuffer);
4869 assert(cb_context);
4870 if (!cb_context) return skip;
4871
John Zulauf36ef9282021-02-02 11:47:24 -07004872 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004873 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004874}
4875
4876void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4877 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
4878 auto *cb_context = GetAccessContext(commandBuffer);
4879 assert(cb_context);
4880 if (!cb_context) return;
4881
John Zulauf36ef9282021-02-02 11:47:24 -07004882 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4883 reset_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004884}
4885
John Zulauf4edde622021-02-15 08:54:50 -07004886bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4887 VkPipelineStageFlags2KHR stageMask) const {
4888 bool skip = false;
4889 const auto *cb_context = GetAccessContext(commandBuffer);
4890 assert(cb_context);
4891 if (!cb_context) return skip;
4892
4893 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4894 return reset_event_op.Validate(*cb_context);
4895}
4896
4897void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4898 VkPipelineStageFlags2KHR stageMask) {
4899 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
4900 auto *cb_context = GetAccessContext(commandBuffer);
4901 assert(cb_context);
4902 if (!cb_context) return;
4903
4904 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4905 reset_event_op.Record(cb_context);
4906}
4907
John Zulauf49beb112020-11-04 16:06:31 -07004908bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4909 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4910 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4911 uint32_t bufferMemoryBarrierCount,
4912 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4913 uint32_t imageMemoryBarrierCount,
4914 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
4915 bool skip = false;
4916 const auto *cb_context = GetAccessContext(commandBuffer);
4917 assert(cb_context);
4918 if (!cb_context) return skip;
4919
John Zulauf36ef9282021-02-02 11:47:24 -07004920 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4921 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4922 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07004923 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004924}
4925
4926void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4927 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4928 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4929 uint32_t bufferMemoryBarrierCount,
4930 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4931 uint32_t imageMemoryBarrierCount,
4932 const VkImageMemoryBarrier *pImageMemoryBarriers) {
4933 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
4934 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
4935 imageMemoryBarrierCount, pImageMemoryBarriers);
4936
4937 auto *cb_context = GetAccessContext(commandBuffer);
4938 assert(cb_context);
4939 if (!cb_context) return;
4940
John Zulauf36ef9282021-02-02 11:47:24 -07004941 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4942 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4943 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
4944 return wait_events_op.Record(cb_context);
John Zulauf4a6105a2020-11-17 15:11:05 -07004945}
4946
John Zulauf4edde622021-02-15 08:54:50 -07004947bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4948 const VkDependencyInfoKHR *pDependencyInfos) const {
4949 bool skip = false;
4950 const auto *cb_context = GetAccessContext(commandBuffer);
4951 assert(cb_context);
4952 if (!cb_context) return skip;
4953
4954 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
4955 skip |= wait_events_op.Validate(*cb_context);
4956 return skip;
4957}
4958
4959void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4960 const VkDependencyInfoKHR *pDependencyInfos) {
4961 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
4962
4963 auto *cb_context = GetAccessContext(commandBuffer);
4964 assert(cb_context);
4965 if (!cb_context) return;
4966
4967 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
4968 wait_events_op.Record(cb_context);
4969}
4970
John Zulauf4a6105a2020-11-17 15:11:05 -07004971void SyncEventState::ResetFirstScope() {
4972 for (const auto address_type : kAddressTypes) {
4973 first_scope[static_cast<size_t>(address_type)].clear();
4974 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07004975 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07004976}
4977
4978// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07004979SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07004980 IgnoreReason reason = NotIgnored;
4981
John Zulauf4edde622021-02-15 08:54:50 -07004982 if ((CMD_WAITEVENTS2KHR == cmd) && (CMD_SETEVENT == last_command)) {
4983 reason = SetVsWait2;
4984 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
4985 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07004986 } else if (unsynchronized_set) {
4987 reason = SetRace;
4988 } else {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004989 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07004990 if (missing_bits) reason = MissingStageBits;
4991 }
4992
4993 return reason;
4994}
4995
Jeremy Gebben40a22942020-12-22 14:22:06 -07004996bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07004997 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
4998 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
4999 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005000}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005001
John Zulauf36ef9282021-02-02 11:47:24 -07005002SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5003 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5004 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005005 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5006 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5007 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07005008 : SyncOpBase(cmd), barriers_(1) {
5009 auto &barrier_set = barriers_[0];
5010 barrier_set.dependency_flags = dependencyFlags;
5011 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
5012 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005013 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07005014 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
5015 pMemoryBarriers);
5016 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5017 bufferMemoryBarrierCount, pBufferMemoryBarriers);
5018 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5019 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005020}
5021
John Zulauf4edde622021-02-15 08:54:50 -07005022SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
5023 const VkDependencyInfoKHR *dep_infos)
5024 : SyncOpBase(cmd), barriers_(event_count) {
5025 for (uint32_t i = 0; i < event_count; i++) {
5026 const auto &dep_info = dep_infos[i];
5027 auto &barrier_set = barriers_[i];
5028 barrier_set.dependency_flags = dep_info.dependencyFlags;
5029 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5030 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5031 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5032 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5033 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5034 dep_info.pMemoryBarriers);
5035 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5036 dep_info.pBufferMemoryBarriers);
5037 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5038 dep_info.pImageMemoryBarriers);
5039 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005040}
5041
John Zulauf36ef9282021-02-02 11:47:24 -07005042SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005043 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5044 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5045 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5046 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5047 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005048 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005049 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5050
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005051SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5052 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005053 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005054
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005055bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5056 bool skip = false;
5057 const auto *context = cb_context.GetCurrentAccessContext();
5058 assert(context);
5059 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005060 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5061
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005062 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005063 const auto &barrier_set = barriers_[0];
5064 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5065 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5066 const auto *image_state = image_barrier.image.get();
5067 if (!image_state) continue;
5068 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5069 if (hazard.hazard) {
5070 // PHASE1 TODO -- add tag information to log msg when useful.
5071 const auto &sync_state = cb_context.GetSyncState();
5072 const auto image_handle = image_state->image;
5073 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5074 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5075 string_SyncHazard(hazard.hazard), image_barrier.index,
5076 sync_state.report_data->FormatHandle(image_handle).c_str(),
5077 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005078 }
5079 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005080 return skip;
5081}
5082
John Zulaufd5115702021-01-18 12:34:33 -07005083struct SyncOpPipelineBarrierFunctorFactory {
5084 using BarrierOpFunctor = PipelineBarrierOp;
5085 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5086 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5087 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5088 using BufferRange = ResourceAccessRange;
5089 using ImageRange = subresource_adapter::ImageRangeGenerator;
5090 using GlobalRange = ResourceAccessRange;
5091
5092 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5093 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5094 }
5095 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
5096 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5097 }
5098 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5099 return GlobalBarrierOpFunctor(barrier, false);
5100 }
5101
5102 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5103 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5104 const auto base_address = ResourceBaseAddress(buffer);
5105 return (range + base_address);
5106 }
5107 ImageRange MakeRangeGen(const IMAGE_STATE &image, const SyncImageMemoryBarrier::SubImageRange &range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005108 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005109
5110 const auto base_address = ResourceBaseAddress(image);
5111 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), range.subresource_range, range.offset,
5112 range.extent, base_address);
5113 return range_gen;
5114 }
5115 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5116};
5117
5118template <typename Barriers, typename FunctorFactory>
5119void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
5120 AccessContext *context) {
5121 for (const auto &barrier : barriers) {
5122 const auto *state = barrier.GetState();
5123 if (state) {
5124 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5125 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5126 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5127 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5128 }
5129 }
5130}
5131
5132template <typename Barriers, typename FunctorFactory>
5133void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
5134 AccessContext *access_context) {
5135 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5136 for (const auto &barrier : barriers) {
5137 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5138 }
5139 for (const auto address_type : kAddressTypes) {
5140 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5141 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5142 }
5143}
5144
John Zulauf36ef9282021-02-02 11:47:24 -07005145void SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005146 SyncOpPipelineBarrierFunctorFactory factory;
5147 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005148 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005149
John Zulauf4edde622021-02-15 08:54:50 -07005150 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5151 assert(barriers_.size() == 1);
5152 const auto &barrier_set = barriers_[0];
5153 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5154 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5155 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
5156
5157 if (barrier_set.single_exec_scope) {
5158 cb_context->ApplyGlobalBarriersToEvents(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
5159 } else {
5160 for (const auto &barrier : barrier_set.memory_barriers) {
5161 cb_context->ApplyGlobalBarriersToEvents(barrier.src_exec_scope, barrier.dst_exec_scope);
5162 }
5163 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005164}
5165
John Zulauf4edde622021-02-15 08:54:50 -07005166void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5167 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5168 const VkMemoryBarrier *barriers) {
5169 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005170 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005171 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005172 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005173 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005174 }
5175 if (0 == memory_barrier_count) {
5176 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005177 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005178 }
John Zulauf4edde622021-02-15 08:54:50 -07005179 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005180}
5181
John Zulauf4edde622021-02-15 08:54:50 -07005182void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5183 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5184 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5185 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005186 for (uint32_t index = 0; index < barrier_count; index++) {
5187 const auto &barrier = barriers[index];
5188 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5189 if (buffer) {
5190 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5191 const auto range = MakeRange(barrier.offset, barrier_size);
5192 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005193 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005194 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005195 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005196 }
5197 }
5198}
5199
John Zulauf4edde622021-02-15 08:54:50 -07005200void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
5201 uint32_t memory_barrier_count, const VkMemoryBarrier2KHR *barriers) {
5202 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005203 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005204 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005205 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5206 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5207 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005208 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005209 }
John Zulauf4edde622021-02-15 08:54:50 -07005210 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005211}
5212
John Zulauf4edde622021-02-15 08:54:50 -07005213void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5214 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5215 const VkBufferMemoryBarrier2KHR *barriers) {
5216 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005217 for (uint32_t index = 0; index < barrier_count; index++) {
5218 const auto &barrier = barriers[index];
5219 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5220 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5221 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5222 if (buffer) {
5223 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5224 const auto range = MakeRange(barrier.offset, barrier_size);
5225 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005226 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005227 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005228 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005229 }
5230 }
5231}
5232
John Zulauf4edde622021-02-15 08:54:50 -07005233void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5234 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5235 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5236 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005237 for (uint32_t index = 0; index < barrier_count; index++) {
5238 const auto &barrier = barriers[index];
5239 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5240 if (image) {
5241 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5242 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005243 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005244 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005245 image_memory_barriers.emplace_back();
5246 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005247 }
5248 }
5249}
John Zulaufd5115702021-01-18 12:34:33 -07005250
John Zulauf4edde622021-02-15 08:54:50 -07005251void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5252 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5253 const VkImageMemoryBarrier2KHR *barriers) {
5254 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005255 for (uint32_t index = 0; index < barrier_count; index++) {
5256 const auto &barrier = barriers[index];
5257 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5258 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5259 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5260 if (image) {
5261 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5262 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005263 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005264 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005265 image_memory_barriers.emplace_back();
5266 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005267 }
5268 }
5269}
5270
John Zulauf36ef9282021-02-02 11:47:24 -07005271SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005272 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5273 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5274 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5275 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005276 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005277 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5278 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005279 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005280}
5281
John Zulauf4edde622021-02-15 08:54:50 -07005282SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5283 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5284 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5285 MakeEventsList(sync_state, eventCount, pEvents);
5286 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5287}
5288
John Zulaufd5115702021-01-18 12:34:33 -07005289bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005290 const char *const ignored = "Wait operation is ignored for this event.";
5291 bool skip = false;
5292 const auto &sync_state = cb_context.GetSyncState();
5293 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer;
5294
John Zulauf4edde622021-02-15 08:54:50 -07005295 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5296 const auto &barrier_set = barriers_[barrier_set_index];
5297 if (barrier_set.single_exec_scope) {
5298 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5299 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5300 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5301 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5302 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5303 } else {
5304 const auto &barriers = barrier_set.memory_barriers;
5305 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5306 const auto &barrier = barriers[barrier_index];
5307 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5308 const std::string vuid =
5309 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5310 skip =
5311 sync_state.LogInfo(command_buffer_handle, vuid,
5312 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5313 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5314 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5315 }
5316 }
5317 }
5318 }
John Zulaufd5115702021-01-18 12:34:33 -07005319 }
5320
Jeremy Gebben40a22942020-12-22 14:22:06 -07005321 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005322 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005323 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005324 const auto *events_context = cb_context.GetCurrentEventsContext();
5325 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005326 size_t barrier_set_index = 0;
5327 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5328 for (size_t event_index = 0; event_index < events_.size(); event_index++)
5329 for (const auto &event : events_) {
5330 const auto *sync_event = events_context->Get(event.get());
5331 const auto &barrier_set = barriers_[barrier_set_index];
5332 if (!sync_event) {
5333 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5334 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5335 // new validation error... wait without previously submitted set event...
5336 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives at *record time*
5337 barrier_set_index += barrier_set_incr;
5338 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005339 }
John Zulauf4edde622021-02-15 08:54:50 -07005340 const auto event_handle = sync_event->event->event;
5341 // TODO add "destroyed" checks
5342
5343 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5344 const auto &src_exec_scope = barrier_set.src_exec_scope;
5345 event_stage_masks |= sync_event->scope.mask_param;
5346 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5347 if (ignore_reason) {
5348 switch (ignore_reason) {
5349 case SyncEventState::ResetWaitRace:
5350 case SyncEventState::Reset2WaitRace: {
5351 // Four permuations of Reset and Wait calls...
5352 const char *vuid =
5353 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5354 if (ignore_reason == SyncEventState::Reset2WaitRace) {
5355 vuid =
Jeremy Gebben476f5e22021-03-01 15:27:20 -07005356 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2KHR-event-03831" : "VUID-vkCmdResetEvent2KHR-event-03832";
John Zulauf4edde622021-02-15 08:54:50 -07005357 }
5358 const char *const message =
5359 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5360 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5361 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
5362 CommandTypeString(sync_event->last_command), ignored);
5363 break;
5364 }
5365 case SyncEventState::SetRace: {
5366 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5367 // this event
5368 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5369 const char *const message =
5370 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5371 const char *const reason = "First synchronization scope is undefined.";
5372 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5373 sync_state.report_data->FormatHandle(event_handle).c_str(),
5374 CommandTypeString(sync_event->last_command), reason, ignored);
5375 break;
5376 }
5377 case SyncEventState::MissingStageBits: {
5378 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5379 // Issue error message that event waited for is not in wait events scope
5380 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5381 const char *const message =
5382 "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5383 ". Bits missing from srcStageMask %s. %s";
5384 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5385 sync_state.report_data->FormatHandle(event_handle).c_str(),
5386 sync_event->scope.mask_param, src_exec_scope.mask_param,
5387 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), ignored);
5388 break;
5389 }
5390 case SyncEventState::SetVsWait2: {
5391 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2KHR-pEvents-03837",
5392 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5393 sync_state.report_data->FormatHandle(event_handle).c_str(),
5394 CommandTypeString(sync_event->last_command));
5395 break;
5396 }
5397 default:
5398 assert(ignore_reason == SyncEventState::NotIgnored);
5399 }
5400 } else if (barrier_set.image_memory_barriers.size()) {
5401 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5402 const auto *context = cb_context.GetCurrentAccessContext();
5403 assert(context);
5404 for (const auto &image_memory_barrier : image_memory_barriers) {
5405 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5406 const auto *image_state = image_memory_barrier.image.get();
5407 if (!image_state) continue;
5408 const auto &subresource_range = image_memory_barrier.range.subresource_range;
5409 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5410 const auto hazard =
5411 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5412 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5413 if (hazard.hazard) {
5414 skip |= sync_state.LogError(image_state->image, string_SyncHazardVUID(hazard.hazard),
5415 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5416 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
5417 sync_state.report_data->FormatHandle(image_state->image).c_str(),
5418 cb_context.FormatUsage(hazard).c_str());
5419 break;
5420 }
John Zulaufd5115702021-01-18 12:34:33 -07005421 }
5422 }
John Zulauf4edde622021-02-15 08:54:50 -07005423 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5424 // 03839
5425 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005426 }
John Zulaufd5115702021-01-18 12:34:33 -07005427
5428 // 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 -07005429 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 -07005430 if (extra_stage_bits) {
5431 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005432 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5433 const char *const vuid =
5434 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2KHR-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005435 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005436 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulaufd5115702021-01-18 12:34:33 -07005437 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005438 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005439 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005440 " vkCmdSetEvent may be in previously submitted command buffer.");
5441 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005442 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005443 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005444 }
5445 }
5446 return skip;
5447}
5448
5449struct SyncOpWaitEventsFunctorFactory {
5450 using BarrierOpFunctor = WaitEventBarrierOp;
5451 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5452 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5453 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5454 using BufferRange = EventSimpleRangeGenerator;
5455 using ImageRange = EventImageRangeGenerator;
5456 using GlobalRange = EventSimpleRangeGenerator;
5457
5458 // Need to restrict to only valid exec and access scope for this event
5459 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5460 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005461 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005462 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5463 return barrier;
5464 }
5465 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5466 auto barrier = RestrictToEvent(barrier_arg);
5467 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5468 }
5469 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
5470 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5471 }
5472 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5473 auto barrier = RestrictToEvent(barrier_arg);
5474 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5475 }
5476
5477 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5478 const AccessAddressType address_type = GetAccessAddressType(buffer);
5479 const auto base_address = ResourceBaseAddress(buffer);
5480 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5481 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5482 return filtered_range_gen;
5483 }
5484 ImageRange MakeRangeGen(const IMAGE_STATE &image, const SyncImageMemoryBarrier::SubImageRange &range) const {
5485 if (!SimpleBinding(image)) return ImageRange();
5486 const auto address_type = GetAccessAddressType(image);
5487 const auto base_address = ResourceBaseAddress(image);
5488 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), range.subresource_range,
5489 range.offset, range.extent, base_address);
5490 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5491
5492 return filtered_range_gen;
5493 }
5494 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5495 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5496 }
5497 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5498 SyncEventState *sync_event;
5499};
5500
John Zulauf36ef9282021-02-02 11:47:24 -07005501void SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
5502 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07005503 auto *access_context = cb_context->GetCurrentAccessContext();
5504 assert(access_context);
5505 if (!access_context) return;
John Zulauf669dfd52021-01-27 17:15:28 -07005506 auto *events_context = cb_context->GetCurrentEventsContext();
5507 assert(events_context);
5508 if (!events_context) return;
John Zulaufd5115702021-01-18 12:34:33 -07005509
5510 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
5511 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
5512 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
5513 access_context->ResolvePreviousAccesses();
5514
John Zulaufd5115702021-01-18 12:34:33 -07005515 // TODO... this needs change the SyncEventContext it's using depending on whether this is replay... the recorded
5516 // 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 -07005517 size_t barrier_set_index = 0;
5518 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5519 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07005520 for (auto &event_shared : events_) {
5521 if (!event_shared.get()) continue;
5522 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07005523
John Zulauf4edde622021-02-15 08:54:50 -07005524 sync_event->last_command = cmd_;
John Zulaufd5115702021-01-18 12:34:33 -07005525
John Zulauf4edde622021-02-15 08:54:50 -07005526 const auto &barrier_set = barriers_[barrier_set_index];
5527 const auto &dst = barrier_set.dst_exec_scope;
5528 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07005529 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
5530 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
5531 // of the barriers is maintained.
5532 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07005533 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5534 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5535 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07005536
5537 // Apply the global barrier to the event itself (for race condition tracking)
5538 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
5539 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
5540 sync_event->barriers |= dst.exec_scope;
5541 } else {
5542 // We ignored this wait, so we don't have any effective synchronization barriers for it.
5543 sync_event->barriers = 0U;
5544 }
John Zulauf4edde622021-02-15 08:54:50 -07005545 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005546 }
5547
5548 // Apply the pending barriers
5549 ResolvePendingBarrierFunctor apply_pending_action(tag);
5550 access_context->ApplyToContext(apply_pending_action);
5551}
5552
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005553bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5554 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5555 bool skip = false;
5556 const auto *cb_access_context = GetAccessContext(commandBuffer);
5557 assert(cb_access_context);
5558 if (!cb_access_context) return skip;
5559
5560 const auto *context = cb_access_context->GetCurrentAccessContext();
5561 assert(context);
5562 if (!context) return skip;
5563
5564 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5565
5566 if (dst_buffer) {
5567 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5568 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
5569 if (hazard.hazard) {
5570 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5571 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
5572 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
5573 string_UsageTag(hazard.tag).c_str());
5574 }
5575 }
5576 return skip;
5577}
5578
John Zulauf669dfd52021-01-27 17:15:28 -07005579void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07005580 events_.reserve(event_count);
5581 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
John Zulauf669dfd52021-01-27 17:15:28 -07005582 events_.emplace_back(sync_state.GetShared<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07005583 }
5584}
John Zulauf6ce24372021-01-30 05:56:25 -07005585
John Zulauf36ef9282021-02-02 11:47:24 -07005586SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005587 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005588 : SyncOpBase(cmd),
5589 event_(sync_state.GetShared<EVENT_STATE>(event)),
5590 exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005591
5592bool SyncOpResetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005593 auto *events_context = cb_context.GetCurrentEventsContext();
5594 assert(events_context);
5595 bool skip = false;
5596 if (!events_context) return skip;
5597
5598 const auto &sync_state = cb_context.GetSyncState();
5599 const auto *sync_event = events_context->Get(event_);
5600 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5601
5602 const char *const set_wait =
5603 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5604 "hazards.";
5605 const char *message = set_wait; // Only one message this call.
5606 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
5607 const char *vuid = nullptr;
5608 switch (sync_event->last_command) {
5609 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005610 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005611 // Needs a barrier between set and reset
5612 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
5613 break;
John Zulauf4edde622021-02-15 08:54:50 -07005614 case CMD_WAITEVENTS:
5615 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07005616 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
5617 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
5618 break;
5619 }
5620 default:
5621 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07005622 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
5623 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07005624 break;
5625 }
5626 if (vuid) {
John Zulauf36ef9282021-02-02 11:47:24 -07005627 skip |= sync_state.LogError(event_->event, vuid, message, CmdName(),
5628 sync_state.report_data->FormatHandle(event_->event).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005629 CommandTypeString(sync_event->last_command));
5630 }
5631 }
5632 return skip;
5633}
5634
John Zulauf36ef9282021-02-02 11:47:24 -07005635void SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005636 auto *events_context = cb_context->GetCurrentEventsContext();
5637 assert(events_context);
5638 if (!events_context) return;
5639
5640 auto *sync_event = events_context->GetFromShared(event_);
5641 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5642
5643 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07005644 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005645 sync_event->unsynchronized_set = CMD_NONE;
5646 sync_event->ResetFirstScope();
5647 sync_event->barriers = 0U;
5648}
5649
John Zulauf36ef9282021-02-02 11:47:24 -07005650SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005651 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005652 : SyncOpBase(cmd),
5653 event_(sync_state.GetShared<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07005654 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
5655 dep_info_() {}
5656
5657SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
5658 const VkDependencyInfoKHR &dep_info)
5659 : SyncOpBase(cmd),
5660 event_(sync_state.GetShared<EVENT_STATE>(event)),
5661 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
5662 dep_info_(new safe_VkDependencyInfoKHR(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005663
5664bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
5665 // 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 -07005666 bool skip = false;
5667
5668 const auto &sync_state = cb_context.GetSyncState();
5669 auto *events_context = cb_context.GetCurrentEventsContext();
5670 assert(events_context);
5671 if (!events_context) return skip;
5672
5673 const auto *sync_event = events_context->Get(event_);
5674 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5675
5676 const char *const reset_set =
5677 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5678 "hazards.";
5679 const char *const wait =
5680 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
5681
5682 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07005683 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07005684 const char *message = nullptr;
5685 switch (sync_event->last_command) {
5686 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005687 case CMD_RESETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005688 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07005689 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07005690 message = reset_set;
5691 break;
5692 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005693 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005694 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07005695 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07005696 message = reset_set;
5697 break;
5698 case CMD_WAITEVENTS:
John Zulauf4edde622021-02-15 08:54:50 -07005699 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005700 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07005701 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07005702 message = wait;
5703 break;
5704 default:
5705 // The only other valid last command that wasn't one.
5706 assert(sync_event->last_command == CMD_NONE);
5707 break;
5708 }
John Zulauf4edde622021-02-15 08:54:50 -07005709 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07005710 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07005711 std::string vuid("SYNC-");
5712 vuid.append(CmdName()).append(vuid_stem);
5713 skip |= sync_state.LogError(event_->event, vuid.c_str(), message, CmdName(),
John Zulauf36ef9282021-02-02 11:47:24 -07005714 sync_state.report_data->FormatHandle(event_->event).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005715 CommandTypeString(sync_event->last_command));
5716 }
5717 }
5718
5719 return skip;
5720}
5721
John Zulauf36ef9282021-02-02 11:47:24 -07005722void SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
5723 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07005724 auto *events_context = cb_context->GetCurrentEventsContext();
5725 auto *access_context = cb_context->GetCurrentAccessContext();
5726 assert(events_context);
5727 if (!events_context) return;
5728
5729 auto *sync_event = events_context->GetFromShared(event_);
5730 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5731
5732 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
5733 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
5734 // any issues caused by naive scope setting here.
5735
5736 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
5737 // Given:
5738 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
5739 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
5740
5741 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
5742 sync_event->unsynchronized_set = sync_event->last_command;
5743 sync_event->ResetFirstScope();
5744 } else if (sync_event->scope.exec_scope == 0) {
5745 // We only set the scope if there isn't one
5746 sync_event->scope = src_exec_scope_;
5747
5748 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
5749 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
5750 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
5751 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
5752 }
5753 };
5754 access_context->ForAll(set_scope);
5755 sync_event->unsynchronized_set = CMD_NONE;
5756 sync_event->first_scope_tag = tag;
5757 }
John Zulauf4edde622021-02-15 08:54:50 -07005758 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
5759 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005760 sync_event->barriers = 0U;
5761}
John Zulauf64ffe552021-02-06 10:25:07 -07005762
5763SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
5764 const VkRenderPassBeginInfo *pRenderPassBegin,
5765 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *cmd_name)
5766 : SyncOpBase(cmd, cmd_name) {
5767 if (pRenderPassBegin) {
5768 rp_state_ = sync_state.GetShared<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
5769 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
5770 const auto *fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
5771 if (fb_state) {
5772 shared_attachments_ = sync_state.GetSharedAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
5773 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
5774 // Note that this a safe to presist as long as shared_attachments is not cleared
5775 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08005776 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07005777 attachments_.emplace_back(attachment.get());
5778 }
5779 }
5780 if (pSubpassBeginInfo) {
5781 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
5782 }
5783 }
5784}
5785
5786bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5787 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
5788 bool skip = false;
5789
5790 assert(rp_state_.get());
5791 if (nullptr == rp_state_.get()) return skip;
5792 auto &rp_state = *rp_state_.get();
5793
5794 const uint32_t subpass = 0;
5795
5796 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
5797 // hasn't happened yet)
5798 const std::vector<AccessContext> empty_context_vector;
5799 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
5800 cb_context.GetCurrentAccessContext());
5801
5802 // Validate attachment operations
5803 if (attachments_.size() == 0) return skip;
5804 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07005805
5806 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
5807 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
5808 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
5809 // operations (though it's currently a messy approach)
5810 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
5811 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07005812
5813 // Validate load operations if there were no layout transition hazards
5814 if (!skip) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07005815 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kCurrentCommandTag);
5816 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07005817 }
5818
5819 return skip;
5820}
5821
5822void SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5823 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5824 assert(rp_state_.get());
5825 if (nullptr == rp_state_.get()) return;
5826 const auto tag = cb_context->NextCommandTag(cmd_);
5827 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
5828}
5829
5830SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
5831 const VkSubpassEndInfo *pSubpassEndInfo, const char *name_override)
5832 : SyncOpBase(cmd, name_override) {
5833 if (pSubpassBeginInfo) {
5834 subpass_begin_info_.initialize(pSubpassBeginInfo);
5835 }
5836 if (pSubpassEndInfo) {
5837 subpass_end_info_.initialize(pSubpassEndInfo);
5838 }
5839}
5840
5841bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
5842 bool skip = false;
5843 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5844 if (!renderpass_context) return skip;
5845
5846 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
5847 return skip;
5848}
5849
5850void SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
5851 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5852 cb_context->RecordNextSubpass(cmd_);
5853}
5854
5855SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo,
5856 const char *name_override)
5857 : SyncOpBase(cmd, name_override) {
5858 if (pSubpassEndInfo) {
5859 subpass_end_info_.initialize(pSubpassEndInfo);
5860 }
5861}
5862
5863bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5864 bool skip = false;
5865 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5866
5867 if (!renderpass_context) return skip;
5868 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
5869 return skip;
5870}
5871
5872void SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5873 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5874 cb_context->RecordEndRenderPass(cmd_);
5875}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005876
5877void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5878 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
5879 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
5880 auto *cb_access_context = GetAccessContext(commandBuffer);
5881 assert(cb_access_context);
5882 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5883 auto *context = cb_access_context->GetCurrentAccessContext();
5884 assert(context);
5885
5886 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5887
5888 if (dst_buffer) {
5889 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5890 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
5891 }
5892}
John Zulaufd05c5842021-03-26 11:32:16 -06005893
5894#ifdef SYNCVAL_DIAGNOSTICS
5895bool SyncValidator::PreCallValidateDestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) const {
5896 sync_diagnostics.InstanceDump(instance);
5897 ImageRangeGen::diag_.Report();
5898 return StateTracker::PreCallValidateDestroyInstance(instance, pAllocator);
5899}
5900#endif
John Zulaufd0ec59f2021-03-13 14:25:08 -07005901
5902AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
5903 : view_(view), view_mask_(), gen_store_() {
5904 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
5905 const IMAGE_STATE &image_state = *view_->image_state.get();
5906 const auto base_address = ResourceBaseAddress(image_state);
5907 const auto *encoder = image_state.fragment_encoder.get();
5908 if (!encoder) return;
5909 const VkOffset3D zero_offset = {0, 0, 0};
5910 const VkExtent3D &image_extent = image_state.createInfo.extent;
5911 // Intentional copy
5912 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
5913 view_mask_ = subres_range.aspectMask;
5914 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
5915 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5916
5917 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
5918 if (depth && (depth != view_mask_)) {
5919 subres_range.aspectMask = depth;
5920 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5921 }
5922 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
5923 if (stencil && (stencil != view_mask_)) {
5924 subres_range.aspectMask = stencil;
5925 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5926 }
5927}
5928
5929const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
5930 const ImageRangeGen *got = nullptr;
5931 switch (gen_type) {
5932 case kViewSubresource:
5933 got = &gen_store_[kViewSubresource];
5934 break;
5935 case kRenderArea:
5936 got = &gen_store_[kRenderArea];
5937 break;
5938 case kDepthOnlyRenderArea:
5939 got =
5940 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
5941 break;
5942 case kStencilOnlyRenderArea:
5943 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
5944 : &gen_store_[Gen::kStencilOnlyRenderArea];
5945 break;
5946 default:
5947 assert(got);
5948 }
5949 return got;
5950}
5951
5952AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
5953 assert(IsValid());
5954 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
5955 if (depth_op) {
5956 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
5957 if (stencil_op) {
5958 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
5959 return kRenderArea;
5960 }
5961 return kDepthOnlyRenderArea;
5962 }
5963 if (stencil_op) {
5964 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
5965 return kStencilOnlyRenderArea;
5966 }
5967
5968 assert(depth_op || stencil_op);
5969 return kRenderArea;
5970}
5971
5972AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }