blob: f4fb0a41c1f33fab0c7ffe4da42be7c6f8f9c7a9 [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 Zulauf264cce02021-02-05 14:40:47 -070029static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
30
John Zulauf29d00532021-03-04 13:28:54 -070031static bool SimpleBinding(const IMAGE_STATE &image_state) {
32 bool simple = SimpleBinding(static_cast<const BINDABLE &>(image_state)) || image_state.is_swapchain_image ||
33 (VK_NULL_HANDLE != image_state.bind_swapchain);
34
35 // If it's not simple we must have an encoder.
36 assert(!simple || image_state.fragment_encoder.get());
37 return simple;
38}
39
John Zulauf43cc7462020-12-03 12:33:12 -070040const static std::array<AccessAddressType, static_cast<size_t>(AccessAddressType::kTypeCount)> kAddressTypes = {
41 AccessAddressType::kLinear, AccessAddressType::kIdealized};
42
John Zulaufd5115702021-01-18 12:34:33 -070043static constexpr AccessAddressType GetAccessAddressType(const BUFFER_STATE &) { return AccessAddressType::kLinear; };
John Zulauf264cce02021-02-05 14:40:47 -070044static AccessAddressType GetAccessAddressType(const IMAGE_STATE &image) {
45 return SimpleBinding(image) ? AccessContext::ImageAddressType(image) : AccessAddressType::kIdealized;
46}
John Zulaufd5115702021-01-18 12:34:33 -070047
John Zulauf9cb530d2019-09-30 14:14:10 -060048static const char *string_SyncHazardVUID(SyncHazard hazard) {
49 switch (hazard) {
50 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070051 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060052 break;
53 case SyncHazard::READ_AFTER_WRITE:
54 return "SYNC-HAZARD-READ_AFTER_WRITE";
55 break;
56 case SyncHazard::WRITE_AFTER_READ:
57 return "SYNC-HAZARD-WRITE_AFTER_READ";
58 break;
59 case SyncHazard::WRITE_AFTER_WRITE:
60 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
61 break;
John Zulauf2f952d22020-02-10 11:34:51 -070062 case SyncHazard::READ_RACING_WRITE:
63 return "SYNC-HAZARD-READ-RACING-WRITE";
64 break;
65 case SyncHazard::WRITE_RACING_WRITE:
66 return "SYNC-HAZARD-WRITE-RACING-WRITE";
67 break;
68 case SyncHazard::WRITE_RACING_READ:
69 return "SYNC-HAZARD-WRITE-RACING-READ";
70 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060071 default:
72 assert(0);
73 }
74 return "SYNC-HAZARD-INVALID";
75}
76
John Zulauf59e25072020-07-17 10:55:21 -060077static bool IsHazardVsRead(SyncHazard hazard) {
78 switch (hazard) {
79 case SyncHazard::NONE:
80 return false;
81 break;
82 case SyncHazard::READ_AFTER_WRITE:
83 return false;
84 break;
85 case SyncHazard::WRITE_AFTER_READ:
86 return true;
87 break;
88 case SyncHazard::WRITE_AFTER_WRITE:
89 return false;
90 break;
91 case SyncHazard::READ_RACING_WRITE:
92 return false;
93 break;
94 case SyncHazard::WRITE_RACING_WRITE:
95 return false;
96 break;
97 case SyncHazard::WRITE_RACING_READ:
98 return true;
99 break;
100 default:
101 assert(0);
102 }
103 return false;
104}
105
John Zulauf9cb530d2019-09-30 14:14:10 -0600106static const char *string_SyncHazard(SyncHazard hazard) {
107 switch (hazard) {
108 case SyncHazard::NONE:
109 return "NONR";
110 break;
111 case SyncHazard::READ_AFTER_WRITE:
112 return "READ_AFTER_WRITE";
113 break;
114 case SyncHazard::WRITE_AFTER_READ:
115 return "WRITE_AFTER_READ";
116 break;
117 case SyncHazard::WRITE_AFTER_WRITE:
118 return "WRITE_AFTER_WRITE";
119 break;
John Zulauf2f952d22020-02-10 11:34:51 -0700120 case SyncHazard::READ_RACING_WRITE:
121 return "READ_RACING_WRITE";
122 break;
123 case SyncHazard::WRITE_RACING_WRITE:
124 return "WRITE_RACING_WRITE";
125 break;
126 case SyncHazard::WRITE_RACING_READ:
127 return "WRITE_RACING_READ";
128 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600129 default:
130 assert(0);
131 }
132 return "INVALID HAZARD";
133}
134
John Zulauf37ceaed2020-07-03 16:18:15 -0600135static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
136 // Return the info for the first bit found
137 const SyncStageAccessInfoType *info = nullptr;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700138 for (size_t i = 0; i < flags.size(); i++) {
139 if (flags.test(i)) {
140 info = &syncStageAccessInfoByStageAccessIndex[i];
141 break;
John Zulauf37ceaed2020-07-03 16:18:15 -0600142 }
143 }
144 return info;
145}
146
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700147static std::string string_SyncStageAccessFlags(const SyncStageAccessFlags &flags, const char *sep = "|") {
John Zulauf59e25072020-07-17 10:55:21 -0600148 std::string out_str;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700149 if (flags.none()) {
John Zulauf389c34b2020-07-28 11:19:35 -0600150 out_str = "0";
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700151 } else {
152 for (size_t i = 0; i < syncStageAccessInfoByStageAccessIndex.size(); i++) {
153 const auto &info = syncStageAccessInfoByStageAccessIndex[i];
154 if ((flags & info.stage_access_bit).any()) {
155 if (!out_str.empty()) {
156 out_str.append(sep);
157 }
158 out_str.append(info.name);
John Zulauf59e25072020-07-17 10:55:21 -0600159 }
John Zulauf59e25072020-07-17 10:55:21 -0600160 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700161 if (out_str.length() == 0) {
162 out_str.append("Unhandled SyncStageAccess");
163 }
John Zulauf59e25072020-07-17 10:55:21 -0600164 }
165 return out_str;
166}
167
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700168static std::string string_UsageTag(const ResourceUsageTag &tag) {
169 std::stringstream out;
170
John Zulauffaea0ee2021-01-14 14:01:32 -0700171 out << "command: " << CommandTypeString(tag.command);
172 out << ", seq_no: " << tag.seq_num;
173 if (tag.sub_command != 0) {
174 out << ", subcmd: " << tag.sub_command;
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700175 }
176 return out.str();
177}
178
John Zulauffaea0ee2021-01-14 14:01:32 -0700179std::string CommandBufferAccessContext::FormatUsage(const HazardResult &hazard) const {
John Zulauf37ceaed2020-07-03 16:18:15 -0600180 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600181 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
182 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600183 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600184 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
185 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf59e25072020-07-17 10:55:21 -0600186 out << "(usage: " << usage_info.name << ", prior_usage: " << stage_access_name;
187 if (IsHazardVsRead(hazard.hazard)) {
188 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
Jeremy Gebben40a22942020-12-22 14:22:06 -0700189 out << ", read_barriers: " << string_VkPipelineStageFlags2KHR(barriers);
John Zulauf59e25072020-07-17 10:55:21 -0600190 } else {
191 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
192 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
193 }
194
John Zulauffaea0ee2021-01-14 14:01:32 -0700195 // PHASE2 TODO -- add comand buffer and reset from secondary if applicable
ZaOniRinku56b86472021-03-23 20:25:05 +0100196 out << ", " << string_UsageTag(tag) << ", reset_no: " << reset_count_ << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600197 return out.str();
198}
199
John Zulaufd14743a2020-07-03 09:42:39 -0600200// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
201// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
202// also reflects this special case for read hazard detection (using access instead of exec scope)
Jeremy Gebben40a22942020-12-22 14:22:06 -0700203static constexpr VkPipelineStageFlags2KHR kColorAttachmentExecScope = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700204static const SyncStageAccessFlags kColorAttachmentAccessScope =
205 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
206 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
207 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
208 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebben40a22942020-12-22 14:22:06 -0700209static constexpr VkPipelineStageFlags2KHR kDepthStencilAttachmentExecScope =
210 VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT_KHR | VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700211static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
212 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
213 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
214 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -0700215static constexpr VkPipelineStageFlags2KHR kRasterAttachmentExecScope = kDepthStencilAttachmentExecScope | kColorAttachmentExecScope;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700216static const SyncStageAccessFlags kRasterAttachmentAccessScope = kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope;
John Zulaufb027cdb2020-05-21 14:25:22 -0600217
John Zulauf8e3c3e92021-01-06 11:19:36 -0700218ResourceAccessState::OrderingBarriers ResourceAccessState::kOrderingRules = {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700219 {{VK_PIPELINE_STAGE_2_NONE_KHR, SyncStageAccessFlags()},
John Zulauf8e3c3e92021-01-06 11:19:36 -0700220 {kColorAttachmentExecScope, kColorAttachmentAccessScope},
221 {kDepthStencilAttachmentExecScope, kDepthStencilAttachmentAccessScope},
222 {kRasterAttachmentExecScope, kRasterAttachmentAccessScope}}};
223
John Zulauf7635de32020-05-29 17:14:15 -0600224// Sometimes we have an internal access conflict, and we using the kCurrentCommandTag to set and detect in temporary/proxy contexts
John Zulauffaea0ee2021-01-14 14:01:32 -0700225static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, ResourceUsageTag::kMaxCount,
226 ResourceUsageTag::kMaxCount, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600227
John Zulaufb02c1eb2020-10-06 16:33:36 -0600228static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) {
229 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
230}
John Zulauf29d00532021-03-04 13:28:54 -0700231static VkDeviceSize ResourceBaseAddress(const IMAGE_STATE &image_state) {
232 VkDeviceSize base_address;
233 if (image_state.is_swapchain_image || (VK_NULL_HANDLE != image_state.bind_swapchain)) {
234 base_address = image_state.swapchain_fake_address;
235 } else {
236 base_address = ResourceBaseAddress(static_cast<const BINDABLE &>(image_state));
237 }
238 return base_address;
239}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600240
locke-lunarg3c038002020-04-30 23:08:08 -0600241inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
242 if (size == VK_WHOLE_SIZE) {
243 return (whole_size - offset);
244 }
245 return size;
246}
247
John Zulauf3e86bf02020-09-12 10:47:57 -0600248static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
249 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
250}
251
John Zulauf16adfc92020-04-08 10:28:33 -0600252template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600253static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600254 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
255}
256
John Zulauf355e49b2020-04-24 15:11:15 -0600257static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600258
John Zulauf3e86bf02020-09-12 10:47:57 -0600259static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
260 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
261}
262
263static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
264 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
265}
266
John Zulauf4a6105a2020-11-17 15:11:05 -0700267// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
268//
John Zulauf10f1f522020-12-18 12:00:35 -0700269// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
270//
John Zulauf4a6105a2020-11-17 15:11:05 -0700271// Usage:
272// Constructor() -- initializes the generator to point to the begin of the space declared.
273// * -- the current range of the generator empty signfies end
274// ++ -- advance to the next non-empty range (or end)
275
276// A wrapper for a single range with the same semantics as the actual generators below
277template <typename KeyType>
278class SingleRangeGenerator {
279 public:
280 SingleRangeGenerator(const KeyType &range) : current_(range) {}
John Zulaufd5115702021-01-18 12:34:33 -0700281 const KeyType &operator*() const { return current_; }
282 const KeyType *operator->() const { return &current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700283 SingleRangeGenerator &operator++() {
284 current_ = KeyType(); // just one real range
285 return *this;
286 }
287
288 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
289
290 private:
291 SingleRangeGenerator() = default;
292 const KeyType range_;
293 KeyType current_;
294};
295
296// Generate the ranges that are the intersection of range and the entries in the FilterMap
297template <typename FilterMap, typename KeyType = typename FilterMap::key_type>
298class FilteredRangeGenerator {
299 public:
John Zulaufd5115702021-01-18 12:34:33 -0700300 // Default constructed is safe to dereference for "empty" test, but for no other operation.
301 FilteredRangeGenerator() : range_(), filter_(nullptr), filter_pos_(), current_() {
302 // Default construction for KeyType *must* be empty range
303 assert(current_.empty());
304 }
John Zulauf4a6105a2020-11-17 15:11:05 -0700305 FilteredRangeGenerator(const FilterMap &filter, const KeyType &range)
306 : range_(range), filter_(&filter), filter_pos_(), current_() {
307 SeekBegin();
308 }
John Zulaufd5115702021-01-18 12:34:33 -0700309 FilteredRangeGenerator(const FilteredRangeGenerator &from) = default;
310
John Zulauf4a6105a2020-11-17 15:11:05 -0700311 const KeyType &operator*() const { return current_; }
312 const KeyType *operator->() const { return &current_; }
313 FilteredRangeGenerator &operator++() {
314 ++filter_pos_;
315 UpdateCurrent();
316 return *this;
317 }
318
319 bool operator==(const FilteredRangeGenerator &other) const { return current_ == other.current_; }
320
321 private:
John Zulauf4a6105a2020-11-17 15:11:05 -0700322 void UpdateCurrent() {
323 if (filter_pos_ != filter_->cend()) {
324 current_ = range_ & filter_pos_->first;
325 } else {
326 current_ = KeyType();
327 }
328 }
329 void SeekBegin() {
330 filter_pos_ = filter_->lower_bound(range_);
331 UpdateCurrent();
332 }
333 const KeyType range_;
334 const FilterMap *filter_;
335 typename FilterMap::const_iterator filter_pos_;
336 KeyType current_;
337};
John Zulaufd5115702021-01-18 12:34:33 -0700338using SingleAccessRangeGenerator = SingleRangeGenerator<ResourceAccessRange>;
John Zulauf4a6105a2020-11-17 15:11:05 -0700339using EventSimpleRangeGenerator = FilteredRangeGenerator<SyncEventState::ScopeMap>;
340
341// Templated to allow for different Range generators or map sources...
342
343// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulauf4a6105a2020-11-17 15:11:05 -0700344template <typename FilterMap, typename RangeGen, typename KeyType = typename FilterMap::key_type>
345class FilteredGeneratorGenerator {
346 public:
John Zulaufd5115702021-01-18 12:34:33 -0700347 // Default constructed is safe to dereference for "empty" test, but for no other operation.
348 FilteredGeneratorGenerator() : filter_(nullptr), gen_(), filter_pos_(), current_() {
349 // Default construction for KeyType *must* be empty range
350 assert(current_.empty());
351 }
352 FilteredGeneratorGenerator(const FilterMap &filter, RangeGen &gen) : filter_(&filter), gen_(gen), filter_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700353 SeekBegin();
354 }
John Zulaufd5115702021-01-18 12:34:33 -0700355 FilteredGeneratorGenerator(const FilteredGeneratorGenerator &from) = default;
John Zulauf4a6105a2020-11-17 15:11:05 -0700356 const KeyType &operator*() const { return current_; }
357 const KeyType *operator->() const { return &current_; }
358 FilteredGeneratorGenerator &operator++() {
359 KeyType gen_range = GenRange();
360 KeyType filter_range = FilterRange();
361 current_ = KeyType();
362 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
363 if (gen_range.end > filter_range.end) {
364 // if the generated range is beyond the filter_range, advance the filter range
365 filter_range = AdvanceFilter();
366 } else {
367 gen_range = AdvanceGen();
368 }
369 current_ = gen_range & filter_range;
370 }
371 return *this;
372 }
373
374 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
375
376 private:
377 KeyType AdvanceFilter() {
378 ++filter_pos_;
379 auto filter_range = FilterRange();
380 if (filter_range.valid()) {
381 FastForwardGen(filter_range);
382 }
383 return filter_range;
384 }
385 KeyType AdvanceGen() {
John Zulaufd5115702021-01-18 12:34:33 -0700386 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700387 auto gen_range = GenRange();
388 if (gen_range.valid()) {
389 FastForwardFilter(gen_range);
390 }
391 return gen_range;
392 }
393
394 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
John Zulaufd5115702021-01-18 12:34:33 -0700395 KeyType GenRange() const { return *gen_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700396
397 KeyType FastForwardFilter(const KeyType &range) {
398 auto filter_range = FilterRange();
399 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700400 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700401 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
402 if (retry_count < kRetryLimit) {
403 ++filter_pos_;
404 filter_range = FilterRange();
405 retry_count++;
406 } else {
407 // Okay we've tried walking, do a seek.
408 filter_pos_ = filter_->lower_bound(range);
409 break;
410 }
411 }
412 return FilterRange();
413 }
414
415 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
416 // faster.
417 KeyType FastForwardGen(const KeyType &range) {
418 auto gen_range = GenRange();
419 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
John Zulaufd5115702021-01-18 12:34:33 -0700420 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700421 gen_range = GenRange();
422 }
423 return gen_range;
424 }
425
426 void SeekBegin() {
427 auto gen_range = GenRange();
428 if (gen_range.empty()) {
429 current_ = KeyType();
430 filter_pos_ = filter_->cend();
431 } else {
432 filter_pos_ = filter_->lower_bound(gen_range);
433 current_ = gen_range & FilterRange();
434 }
435 }
436
John Zulauf4a6105a2020-11-17 15:11:05 -0700437 const FilterMap *filter_;
John Zulaufd5115702021-01-18 12:34:33 -0700438 RangeGen gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700439 typename FilterMap::const_iterator filter_pos_;
440 KeyType current_;
441};
442
443using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
444
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700445static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700446
John Zulauf3e86bf02020-09-12 10:47:57 -0600447ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
448 VkDeviceSize stride) {
449 VkDeviceSize range_start = offset + first_index * stride;
450 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600451 if (count == UINT32_MAX) {
452 range_size = buf_whole_size - range_start;
453 } else {
454 range_size = count * stride;
455 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600456 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600457}
458
locke-lunarg654e3692020-06-04 17:19:15 -0600459SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
460 VkShaderStageFlagBits stage_flag) {
461 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
462 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
463 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
464 }
465 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
466 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
467 assert(0);
468 }
469 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
470 return stage_access->second.uniform_read;
471 }
472
473 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
474 // Because if write hazard happens, read hazard might or might not happen.
475 // But if write hazard doesn't happen, read hazard is impossible to happen.
476 if (descriptor_data.is_writable) {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700477 return stage_access->second.storage_write;
locke-lunarg654e3692020-06-04 17:19:15 -0600478 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700479 // TODO: sampled_read
480 return stage_access->second.storage_read;
locke-lunarg654e3692020-06-04 17:19:15 -0600481}
482
locke-lunarg37047832020-06-12 13:44:45 -0600483bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
484 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
485 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
486 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
487 ? true
488 : false;
489}
490
491bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
492 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
493 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
494 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
495 ? true
496 : false;
497}
498
John Zulauf355e49b2020-04-24 15:11:15 -0600499// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600500template <typename Action>
501static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
502 Action &action) {
503 // At this point the "apply over range" logic only supports a single memory binding
504 if (!SimpleBinding(image_state)) return;
505 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600506 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700507 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
508 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600509 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700510 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600511 }
512}
513
John Zulauf7635de32020-05-29 17:14:15 -0600514// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
515// Used by both validation and record operations
516//
517// The signature for Action() reflect the needs of both uses.
518template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700519void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
520 uint32_t subpass) {
John Zulauf7635de32020-05-29 17:14:15 -0600521 const auto &rp_ci = rp_state.createInfo;
522 const auto *attachment_ci = rp_ci.pAttachments;
523 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
524
525 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
526 const auto *color_attachments = subpass_ci.pColorAttachments;
527 const auto *color_resolve = subpass_ci.pResolveAttachments;
528 if (color_resolve && color_attachments) {
529 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
530 const auto &color_attach = color_attachments[i].attachment;
531 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
532 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
533 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700534 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ,
535 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600536 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700537 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
538 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600539 }
540 }
541 }
542
543 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700544 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600545 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
546 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
547 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
548 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
549 const auto src_ci = attachment_ci[src_at];
550 // The formats are required to match so we can pick either
551 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
552 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
553 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
John Zulauf7635de32020-05-29 17:14:15 -0600554
555 // Figure out which aspects are actually touched during resolve operations
556 const char *aspect_string = nullptr;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700557 AttachmentViewGen::Gen gen_type = AttachmentViewGen::Gen::kRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600558 if (resolve_depth && resolve_stencil) {
John Zulauf7635de32020-05-29 17:14:15 -0600559 aspect_string = "depth/stencil";
560 } else if (resolve_depth) {
561 // Validate depth only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700562 gen_type = AttachmentViewGen::Gen::kDepthOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600563 aspect_string = "depth";
564 } else if (resolve_stencil) {
565 // Validate all stencil only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700566 gen_type = AttachmentViewGen::Gen::kStencilOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600567 aspect_string = "stencil";
568 }
569
John Zulaufd0ec59f2021-03-13 14:25:08 -0700570 if (aspect_string) {
571 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at], gen_type,
572 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster);
573 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at], gen_type,
574 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulauf7635de32020-05-29 17:14:15 -0600575 }
576 }
577}
578
579// Action for validating resolve operations
580class ValidateResolveAction {
581 public:
John Zulauffaea0ee2021-01-14 14:01:32 -0700582 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context,
John Zulauf64ffe552021-02-06 10:25:07 -0700583 const CommandExecutionContext &ex_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600584 : render_pass_(render_pass),
585 subpass_(subpass),
586 context_(context),
John Zulauf64ffe552021-02-06 10:25:07 -0700587 ex_context_(ex_context),
John Zulauf7635de32020-05-29 17:14:15 -0600588 func_name_(func_name),
589 skip_(false) {}
590 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 -0700591 const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage,
592 SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600593 HazardResult hazard;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700594 hazard = context_.DetectHazard(view_gen, gen_type, current_usage, ordering_rule);
John Zulauf7635de32020-05-29 17:14:15 -0600595 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -0700596 skip_ |=
John Zulauf64ffe552021-02-06 10:25:07 -0700597 ex_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700598 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
599 " to resolve attachment %" PRIu32 ". Access info %s.",
600 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name,
John Zulauf64ffe552021-02-06 10:25:07 -0700601 attachment_name, src_at, dst_at, ex_context_.FormatUsage(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600602 }
603 }
604 // Providing a mechanism for the constructing caller to get the result of the validation
605 bool GetSkip() const { return skip_; }
606
607 private:
608 VkRenderPass render_pass_;
609 const uint32_t subpass_;
610 const AccessContext &context_;
John Zulauf64ffe552021-02-06 10:25:07 -0700611 const CommandExecutionContext &ex_context_;
John Zulauf7635de32020-05-29 17:14:15 -0600612 const char *func_name_;
613 bool skip_;
614};
615
616// Update action for resolve operations
617class UpdateStateResolveAction {
618 public:
619 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
John Zulaufd0ec59f2021-03-13 14:25:08 -0700620 void operator()(const char *, const char *, uint32_t, uint32_t, const AttachmentViewGen &view_gen,
621 AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600622 // Ignores validation only arguments...
John Zulaufd0ec59f2021-03-13 14:25:08 -0700623 context_.UpdateAccessState(view_gen, gen_type, current_usage, ordering_rule, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600624 }
625
626 private:
627 AccessContext &context_;
628 const ResourceUsageTag &tag_;
629};
630
John Zulauf59e25072020-07-17 10:55:21 -0600631void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700632 const SyncStageAccessFlags &prior_, const ResourceUsageTag &tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600633 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
634 usage_index = usage_index_;
635 hazard = hazard_;
636 prior_access = prior_;
637 tag = tag_;
638}
639
John Zulauf540266b2020-04-06 18:54:53 -0600640AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
641 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600642 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600643 Reset();
644 const auto &subpass_dep = dependencies[subpass];
John Zulauf22aefed2021-03-11 18:14:35 -0700645 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
646 prev_.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
John Zulauf355e49b2020-04-24 15:11:15 -0600647 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600648 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600649 const auto prev_pass = prev_dep.first->pass;
650 const auto &prev_barriers = prev_dep.second;
651 assert(prev_dep.second.size());
652 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
653 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700654 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600655
656 async_.reserve(subpass_dep.async.size());
657 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700658 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600659 }
John Zulauf22aefed2021-03-11 18:14:35 -0700660 if (has_barrier_from_external) {
661 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
662 prev_.emplace_back(external_context, queue_flags, subpass_dep.barrier_from_external);
663 src_external_ = &prev_.back();
John Zulaufe5da6e52020-03-18 15:32:18 -0600664 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600665 if (subpass_dep.barrier_to_external.size()) {
666 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600667 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700668}
669
John Zulauf5f13a792020-03-10 07:31:21 -0600670template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700671HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600672 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600673 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600674 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600675
676 HazardResult hazard;
677 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
678 hazard = detector.Detect(prev);
679 }
680 return hazard;
681}
682
John Zulauf4a6105a2020-11-17 15:11:05 -0700683template <typename Action>
684void AccessContext::ForAll(Action &&action) {
685 for (const auto address_type : kAddressTypes) {
686 auto &accesses = GetAccessStateMap(address_type);
687 for (const auto &access : accesses) {
688 action(address_type, access);
689 }
690 }
691}
692
John Zulauf3d84f1b2020-03-09 13:33:25 -0600693// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
694// the DAG of the contexts (for example subpasses)
695template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700696HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600697 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600698 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600699
John Zulauf1a224292020-06-30 14:52:13 -0600700 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600701 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
702 // so we'll check these first
703 for (const auto &async_context : async_) {
704 hazard = async_context->DetectAsyncHazard(type, detector, range);
705 if (hazard.hazard) return hazard;
706 }
John Zulauf5f13a792020-03-10 07:31:21 -0600707 }
708
John Zulauf1a224292020-06-30 14:52:13 -0600709 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600710
John Zulauf69133422020-05-20 14:55:53 -0600711 const auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600712 const auto the_end = accesses.cend(); // End is not invalidated
713 auto pos = accesses.lower_bound(range);
John Zulauf69133422020-05-20 14:55:53 -0600714 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600715
John Zulauf3cafbf72021-03-26 16:55:19 -0600716 while (pos != the_end && pos->first.begin < range.end) {
John Zulauf69133422020-05-20 14:55:53 -0600717 // Cover any leading gap, or gap between entries
718 if (detect_prev) {
719 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
720 // Cover any leading gap, or gap between entries
721 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600722 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600723 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600724 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600725 if (hazard.hazard) return hazard;
726 }
John Zulauf69133422020-05-20 14:55:53 -0600727 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
728 gap.begin = pos->first.end;
729 }
730
731 hazard = detector.Detect(pos);
732 if (hazard.hazard) return hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600733 ++pos;
John Zulauf69133422020-05-20 14:55:53 -0600734 }
735
736 if (detect_prev) {
737 // Detect in the trailing empty as needed
738 gap.end = range.end;
739 if (gap.non_empty()) {
740 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600741 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600742 }
743
744 return hazard;
745}
746
747// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
748template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700749HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
750 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600751 auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600752 auto pos = accesses.lower_bound(range);
753 const auto the_end = accesses.end();
John Zulauf16adfc92020-04-08 10:28:33 -0600754
John Zulauf3d84f1b2020-03-09 13:33:25 -0600755 HazardResult hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600756 while (pos != the_end && pos->first.begin < range.end) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700757 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3cafbf72021-03-26 16:55:19 -0600758 if (hazard.hazard) break;
759 ++pos;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600760 }
John Zulauf16adfc92020-04-08 10:28:33 -0600761
John Zulauf3d84f1b2020-03-09 13:33:25 -0600762 return hazard;
763}
764
John Zulaufb02c1eb2020-10-06 16:33:36 -0600765struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700766 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600767 void operator()(ResourceAccessState *access) const {
768 assert(access);
769 access->ApplyBarriers(barriers, true);
770 }
771 const std::vector<SyncBarrier> &barriers;
772};
773
John Zulauf22aefed2021-03-11 18:14:35 -0700774struct ApplyTrackbackStackAction {
775 explicit ApplyTrackbackStackAction(const std::vector<SyncBarrier> &barriers_,
776 const ResourceAccessStateFunction *previous_barrier_ = nullptr)
777 : barriers(barriers_), previous_barrier(previous_barrier_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600778 void operator()(ResourceAccessState *access) const {
779 assert(access);
780 assert(!access->HasPendingState());
781 access->ApplyBarriers(barriers, false);
782 access->ApplyPendingBarriers(kCurrentCommandTag);
John Zulauf22aefed2021-03-11 18:14:35 -0700783 if (previous_barrier) {
784 assert(bool(*previous_barrier));
785 (*previous_barrier)(access);
786 }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600787 }
788 const std::vector<SyncBarrier> &barriers;
John Zulauf22aefed2021-03-11 18:14:35 -0700789 const ResourceAccessStateFunction *previous_barrier;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600790};
791
792// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
793// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
794// *different* map from dest.
795// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
796// range [first, last)
797template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600798static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
799 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600800 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600801 auto at = entry;
802 for (auto pos = first; pos != last; ++pos) {
803 // Every member of the input iterator range must fit within the remaining portion of entry
804 assert(at->first.includes(pos->first));
805 assert(at != dest->end());
806 // Trim up at to the same size as the entry to resolve
807 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600808 auto access = pos->second; // intentional copy
809 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600810 at->second.Resolve(access);
811 ++at; // Go to the remaining unused section of entry
812 }
813}
814
John Zulaufa0a98292020-09-18 09:30:10 -0600815static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
816 SyncBarrier merged = {};
817 for (const auto &barrier : barriers) {
818 merged.Merge(barrier);
819 }
820 return merged;
821}
822
John Zulaufb02c1eb2020-10-06 16:33:36 -0600823template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700824void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600825 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
826 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600827 if (!range.non_empty()) return;
828
John Zulauf355e49b2020-04-24 15:11:15 -0600829 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
830 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600831 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600832 if (current->pos_B->valid) {
833 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600834 auto access = src_pos->second; // intentional copy
835 barrier_action(&access);
836
John Zulauf16adfc92020-04-08 10:28:33 -0600837 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600838 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
839 trimmed->second.Resolve(access);
840 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600841 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600842 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600843 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600844 }
John Zulauf16adfc92020-04-08 10:28:33 -0600845 } else {
846 // we have to descend to fill this gap
847 if (recur_to_infill) {
John Zulauf22aefed2021-03-11 18:14:35 -0700848 ResourceAccessRange recurrence_range = current_range;
849 // The current context is empty for the current range, so recur to fill the gap.
850 // Since we will be recurring back up the DAG, expand the gap descent to cover the full range for which B
851 // is not valid, to minimize that recurrence
852 if (current->pos_B.at_end()) {
853 // Do the remainder here....
854 recurrence_range.end = range.end;
John Zulauf355e49b2020-04-24 15:11:15 -0600855 } else {
John Zulauf22aefed2021-03-11 18:14:35 -0700856 // Recur only over the range until B becomes valid (within the limits of range).
857 recurrence_range.end = std::min(range.end, current->pos_B->lower_bound->first.begin);
John Zulauf355e49b2020-04-24 15:11:15 -0600858 }
John Zulauf22aefed2021-03-11 18:14:35 -0700859 ResolvePreviousAccessStack(type, recurrence_range, resolve_map, infill_state, barrier_action);
860
John Zulauf355e49b2020-04-24 15:11:15 -0600861 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
862 // iterator of the outer while.
863
864 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
865 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
866 // we stepped on the dest map
John Zulauf22aefed2021-03-11 18:14:35 -0700867 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 -0600868 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600869 current.seek(seek_to);
870 } else if (!current->pos_A->valid && infill_state) {
871 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
872 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
873 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600874 }
John Zulauf5f13a792020-03-10 07:31:21 -0600875 }
John Zulauf16adfc92020-04-08 10:28:33 -0600876 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600877 }
John Zulauf1a224292020-06-30 14:52:13 -0600878
879 // Infill if range goes passed both the current and resolve map prior contents
880 if (recur_to_infill && (current->range.end < range.end)) {
881 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
John Zulauf22aefed2021-03-11 18:14:35 -0700882 ResolvePreviousAccessStack<BarrierAction>(type, trailing_fill_range, resolve_map, infill_state, barrier_action);
John Zulauf1a224292020-06-30 14:52:13 -0600883 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600884}
885
John Zulauf22aefed2021-03-11 18:14:35 -0700886template <typename BarrierAction>
887void AccessContext::ResolvePreviousAccessStack(AccessAddressType type, const ResourceAccessRange &range,
888 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
889 const BarrierAction &previous_barrier) const {
890 ResourceAccessStateFunction stacked_barrier(std::ref(previous_barrier));
891 ResolvePreviousAccess(type, range, descent_map, infill_state, &stacked_barrier);
892}
893
John Zulauf43cc7462020-12-03 12:33:12 -0700894void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
John Zulauf22aefed2021-03-11 18:14:35 -0700895 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
896 const ResourceAccessStateFunction *previous_barrier) const {
897 if (prev_.size() == 0) {
John Zulauf5f13a792020-03-10 07:31:21 -0600898 if (range.non_empty() && infill_state) {
John Zulauf22aefed2021-03-11 18:14:35 -0700899 // Fill the empty poritions of descent_map with the default_state with the barrier function applied (iff present)
900 ResourceAccessState state_copy;
901 if (previous_barrier) {
902 assert(bool(*previous_barrier));
903 state_copy = *infill_state;
904 (*previous_barrier)(&state_copy);
905 infill_state = &state_copy;
906 }
907 sparse_container::update_range_value(*descent_map, range, *infill_state,
908 sparse_container::value_precedence::prefer_dest);
John Zulauf5f13a792020-03-10 07:31:21 -0600909 }
910 } else {
911 // Look for something to fill the gap further along.
912 for (const auto &prev_dep : prev_) {
John Zulauf22aefed2021-03-11 18:14:35 -0700913 const ApplyTrackbackStackAction barrier_action(prev_dep.barriers, previous_barrier);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600914 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600915 }
John Zulauf5f13a792020-03-10 07:31:21 -0600916 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600917}
918
John Zulauf4a6105a2020-11-17 15:11:05 -0700919// Non-lazy import of all accesses, WaitEvents needs this.
920void AccessContext::ResolvePreviousAccesses() {
921 ResourceAccessState default_state;
John Zulauf22aefed2021-03-11 18:14:35 -0700922 if (!prev_.size()) return; // If no previous contexts, nothing to do
923
John Zulauf4a6105a2020-11-17 15:11:05 -0700924 for (const auto address_type : kAddressTypes) {
925 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
926 }
927}
928
John Zulauf43cc7462020-12-03 12:33:12 -0700929AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
930 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -0600931}
932
John Zulauf1507ee42020-05-18 11:33:09 -0600933static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
934 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
935 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
936 return stage_access;
937}
938static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
939 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
940 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
941 return stage_access;
942}
943
John Zulauf7635de32020-05-29 17:14:15 -0600944// Caller must manage returned pointer
945static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -0700946 uint32_t subpass, const AttachmentViewGenVector &attachment_views) {
John Zulauf7635de32020-05-29 17:14:15 -0600947 auto *proxy = new AccessContext(context);
John Zulaufd0ec59f2021-03-13 14:25:08 -0700948 proxy->UpdateAttachmentResolveAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
949 proxy->UpdateAttachmentStoreAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600950 return proxy;
951}
952
John Zulaufb02c1eb2020-10-06 16:33:36 -0600953template <typename BarrierAction>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700954void AccessContext::ResolveAccessRange(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
955 BarrierAction &barrier_action, ResourceAccessRangeMap *descent_map,
956 const ResourceAccessState *infill_state) const {
957 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
958 if (!attachment_gen) return;
959
960 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
961 const AccessAddressType address_type = view_gen.GetAddressType();
962 for (; range_gen->non_empty(); ++range_gen) {
963 ResolveAccessRange(address_type, *range_gen, barrier_action, descent_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600964 }
John Zulauf62f10592020-04-03 12:20:02 -0600965}
966
John Zulauf7635de32020-05-29 17:14:15 -0600967// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf64ffe552021-02-06 10:25:07 -0700968bool AccessContext::ValidateLayoutTransitions(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600969 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -0700970 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600971 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600972 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
973 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
974 // those affects have not been recorded yet.
975 //
976 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
977 // to apply and only copy then, if this proves a hot spot.
978 std::unique_ptr<AccessContext> proxy_for_prev;
979 TrackBack proxy_track_back;
980
John Zulauf355e49b2020-04-24 15:11:15 -0600981 const auto &transitions = rp_state.subpass_transitions[subpass];
982 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600983 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
984
985 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
John Zulauf22aefed2021-03-11 18:14:35 -0700986 assert(track_back);
John Zulauf7635de32020-05-29 17:14:15 -0600987 if (prev_needs_proxy) {
988 if (!proxy_for_prev) {
John Zulaufd0ec59f2021-03-13 14:25:08 -0700989 proxy_for_prev.reset(
990 CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass, attachment_views));
John Zulauf7635de32020-05-29 17:14:15 -0600991 proxy_track_back = *track_back;
992 proxy_track_back.context = proxy_for_prev.get();
993 }
994 track_back = &proxy_track_back;
995 }
996 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600997 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600998 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700999 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1000 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
1001 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1002 string_VkImageLayout(transition.old_layout),
1003 string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07001004 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001005 }
1006 }
1007 return skip;
1008}
1009
John Zulauf64ffe552021-02-06 10:25:07 -07001010bool AccessContext::ValidateLoadOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001011 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001012 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001013 bool skip = false;
1014 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufa0a98292020-09-18 09:30:10 -06001015
John Zulauf1507ee42020-05-18 11:33:09 -06001016 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1017 if (subpass == rp_state.attachment_first_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001018 const auto &view_gen = attachment_views[i];
1019 if (!view_gen.IsValid()) continue;
John Zulauf1507ee42020-05-18 11:33:09 -06001020 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001021
1022 // Need check in the following way
1023 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1024 // vs. transition
1025 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1026 // for each aspect loaded.
1027
1028 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001029 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001030 const bool is_color = !(has_depth || has_stencil);
1031
1032 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001033 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001034
John Zulaufaff20662020-06-01 14:07:58 -06001035 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001036 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001037
John Zulaufb02c1eb2020-10-06 16:33:36 -06001038 bool checked_stencil = false;
1039 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001040 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea, load_index, SyncOrdering::kColorAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001041 aspect = "color";
1042 } else {
1043 if (has_depth) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001044 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_index,
1045 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001046 aspect = "depth";
1047 }
1048 if (!hazard.hazard && has_stencil) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001049 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, stencil_load_index,
1050 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001051 aspect = "stencil";
1052 checked_stencil = true;
1053 }
1054 }
1055
1056 if (hazard.hazard) {
1057 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001058 const auto &sync_state = ex_context.GetSyncState();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001059 if (hazard.tag == kCurrentCommandTag) {
1060 // Hazard vs. ILT
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001061 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulaufb02c1eb2020-10-06 16:33:36 -06001062 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1063 " aspect %s during load with loadOp %s.",
1064 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1065 } else {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001066 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauf1507ee42020-05-18 11:33:09 -06001067 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001068 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001069 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf64ffe552021-02-06 10:25:07 -07001070 ex_context.FormatUsage(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001071 }
1072 }
1073 }
1074 }
1075 return skip;
1076}
1077
John Zulaufaff20662020-06-01 14:07:58 -06001078// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1079// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1080// store is part of the same Next/End operation.
1081// The latter is handled in layout transistion validation directly
John Zulauf64ffe552021-02-06 10:25:07 -07001082bool AccessContext::ValidateStoreOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufaff20662020-06-01 14:07:58 -06001083 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001084 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001085 bool skip = false;
1086 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001087
1088 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1089 if (subpass == rp_state.attachment_last_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001090 const AttachmentViewGen &view_gen = attachment_views[i];
1091 if (!view_gen.IsValid()) continue;
John Zulaufaff20662020-06-01 14:07:58 -06001092 const auto &ci = attachment_ci[i];
1093
1094 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1095 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1096 // sake, we treat DONT_CARE as writing.
1097 const bool has_depth = FormatHasDepth(ci.format);
1098 const bool has_stencil = FormatHasStencil(ci.format);
1099 const bool is_color = !(has_depth || has_stencil);
1100 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1101 if (!has_stencil && !store_op_stores) continue;
1102
1103 HazardResult hazard;
1104 const char *aspect = nullptr;
1105 bool checked_stencil = false;
1106 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001107 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
1108 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001109 aspect = "color";
1110 } else {
1111 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
John Zulaufaff20662020-06-01 14:07:58 -06001112 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001113 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1114 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001115 aspect = "depth";
1116 }
1117 if (!hazard.hazard && has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001118 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1119 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001120 aspect = "stencil";
1121 checked_stencil = true;
1122 }
1123 }
1124
1125 if (hazard.hazard) {
1126 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1127 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001128 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001129 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1130 " %s aspect during store with %s %s. Access info %s",
1131 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect,
John Zulauf64ffe552021-02-06 10:25:07 -07001132 op_type_string, store_op_string, ex_context.FormatUsage(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001133 }
1134 }
1135 }
1136 return skip;
1137}
1138
John Zulauf64ffe552021-02-06 10:25:07 -07001139bool AccessContext::ValidateResolveOperations(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001140 const VkRect2D &render_area, const AttachmentViewGenVector &attachment_views,
1141 const char *func_name, uint32_t subpass) const {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001142 ValidateResolveAction validate_action(rp_state.renderPass(), subpass, *this, ex_context, func_name);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001143 ResolveOperation(validate_action, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001144 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001145}
1146
John Zulauf3d84f1b2020-03-09 13:33:25 -06001147class HazardDetector {
1148 SyncStageAccessIndex usage_index_;
1149
1150 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001151 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001152 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1153 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001154 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001155 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001156};
1157
John Zulauf69133422020-05-20 14:55:53 -06001158class HazardDetectorWithOrdering {
1159 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001160 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001161
1162 public:
1163 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001164 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001165 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001166 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1167 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001168 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001169 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001170};
1171
John Zulauf16adfc92020-04-08 10:28:33 -06001172HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001173 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001174 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001175 const auto base_address = ResourceBaseAddress(buffer);
1176 HazardDetector detector(usage_index);
1177 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001178}
1179
John Zulauf69133422020-05-20 14:55:53 -06001180template <typename Detector>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001181HazardResult AccessContext::DetectHazard(Detector &detector, const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1182 DetectOptions options) const {
1183 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1184 if (!attachment_gen) return HazardResult();
1185
1186 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1187 const auto address_type = view_gen.GetAddressType();
1188 for (; range_gen->non_empty(); ++range_gen) {
1189 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1190 if (hazard.hazard) return hazard;
1191 }
1192
1193 return HazardResult();
1194}
1195
1196template <typename Detector>
John Zulauf69133422020-05-20 14:55:53 -06001197HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1198 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1199 const VkExtent3D &extent, DetectOptions options) const {
1200 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001201 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001202 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1203 base_address);
1204 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001205 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001206 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001207 if (hazard.hazard) return hazard;
1208 }
1209 return HazardResult();
1210}
John Zulauf110413c2021-03-20 05:38:38 -06001211template <typename Detector>
1212HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1213 const VkImageSubresourceRange &subresource_range, DetectOptions options) const {
1214 if (!SimpleBinding(image)) return HazardResult();
1215 const auto base_address = ResourceBaseAddress(image);
1216 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1217 const auto address_type = ImageAddressType(image);
1218 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf110413c2021-03-20 05:38:38 -06001219 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1220 if (hazard.hazard) return hazard;
1221 }
1222 return HazardResult();
1223}
John Zulauf69133422020-05-20 14:55:53 -06001224
John Zulauf540266b2020-04-06 18:54:53 -06001225HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1226 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1227 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001228 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1229 subresource.layerCount};
John Zulauf110413c2021-03-20 05:38:38 -06001230 HazardDetector detector(current_usage);
1231 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf1507ee42020-05-18 11:33:09 -06001232}
1233
1234HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf110413c2021-03-20 05:38:38 -06001235 const VkImageSubresourceRange &subresource_range) const {
John Zulauf69133422020-05-20 14:55:53 -06001236 HazardDetector detector(current_usage);
John Zulauf110413c2021-03-20 05:38:38 -06001237 return DetectHazard(detector, image, subresource_range, DetectOptions::kDetectAll);
John Zulauf69133422020-05-20 14:55:53 -06001238}
1239
John Zulaufd0ec59f2021-03-13 14:25:08 -07001240HazardResult AccessContext::DetectHazard(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1241 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) const {
1242 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
1243 return DetectHazard(detector, view_gen, gen_type, DetectOptions::kDetectAll);
1244}
1245
John Zulauf69133422020-05-20 14:55:53 -06001246HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001247 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001248 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001249 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001250 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001251}
1252
John Zulauf3d84f1b2020-03-09 13:33:25 -06001253class BarrierHazardDetector {
1254 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001255 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001256 SyncStageAccessFlags src_access_scope)
1257 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1258
John Zulauf5f13a792020-03-10 07:31:21 -06001259 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1260 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001261 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001262 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001263 // 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 -07001264 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001265 }
1266
1267 private:
1268 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001269 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001270 SyncStageAccessFlags src_access_scope_;
1271};
1272
John Zulauf4a6105a2020-11-17 15:11:05 -07001273class EventBarrierHazardDetector {
1274 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001275 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001276 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
1277 const ResourceUsageTag &scope_tag)
1278 : usage_index_(usage_index),
1279 src_exec_scope_(src_exec_scope),
1280 src_access_scope_(src_access_scope),
1281 event_scope_(event_scope),
1282 scope_pos_(event_scope.cbegin()),
1283 scope_end_(event_scope.cend()),
1284 scope_tag_(scope_tag) {}
1285
1286 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1287 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1288 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1289 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1290 if (scope_pos_ == scope_end_) return HazardResult();
1291 if (!scope_pos_->first.intersects(pos->first)) {
1292 event_scope_.lower_bound(pos->first);
1293 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1294 }
1295
1296 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1297 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1298 }
1299 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1300 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1301 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1302 }
1303
1304 private:
1305 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001306 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001307 SyncStageAccessFlags src_access_scope_;
1308 const SyncEventState::ScopeMap &event_scope_;
1309 SyncEventState::ScopeMap::const_iterator scope_pos_;
1310 SyncEventState::ScopeMap::const_iterator scope_end_;
1311 const ResourceUsageTag &scope_tag_;
1312};
1313
Jeremy Gebben40a22942020-12-22 14:22:06 -07001314HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001315 const SyncStageAccessFlags &src_access_scope,
1316 const VkImageSubresourceRange &subresource_range,
1317 const SyncEventState &sync_event, DetectOptions options) const {
1318 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1319 // first access scope map to use, and there's no easy way to plumb it in below.
1320 const auto address_type = ImageAddressType(image);
1321 const auto &event_scope = sync_event.FirstScope(address_type);
1322
1323 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1324 event_scope, sync_event.first_scope_tag);
John Zulauf110413c2021-03-20 05:38:38 -06001325 return DetectHazard(detector, image, subresource_range, options);
John Zulauf4a6105a2020-11-17 15:11:05 -07001326}
1327
John Zulaufd0ec59f2021-03-13 14:25:08 -07001328HazardResult AccessContext::DetectImageBarrierHazard(const AttachmentViewGen &view_gen, const SyncBarrier &barrier,
1329 DetectOptions options) const {
1330 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, barrier.src_exec_scope.exec_scope,
1331 barrier.src_access_scope);
1332 return DetectHazard(detector, view_gen, AttachmentViewGen::Gen::kViewSubresource, options);
1333}
1334
Jeremy Gebben40a22942020-12-22 14:22:06 -07001335HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001336 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001337 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001338 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001339 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
John Zulauf110413c2021-03-20 05:38:38 -06001340 return DetectHazard(detector, image, subresource_range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001341}
1342
Jeremy Gebben40a22942020-12-22 14:22:06 -07001343HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001344 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001345 const VkImageMemoryBarrier &barrier) const {
1346 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1347 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1348 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1349}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001350HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07001351 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope.exec_scope,
John Zulauf110413c2021-03-20 05:38:38 -06001352 image_barrier.barrier.src_access_scope, image_barrier.range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001353}
John Zulauf355e49b2020-04-24 15:11:15 -06001354
John Zulauf9cb530d2019-09-30 14:14:10 -06001355template <typename Flags, typename Map>
1356SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1357 SyncStageAccessFlags scope = 0;
1358 for (const auto &bit_scope : map) {
1359 if (flag_mask < bit_scope.first) break;
1360
1361 if (flag_mask & bit_scope.first) {
1362 scope |= bit_scope.second;
1363 }
1364 }
1365 return scope;
1366}
1367
Jeremy Gebben40a22942020-12-22 14:22:06 -07001368SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags2KHR stages) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001369 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1370}
1371
Jeremy Gebben40a22942020-12-22 14:22:06 -07001372SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags2KHR accesses) {
1373 return AccessScopeImpl(sync_utils::ExpandAccessFlags(accesses), syncStageAccessMaskByAccessBit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001374}
1375
Jeremy Gebben40a22942020-12-22 14:22:06 -07001376// Getting from stage mask and access mask to stage/access masks is something we need to be good at...
1377SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001378 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1379 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1380 // 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 -06001381 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1382}
1383
1384template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001385void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001386 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1387 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001388 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001389 auto pos = accesses->lower_bound(range);
1390 if (pos == accesses->end() || !pos->first.intersects(range)) {
1391 // The range is empty, fill it with a default value.
1392 pos = action.Infill(accesses, pos, range);
1393 } else if (range.begin < pos->first.begin) {
1394 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001395 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001396 } else if (pos->first.begin < range.begin) {
1397 // Trim the beginning if needed
1398 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1399 ++pos;
1400 }
1401
1402 const auto the_end = accesses->end();
1403 while ((pos != the_end) && pos->first.intersects(range)) {
1404 if (pos->first.end > range.end) {
1405 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1406 }
1407
1408 pos = action(accesses, pos);
1409 if (pos == the_end) break;
1410
1411 auto next = pos;
1412 ++next;
1413 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1414 // Need to infill if next is disjoint
1415 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001416 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001417 next = action.Infill(accesses, next, new_range);
1418 }
1419 pos = next;
1420 }
1421}
John Zulaufd5115702021-01-18 12:34:33 -07001422
1423// Give a comparable interface for range generators and ranges
1424template <typename Action>
1425inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1426 assert(range);
1427 UpdateMemoryAccessState(accesses, *range, action);
1428}
1429
John Zulauf4a6105a2020-11-17 15:11:05 -07001430template <typename Action, typename RangeGen>
1431void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1432 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001433 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 -07001434 for (; range_gen->non_empty(); ++range_gen) {
1435 UpdateMemoryAccessState(accesses, *range_gen, action);
1436 }
1437}
John Zulauf9cb530d2019-09-30 14:14:10 -06001438
John Zulaufd0ec59f2021-03-13 14:25:08 -07001439template <typename Action, typename RangeGen>
1440void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, const RangeGen &range_gen_prebuilt) {
1441 RangeGen range_gen(range_gen_prebuilt); // RangeGenerators can be expensive to create from scratch... initialize from built
1442 for (; range_gen->non_empty(); ++range_gen) {
1443 UpdateMemoryAccessState(accesses, *range_gen, action);
1444 }
1445}
John Zulauf9cb530d2019-09-30 14:14:10 -06001446struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001447 using Iterator = ResourceAccessRangeMap::iterator;
1448 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001449 // this is only called on gaps, and never returns a gap.
1450 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001451 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001452 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001453 }
John Zulauf5f13a792020-03-10 07:31:21 -06001454
John Zulauf5c5e88d2019-12-26 11:22:02 -07001455 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001456 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001457 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001458 return pos;
1459 }
1460
John Zulauf43cc7462020-12-03 12:33:12 -07001461 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001462 SyncOrdering ordering_rule_, const ResourceUsageTag &tag_)
1463 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001464 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001465 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001466 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001467 const SyncOrdering ordering_rule;
John Zulauf9cb530d2019-09-30 14:14:10 -06001468 const ResourceUsageTag &tag;
1469};
1470
John Zulauf4a6105a2020-11-17 15:11:05 -07001471// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001472struct PipelineBarrierOp {
1473 SyncBarrier barrier;
1474 bool layout_transition;
1475 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1476 : barrier(barrier_), layout_transition(layout_transition_) {}
1477 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001478 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001479 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1480};
John Zulauf4a6105a2020-11-17 15:11:05 -07001481// The barrier operation for wait events
1482struct WaitEventBarrierOp {
1483 const ResourceUsageTag *scope_tag;
1484 SyncBarrier barrier;
1485 bool layout_transition;
1486 WaitEventBarrierOp(const ResourceUsageTag &scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1487 : scope_tag(&scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
1488 WaitEventBarrierOp() = default;
1489 void operator()(ResourceAccessState *access_state) const {
1490 assert(scope_tag); // Not valid to have a non-scope op executed, default construct included for std::vector support
1491 access_state->ApplyBarrier(*scope_tag, barrier, layout_transition);
1492 }
1493};
John Zulauf1e331ec2020-12-04 18:29:38 -07001494
John Zulauf4a6105a2020-11-17 15:11:05 -07001495// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1496// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1497// of a collection is known/present.
John Zulauf1e331ec2020-12-04 18:29:38 -07001498template <typename BarrierOp>
John Zulauf89311b42020-09-29 16:28:47 -06001499class ApplyBarrierOpsFunctor {
1500 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001501 using Iterator = ResourceAccessRangeMap::iterator;
1502 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001503
John Zulauf5c5e88d2019-12-26 11:22:02 -07001504 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001505 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001506 for (const auto &op : barrier_ops_) {
1507 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001508 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001509
John Zulauf89311b42020-09-29 16:28:47 -06001510 if (resolve_) {
1511 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1512 // another walk
1513 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001514 }
1515 return pos;
1516 }
1517
John Zulauf89311b42020-09-29 16:28:47 -06001518 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulaufd5115702021-01-18 12:34:33 -07001519 ApplyBarrierOpsFunctor(bool resolve, size_t size_hint, const ResourceUsageTag &tag)
1520 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1521 barrier_ops_.reserve(size_hint);
1522 }
1523 void EmplaceBack(const BarrierOp &op) { barrier_ops_.emplace_back(op); }
John Zulauf89311b42020-09-29 16:28:47 -06001524
1525 private:
1526 bool resolve_;
John Zulaufd5115702021-01-18 12:34:33 -07001527 std::vector<BarrierOp> barrier_ops_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001528 const ResourceUsageTag &tag_;
1529};
1530
John Zulauf4a6105a2020-11-17 15:11:05 -07001531// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1532// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1533template <typename BarrierOp>
1534class ApplyBarrierFunctor {
1535 public:
1536 using Iterator = ResourceAccessRangeMap::iterator;
1537 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1538
1539 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1540 auto &access_state = pos->second;
1541 barrier_op_(&access_state);
1542 return pos;
1543 }
1544
1545 ApplyBarrierFunctor(const BarrierOp &barrier_op) : barrier_op_(barrier_op) {}
1546
1547 private:
John Zulaufd5115702021-01-18 12:34:33 -07001548 BarrierOp barrier_op_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001549};
1550
John Zulauf1e331ec2020-12-04 18:29:38 -07001551// This functor resolves the pendinging state.
1552class ResolvePendingBarrierFunctor {
1553 public:
1554 using Iterator = ResourceAccessRangeMap::iterator;
1555 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1556
1557 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1558 auto &access_state = pos->second;
1559 access_state.ApplyPendingBarriers(tag_);
1560 return pos;
1561 }
1562
1563 ResolvePendingBarrierFunctor(const ResourceUsageTag &tag) : tag_(tag) {}
1564
1565 private:
John Zulauf89311b42020-09-29 16:28:47 -06001566 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001567};
1568
John Zulauf8e3c3e92021-01-06 11:19:36 -07001569void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
1570 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
1571 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001572 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001573}
1574
John Zulauf8e3c3e92021-01-06 11:19:36 -07001575void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001576 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001577 if (!SimpleBinding(buffer)) return;
1578 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001579 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001580}
John Zulauf355e49b2020-04-24 15:11:15 -06001581
John Zulauf8e3c3e92021-01-06 11:19:36 -07001582void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf110413c2021-03-20 05:38:38 -06001583 const VkImageSubresourceRange &subresource_range, const ResourceUsageTag &tag) {
1584 if (!SimpleBinding(image)) return;
1585 const auto base_address = ResourceBaseAddress(image);
1586 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1587 const auto address_type = ImageAddressType(image);
1588 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1589 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
1590}
1591void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001592 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001593 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001594 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001595 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001596 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1597 base_address);
1598 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001599 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf110413c2021-03-20 05:38:38 -06001600 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001601}
John Zulaufd0ec59f2021-03-13 14:25:08 -07001602
1603void AccessContext::UpdateAccessState(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1604 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule, const ResourceUsageTag &tag) {
1605 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1606 if (!gen) return;
1607 subresource_adapter::ImageRangeGenerator range_gen(*gen);
1608 const auto address_type = view_gen.GetAddressType();
1609 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1610 ApplyUpdateAction(address_type, action, &range_gen);
John Zulauf7635de32020-05-29 17:14:15 -06001611}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001612
John Zulauf8e3c3e92021-01-06 11:19:36 -07001613void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001614 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1615 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001616 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1617 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001618 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001619}
1620
John Zulaufd0ec59f2021-03-13 14:25:08 -07001621template <typename Action, typename RangeGen>
1622void AccessContext::ApplyUpdateAction(AccessAddressType address_type, const Action &action, RangeGen *range_gen_arg) {
1623 assert(range_gen_arg); // Old Google C++ styleguide require non-const object pass by * not &, but this isn't an optional arg.
1624 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, range_gen_arg);
John Zulauf540266b2020-04-06 18:54:53 -06001625}
1626
1627template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001628void AccessContext::ApplyUpdateAction(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, const Action &action) {
1629 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1630 if (!gen) return;
1631 UpdateMemoryAccessState(&GetAccessStateMap(view_gen.GetAddressType()), action, *gen);
John Zulauf540266b2020-04-06 18:54:53 -06001632}
1633
John Zulaufd0ec59f2021-03-13 14:25:08 -07001634void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state,
1635 const AttachmentViewGenVector &attachment_views, uint32_t subpass,
John Zulauf7635de32020-05-29 17:14:15 -06001636 const ResourceUsageTag &tag) {
1637 UpdateStateResolveAction update(*this, tag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001638 ResolveOperation(update, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001639}
1640
John Zulaufd0ec59f2021-03-13 14:25:08 -07001641void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
1642 uint32_t subpass, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001643 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001644
1645 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1646 if (rp_state.attachment_last_subpass[i] == subpass) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001647 const auto &view_gen = attachment_views[i];
1648 if (!view_gen.IsValid()) continue; // UNUSED
John Zulaufaff20662020-06-01 14:07:58 -06001649
1650 const auto &ci = attachment_ci[i];
1651 const bool has_depth = FormatHasDepth(ci.format);
1652 const bool has_stencil = FormatHasStencil(ci.format);
1653 const bool is_color = !(has_depth || has_stencil);
1654 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1655
1656 if (is_color && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001657 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
1658 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001659 } else {
John Zulaufaff20662020-06-01 14:07:58 -06001660 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001661 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1662 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001663 }
1664 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1665 if (has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001666 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1667 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001668 }
1669 }
1670 }
1671 }
1672}
1673
John Zulauf540266b2020-04-06 18:54:53 -06001674template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001675void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001676 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001677 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001678 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001679 }
1680}
1681
1682void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001683 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1684 auto &context = contexts[subpass_index];
John Zulauf22aefed2021-03-11 18:14:35 -07001685 ApplyTrackbackStackAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001686 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001687 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001688 }
1689 }
1690}
1691
John Zulauf355e49b2020-04-24 15:11:15 -06001692// Suitable only for *subpass* access contexts
John Zulaufd0ec59f2021-03-13 14:25:08 -07001693HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const AttachmentViewGen &attach_view) const {
1694 if (!attach_view.IsValid()) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001695
John Zulauf355e49b2020-04-24 15:11:15 -06001696 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001697 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001698
1699 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001700 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1701 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001702 HazardResult hazard = track_back.context->DetectImageBarrierHazard(attach_view, merged_barrier, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001703 if (!hazard.hazard) {
1704 // The Async hazard check is against the current context's async set.
John Zulaufd0ec59f2021-03-13 14:25:08 -07001705 hazard = DetectImageBarrierHazard(attach_view, merged_barrier, kDetectAsync);
John Zulauf355e49b2020-04-24 15:11:15 -06001706 }
John Zulaufa0a98292020-09-18 09:30:10 -06001707
John Zulauf355e49b2020-04-24 15:11:15 -06001708 return hazard;
1709}
1710
John Zulaufb02c1eb2020-10-06 16:33:36 -06001711void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001712 const AttachmentViewGenVector &attachment_views, const ResourceUsageTag &tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001713 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001714 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001715 for (const auto &transition : transitions) {
1716 const auto prev_pass = transition.prev_pass;
John Zulaufd0ec59f2021-03-13 14:25:08 -07001717 const auto &view_gen = attachment_views[transition.attachment];
1718 if (!view_gen.IsValid()) continue;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001719
1720 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1721 assert(trackback);
1722
1723 // Import the attachments into the current context
1724 const auto *prev_context = trackback->context;
1725 assert(prev_context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001726 const auto address_type = view_gen.GetAddressType();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001727 auto &target_map = GetAccessStateMap(address_type);
1728 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001729 prev_context->ResolveAccessRange(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action, &target_map,
1730 &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001731 }
1732
John Zulauf86356ca2020-10-19 11:46:41 -06001733 // If there were no transitions skip this global map walk
1734 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001735 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001736 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001737 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001738}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001739
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001740void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
1741 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
John Zulauf669dfd52021-01-27 17:15:28 -07001742
1743 auto *events_context = GetCurrentEventsContext();
1744 assert(events_context);
1745 for (auto &event_pair : *events_context) {
John Zulauf4a6105a2020-11-17 15:11:05 -07001746 assert(event_pair.second); // Shouldn't be storing empty
1747 auto &sync_event = *event_pair.second;
1748 // 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 -07001749 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
1750 sync_event.barriers |= dst.exec_scope;
1751 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
John Zulauf4a6105a2020-11-17 15:11:05 -07001752 }
1753 }
1754}
1755
John Zulauf355e49b2020-04-24 15:11:15 -06001756
locke-lunarg61870c22020-06-09 14:51:50 -06001757bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1758 const char *func_name) const {
1759 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001760 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001761 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001762 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001763 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001764 return skip;
1765 }
1766
1767 using DescriptorClass = cvdescriptorset::DescriptorClass;
1768 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1769 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1770 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1771 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1772
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001773 for (const auto &stage_state : pipe->stage_state) {
1774 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1775 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001776 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001777 }
locke-lunarg61870c22020-06-09 14:51:50 -06001778 for (const auto &set_binding : stage_state.descriptor_uses) {
1779 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1780 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1781 set_binding.first.second);
1782 const auto descriptor_type = binding_it.GetType();
1783 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1784 auto array_idx = 0;
1785
1786 if (binding_it.IsVariableDescriptorCount()) {
1787 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1788 }
1789 SyncStageAccessIndex sync_index =
1790 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1791
1792 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1793 uint32_t index = i - index_range.start;
1794 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1795 switch (descriptor->GetClass()) {
1796 case DescriptorClass::ImageSampler:
1797 case DescriptorClass::Image: {
1798 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001799 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001800 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001801 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1802 img_view_state = image_sampler_descriptor->GetImageViewState();
1803 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001804 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001805 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1806 img_view_state = image_descriptor->GetImageViewState();
1807 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001808 }
1809 if (!img_view_state) continue;
John Zulauf361fb532020-07-22 10:45:39 -06001810 HazardResult hazard;
John Zulauf110413c2021-03-20 05:38:38 -06001811 const IMAGE_STATE *img_state = img_view_state->image_state.get();
John Zulauf361fb532020-07-22 10:45:39 -06001812 const auto &subresource_range = img_view_state->normalized_subresource_range;
John Zulauf110413c2021-03-20 05:38:38 -06001813
1814 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1815 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1816 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
John Zulauf361fb532020-07-22 10:45:39 -06001817 // Input attachments are subject to raster ordering rules
1818 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001819 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001820 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001821 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range);
John Zulauf361fb532020-07-22 10:45:39 -06001822 }
John Zulauf110413c2021-03-20 05:38:38 -06001823
John Zulauf33fc1d52020-07-17 11:01:10 -06001824 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001825 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001826 img_view_state->image_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001827 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1828 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001829 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001830 sync_state_->report_data->FormatHandle(img_view_state->image_view()).c_str(),
1831 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1832 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001833 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1834 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
John Zulauffaea0ee2021-01-14 14:01:32 -07001835 set_binding.first.second, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001836 }
1837 break;
1838 }
1839 case DescriptorClass::TexelBuffer: {
1840 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1841 if (!buf_view_state) continue;
1842 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001843 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001844 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001845 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001846 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001847 buf_view_state->buffer_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001848 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1849 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001850 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view()).c_str(),
1851 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1852 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001853 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1854 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001855 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001856 }
1857 break;
1858 }
1859 case DescriptorClass::GeneralBuffer: {
1860 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1861 auto buf_state = buffer_descriptor->GetBufferState();
1862 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001863 const ResourceAccessRange range =
1864 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001865 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001866 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001867 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001868 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001869 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1870 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001871 sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
1872 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1873 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001874 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1875 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001876 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001877 }
1878 break;
1879 }
1880 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1881 default:
1882 break;
1883 }
1884 }
1885 }
1886 }
1887 return skip;
1888}
1889
1890void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1891 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001892 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001893 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001894 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001895 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001896 return;
1897 }
1898
1899 using DescriptorClass = cvdescriptorset::DescriptorClass;
1900 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1901 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1902 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1903 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1904
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001905 for (const auto &stage_state : pipe->stage_state) {
1906 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1907 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001908 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001909 }
locke-lunarg61870c22020-06-09 14:51:50 -06001910 for (const auto &set_binding : stage_state.descriptor_uses) {
1911 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1912 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1913 set_binding.first.second);
1914 const auto descriptor_type = binding_it.GetType();
1915 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1916 auto array_idx = 0;
1917
1918 if (binding_it.IsVariableDescriptorCount()) {
1919 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1920 }
1921 SyncStageAccessIndex sync_index =
1922 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1923
1924 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1925 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1926 switch (descriptor->GetClass()) {
1927 case DescriptorClass::ImageSampler:
1928 case DescriptorClass::Image: {
1929 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1930 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1931 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1932 } else {
1933 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1934 }
1935 if (!img_view_state) continue;
1936 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001937 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
John Zulauf110413c2021-03-20 05:38:38 -06001938 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1939 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1940 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kRaster,
1941 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001942 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001943 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kNonAttachment,
1944 img_view_state->normalized_subresource_range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001945 }
locke-lunarg61870c22020-06-09 14:51:50 -06001946 break;
1947 }
1948 case DescriptorClass::TexelBuffer: {
1949 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1950 if (!buf_view_state) continue;
1951 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001952 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001953 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001954 break;
1955 }
1956 case DescriptorClass::GeneralBuffer: {
1957 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1958 auto buf_state = buffer_descriptor->GetBufferState();
1959 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001960 const ResourceAccessRange range =
1961 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07001962 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001963 break;
1964 }
1965 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1966 default:
1967 break;
1968 }
1969 }
1970 }
1971 }
1972}
1973
1974bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1975 bool skip = false;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001976 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001977 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06001978 return skip;
1979 }
1980
1981 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1982 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001983 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06001984
1985 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001986 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06001987 if (binding_description.binding < binding_buffers_size) {
1988 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06001989 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001990
locke-lunarg1ae57d62020-11-18 10:49:19 -07001991 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001992 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1993 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07001994 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06001995 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001996 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001997 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
1998 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
1999 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002000 }
2001 }
2002 }
2003 return skip;
2004}
2005
2006void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002007 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002008 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002009 return;
2010 }
2011 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2012 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002013 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002014
2015 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002016 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002017 if (binding_description.binding < binding_buffers_size) {
2018 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002019 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002020
locke-lunarg1ae57d62020-11-18 10:49:19 -07002021 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002022 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2023 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002024 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2025 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002026 }
2027 }
2028}
2029
2030bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2031 bool skip = false;
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002032 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->Destroyed()) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002033 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002034 }
locke-lunarg61870c22020-06-09 14:51:50 -06002035
locke-lunarg1ae57d62020-11-18 10:49:19 -07002036 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002037 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002038 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2039 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002040 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002041 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002042 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002043 index_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
2044 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer()).c_str(),
2045 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002046 }
2047
2048 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2049 // We will detect more accurate range in the future.
2050 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2051 return skip;
2052}
2053
2054void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002055 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 -06002056
locke-lunarg1ae57d62020-11-18 10:49:19 -07002057 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002058 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002059 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2060 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002061 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002062
2063 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2064 // We will detect more accurate range in the future.
2065 RecordDrawVertex(UINT32_MAX, 0, tag);
2066}
2067
2068bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002069 bool skip = false;
2070 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002071 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002072 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002073}
2074
2075void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002076 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002077 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002078 }
locke-lunarg61870c22020-06-09 14:51:50 -06002079}
2080
John Zulauf64ffe552021-02-06 10:25:07 -07002081void CommandBufferAccessContext::RecordBeginRenderPass(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2082 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2083 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002084 // Create an access context the current renderpass.
John Zulauf64ffe552021-02-06 10:25:07 -07002085 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002086 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf64ffe552021-02-06 10:25:07 -07002087 current_renderpass_context_->RecordBeginRenderPass(tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002088 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002089}
2090
John Zulauf64ffe552021-02-06 10:25:07 -07002091void CommandBufferAccessContext::RecordNextSubpass(CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002092 assert(current_renderpass_context_);
John Zulauffaea0ee2021-01-14 14:01:32 -07002093 auto prev_tag = NextCommandTag(command);
2094 auto next_tag = NextSubcommandTag(command);
John Zulauf64ffe552021-02-06 10:25:07 -07002095 current_renderpass_context_->RecordNextSubpass(prev_tag, next_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002096 current_context_ = &current_renderpass_context_->CurrentContext();
2097}
2098
John Zulauf64ffe552021-02-06 10:25:07 -07002099void CommandBufferAccessContext::RecordEndRenderPass(CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002100 assert(current_renderpass_context_);
2101 if (!current_renderpass_context_) return;
2102
John Zulauf64ffe552021-02-06 10:25:07 -07002103 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, NextCommandTag(command));
John Zulauf355e49b2020-04-24 15:11:15 -06002104 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002105 current_renderpass_context_ = nullptr;
2106}
2107
John Zulauf4a6105a2020-11-17 15:11:05 -07002108void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2109 // Erase is okay with the key not being
John Zulauf669dfd52021-01-27 17:15:28 -07002110 const auto *event_state = sync_state_->Get<EVENT_STATE>(event);
2111 if (event_state) {
2112 GetCurrentEventsContext()->Destroy(event_state);
John Zulaufd5115702021-01-18 12:34:33 -07002113 }
2114}
2115
John Zulauf64ffe552021-02-06 10:25:07 -07002116bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
John Zulauffaea0ee2021-01-14 14:01:32 -07002117 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002118 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002119 const auto &sync_state = ex_context.GetSyncState();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002120 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002121 if (!pipe ||
2122 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002123 return skip;
2124 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002125 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002126 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg37047832020-06-12 13:44:45 -06002127
John Zulauf1a224292020-06-30 14:52:13 -06002128 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002129 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002130 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2131 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002132 if (location >= subpass.colorAttachmentCount ||
2133 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002134 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002135 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002136 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2137 if (!view_gen.IsValid()) continue;
2138 HazardResult hazard =
2139 current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
2140 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment);
locke-lunarg96dc9632020-06-10 17:22:18 -06002141 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002142 const VkImageView view_handle = view_gen.GetViewState()->image_view();
John Zulaufd0ec59f2021-03-13 14:25:08 -07002143 skip |= sync_state.LogError(view_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002144 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002145 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002146 sync_state.report_data->FormatHandle(view_handle).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002147 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002148 location, ex_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002149 }
2150 }
2151 }
locke-lunarg37047832020-06-12 13:44:45 -06002152
2153 // PHASE1 TODO: Add layout based read/vs. write selection.
2154 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002155 const uint32_t depth_stencil_attachment =
2156 GetSubpassDepthStencilAttachmentIndex(pipe->graphicsPipelineCI.pDepthStencilState, subpass.pDepthStencilAttachment);
2157
2158 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2159 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2160 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002161 bool depth_write = false, stencil_write = false;
2162
2163 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002164 if (!FormatIsStencilOnly(view_state.create_info.format) && pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002165 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002166 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2167 depth_write = true;
2168 }
2169 // PHASE1 TODO: It needs to check if stencil is writable.
2170 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2171 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2172 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002173 if (!FormatIsDepthOnly(view_state.create_info.format) && pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002174 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2175 stencil_write = true;
2176 }
2177
2178 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2179 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002180 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2181 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2182 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002183 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002184 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002185 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002186 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002187 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002188 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2189 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002190 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002191 }
2192 }
2193 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002194 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2195 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2196 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002197 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002198 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002199 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002200 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002201 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002202 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2203 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002204 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002205 }
locke-lunarg61870c22020-06-09 14:51:50 -06002206 }
2207 }
2208 return skip;
2209}
2210
John Zulauf64ffe552021-02-06 10:25:07 -07002211void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag &tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002212 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002213 if (!pipe ||
2214 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002215 return;
2216 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002217 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002218 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002219
John Zulauf1a224292020-06-30 14:52:13 -06002220 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002221 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002222 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2223 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002224 if (location >= subpass.colorAttachmentCount ||
2225 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002226 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002227 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002228 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2229 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2230 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2231 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002232 }
2233 }
locke-lunarg37047832020-06-12 13:44:45 -06002234
2235 // PHASE1 TODO: Add layout based read/vs. write selection.
2236 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002237 const uint32_t depth_stencil_attachment =
2238 GetSubpassDepthStencilAttachmentIndex(pipe->graphicsPipelineCI.pDepthStencilState, subpass.pDepthStencilAttachment);
2239 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2240 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2241 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002242 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002243 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2244 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002245
2246 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002247 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002248 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2249 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002250 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2251 depth_write = true;
2252 }
2253 // PHASE1 TODO: It needs to check if stencil is writable.
2254 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2255 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2256 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002257 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002258 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002259 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2260 stencil_write = true;
2261 }
2262
John Zulaufd0ec59f2021-03-13 14:25:08 -07002263 if (depth_write || stencil_write) {
2264 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2265 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2266 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2267 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002268 }
locke-lunarg61870c22020-06-09 14:51:50 -06002269 }
2270}
2271
John Zulauf64ffe552021-02-06 10:25:07 -07002272bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002273 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002274 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002275 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002276 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002277 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002278 func_name);
2279
John Zulauf355e49b2020-04-24 15:11:15 -06002280 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002281 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002282 skip |=
2283 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002284 if (!skip) {
2285 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2286 // on a copy of the (empty) next context.
2287 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2288 AccessContext temp_context(next_context);
2289 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002290 skip |=
2291 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002292 }
John Zulauf7635de32020-05-29 17:14:15 -06002293 return skip;
2294}
John Zulauf64ffe552021-02-06 10:25:07 -07002295bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002296 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002297 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002298 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002299 current_subpass_);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002300 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_,
2301
2302 attachment_views_, func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002303 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002304 return skip;
2305}
2306
John Zulauf64ffe552021-02-06 10:25:07 -07002307AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002308 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002309}
2310
John Zulauf64ffe552021-02-06 10:25:07 -07002311bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2312 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002313 bool skip = false;
2314
John Zulauf7635de32020-05-29 17:14:15 -06002315 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2316 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2317 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2318 // to apply and only copy then, if this proves a hot spot.
2319 std::unique_ptr<AccessContext> proxy_for_current;
2320
John Zulauf355e49b2020-04-24 15:11:15 -06002321 // Validate the "finalLayout" transitions to external
2322 // Get them from where there we're hidding in the extra entry.
2323 const auto &final_transitions = rp_state_->subpass_transitions.back();
2324 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002325 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002326 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2327 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002328 auto *context = trackback.context;
2329
2330 if (transition.prev_pass == current_subpass_) {
2331 if (!proxy_for_current) {
2332 // 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 -07002333 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002334 }
2335 context = proxy_for_current.get();
2336 }
2337
John Zulaufa0a98292020-09-18 09:30:10 -06002338 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2339 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002340 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002341 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002342 skip |= ex_context.GetSyncState().LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002343 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07002344 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2345 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2346 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2347 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002348 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002349 }
2350 }
2351 return skip;
2352}
2353
2354void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2355 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002356 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002357}
2358
John Zulauf64ffe552021-02-06 10:25:07 -07002359void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag &tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002360 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2361 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002362
2363 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2364 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002365 const AttachmentViewGen &view_gen = attachment_views_[i];
2366 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002367
2368 const auto &ci = attachment_ci[i];
2369 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002370 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002371 const bool is_color = !(has_depth || has_stencil);
2372
2373 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002374 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, ColorLoadUsage(ci.loadOp),
2375 SyncOrdering::kColorAttachment, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002376 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002377 if (has_depth) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002378 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2379 DepthStencilLoadUsage(ci.loadOp), SyncOrdering::kDepthStencilAttachment, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002380 }
2381 if (has_stencil) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002382 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2383 DepthStencilLoadUsage(ci.stencilLoadOp),
2384 SyncOrdering::kDepthStencilAttachment, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002385 }
2386 }
2387 }
2388 }
2389}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002390AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2391 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2392 AttachmentViewGenVector view_gens;
2393 VkExtent3D extent = CastTo3D(render_area.extent);
2394 VkOffset3D offset = CastTo3D(render_area.offset);
2395 view_gens.reserve(attachment_views.size());
2396 for (const auto *view : attachment_views) {
2397 view_gens.emplace_back(view, offset, extent);
2398 }
2399 return view_gens;
2400}
John Zulauf64ffe552021-02-06 10:25:07 -07002401RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2402 VkQueueFlags queue_flags,
2403 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2404 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002405 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002406 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002407 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002408 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002409 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002410 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002411 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002412}
2413void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
2414 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002415 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002416 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002417 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002418}
John Zulauf1507ee42020-05-18 11:33:09 -06002419
John Zulauf64ffe552021-02-06 10:25:07 -07002420void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag &prev_subpass_tag,
John Zulauffaea0ee2021-01-14 14:01:32 -07002421 const ResourceUsageTag &next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002422 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulaufd0ec59f2021-03-13 14:25:08 -07002423 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
2424 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002425
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002426 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2427 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002428 current_subpass_++;
2429 assert(current_subpass_ < subpass_contexts_.size());
John Zulauffaea0ee2021-01-14 14:01:32 -07002430 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2431 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002432 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002433}
2434
John Zulauf64ffe552021-02-06 10:25:07 -07002435void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002436 // Add the resolve and store accesses
John Zulaufd0ec59f2021-03-13 14:25:08 -07002437 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, tag);
2438 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002439
John Zulauf355e49b2020-04-24 15:11:15 -06002440 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002441 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002442
2443 // Add the "finalLayout" transitions to external
2444 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002445 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2446 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2447 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002448 const auto &final_transitions = rp_state_->subpass_transitions.back();
2449 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002450 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002451 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002452 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002453 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002454 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002455 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002456 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002457 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002458 }
2459}
2460
Jeremy Gebben40a22942020-12-22 14:22:06 -07002461SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002462 SyncExecScope result;
2463 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002464 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2465 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002466 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2467 return result;
2468}
2469
Jeremy Gebben40a22942020-12-22 14:22:06 -07002470SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002471 SyncExecScope result;
2472 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002473 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2474 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002475 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2476 return result;
2477}
2478
2479SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002480 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002481 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002482 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002483 dst_access_scope = 0;
2484}
2485
2486template <typename Barrier>
2487SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002488 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002489 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002490 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002491 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2492}
2493
2494SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002495 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2496 if (barrier) {
2497 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002498 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002499 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002500
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002501 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002502 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002503 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2504
2505 } else {
2506 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002507 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002508 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2509
2510 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002511 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002512 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2513 }
2514}
2515
2516template <typename Barrier>
2517SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2518 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2519 src_exec_scope = src.exec_scope;
2520 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2521
2522 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002523 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002524 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002525}
2526
John Zulaufb02c1eb2020-10-06 16:33:36 -06002527// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2528void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2529 for (const auto &barrier : barriers) {
2530 ApplyBarrier(barrier, layout_transition);
2531 }
2532}
2533
John Zulauf89311b42020-09-29 16:28:47 -06002534// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2535// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2536// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002537void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2538 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002539 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002540 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002541 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002542 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002543 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002544 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002545}
John Zulauf9cb530d2019-09-30 14:14:10 -06002546HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2547 HazardResult hazard;
2548 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002549 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002550 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002551 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002552 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002553 }
2554 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002555 // Write operation:
2556 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2557 // If reads exists -- test only against them because either:
2558 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2559 // * 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
2560 // the current write happens after the reads, so just test the write against the reades
2561 // Otherwise test against last_write
2562 //
2563 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002564 if (last_reads.size()) {
2565 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002566 if (IsReadHazard(usage_stage, read_access)) {
2567 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2568 break;
2569 }
2570 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002571 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002572 // Write-After-Write check -- if we have a previous write to test against
2573 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002574 }
2575 }
2576 return hazard;
2577}
2578
John Zulauf8e3c3e92021-01-06 11:19:36 -07002579HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering &ordering_rule) const {
2580 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06002581 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2582 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002583 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002584 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002585 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2586 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002587 if (IsRead(usage_bit)) {
2588 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2589 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2590 if (is_raw_hazard) {
2591 // NOTE: we know last_write is non-zero
2592 // See if the ordering rules save us from the simple RAW check above
2593 // First check to see if the current usage is covered by the ordering rules
2594 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2595 const bool usage_is_ordered =
2596 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2597 if (usage_is_ordered) {
2598 // Now see of the most recent write (or a subsequent read) are ordered
2599 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2600 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002601 }
2602 }
John Zulauf4285ee92020-09-23 10:20:52 -06002603 if (is_raw_hazard) {
2604 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2605 }
John Zulauf361fb532020-07-22 10:45:39 -06002606 } else {
2607 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002608 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002609 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002610 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002611 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002612 if (usage_write_is_ordered) {
2613 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2614 ordered_stages = GetOrderedStages(ordering);
2615 }
2616 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2617 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002618 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002619 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2620 if (IsReadHazard(usage_stage, read_access)) {
2621 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2622 break;
2623 }
John Zulaufd14743a2020-07-03 09:42:39 -06002624 }
2625 }
John Zulauf4285ee92020-09-23 10:20:52 -06002626 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002627 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002628 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002629 }
John Zulauf69133422020-05-20 14:55:53 -06002630 }
2631 }
2632 return hazard;
2633}
2634
John Zulauf2f952d22020-02-10 11:34:51 -07002635// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002636HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002637 HazardResult hazard;
2638 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002639 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2640 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2641 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002642 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002643 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002644 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002645 }
2646 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002647 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002648 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002649 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002650 // 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 -07002651 for (const auto &read_access : last_reads) {
2652 if (read_access.tag.index >= start_tag.index) {
2653 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002654 break;
2655 }
2656 }
John Zulauf2f952d22020-02-10 11:34:51 -07002657 }
2658 }
2659 return hazard;
2660}
2661
Jeremy Gebben40a22942020-12-22 14:22:06 -07002662HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002663 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002664 // Only supporting image layout transitions for now
2665 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2666 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002667 // only test for WAW if there no intervening read operations.
2668 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002669 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002670 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002671 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002672 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002673 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002674 break;
2675 }
2676 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002677 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2678 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2679 }
2680
2681 return hazard;
2682}
2683
Jeremy Gebben40a22942020-12-22 14:22:06 -07002684HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07002685 const SyncStageAccessFlags &src_access_scope,
2686 const ResourceUsageTag &event_tag) const {
2687 // Only supporting image layout transitions for now
2688 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2689 HazardResult hazard;
2690 // only test for WAW if there no intervening read operations.
2691 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2692
John Zulaufab7756b2020-12-29 16:10:16 -07002693 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002694 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
2695 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07002696 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002697 if (read_access.tag.IsBefore(event_tag)) {
2698 // The read is in the events first synchronization scope, so we use a barrier hazard check
2699 // If the read stage is not in the src sync scope
2700 // *AND* not execution chained with an existing sync barrier (that's the or)
2701 // then the barrier access is unsafe (R/W after R)
2702 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
2703 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2704 break;
2705 }
2706 } else {
2707 // The read not in the event first sync scope and so is a hazard vs. the layout transition
2708 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2709 }
2710 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002711 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002712 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
2713 if (write_tag.IsBefore(event_tag)) {
2714 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
2715 // So do a normal barrier hazard check
2716 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2717 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2718 }
2719 } else {
2720 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06002721 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2722 }
John Zulaufd14743a2020-07-03 09:42:39 -06002723 }
John Zulauf361fb532020-07-22 10:45:39 -06002724
John Zulauf0cb5be22020-01-23 12:18:22 -07002725 return hazard;
2726}
2727
John Zulauf5f13a792020-03-10 07:31:21 -06002728// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2729// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2730// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2731void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2732 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002733 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2734 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002735 *this = other;
2736 } else if (!other.write_tag.IsBefore(write_tag)) {
2737 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2738 // dependency chaining logic or any stage expansion)
2739 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002740 pending_write_barriers |= other.pending_write_barriers;
2741 pending_layout_transition |= other.pending_layout_transition;
2742 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002743
John Zulaufd14743a2020-07-03 09:42:39 -06002744 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07002745 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06002746 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07002747 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002748 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002749 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002750 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002751 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2752 // but we should wait on profiling data for that.
2753 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002754 auto &my_read = last_reads[my_read_index];
2755 if (other_read.stage == my_read.stage) {
2756 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002757 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002758 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002759 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002760 my_read.pending_dep_chain = other_read.pending_dep_chain;
2761 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2762 // May require tracking more than one access per stage.
2763 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002764 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06002765 // Since I'm overwriting the fragement stage read, also update the input attachment info
2766 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002767 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002768 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002769 } else if (other_read.tag.IsBefore(my_read.tag)) {
2770 // The read tags match so merge the barriers
2771 my_read.barriers |= other_read.barriers;
2772 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002773 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002774
John Zulauf5f13a792020-03-10 07:31:21 -06002775 break;
2776 }
2777 }
2778 } else {
2779 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07002780 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06002781 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002782 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002783 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002784 }
John Zulauf5f13a792020-03-10 07:31:21 -06002785 }
2786 }
John Zulauf361fb532020-07-22 10:45:39 -06002787 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002788 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2789 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07002790
2791 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
2792 // of the copy and other into this using the update first logic.
2793 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
2794 // of the other first_accesses... )
2795 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
2796 FirstAccesses firsts(std::move(first_accesses_));
2797 first_accesses_.clear();
2798 first_read_stages_ = 0U;
2799 auto a = firsts.begin();
2800 auto a_end = firsts.end();
2801 for (auto &b : other.first_accesses_) {
2802 // TODO: Determine whether "IsBefore" or "IsGloballyBefore" is needed...
2803 while (a != a_end && a->tag.IsBefore(b.tag)) {
2804 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2805 ++a;
2806 }
2807 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
2808 }
2809 for (; a != a_end; ++a) {
2810 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2811 }
2812 }
John Zulauf5f13a792020-03-10 07:31:21 -06002813}
2814
John Zulauf8e3c3e92021-01-06 11:19:36 -07002815void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag &tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002816 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2817 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002818 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002819 // Mulitple outstanding reads may be of interest and do dependency chains independently
2820 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2821 const auto usage_stage = PipelineStageBit(usage_index);
2822 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002823 for (auto &read_access : last_reads) {
2824 if (read_access.stage == usage_stage) {
2825 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002826 break;
2827 }
2828 }
2829 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07002830 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002831 last_read_stages |= usage_stage;
2832 }
John Zulauf4285ee92020-09-23 10:20:52 -06002833
2834 // 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 -07002835 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002836 // TODO Revisit re: multiple reads for a given stage
2837 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002838 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002839 } else {
2840 // Assume write
2841 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002842 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002843 }
John Zulauffaea0ee2021-01-14 14:01:32 -07002844 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06002845}
John Zulauf5f13a792020-03-10 07:31:21 -06002846
John Zulauf89311b42020-09-29 16:28:47 -06002847// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2848// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2849// We can overwrite them as *this* write is now after them.
2850//
2851// 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 -07002852void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002853 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06002854 last_read_stages = 0;
2855 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002856 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002857
2858 write_barriers = 0;
2859 write_dependency_chain = 0;
2860 write_tag = tag;
2861 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002862}
2863
John Zulauf89311b42020-09-29 16:28:47 -06002864// Apply the memory barrier without updating the existing barriers. The execution barrier
2865// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2866// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2867// replace the current write barriers or add to them, so accumulate to pending as well.
2868void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2869 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2870 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002871 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
2872 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
2873 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
2874 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07002875 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06002876 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07002877 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002878 }
John Zulauf89311b42020-09-29 16:28:47 -06002879 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2880 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002881
John Zulauf89311b42020-09-29 16:28:47 -06002882 if (!pending_layout_transition) {
2883 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2884 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002885 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06002886 // 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 -07002887 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
2888 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002889 }
2890 }
John Zulaufa0a98292020-09-18 09:30:10 -06002891 }
John Zulaufa0a98292020-09-18 09:30:10 -06002892}
2893
John Zulauf4a6105a2020-11-17 15:11:05 -07002894// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
2895// changes the "chaining" state, but to keep barriers independent. See discussion above.
2896void ResourceAccessState::ApplyBarrier(const ResourceUsageTag &scope_tag, const SyncBarrier &barrier, bool layout_transition) {
2897 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
2898 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
2899 // in order to know if it's in the excecution scope
2900 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
2901 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
2902 // errors w.r.t. "most recent" accesses.
2903 if (layout_transition || ((write_tag.IsBefore(scope_tag)) && (barrier.src_access_scope & last_write).any())) {
2904 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07002905 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002906 }
2907 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2908 pending_layout_transition |= layout_transition;
2909
2910 if (!pending_layout_transition) {
2911 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2912 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002913 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002914 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
2915 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
2916 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
2917 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
2918 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
2919 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
2920 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulaufc523bf62021-02-16 08:20:34 -07002921 if (read_access.tag.IsBefore(scope_tag) &&
2922 (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers))) {
2923 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002924 }
2925 }
2926 }
2927}
John Zulauf89311b42020-09-29 16:28:47 -06002928void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
2929 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06002930 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2931 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07002932 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf89311b42020-09-29 16:28:47 -06002933 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002934 }
John Zulauf89311b42020-09-29 16:28:47 -06002935
2936 // Apply the accumulate execution barriers (and thus update chaining information)
2937 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07002938 for (auto &read_access : last_reads) {
2939 read_access.barriers |= read_access.pending_dep_chain;
2940 read_execution_barriers |= read_access.barriers;
2941 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06002942 }
2943
2944 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2945 write_dependency_chain |= pending_write_dep_chain;
2946 write_barriers |= pending_write_barriers;
2947 pending_write_dep_chain = 0;
2948 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002949}
2950
John Zulauf59e25072020-07-17 10:55:21 -06002951// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07002952VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
2953 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002954
John Zulaufab7756b2020-12-29 16:10:16 -07002955 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002956 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06002957 barriers = read_access.barriers;
2958 break;
John Zulauf59e25072020-07-17 10:55:21 -06002959 }
2960 }
John Zulauf4285ee92020-09-23 10:20:52 -06002961
John Zulauf59e25072020-07-17 10:55:21 -06002962 return barriers;
2963}
2964
Jeremy Gebben40a22942020-12-22 14:22:06 -07002965inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002966 assert(IsRead(usage));
2967 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
2968 // * the previous reads are not hazards, and thus last_write must be visible and available to
2969 // any reads that happen after.
2970 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2971 // 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 -07002972 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06002973}
2974
Jeremy Gebben40a22942020-12-22 14:22:06 -07002975VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002976 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07002977 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06002978 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002979 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06002980 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06002981 // 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 -07002982 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06002983 }
2984
2985 return ordered_stages;
2986}
2987
John Zulauffaea0ee2021-01-14 14:01:32 -07002988void ResourceAccessState::UpdateFirst(const ResourceUsageTag &tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
2989 // Only record until we record a write.
2990 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07002991 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07002992 if (0 == (usage_stage & first_read_stages_)) {
2993 // If this is a read we haven't seen or a write, record.
2994 first_read_stages_ |= usage_stage;
2995 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
2996 }
2997 }
2998}
2999
John Zulaufd1f85d42020-04-15 12:23:15 -06003000void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003001 auto *access_context = GetAccessContextNoInsert(command_buffer);
3002 if (access_context) {
3003 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003004 }
3005}
3006
John Zulaufd1f85d42020-04-15 12:23:15 -06003007void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3008 auto access_found = cb_access_state.find(command_buffer);
3009 if (access_found != cb_access_state.end()) {
3010 access_found->second->Reset();
3011 cb_access_state.erase(access_found);
3012 }
3013}
3014
John Zulauf9cb530d2019-09-30 14:14:10 -06003015bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3016 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3017 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003018 const auto *cb_context = GetAccessContext(commandBuffer);
3019 assert(cb_context);
3020 if (!cb_context) return skip;
3021 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003022
John Zulauf3d84f1b2020-03-09 13:33:25 -06003023 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003024 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003025 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003026
3027 for (uint32_t region = 0; region < regionCount; region++) {
3028 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003029 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003030 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003031 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003032 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003033 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003034 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003035 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003036 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003037 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003038 }
John Zulauf16adfc92020-04-08 10:28:33 -06003039 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003040 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003041 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003042 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003043 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003044 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003045 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003046 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003047 }
3048 }
3049 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003050 }
3051 return skip;
3052}
3053
3054void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3055 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003056 auto *cb_context = GetAccessContext(commandBuffer);
3057 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003058 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003059 auto *context = cb_context->GetCurrentAccessContext();
3060
John Zulauf9cb530d2019-09-30 14:14:10 -06003061 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003062 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003063
3064 for (uint32_t region = 0; region < regionCount; region++) {
3065 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003066 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003067 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003068 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003069 }
John Zulauf16adfc92020-04-08 10:28:33 -06003070 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003071 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003072 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003073 }
3074 }
3075}
3076
John Zulauf4a6105a2020-11-17 15:11:05 -07003077void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3078 // Clear out events from the command buffer contexts
3079 for (auto &cb_context : cb_access_state) {
3080 cb_context.second->RecordDestroyEvent(event);
3081 }
3082}
3083
Jeff Leger178b1e52020-10-05 12:22:23 -04003084bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3085 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3086 bool skip = false;
3087 const auto *cb_context = GetAccessContext(commandBuffer);
3088 assert(cb_context);
3089 if (!cb_context) return skip;
3090 const auto *context = cb_context->GetCurrentAccessContext();
3091
3092 // If we have no previous accesses, we have no hazards
3093 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3094 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3095
3096 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3097 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3098 if (src_buffer) {
3099 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003100 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003101 if (hazard.hazard) {
3102 // TODO -- add tag information to log msg when useful.
3103 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3104 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3105 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003106 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003107 }
3108 }
3109 if (dst_buffer && !skip) {
3110 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003111 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003112 if (hazard.hazard) {
3113 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3114 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3115 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003116 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003117 }
3118 }
3119 if (skip) break;
3120 }
3121 return skip;
3122}
3123
3124void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3125 auto *cb_context = GetAccessContext(commandBuffer);
3126 assert(cb_context);
3127 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3128 auto *context = cb_context->GetCurrentAccessContext();
3129
3130 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3131 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3132
3133 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3134 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3135 if (src_buffer) {
3136 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003137 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003138 }
3139 if (dst_buffer) {
3140 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003141 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003142 }
3143 }
3144}
3145
John Zulauf5c5e88d2019-12-26 11:22:02 -07003146bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3147 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3148 const VkImageCopy *pRegions) const {
3149 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003150 const auto *cb_access_context = GetAccessContext(commandBuffer);
3151 assert(cb_access_context);
3152 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003153
John Zulauf3d84f1b2020-03-09 13:33:25 -06003154 const auto *context = cb_access_context->GetCurrentAccessContext();
3155 assert(context);
3156 if (!context) return skip;
3157
3158 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3159 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003160 for (uint32_t region = 0; region < regionCount; region++) {
3161 const auto &copy_region = pRegions[region];
3162 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003163 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003164 copy_region.srcOffset, copy_region.extent);
3165 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003166 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003167 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003168 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003169 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003170 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003171 }
3172
3173 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003174 VkExtent3D dst_copy_extent =
3175 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003176 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003177 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003178 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003179 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003180 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003181 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003182 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003183 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003184 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003185 }
3186 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003187
John Zulauf5c5e88d2019-12-26 11:22:02 -07003188 return skip;
3189}
3190
3191void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3192 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3193 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003194 auto *cb_access_context = GetAccessContext(commandBuffer);
3195 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003196 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003197 auto *context = cb_access_context->GetCurrentAccessContext();
3198 assert(context);
3199
John Zulauf5c5e88d2019-12-26 11:22:02 -07003200 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003201 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003202
3203 for (uint32_t region = 0; region < regionCount; region++) {
3204 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003205 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003206 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003207 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003208 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003209 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003210 VkExtent3D dst_copy_extent =
3211 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003212 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003213 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003214 }
3215 }
3216}
3217
Jeff Leger178b1e52020-10-05 12:22:23 -04003218bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3219 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3220 bool skip = false;
3221 const auto *cb_access_context = GetAccessContext(commandBuffer);
3222 assert(cb_access_context);
3223 if (!cb_access_context) return skip;
3224
3225 const auto *context = cb_access_context->GetCurrentAccessContext();
3226 assert(context);
3227 if (!context) return skip;
3228
3229 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3230 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3231 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3232 const auto &copy_region = pCopyImageInfo->pRegions[region];
3233 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003234 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003235 copy_region.srcOffset, copy_region.extent);
3236 if (hazard.hazard) {
3237 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3238 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3239 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003240 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003241 }
3242 }
3243
3244 if (dst_image) {
3245 VkExtent3D dst_copy_extent =
3246 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003247 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003248 copy_region.dstOffset, dst_copy_extent);
3249 if (hazard.hazard) {
3250 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3251 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3252 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003253 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003254 }
3255 if (skip) break;
3256 }
3257 }
3258
3259 return skip;
3260}
3261
3262void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3263 auto *cb_access_context = GetAccessContext(commandBuffer);
3264 assert(cb_access_context);
3265 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3266 auto *context = cb_access_context->GetCurrentAccessContext();
3267 assert(context);
3268
3269 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3270 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3271
3272 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3273 const auto &copy_region = pCopyImageInfo->pRegions[region];
3274 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003275 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003276 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003277 }
3278 if (dst_image) {
3279 VkExtent3D dst_copy_extent =
3280 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003281 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003282 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003283 }
3284 }
3285}
3286
John Zulauf9cb530d2019-09-30 14:14:10 -06003287bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3288 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3289 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3290 uint32_t bufferMemoryBarrierCount,
3291 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3292 uint32_t imageMemoryBarrierCount,
3293 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3294 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003295 const auto *cb_access_context = GetAccessContext(commandBuffer);
3296 assert(cb_access_context);
3297 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003298
John Zulauf36ef9282021-02-02 11:47:24 -07003299 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3300 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3301 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3302 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003303 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003304 return skip;
3305}
3306
3307void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3308 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3309 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3310 uint32_t bufferMemoryBarrierCount,
3311 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3312 uint32_t imageMemoryBarrierCount,
3313 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003314 auto *cb_access_context = GetAccessContext(commandBuffer);
3315 assert(cb_access_context);
3316 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003317
John Zulauf36ef9282021-02-02 11:47:24 -07003318 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3319 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3320 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3321 pImageMemoryBarriers);
3322 pipeline_barrier.Record(cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003323}
3324
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003325bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3326 const VkDependencyInfoKHR *pDependencyInfo) const {
3327 bool skip = false;
3328 const auto *cb_access_context = GetAccessContext(commandBuffer);
3329 assert(cb_access_context);
3330 if (!cb_access_context) return skip;
3331
3332 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3333 skip = pipeline_barrier.Validate(*cb_access_context);
3334 return skip;
3335}
3336
3337void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3338 auto *cb_access_context = GetAccessContext(commandBuffer);
3339 assert(cb_access_context);
3340 if (!cb_access_context) return;
3341
3342 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3343 pipeline_barrier.Record(cb_access_context);
3344}
3345
John Zulauf9cb530d2019-09-30 14:14:10 -06003346void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3347 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3348 // The state tracker sets up the device state
3349 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3350
John Zulauf5f13a792020-03-10 07:31:21 -06003351 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3352 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003353 // TODO: Find a good way to do this hooklessly.
3354 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3355 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3356 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3357
John Zulaufd1f85d42020-04-15 12:23:15 -06003358 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3359 sync_device_state->ResetCommandBufferCallback(command_buffer);
3360 });
3361 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3362 sync_device_state->FreeCommandBufferCallback(command_buffer);
3363 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003364}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003365
John Zulauf355e49b2020-04-24 15:11:15 -06003366bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf64ffe552021-02-06 10:25:07 -07003367 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd, const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003368 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003369 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003370 if (cb_context) {
3371 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo, cmd_name);
3372 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003373 }
John Zulauf355e49b2020-04-24 15:11:15 -06003374 return skip;
3375}
3376
3377bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3378 VkSubpassContents contents) const {
3379 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003380 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003381 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003382 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003383 return skip;
3384}
3385
3386bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003387 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003388 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003389 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003390 return skip;
3391}
3392
John Zulauf64ffe552021-02-06 10:25:07 -07003393static const char *kBeginRenderPass2KhrName = "vkCmdBeginRenderPass2KHR";
John Zulauf355e49b2020-04-24 15:11:15 -06003394bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3395 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003396 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003397 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003398 skip |=
3399 ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2, kBeginRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003400 return skip;
3401}
3402
John Zulauf3d84f1b2020-03-09 13:33:25 -06003403void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3404 VkResult result) {
3405 // The state tracker sets up the command buffer state
3406 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3407
3408 // Create/initialize the structure that trackers accesses at the command buffer scope.
3409 auto cb_access_context = GetAccessContext(commandBuffer);
3410 assert(cb_access_context);
3411 cb_access_context->Reset();
3412}
3413
3414void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf64ffe552021-02-06 10:25:07 -07003415 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd, const char *cmd_name) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003416 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003417 if (cb_context) {
John Zulauf64ffe552021-02-06 10:25:07 -07003418 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo, cmd_name);
3419 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003420 }
3421}
3422
3423void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3424 VkSubpassContents contents) {
3425 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003426 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003427 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003428 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003429}
3430
3431void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3432 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3433 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003434 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003435}
3436
3437void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3438 const VkRenderPassBeginInfo *pRenderPassBegin,
3439 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3440 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003441 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2, kBeginRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003442}
3443
Mike Schuchardt2df08912020-12-15 16:28:09 -08003444bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf64ffe552021-02-06 10:25:07 -07003445 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd, const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003446 bool skip = false;
3447
3448 auto cb_context = GetAccessContext(commandBuffer);
3449 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003450 if (!cb_context) return skip;
3451 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo, cmd_name);
3452 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003453}
3454
3455bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3456 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003457 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003458 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003459 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003460 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3461 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003462 return skip;
3463}
3464
John Zulauf64ffe552021-02-06 10:25:07 -07003465static const char *kNextSubpass2KhrName = "vkCmdNextSubpass2KHR";
Mike Schuchardt2df08912020-12-15 16:28:09 -08003466bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3467 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003468 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003469 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2, kNextSubpass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003470 return skip;
3471}
3472
3473bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3474 const VkSubpassEndInfo *pSubpassEndInfo) const {
3475 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003476 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003477 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003478}
3479
3480void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf64ffe552021-02-06 10:25:07 -07003481 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd, const char *cmd_name) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003482 auto cb_context = GetAccessContext(commandBuffer);
3483 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003484 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003485
John Zulauf64ffe552021-02-06 10:25:07 -07003486 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo, cmd_name);
3487 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003488}
3489
3490void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3491 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003492 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003493 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003494 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003495}
3496
3497void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3498 const VkSubpassEndInfo *pSubpassEndInfo) {
3499 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003500 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003501}
3502
3503void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3504 const VkSubpassEndInfo *pSubpassEndInfo) {
3505 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003506 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2, kNextSubpass2KhrName);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003507}
3508
John Zulauf64ffe552021-02-06 10:25:07 -07003509bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd,
3510 const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003511 bool skip = false;
3512
3513 auto cb_context = GetAccessContext(commandBuffer);
3514 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003515 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003516
John Zulauf64ffe552021-02-06 10:25:07 -07003517 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo, cmd_name);
3518 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003519 return skip;
3520}
3521
3522bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3523 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003524 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003525 return skip;
3526}
3527
Mike Schuchardt2df08912020-12-15 16:28:09 -08003528bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003529 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003530 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003531 return skip;
3532}
3533
John Zulauf64ffe552021-02-06 10:25:07 -07003534const static char *kEndRenderPass2KhrName = "vkEndRenderPass2KHR";
John Zulauf355e49b2020-04-24 15:11:15 -06003535bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003536 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003537 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003538 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2, kEndRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003539 return skip;
3540}
3541
John Zulauf64ffe552021-02-06 10:25:07 -07003542void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd,
3543 const char *cmd_name) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003544 // Resolve the all subpass contexts to the command buffer contexts
3545 auto cb_context = GetAccessContext(commandBuffer);
3546 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003547 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003548
John Zulauf64ffe552021-02-06 10:25:07 -07003549 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo, cmd_name);
3550 sync_op.Record(cb_context);
3551 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003552}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003553
John Zulauf33fc1d52020-07-17 11:01:10 -06003554// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3555// updates to a resource which do not conflict at the byte level.
3556// TODO: Revisit this rule to see if it needs to be tighter or looser
3557// TODO: Add programatic control over suppression heuristics
3558bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3559 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3560}
3561
John Zulauf3d84f1b2020-03-09 13:33:25 -06003562void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003563 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003564 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003565}
3566
3567void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003568 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003569 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003570}
3571
3572void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf64ffe552021-02-06 10:25:07 -07003573 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2, kEndRenderPass2KhrName);
John Zulauf5a1a5382020-06-22 17:23:25 -06003574 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003575}
locke-lunarga19c71d2020-03-02 18:17:04 -07003576
Jeff Leger178b1e52020-10-05 12:22:23 -04003577template <typename BufferImageCopyRegionType>
3578bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3579 VkImageLayout dstImageLayout, uint32_t regionCount,
3580 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003581 bool skip = false;
3582 const auto *cb_access_context = GetAccessContext(commandBuffer);
3583 assert(cb_access_context);
3584 if (!cb_access_context) return skip;
3585
Jeff Leger178b1e52020-10-05 12:22:23 -04003586 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3587 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3588
locke-lunarga19c71d2020-03-02 18:17:04 -07003589 const auto *context = cb_access_context->GetCurrentAccessContext();
3590 assert(context);
3591 if (!context) return skip;
3592
3593 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003594 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3595
3596 for (uint32_t region = 0; region < regionCount; region++) {
3597 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003598 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003599 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003600 if (src_buffer) {
3601 ResourceAccessRange src_range =
3602 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003603 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07003604 if (hazard.hazard) {
3605 // PHASE1 TODO -- add tag information to log msg when useful.
3606 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3607 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3608 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003609 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003610 }
3611 }
3612
Jeremy Gebben40a22942020-12-22 14:22:06 -07003613 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07003614 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003615 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003616 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003617 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003618 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003619 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003620 }
3621 if (skip) break;
3622 }
3623 if (skip) break;
3624 }
3625 return skip;
3626}
3627
Jeff Leger178b1e52020-10-05 12:22:23 -04003628bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3629 VkImageLayout dstImageLayout, uint32_t regionCount,
3630 const VkBufferImageCopy *pRegions) const {
3631 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3632 COPY_COMMAND_VERSION_1);
3633}
3634
3635bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3636 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3637 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3638 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3639 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3640}
3641
3642template <typename BufferImageCopyRegionType>
3643void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3644 VkImageLayout dstImageLayout, uint32_t regionCount,
3645 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003646 auto *cb_access_context = GetAccessContext(commandBuffer);
3647 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003648
3649 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3650 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3651
3652 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003653 auto *context = cb_access_context->GetCurrentAccessContext();
3654 assert(context);
3655
3656 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003657 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003658
3659 for (uint32_t region = 0; region < regionCount; region++) {
3660 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07003661 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003662 if (src_buffer) {
3663 ResourceAccessRange src_range =
3664 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003665 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003666 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07003667 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003668 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003669 }
3670 }
3671}
3672
Jeff Leger178b1e52020-10-05 12:22:23 -04003673void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3674 VkImageLayout dstImageLayout, uint32_t regionCount,
3675 const VkBufferImageCopy *pRegions) {
3676 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3677 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3678}
3679
3680void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3681 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3682 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3683 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3684 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3685 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3686}
3687
3688template <typename BufferImageCopyRegionType>
3689bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3690 VkBuffer dstBuffer, uint32_t regionCount,
3691 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003692 bool skip = false;
3693 const auto *cb_access_context = GetAccessContext(commandBuffer);
3694 assert(cb_access_context);
3695 if (!cb_access_context) return skip;
3696
Jeff Leger178b1e52020-10-05 12:22:23 -04003697 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3698 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3699
locke-lunarga19c71d2020-03-02 18:17:04 -07003700 const auto *context = cb_access_context->GetCurrentAccessContext();
3701 assert(context);
3702 if (!context) return skip;
3703
3704 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3705 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003706 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem() : VK_NULL_HANDLE;
locke-lunarga19c71d2020-03-02 18:17:04 -07003707 for (uint32_t region = 0; region < regionCount; region++) {
3708 const auto &copy_region = pRegions[region];
3709 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003710 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003711 copy_region.imageOffset, copy_region.imageExtent);
3712 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003713 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003714 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003715 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003716 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003717 }
John Zulauf477700e2021-01-06 11:41:49 -07003718 if (dst_mem) {
3719 ResourceAccessRange dst_range =
3720 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003721 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07003722 if (hazard.hazard) {
3723 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3724 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3725 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003726 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003727 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003728 }
3729 }
3730 if (skip) break;
3731 }
3732 return skip;
3733}
3734
Jeff Leger178b1e52020-10-05 12:22:23 -04003735bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3736 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3737 const VkBufferImageCopy *pRegions) const {
3738 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3739 COPY_COMMAND_VERSION_1);
3740}
3741
3742bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3743 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3744 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3745 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3746 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3747}
3748
3749template <typename BufferImageCopyRegionType>
3750void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3751 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3752 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003753 auto *cb_access_context = GetAccessContext(commandBuffer);
3754 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003755
3756 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3757 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3758
3759 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003760 auto *context = cb_access_context->GetCurrentAccessContext();
3761 assert(context);
3762
3763 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003764 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003765 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 -06003766 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003767
3768 for (uint32_t region = 0; region < regionCount; region++) {
3769 const auto &copy_region = pRegions[region];
3770 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003771 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003772 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003773 if (dst_buffer) {
3774 ResourceAccessRange dst_range =
3775 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003776 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003777 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003778 }
3779 }
3780}
3781
Jeff Leger178b1e52020-10-05 12:22:23 -04003782void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3783 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3784 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3785 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3786}
3787
3788void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3789 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3790 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3791 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3792 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3793 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3794}
3795
3796template <typename RegionType>
3797bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3798 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3799 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003800 bool skip = false;
3801 const auto *cb_access_context = GetAccessContext(commandBuffer);
3802 assert(cb_access_context);
3803 if (!cb_access_context) return skip;
3804
3805 const auto *context = cb_access_context->GetCurrentAccessContext();
3806 assert(context);
3807 if (!context) return skip;
3808
3809 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3810 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3811
3812 for (uint32_t region = 0; region < regionCount; region++) {
3813 const auto &blit_region = pRegions[region];
3814 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003815 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3816 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3817 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3818 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3819 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3820 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003821 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003822 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003823 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003824 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003825 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003826 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003827 }
3828 }
3829
3830 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003831 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3832 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3833 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3834 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3835 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3836 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003837 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003838 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003839 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003840 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003841 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003842 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003843 }
3844 if (skip) break;
3845 }
3846 }
3847
3848 return skip;
3849}
3850
Jeff Leger178b1e52020-10-05 12:22:23 -04003851bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3852 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3853 const VkImageBlit *pRegions, VkFilter filter) const {
3854 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3855 "vkCmdBlitImage");
3856}
3857
3858bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3859 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3860 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3861 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3862 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3863}
3864
3865template <typename RegionType>
3866void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3867 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3868 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003869 auto *cb_access_context = GetAccessContext(commandBuffer);
3870 assert(cb_access_context);
3871 auto *context = cb_access_context->GetCurrentAccessContext();
3872 assert(context);
3873
3874 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003875 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003876
3877 for (uint32_t region = 0; region < regionCount; region++) {
3878 const auto &blit_region = pRegions[region];
3879 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003880 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3881 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3882 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3883 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3884 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3885 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003886 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003887 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003888 }
3889 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003890 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3891 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3892 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3893 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3894 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3895 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003896 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003897 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003898 }
3899 }
3900}
locke-lunarg36ba2592020-04-03 09:42:04 -06003901
Jeff Leger178b1e52020-10-05 12:22:23 -04003902void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3903 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3904 const VkImageBlit *pRegions, VkFilter filter) {
3905 auto *cb_access_context = GetAccessContext(commandBuffer);
3906 assert(cb_access_context);
3907 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3908 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3909 pRegions, filter);
3910 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3911}
3912
3913void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3914 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3915 auto *cb_access_context = GetAccessContext(commandBuffer);
3916 assert(cb_access_context);
3917 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3918 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3919 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3920 pBlitImageInfo->filter, tag);
3921}
3922
John Zulauffaea0ee2021-01-14 14:01:32 -07003923bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
3924 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
3925 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
3926 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003927 bool skip = false;
3928 if (drawCount == 0) return skip;
3929
3930 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3931 VkDeviceSize size = struct_size;
3932 if (drawCount == 1 || stride == size) {
3933 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003934 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003935 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3936 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003937 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003938 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003939 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003940 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003941 }
3942 } else {
3943 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003944 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003945 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3946 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003947 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003948 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3949 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003950 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003951 break;
3952 }
3953 }
3954 }
3955 return skip;
3956}
3957
locke-lunarg61870c22020-06-09 14:51:50 -06003958void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3959 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3960 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003961 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3962 VkDeviceSize size = struct_size;
3963 if (drawCount == 1 || stride == size) {
3964 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003965 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003966 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003967 } else {
3968 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003969 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003970 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
3971 tag);
locke-lunargff255f92020-05-13 18:53:52 -06003972 }
3973 }
3974}
3975
John Zulauffaea0ee2021-01-14 14:01:32 -07003976bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
3977 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3978 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003979 bool skip = false;
3980
3981 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003982 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003983 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3984 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003985 skip |= LogError(count_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003986 "%s: Hazard %s for countBuffer %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 return skip;
3991}
3992
locke-lunarg61870c22020-06-09 14:51:50 -06003993void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003994 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003995 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003996 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003997}
3998
locke-lunarg36ba2592020-04-03 09:42:04 -06003999bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004000 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004001 const auto *cb_access_context = GetAccessContext(commandBuffer);
4002 assert(cb_access_context);
4003 if (!cb_access_context) return skip;
4004
locke-lunarg61870c22020-06-09 14:51:50 -06004005 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004006 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004007}
4008
4009void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004010 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004011 auto *cb_access_context = GetAccessContext(commandBuffer);
4012 assert(cb_access_context);
4013 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004014
locke-lunarg61870c22020-06-09 14:51:50 -06004015 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004016}
locke-lunarge1a67022020-04-29 00:15:36 -06004017
4018bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004019 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004020 const auto *cb_access_context = GetAccessContext(commandBuffer);
4021 assert(cb_access_context);
4022 if (!cb_access_context) return skip;
4023
4024 const auto *context = cb_access_context->GetCurrentAccessContext();
4025 assert(context);
4026 if (!context) return skip;
4027
locke-lunarg61870c22020-06-09 14:51:50 -06004028 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004029 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4030 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004031 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004032}
4033
4034void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004035 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004036 auto *cb_access_context = GetAccessContext(commandBuffer);
4037 assert(cb_access_context);
4038 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4039 auto *context = cb_access_context->GetCurrentAccessContext();
4040 assert(context);
4041
locke-lunarg61870c22020-06-09 14:51:50 -06004042 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4043 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004044}
4045
4046bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4047 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -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_GRAPHICS, "vkCmdDraw");
4054 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4055 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004056 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004057}
4058
4059void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4060 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004061 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004062 auto *cb_access_context = GetAccessContext(commandBuffer);
4063 assert(cb_access_context);
4064 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004065
locke-lunarg61870c22020-06-09 14:51:50 -06004066 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4067 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4068 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004069}
4070
4071bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4072 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004073 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004074 const auto *cb_access_context = GetAccessContext(commandBuffer);
4075 assert(cb_access_context);
4076 if (!cb_access_context) return skip;
4077
locke-lunarg61870c22020-06-09 14:51:50 -06004078 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4079 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4080 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004081 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004082}
4083
4084void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4085 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004086 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004087 auto *cb_access_context = GetAccessContext(commandBuffer);
4088 assert(cb_access_context);
4089 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004090
locke-lunarg61870c22020-06-09 14:51:50 -06004091 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4092 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4093 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004094}
4095
4096bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4097 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004098 bool skip = false;
4099 if (drawCount == 0) return skip;
4100
locke-lunargff255f92020-05-13 18:53:52 -06004101 const auto *cb_access_context = GetAccessContext(commandBuffer);
4102 assert(cb_access_context);
4103 if (!cb_access_context) return skip;
4104
4105 const auto *context = cb_access_context->GetCurrentAccessContext();
4106 assert(context);
4107 if (!context) return skip;
4108
locke-lunarg61870c22020-06-09 14:51:50 -06004109 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4110 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004111 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4112 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004113
4114 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4115 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4116 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004117 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004118 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004119}
4120
4121void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4122 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004123 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004124 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004125 auto *cb_access_context = GetAccessContext(commandBuffer);
4126 assert(cb_access_context);
4127 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4128 auto *context = cb_access_context->GetCurrentAccessContext();
4129 assert(context);
4130
locke-lunarg61870c22020-06-09 14:51:50 -06004131 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4132 cb_access_context->RecordDrawSubpassAttachment(tag);
4133 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004134
4135 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4136 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4137 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004138 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004139}
4140
4141bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4142 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004143 bool skip = false;
4144 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004145 const auto *cb_access_context = GetAccessContext(commandBuffer);
4146 assert(cb_access_context);
4147 if (!cb_access_context) return skip;
4148
4149 const auto *context = cb_access_context->GetCurrentAccessContext();
4150 assert(context);
4151 if (!context) return skip;
4152
locke-lunarg61870c22020-06-09 14:51:50 -06004153 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4154 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004155 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4156 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004157
4158 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4159 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4160 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004161 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004162 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004163}
4164
4165void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4166 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004167 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004168 auto *cb_access_context = GetAccessContext(commandBuffer);
4169 assert(cb_access_context);
4170 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4171 auto *context = cb_access_context->GetCurrentAccessContext();
4172 assert(context);
4173
locke-lunarg61870c22020-06-09 14:51:50 -06004174 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4175 cb_access_context->RecordDrawSubpassAttachment(tag);
4176 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004177
4178 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4179 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4180 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004181 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004182}
4183
4184bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4185 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4186 uint32_t stride, const char *function) const {
4187 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004188 const auto *cb_access_context = GetAccessContext(commandBuffer);
4189 assert(cb_access_context);
4190 if (!cb_access_context) return skip;
4191
4192 const auto *context = cb_access_context->GetCurrentAccessContext();
4193 assert(context);
4194 if (!context) return skip;
4195
locke-lunarg61870c22020-06-09 14:51:50 -06004196 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4197 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004198 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4199 maxDrawCount, stride, function);
4200 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004201
4202 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4203 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4204 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004205 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004206 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004207}
4208
4209bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4210 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4211 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004212 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4213 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004214}
4215
4216void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4217 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4218 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004219 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4220 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004221 auto *cb_access_context = GetAccessContext(commandBuffer);
4222 assert(cb_access_context);
4223 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
4224 auto *context = cb_access_context->GetCurrentAccessContext();
4225 assert(context);
4226
locke-lunarg61870c22020-06-09 14:51:50 -06004227 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4228 cb_access_context->RecordDrawSubpassAttachment(tag);
4229 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4230 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004231
4232 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4233 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4234 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004235 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004236}
4237
4238bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4239 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4240 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004241 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4242 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004243}
4244
4245void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4246 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4247 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004248 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4249 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004250 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004251}
4252
4253bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4254 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4255 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004256 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4257 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004258}
4259
4260void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4261 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4262 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004263 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4264 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004265 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4266}
4267
4268bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4269 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4270 uint32_t stride, const char *function) const {
4271 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004272 const auto *cb_access_context = GetAccessContext(commandBuffer);
4273 assert(cb_access_context);
4274 if (!cb_access_context) return skip;
4275
4276 const auto *context = cb_access_context->GetCurrentAccessContext();
4277 assert(context);
4278 if (!context) return skip;
4279
locke-lunarg61870c22020-06-09 14:51:50 -06004280 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4281 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004282 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4283 offset, maxDrawCount, stride, function);
4284 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004285
4286 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4287 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4288 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004289 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004290 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004291}
4292
4293bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4294 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4295 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004296 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4297 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004298}
4299
4300void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4301 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4302 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004303 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4304 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004305 auto *cb_access_context = GetAccessContext(commandBuffer);
4306 assert(cb_access_context);
4307 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4308 auto *context = cb_access_context->GetCurrentAccessContext();
4309 assert(context);
4310
locke-lunarg61870c22020-06-09 14:51:50 -06004311 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4312 cb_access_context->RecordDrawSubpassAttachment(tag);
4313 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4314 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004315
4316 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4317 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004318 // We will update the index and vertex buffer in SubmitQueue in the future.
4319 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004320}
4321
4322bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4323 VkDeviceSize offset, VkBuffer countBuffer,
4324 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4325 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004326 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4327 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004328}
4329
4330void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4331 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4332 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004333 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4334 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004335 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4336}
4337
4338bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4339 VkDeviceSize offset, VkBuffer countBuffer,
4340 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4341 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004342 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4343 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004344}
4345
4346void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4347 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4348 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004349 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4350 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004351 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4352}
4353
4354bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4355 const VkClearColorValue *pColor, uint32_t rangeCount,
4356 const VkImageSubresourceRange *pRanges) const {
4357 bool skip = false;
4358 const auto *cb_access_context = GetAccessContext(commandBuffer);
4359 assert(cb_access_context);
4360 if (!cb_access_context) return skip;
4361
4362 const auto *context = cb_access_context->GetCurrentAccessContext();
4363 assert(context);
4364 if (!context) return skip;
4365
4366 const auto *image_state = Get<IMAGE_STATE>(image);
4367
4368 for (uint32_t index = 0; index < rangeCount; index++) {
4369 const auto &range = pRanges[index];
4370 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004371 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004372 if (hazard.hazard) {
4373 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004374 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004375 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004376 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004377 }
4378 }
4379 }
4380 return skip;
4381}
4382
4383void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4384 const VkClearColorValue *pColor, uint32_t rangeCount,
4385 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004386 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004387 auto *cb_access_context = GetAccessContext(commandBuffer);
4388 assert(cb_access_context);
4389 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4390 auto *context = cb_access_context->GetCurrentAccessContext();
4391 assert(context);
4392
4393 const auto *image_state = Get<IMAGE_STATE>(image);
4394
4395 for (uint32_t index = 0; index < rangeCount; index++) {
4396 const auto &range = pRanges[index];
4397 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004398 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004399 }
4400 }
4401}
4402
4403bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4404 VkImageLayout imageLayout,
4405 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4406 const VkImageSubresourceRange *pRanges) const {
4407 bool skip = false;
4408 const auto *cb_access_context = GetAccessContext(commandBuffer);
4409 assert(cb_access_context);
4410 if (!cb_access_context) return skip;
4411
4412 const auto *context = cb_access_context->GetCurrentAccessContext();
4413 assert(context);
4414 if (!context) return skip;
4415
4416 const auto *image_state = Get<IMAGE_STATE>(image);
4417
4418 for (uint32_t index = 0; index < rangeCount; index++) {
4419 const auto &range = pRanges[index];
4420 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004421 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004422 if (hazard.hazard) {
4423 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004424 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004425 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004426 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004427 }
4428 }
4429 }
4430 return skip;
4431}
4432
4433void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4434 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4435 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004436 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004437 auto *cb_access_context = GetAccessContext(commandBuffer);
4438 assert(cb_access_context);
4439 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4440 auto *context = cb_access_context->GetCurrentAccessContext();
4441 assert(context);
4442
4443 const auto *image_state = Get<IMAGE_STATE>(image);
4444
4445 for (uint32_t index = 0; index < rangeCount; index++) {
4446 const auto &range = pRanges[index];
4447 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004448 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004449 }
4450 }
4451}
4452
4453bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4454 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4455 VkDeviceSize dstOffset, VkDeviceSize stride,
4456 VkQueryResultFlags flags) 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 *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4467
4468 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004469 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004470 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004471 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004472 skip |=
4473 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4474 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004475 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004476 }
4477 }
locke-lunargff255f92020-05-13 18:53:52 -06004478
4479 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004480 return skip;
4481}
4482
4483void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4484 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4485 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004486 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4487 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004488 auto *cb_access_context = GetAccessContext(commandBuffer);
4489 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004490 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004491 auto *context = cb_access_context->GetCurrentAccessContext();
4492 assert(context);
4493
4494 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4495
4496 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004497 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004498 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004499 }
locke-lunargff255f92020-05-13 18:53:52 -06004500
4501 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004502}
4503
4504bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4505 VkDeviceSize size, uint32_t data) const {
4506 bool skip = false;
4507 const auto *cb_access_context = GetAccessContext(commandBuffer);
4508 assert(cb_access_context);
4509 if (!cb_access_context) return skip;
4510
4511 const auto *context = cb_access_context->GetCurrentAccessContext();
4512 assert(context);
4513 if (!context) return skip;
4514
4515 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4516
4517 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004518 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004519 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004520 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004521 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004522 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004523 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004524 }
4525 }
4526 return skip;
4527}
4528
4529void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4530 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004531 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004532 auto *cb_access_context = GetAccessContext(commandBuffer);
4533 assert(cb_access_context);
4534 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4535 auto *context = cb_access_context->GetCurrentAccessContext();
4536 assert(context);
4537
4538 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4539
4540 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004541 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004542 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004543 }
4544}
4545
4546bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4547 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4548 const VkImageResolve *pRegions) const {
4549 bool skip = false;
4550 const auto *cb_access_context = GetAccessContext(commandBuffer);
4551 assert(cb_access_context);
4552 if (!cb_access_context) return skip;
4553
4554 const auto *context = cb_access_context->GetCurrentAccessContext();
4555 assert(context);
4556 if (!context) return skip;
4557
4558 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4559 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4560
4561 for (uint32_t region = 0; region < regionCount; region++) {
4562 const auto &resolve_region = pRegions[region];
4563 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004564 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004565 resolve_region.srcOffset, resolve_region.extent);
4566 if (hazard.hazard) {
4567 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004568 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004569 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004570 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004571 }
4572 }
4573
4574 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004575 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004576 resolve_region.dstOffset, resolve_region.extent);
4577 if (hazard.hazard) {
4578 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004579 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004580 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004581 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004582 }
4583 if (skip) break;
4584 }
4585 }
4586
4587 return skip;
4588}
4589
4590void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4591 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4592 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004593 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4594 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004595 auto *cb_access_context = GetAccessContext(commandBuffer);
4596 assert(cb_access_context);
4597 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4598 auto *context = cb_access_context->GetCurrentAccessContext();
4599 assert(context);
4600
4601 auto *src_image = Get<IMAGE_STATE>(srcImage);
4602 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4603
4604 for (uint32_t region = 0; region < regionCount; region++) {
4605 const auto &resolve_region = pRegions[region];
4606 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004607 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004608 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004609 }
4610 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004611 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004612 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004613 }
4614 }
4615}
4616
Jeff Leger178b1e52020-10-05 12:22:23 -04004617bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4618 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4619 bool skip = false;
4620 const auto *cb_access_context = GetAccessContext(commandBuffer);
4621 assert(cb_access_context);
4622 if (!cb_access_context) return skip;
4623
4624 const auto *context = cb_access_context->GetCurrentAccessContext();
4625 assert(context);
4626 if (!context) return skip;
4627
4628 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4629 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4630
4631 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4632 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4633 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004634 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004635 resolve_region.srcOffset, resolve_region.extent);
4636 if (hazard.hazard) {
4637 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4638 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4639 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004640 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004641 }
4642 }
4643
4644 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004645 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004646 resolve_region.dstOffset, resolve_region.extent);
4647 if (hazard.hazard) {
4648 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4649 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4650 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004651 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004652 }
4653 if (skip) break;
4654 }
4655 }
4656
4657 return skip;
4658}
4659
4660void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4661 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4662 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4663 auto *cb_access_context = GetAccessContext(commandBuffer);
4664 assert(cb_access_context);
4665 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4666 auto *context = cb_access_context->GetCurrentAccessContext();
4667 assert(context);
4668
4669 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4670 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4671
4672 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4673 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4674 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004675 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004676 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004677 }
4678 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004679 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004680 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004681 }
4682 }
4683}
4684
locke-lunarge1a67022020-04-29 00:15:36 -06004685bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4686 VkDeviceSize dataSize, const void *pData) const {
4687 bool skip = false;
4688 const auto *cb_access_context = GetAccessContext(commandBuffer);
4689 assert(cb_access_context);
4690 if (!cb_access_context) return skip;
4691
4692 const auto *context = cb_access_context->GetCurrentAccessContext();
4693 assert(context);
4694 if (!context) return skip;
4695
4696 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4697
4698 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004699 // VK_WHOLE_SIZE not allowed
4700 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004701 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004702 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004703 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004704 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004705 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004706 }
4707 }
4708 return skip;
4709}
4710
4711void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4712 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004713 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004714 auto *cb_access_context = GetAccessContext(commandBuffer);
4715 assert(cb_access_context);
4716 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4717 auto *context = cb_access_context->GetCurrentAccessContext();
4718 assert(context);
4719
4720 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4721
4722 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004723 // VK_WHOLE_SIZE not allowed
4724 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004725 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004726 }
4727}
locke-lunargff255f92020-05-13 18:53:52 -06004728
4729bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4730 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4731 bool skip = false;
4732 const auto *cb_access_context = GetAccessContext(commandBuffer);
4733 assert(cb_access_context);
4734 if (!cb_access_context) return skip;
4735
4736 const auto *context = cb_access_context->GetCurrentAccessContext();
4737 assert(context);
4738 if (!context) return skip;
4739
4740 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4741
4742 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004743 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004744 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06004745 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004746 skip |=
4747 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4748 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004749 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004750 }
4751 }
4752 return skip;
4753}
4754
4755void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4756 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004757 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004758 auto *cb_access_context = GetAccessContext(commandBuffer);
4759 assert(cb_access_context);
4760 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4761 auto *context = cb_access_context->GetCurrentAccessContext();
4762 assert(context);
4763
4764 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4765
4766 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004767 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004768 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004769 }
4770}
John Zulauf49beb112020-11-04 16:06:31 -07004771
4772bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
4773 bool skip = false;
4774 const auto *cb_context = GetAccessContext(commandBuffer);
4775 assert(cb_context);
4776 if (!cb_context) return skip;
4777
John Zulauf36ef9282021-02-02 11:47:24 -07004778 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004779 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004780}
4781
4782void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4783 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
4784 auto *cb_context = GetAccessContext(commandBuffer);
4785 assert(cb_context);
4786 if (!cb_context) return;
John Zulauf36ef9282021-02-02 11:47:24 -07004787 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4788 set_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004789}
4790
John Zulauf4edde622021-02-15 08:54:50 -07004791bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4792 const VkDependencyInfoKHR *pDependencyInfo) const {
4793 bool skip = false;
4794 const auto *cb_context = GetAccessContext(commandBuffer);
4795 assert(cb_context);
4796 if (!cb_context || !pDependencyInfo) return skip;
4797
4798 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4799 return set_event_op.Validate(*cb_context);
4800}
4801
4802void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4803 const VkDependencyInfoKHR *pDependencyInfo) {
4804 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
4805 auto *cb_context = GetAccessContext(commandBuffer);
4806 assert(cb_context);
4807 if (!cb_context || !pDependencyInfo) return;
4808
4809 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4810 set_event_op.Record(cb_context);
4811}
4812
John Zulauf49beb112020-11-04 16:06:31 -07004813bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
4814 VkPipelineStageFlags stageMask) const {
4815 bool skip = false;
4816 const auto *cb_context = GetAccessContext(commandBuffer);
4817 assert(cb_context);
4818 if (!cb_context) return skip;
4819
John Zulauf36ef9282021-02-02 11:47:24 -07004820 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004821 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004822}
4823
4824void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4825 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
4826 auto *cb_context = GetAccessContext(commandBuffer);
4827 assert(cb_context);
4828 if (!cb_context) return;
4829
John Zulauf36ef9282021-02-02 11:47:24 -07004830 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4831 reset_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004832}
4833
John Zulauf4edde622021-02-15 08:54:50 -07004834bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4835 VkPipelineStageFlags2KHR stageMask) const {
4836 bool skip = false;
4837 const auto *cb_context = GetAccessContext(commandBuffer);
4838 assert(cb_context);
4839 if (!cb_context) return skip;
4840
4841 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4842 return reset_event_op.Validate(*cb_context);
4843}
4844
4845void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4846 VkPipelineStageFlags2KHR stageMask) {
4847 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
4848 auto *cb_context = GetAccessContext(commandBuffer);
4849 assert(cb_context);
4850 if (!cb_context) return;
4851
4852 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4853 reset_event_op.Record(cb_context);
4854}
4855
John Zulauf49beb112020-11-04 16:06:31 -07004856bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4857 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4858 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4859 uint32_t bufferMemoryBarrierCount,
4860 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4861 uint32_t imageMemoryBarrierCount,
4862 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
4863 bool skip = false;
4864 const auto *cb_context = GetAccessContext(commandBuffer);
4865 assert(cb_context);
4866 if (!cb_context) return skip;
4867
John Zulauf36ef9282021-02-02 11:47:24 -07004868 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4869 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4870 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07004871 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004872}
4873
4874void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4875 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4876 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4877 uint32_t bufferMemoryBarrierCount,
4878 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4879 uint32_t imageMemoryBarrierCount,
4880 const VkImageMemoryBarrier *pImageMemoryBarriers) {
4881 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
4882 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
4883 imageMemoryBarrierCount, pImageMemoryBarriers);
4884
4885 auto *cb_context = GetAccessContext(commandBuffer);
4886 assert(cb_context);
4887 if (!cb_context) return;
4888
John Zulauf36ef9282021-02-02 11:47:24 -07004889 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4890 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4891 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
4892 return wait_events_op.Record(cb_context);
John Zulauf4a6105a2020-11-17 15:11:05 -07004893}
4894
John Zulauf4edde622021-02-15 08:54:50 -07004895bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4896 const VkDependencyInfoKHR *pDependencyInfos) const {
4897 bool skip = false;
4898 const auto *cb_context = GetAccessContext(commandBuffer);
4899 assert(cb_context);
4900 if (!cb_context) return skip;
4901
4902 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
4903 skip |= wait_events_op.Validate(*cb_context);
4904 return skip;
4905}
4906
4907void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4908 const VkDependencyInfoKHR *pDependencyInfos) {
4909 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
4910
4911 auto *cb_context = GetAccessContext(commandBuffer);
4912 assert(cb_context);
4913 if (!cb_context) return;
4914
4915 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
4916 wait_events_op.Record(cb_context);
4917}
4918
John Zulauf4a6105a2020-11-17 15:11:05 -07004919void SyncEventState::ResetFirstScope() {
4920 for (const auto address_type : kAddressTypes) {
4921 first_scope[static_cast<size_t>(address_type)].clear();
4922 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07004923 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07004924}
4925
4926// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07004927SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07004928 IgnoreReason reason = NotIgnored;
4929
John Zulauf4edde622021-02-15 08:54:50 -07004930 if ((CMD_WAITEVENTS2KHR == cmd) && (CMD_SETEVENT == last_command)) {
4931 reason = SetVsWait2;
4932 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
4933 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07004934 } else if (unsynchronized_set) {
4935 reason = SetRace;
4936 } else {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004937 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07004938 if (missing_bits) reason = MissingStageBits;
4939 }
4940
4941 return reason;
4942}
4943
Jeremy Gebben40a22942020-12-22 14:22:06 -07004944bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07004945 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
4946 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
4947 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07004948}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004949
John Zulauf36ef9282021-02-02 11:47:24 -07004950SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
4951 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4952 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07004953 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
4954 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
4955 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07004956 : SyncOpBase(cmd), barriers_(1) {
4957 auto &barrier_set = barriers_[0];
4958 barrier_set.dependency_flags = dependencyFlags;
4959 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
4960 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004961 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07004962 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
4963 pMemoryBarriers);
4964 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
4965 bufferMemoryBarrierCount, pBufferMemoryBarriers);
4966 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
4967 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004968}
4969
John Zulauf4edde622021-02-15 08:54:50 -07004970SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
4971 const VkDependencyInfoKHR *dep_infos)
4972 : SyncOpBase(cmd), barriers_(event_count) {
4973 for (uint32_t i = 0; i < event_count; i++) {
4974 const auto &dep_info = dep_infos[i];
4975 auto &barrier_set = barriers_[i];
4976 barrier_set.dependency_flags = dep_info.dependencyFlags;
4977 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
4978 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
4979 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
4980 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
4981 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
4982 dep_info.pMemoryBarriers);
4983 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
4984 dep_info.pBufferMemoryBarriers);
4985 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
4986 dep_info.pImageMemoryBarriers);
4987 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07004988}
4989
John Zulauf36ef9282021-02-02 11:47:24 -07004990SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07004991 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4992 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
4993 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
4994 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
4995 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07004996 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07004997 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
4998
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07004999SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5000 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005001 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005002
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005003bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5004 bool skip = false;
5005 const auto *context = cb_context.GetCurrentAccessContext();
5006 assert(context);
5007 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005008 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5009
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005010 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005011 const auto &barrier_set = barriers_[0];
5012 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5013 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5014 const auto *image_state = image_barrier.image.get();
5015 if (!image_state) continue;
5016 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5017 if (hazard.hazard) {
5018 // PHASE1 TODO -- add tag information to log msg when useful.
5019 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005020 const auto image_handle = image_state->image();
John Zulauf6fdf3d02021-03-05 16:50:47 -07005021 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5022 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5023 string_SyncHazard(hazard.hazard), image_barrier.index,
5024 sync_state.report_data->FormatHandle(image_handle).c_str(),
5025 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005026 }
5027 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005028 return skip;
5029}
5030
John Zulaufd5115702021-01-18 12:34:33 -07005031struct SyncOpPipelineBarrierFunctorFactory {
5032 using BarrierOpFunctor = PipelineBarrierOp;
5033 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5034 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5035 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5036 using BufferRange = ResourceAccessRange;
5037 using ImageRange = subresource_adapter::ImageRangeGenerator;
5038 using GlobalRange = ResourceAccessRange;
5039
5040 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5041 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5042 }
5043 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
5044 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5045 }
5046 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5047 return GlobalBarrierOpFunctor(barrier, false);
5048 }
5049
5050 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5051 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5052 const auto base_address = ResourceBaseAddress(buffer);
5053 return (range + base_address);
5054 }
John Zulauf110413c2021-03-20 05:38:38 -06005055 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005056 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005057
5058 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005059 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005060 return range_gen;
5061 }
5062 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5063};
5064
5065template <typename Barriers, typename FunctorFactory>
5066void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
5067 AccessContext *context) {
5068 for (const auto &barrier : barriers) {
5069 const auto *state = barrier.GetState();
5070 if (state) {
5071 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5072 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5073 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5074 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5075 }
5076 }
5077}
5078
5079template <typename Barriers, typename FunctorFactory>
5080void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
5081 AccessContext *access_context) {
5082 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5083 for (const auto &barrier : barriers) {
5084 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5085 }
5086 for (const auto address_type : kAddressTypes) {
5087 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5088 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5089 }
5090}
5091
John Zulauf36ef9282021-02-02 11:47:24 -07005092void SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005093 SyncOpPipelineBarrierFunctorFactory factory;
5094 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005095 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005096
John Zulauf4edde622021-02-15 08:54:50 -07005097 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5098 assert(barriers_.size() == 1);
5099 const auto &barrier_set = barriers_[0];
5100 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5101 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5102 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
5103
5104 if (barrier_set.single_exec_scope) {
5105 cb_context->ApplyGlobalBarriersToEvents(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
5106 } else {
5107 for (const auto &barrier : barrier_set.memory_barriers) {
5108 cb_context->ApplyGlobalBarriersToEvents(barrier.src_exec_scope, barrier.dst_exec_scope);
5109 }
5110 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005111}
5112
John Zulauf4edde622021-02-15 08:54:50 -07005113void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5114 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5115 const VkMemoryBarrier *barriers) {
5116 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005117 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005118 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005119 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005120 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005121 }
5122 if (0 == memory_barrier_count) {
5123 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005124 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005125 }
John Zulauf4edde622021-02-15 08:54:50 -07005126 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005127}
5128
John Zulauf4edde622021-02-15 08:54:50 -07005129void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5130 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5131 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5132 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005133 for (uint32_t index = 0; index < barrier_count; index++) {
5134 const auto &barrier = barriers[index];
5135 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5136 if (buffer) {
5137 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5138 const auto range = MakeRange(barrier.offset, barrier_size);
5139 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005140 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005141 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005142 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005143 }
5144 }
5145}
5146
John Zulauf4edde622021-02-15 08:54:50 -07005147void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
5148 uint32_t memory_barrier_count, const VkMemoryBarrier2KHR *barriers) {
5149 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005150 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005151 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005152 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5153 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5154 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005155 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005156 }
John Zulauf4edde622021-02-15 08:54:50 -07005157 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005158}
5159
John Zulauf4edde622021-02-15 08:54:50 -07005160void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5161 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5162 const VkBufferMemoryBarrier2KHR *barriers) {
5163 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005164 for (uint32_t index = 0; index < barrier_count; index++) {
5165 const auto &barrier = barriers[index];
5166 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5167 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5168 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5169 if (buffer) {
5170 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5171 const auto range = MakeRange(barrier.offset, barrier_size);
5172 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005173 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005174 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005175 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005176 }
5177 }
5178}
5179
John Zulauf4edde622021-02-15 08:54:50 -07005180void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5181 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5182 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5183 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005184 for (uint32_t index = 0; index < barrier_count; index++) {
5185 const auto &barrier = barriers[index];
5186 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5187 if (image) {
5188 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5189 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005190 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005191 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005192 image_memory_barriers.emplace_back();
5193 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005194 }
5195 }
5196}
John Zulaufd5115702021-01-18 12:34:33 -07005197
John Zulauf4edde622021-02-15 08:54:50 -07005198void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5199 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5200 const VkImageMemoryBarrier2KHR *barriers) {
5201 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005202 for (uint32_t index = 0; index < barrier_count; index++) {
5203 const auto &barrier = barriers[index];
5204 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5205 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5206 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5207 if (image) {
5208 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5209 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005210 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005211 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005212 image_memory_barriers.emplace_back();
5213 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005214 }
5215 }
5216}
5217
John Zulauf36ef9282021-02-02 11:47:24 -07005218SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005219 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5220 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5221 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5222 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005223 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005224 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5225 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005226 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005227}
5228
John Zulauf4edde622021-02-15 08:54:50 -07005229SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5230 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5231 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5232 MakeEventsList(sync_state, eventCount, pEvents);
5233 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5234}
5235
John Zulaufd5115702021-01-18 12:34:33 -07005236bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005237 const char *const ignored = "Wait operation is ignored for this event.";
5238 bool skip = false;
5239 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005240 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005241
John Zulauf4edde622021-02-15 08:54:50 -07005242 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5243 const auto &barrier_set = barriers_[barrier_set_index];
5244 if (barrier_set.single_exec_scope) {
5245 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5246 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5247 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5248 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5249 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5250 } else {
5251 const auto &barriers = barrier_set.memory_barriers;
5252 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5253 const auto &barrier = barriers[barrier_index];
5254 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5255 const std::string vuid =
5256 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5257 skip =
5258 sync_state.LogInfo(command_buffer_handle, vuid,
5259 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5260 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5261 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5262 }
5263 }
5264 }
5265 }
John Zulaufd5115702021-01-18 12:34:33 -07005266 }
5267
Jeremy Gebben40a22942020-12-22 14:22:06 -07005268 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005269 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005270 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005271 const auto *events_context = cb_context.GetCurrentEventsContext();
5272 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005273 size_t barrier_set_index = 0;
5274 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5275 for (size_t event_index = 0; event_index < events_.size(); event_index++)
5276 for (const auto &event : events_) {
5277 const auto *sync_event = events_context->Get(event.get());
5278 const auto &barrier_set = barriers_[barrier_set_index];
5279 if (!sync_event) {
5280 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5281 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5282 // new validation error... wait without previously submitted set event...
5283 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives at *record time*
5284 barrier_set_index += barrier_set_incr;
5285 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005286 }
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005287 const auto event_handle = sync_event->event->event();
John Zulauf4edde622021-02-15 08:54:50 -07005288 // TODO add "destroyed" checks
5289
5290 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5291 const auto &src_exec_scope = barrier_set.src_exec_scope;
5292 event_stage_masks |= sync_event->scope.mask_param;
5293 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5294 if (ignore_reason) {
5295 switch (ignore_reason) {
5296 case SyncEventState::ResetWaitRace:
5297 case SyncEventState::Reset2WaitRace: {
5298 // Four permuations of Reset and Wait calls...
5299 const char *vuid =
5300 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5301 if (ignore_reason == SyncEventState::Reset2WaitRace) {
5302 vuid =
Jeremy Gebben476f5e22021-03-01 15:27:20 -07005303 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2KHR-event-03831" : "VUID-vkCmdResetEvent2KHR-event-03832";
John Zulauf4edde622021-02-15 08:54:50 -07005304 }
5305 const char *const message =
5306 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5307 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5308 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
5309 CommandTypeString(sync_event->last_command), ignored);
5310 break;
5311 }
5312 case SyncEventState::SetRace: {
5313 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5314 // this event
5315 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5316 const char *const message =
5317 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5318 const char *const reason = "First synchronization scope is undefined.";
5319 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5320 sync_state.report_data->FormatHandle(event_handle).c_str(),
5321 CommandTypeString(sync_event->last_command), reason, ignored);
5322 break;
5323 }
5324 case SyncEventState::MissingStageBits: {
5325 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5326 // Issue error message that event waited for is not in wait events scope
5327 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5328 const char *const message =
5329 "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5330 ". Bits missing from srcStageMask %s. %s";
5331 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5332 sync_state.report_data->FormatHandle(event_handle).c_str(),
5333 sync_event->scope.mask_param, src_exec_scope.mask_param,
5334 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), ignored);
5335 break;
5336 }
5337 case SyncEventState::SetVsWait2: {
5338 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2KHR-pEvents-03837",
5339 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5340 sync_state.report_data->FormatHandle(event_handle).c_str(),
5341 CommandTypeString(sync_event->last_command));
5342 break;
5343 }
5344 default:
5345 assert(ignore_reason == SyncEventState::NotIgnored);
5346 }
5347 } else if (barrier_set.image_memory_barriers.size()) {
5348 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5349 const auto *context = cb_context.GetCurrentAccessContext();
5350 assert(context);
5351 for (const auto &image_memory_barrier : image_memory_barriers) {
5352 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5353 const auto *image_state = image_memory_barrier.image.get();
5354 if (!image_state) continue;
John Zulauf110413c2021-03-20 05:38:38 -06005355 const auto &subresource_range = image_memory_barrier.range;
John Zulauf4edde622021-02-15 08:54:50 -07005356 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5357 const auto hazard =
5358 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5359 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5360 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005361 skip |= sync_state.LogError(image_state->image(), string_SyncHazardVUID(hazard.hazard),
John Zulauf4edde622021-02-15 08:54:50 -07005362 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5363 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005364 sync_state.report_data->FormatHandle(image_state->image()).c_str(),
John Zulauf4edde622021-02-15 08:54:50 -07005365 cb_context.FormatUsage(hazard).c_str());
5366 break;
5367 }
John Zulaufd5115702021-01-18 12:34:33 -07005368 }
5369 }
John Zulauf4edde622021-02-15 08:54:50 -07005370 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5371 // 03839
5372 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005373 }
John Zulaufd5115702021-01-18 12:34:33 -07005374
5375 // 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 -07005376 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 -07005377 if (extra_stage_bits) {
5378 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005379 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5380 const char *const vuid =
5381 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2KHR-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005382 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005383 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulaufd5115702021-01-18 12:34:33 -07005384 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005385 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005386 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005387 " vkCmdSetEvent may be in previously submitted command buffer.");
5388 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005389 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005390 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005391 }
5392 }
5393 return skip;
5394}
5395
5396struct SyncOpWaitEventsFunctorFactory {
5397 using BarrierOpFunctor = WaitEventBarrierOp;
5398 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5399 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5400 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5401 using BufferRange = EventSimpleRangeGenerator;
5402 using ImageRange = EventImageRangeGenerator;
5403 using GlobalRange = EventSimpleRangeGenerator;
5404
5405 // Need to restrict to only valid exec and access scope for this event
5406 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5407 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005408 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005409 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5410 return barrier;
5411 }
5412 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5413 auto barrier = RestrictToEvent(barrier_arg);
5414 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5415 }
5416 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
5417 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5418 }
5419 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5420 auto barrier = RestrictToEvent(barrier_arg);
5421 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5422 }
5423
5424 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5425 const AccessAddressType address_type = GetAccessAddressType(buffer);
5426 const auto base_address = ResourceBaseAddress(buffer);
5427 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5428 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5429 return filtered_range_gen;
5430 }
John Zulauf110413c2021-03-20 05:38:38 -06005431 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07005432 if (!SimpleBinding(image)) return ImageRange();
5433 const auto address_type = GetAccessAddressType(image);
5434 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005435 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005436 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5437
5438 return filtered_range_gen;
5439 }
5440 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5441 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5442 }
5443 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5444 SyncEventState *sync_event;
5445};
5446
John Zulauf36ef9282021-02-02 11:47:24 -07005447void SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
5448 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07005449 auto *access_context = cb_context->GetCurrentAccessContext();
5450 assert(access_context);
5451 if (!access_context) return;
John Zulauf669dfd52021-01-27 17:15:28 -07005452 auto *events_context = cb_context->GetCurrentEventsContext();
5453 assert(events_context);
5454 if (!events_context) return;
John Zulaufd5115702021-01-18 12:34:33 -07005455
5456 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
5457 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
5458 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
5459 access_context->ResolvePreviousAccesses();
5460
John Zulaufd5115702021-01-18 12:34:33 -07005461 // TODO... this needs change the SyncEventContext it's using depending on whether this is replay... the recorded
5462 // 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 -07005463 size_t barrier_set_index = 0;
5464 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5465 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07005466 for (auto &event_shared : events_) {
5467 if (!event_shared.get()) continue;
5468 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07005469
John Zulauf4edde622021-02-15 08:54:50 -07005470 sync_event->last_command = cmd_;
John Zulaufd5115702021-01-18 12:34:33 -07005471
John Zulauf4edde622021-02-15 08:54:50 -07005472 const auto &barrier_set = barriers_[barrier_set_index];
5473 const auto &dst = barrier_set.dst_exec_scope;
5474 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07005475 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
5476 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
5477 // of the barriers is maintained.
5478 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07005479 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5480 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5481 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07005482
5483 // Apply the global barrier to the event itself (for race condition tracking)
5484 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
5485 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
5486 sync_event->barriers |= dst.exec_scope;
5487 } else {
5488 // We ignored this wait, so we don't have any effective synchronization barriers for it.
5489 sync_event->barriers = 0U;
5490 }
John Zulauf4edde622021-02-15 08:54:50 -07005491 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005492 }
5493
5494 // Apply the pending barriers
5495 ResolvePendingBarrierFunctor apply_pending_action(tag);
5496 access_context->ApplyToContext(apply_pending_action);
5497}
5498
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005499bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5500 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5501 bool skip = false;
5502 const auto *cb_access_context = GetAccessContext(commandBuffer);
5503 assert(cb_access_context);
5504 if (!cb_access_context) return skip;
5505
5506 const auto *context = cb_access_context->GetCurrentAccessContext();
5507 assert(context);
5508 if (!context) return skip;
5509
5510 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5511
5512 if (dst_buffer) {
5513 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5514 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
5515 if (hazard.hazard) {
5516 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5517 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
5518 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
5519 string_UsageTag(hazard.tag).c_str());
5520 }
5521 }
5522 return skip;
5523}
5524
John Zulauf669dfd52021-01-27 17:15:28 -07005525void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07005526 events_.reserve(event_count);
5527 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
John Zulauf669dfd52021-01-27 17:15:28 -07005528 events_.emplace_back(sync_state.GetShared<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07005529 }
5530}
John Zulauf6ce24372021-01-30 05:56:25 -07005531
John Zulauf36ef9282021-02-02 11:47:24 -07005532SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005533 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005534 : SyncOpBase(cmd),
5535 event_(sync_state.GetShared<EVENT_STATE>(event)),
5536 exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005537
5538bool SyncOpResetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005539 auto *events_context = cb_context.GetCurrentEventsContext();
5540 assert(events_context);
5541 bool skip = false;
5542 if (!events_context) return skip;
5543
5544 const auto &sync_state = cb_context.GetSyncState();
5545 const auto *sync_event = events_context->Get(event_);
5546 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5547
5548 const char *const set_wait =
5549 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5550 "hazards.";
5551 const char *message = set_wait; // Only one message this call.
5552 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
5553 const char *vuid = nullptr;
5554 switch (sync_event->last_command) {
5555 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005556 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005557 // Needs a barrier between set and reset
5558 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
5559 break;
John Zulauf4edde622021-02-15 08:54:50 -07005560 case CMD_WAITEVENTS:
5561 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07005562 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
5563 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
5564 break;
5565 }
5566 default:
5567 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07005568 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
5569 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07005570 break;
5571 }
5572 if (vuid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005573 skip |= sync_state.LogError(event_->event(), vuid, message, CmdName(),
5574 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005575 CommandTypeString(sync_event->last_command));
5576 }
5577 }
5578 return skip;
5579}
5580
John Zulauf36ef9282021-02-02 11:47:24 -07005581void SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005582 auto *events_context = cb_context->GetCurrentEventsContext();
5583 assert(events_context);
5584 if (!events_context) return;
5585
5586 auto *sync_event = events_context->GetFromShared(event_);
5587 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5588
5589 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07005590 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005591 sync_event->unsynchronized_set = CMD_NONE;
5592 sync_event->ResetFirstScope();
5593 sync_event->barriers = 0U;
5594}
5595
John Zulauf36ef9282021-02-02 11:47:24 -07005596SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005597 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005598 : SyncOpBase(cmd),
5599 event_(sync_state.GetShared<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07005600 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
5601 dep_info_() {}
5602
5603SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
5604 const VkDependencyInfoKHR &dep_info)
5605 : SyncOpBase(cmd),
5606 event_(sync_state.GetShared<EVENT_STATE>(event)),
5607 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
5608 dep_info_(new safe_VkDependencyInfoKHR(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005609
5610bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
5611 // 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 -07005612 bool skip = false;
5613
5614 const auto &sync_state = cb_context.GetSyncState();
5615 auto *events_context = cb_context.GetCurrentEventsContext();
5616 assert(events_context);
5617 if (!events_context) return skip;
5618
5619 const auto *sync_event = events_context->Get(event_);
5620 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5621
5622 const char *const reset_set =
5623 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5624 "hazards.";
5625 const char *const wait =
5626 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
5627
5628 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07005629 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07005630 const char *message = nullptr;
5631 switch (sync_event->last_command) {
5632 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005633 case CMD_RESETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005634 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07005635 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07005636 message = reset_set;
5637 break;
5638 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005639 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005640 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07005641 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07005642 message = reset_set;
5643 break;
5644 case CMD_WAITEVENTS:
John Zulauf4edde622021-02-15 08:54:50 -07005645 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005646 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07005647 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07005648 message = wait;
5649 break;
5650 default:
5651 // The only other valid last command that wasn't one.
5652 assert(sync_event->last_command == CMD_NONE);
5653 break;
5654 }
John Zulauf4edde622021-02-15 08:54:50 -07005655 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07005656 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07005657 std::string vuid("SYNC-");
5658 vuid.append(CmdName()).append(vuid_stem);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005659 skip |= sync_state.LogError(event_->event(), vuid.c_str(), message, CmdName(),
5660 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005661 CommandTypeString(sync_event->last_command));
5662 }
5663 }
5664
5665 return skip;
5666}
5667
John Zulauf36ef9282021-02-02 11:47:24 -07005668void SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
5669 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07005670 auto *events_context = cb_context->GetCurrentEventsContext();
5671 auto *access_context = cb_context->GetCurrentAccessContext();
5672 assert(events_context);
5673 if (!events_context) return;
5674
5675 auto *sync_event = events_context->GetFromShared(event_);
5676 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5677
5678 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
5679 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
5680 // any issues caused by naive scope setting here.
5681
5682 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
5683 // Given:
5684 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
5685 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
5686
5687 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
5688 sync_event->unsynchronized_set = sync_event->last_command;
5689 sync_event->ResetFirstScope();
5690 } else if (sync_event->scope.exec_scope == 0) {
5691 // We only set the scope if there isn't one
5692 sync_event->scope = src_exec_scope_;
5693
5694 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
5695 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
5696 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
5697 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
5698 }
5699 };
5700 access_context->ForAll(set_scope);
5701 sync_event->unsynchronized_set = CMD_NONE;
5702 sync_event->first_scope_tag = tag;
5703 }
John Zulauf4edde622021-02-15 08:54:50 -07005704 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
5705 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005706 sync_event->barriers = 0U;
5707}
John Zulauf64ffe552021-02-06 10:25:07 -07005708
5709SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
5710 const VkRenderPassBeginInfo *pRenderPassBegin,
5711 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *cmd_name)
5712 : SyncOpBase(cmd, cmd_name) {
5713 if (pRenderPassBegin) {
5714 rp_state_ = sync_state.GetShared<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
5715 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
5716 const auto *fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
5717 if (fb_state) {
5718 shared_attachments_ = sync_state.GetSharedAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
5719 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
5720 // Note that this a safe to presist as long as shared_attachments is not cleared
5721 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08005722 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07005723 attachments_.emplace_back(attachment.get());
5724 }
5725 }
5726 if (pSubpassBeginInfo) {
5727 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
5728 }
5729 }
5730}
5731
5732bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5733 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
5734 bool skip = false;
5735
5736 assert(rp_state_.get());
5737 if (nullptr == rp_state_.get()) return skip;
5738 auto &rp_state = *rp_state_.get();
5739
5740 const uint32_t subpass = 0;
5741
5742 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
5743 // hasn't happened yet)
5744 const std::vector<AccessContext> empty_context_vector;
5745 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
5746 cb_context.GetCurrentAccessContext());
5747
5748 // Validate attachment operations
5749 if (attachments_.size() == 0) return skip;
5750 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07005751
5752 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
5753 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
5754 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
5755 // operations (though it's currently a messy approach)
5756 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
5757 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07005758
5759 // Validate load operations if there were no layout transition hazards
5760 if (!skip) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07005761 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kCurrentCommandTag);
5762 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07005763 }
5764
5765 return skip;
5766}
5767
5768void SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5769 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5770 assert(rp_state_.get());
5771 if (nullptr == rp_state_.get()) return;
5772 const auto tag = cb_context->NextCommandTag(cmd_);
5773 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
5774}
5775
5776SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
5777 const VkSubpassEndInfo *pSubpassEndInfo, const char *name_override)
5778 : SyncOpBase(cmd, name_override) {
5779 if (pSubpassBeginInfo) {
5780 subpass_begin_info_.initialize(pSubpassBeginInfo);
5781 }
5782 if (pSubpassEndInfo) {
5783 subpass_end_info_.initialize(pSubpassEndInfo);
5784 }
5785}
5786
5787bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
5788 bool skip = false;
5789 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5790 if (!renderpass_context) return skip;
5791
5792 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
5793 return skip;
5794}
5795
5796void SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
5797 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5798 cb_context->RecordNextSubpass(cmd_);
5799}
5800
5801SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo,
5802 const char *name_override)
5803 : SyncOpBase(cmd, name_override) {
5804 if (pSubpassEndInfo) {
5805 subpass_end_info_.initialize(pSubpassEndInfo);
5806 }
5807}
5808
5809bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5810 bool skip = false;
5811 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5812
5813 if (!renderpass_context) return skip;
5814 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
5815 return skip;
5816}
5817
5818void SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5819 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5820 cb_context->RecordEndRenderPass(cmd_);
5821}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005822
5823void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5824 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
5825 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
5826 auto *cb_access_context = GetAccessContext(commandBuffer);
5827 assert(cb_access_context);
5828 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5829 auto *context = cb_access_context->GetCurrentAccessContext();
5830 assert(context);
5831
5832 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5833
5834 if (dst_buffer) {
5835 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5836 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
5837 }
5838}
John Zulaufd05c5842021-03-26 11:32:16 -06005839
John Zulaufd0ec59f2021-03-13 14:25:08 -07005840AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
5841 : view_(view), view_mask_(), gen_store_() {
5842 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
5843 const IMAGE_STATE &image_state = *view_->image_state.get();
5844 const auto base_address = ResourceBaseAddress(image_state);
5845 const auto *encoder = image_state.fragment_encoder.get();
5846 if (!encoder) return;
5847 const VkOffset3D zero_offset = {0, 0, 0};
5848 const VkExtent3D &image_extent = image_state.createInfo.extent;
5849 // Intentional copy
5850 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
5851 view_mask_ = subres_range.aspectMask;
5852 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
5853 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5854
5855 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
5856 if (depth && (depth != view_mask_)) {
5857 subres_range.aspectMask = depth;
5858 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5859 }
5860 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
5861 if (stencil && (stencil != view_mask_)) {
5862 subres_range.aspectMask = stencil;
5863 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5864 }
5865}
5866
5867const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
5868 const ImageRangeGen *got = nullptr;
5869 switch (gen_type) {
5870 case kViewSubresource:
5871 got = &gen_store_[kViewSubresource];
5872 break;
5873 case kRenderArea:
5874 got = &gen_store_[kRenderArea];
5875 break;
5876 case kDepthOnlyRenderArea:
5877 got =
5878 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
5879 break;
5880 case kStencilOnlyRenderArea:
5881 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
5882 : &gen_store_[Gen::kStencilOnlyRenderArea];
5883 break;
5884 default:
5885 assert(got);
5886 }
5887 return got;
5888}
5889
5890AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
5891 assert(IsValid());
5892 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
5893 if (depth_op) {
5894 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
5895 if (stencil_op) {
5896 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
5897 return kRenderArea;
5898 }
5899 return kDepthOnlyRenderArea;
5900 }
5901 if (stencil_op) {
5902 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
5903 return kStencilOnlyRenderArea;
5904 }
5905
5906 assert(depth_op || stencil_op);
5907 return kRenderArea;
5908}
5909
5910AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }