blob: d5546bdca8eca129dd3a29b5dec8cc1fab12b46d [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>
519void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
520 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
521 VkExtent3D extent = CastTo3D(render_area.extent);
522 VkOffset3D offset = CastTo3D(render_area.offset);
523 const auto &rp_ci = rp_state.createInfo;
524 const auto *attachment_ci = rp_ci.pAttachments;
525 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
526
527 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
528 const auto *color_attachments = subpass_ci.pColorAttachments;
529 const auto *color_resolve = subpass_ci.pResolveAttachments;
530 if (color_resolve && color_attachments) {
531 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
532 const auto &color_attach = color_attachments[i].attachment;
533 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
534 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
535 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulauf8e3c3e92021-01-06 11:19:36 -0700536 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kColorAttachment, offset, extent, 0);
John Zulauf7635de32020-05-29 17:14:15 -0600537 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulauf8e3c3e92021-01-06 11:19:36 -0700538 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment, offset, extent, 0);
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;
554 VkImageAspectFlags aspect_mask = 0u;
555
556 // Figure out which aspects are actually touched during resolve operations
557 const char *aspect_string = nullptr;
558 if (resolve_depth && resolve_stencil) {
559 // Validate all aspects together
560 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
561 aspect_string = "depth/stencil";
562 } else if (resolve_depth) {
563 // Validate depth only
564 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
565 aspect_string = "depth";
566 } else if (resolve_stencil) {
567 // Validate all stencil only
568 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
569 aspect_string = "stencil";
570 }
571
572 if (aspect_mask) {
573 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
John Zulauf8e3c3e92021-01-06 11:19:36 -0700574 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster, offset, extent, aspect_mask);
John Zulauf7635de32020-05-29 17:14:15 -0600575 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
John Zulauf8e3c3e92021-01-06 11:19:36 -0700576 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, offset, extent, aspect_mask);
John Zulauf7635de32020-05-29 17:14:15 -0600577 }
578 }
579}
580
581// Action for validating resolve operations
582class ValidateResolveAction {
583 public:
John Zulauffaea0ee2021-01-14 14:01:32 -0700584 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context,
John Zulauf64ffe552021-02-06 10:25:07 -0700585 const CommandExecutionContext &ex_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600586 : render_pass_(render_pass),
587 subpass_(subpass),
588 context_(context),
John Zulauf64ffe552021-02-06 10:25:07 -0700589 ex_context_(ex_context),
John Zulauf7635de32020-05-29 17:14:15 -0600590 func_name_(func_name),
591 skip_(false) {}
592 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
John Zulauf8e3c3e92021-01-06 11:19:36 -0700593 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf7635de32020-05-29 17:14:15 -0600594 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
595 HazardResult hazard;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700596 hazard = context_.DetectHazard(view, current_usage, ordering_rule, offset, extent, aspect_mask);
John Zulauf7635de32020-05-29 17:14:15 -0600597 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -0700598 skip_ |=
John Zulauf64ffe552021-02-06 10:25:07 -0700599 ex_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700600 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
601 " to resolve attachment %" PRIu32 ". Access info %s.",
602 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name,
John Zulauf64ffe552021-02-06 10:25:07 -0700603 attachment_name, src_at, dst_at, ex_context_.FormatUsage(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600604 }
605 }
606 // Providing a mechanism for the constructing caller to get the result of the validation
607 bool GetSkip() const { return skip_; }
608
609 private:
610 VkRenderPass render_pass_;
611 const uint32_t subpass_;
612 const AccessContext &context_;
John Zulauf64ffe552021-02-06 10:25:07 -0700613 const CommandExecutionContext &ex_context_;
John Zulauf7635de32020-05-29 17:14:15 -0600614 const char *func_name_;
615 bool skip_;
616};
617
618// Update action for resolve operations
619class UpdateStateResolveAction {
620 public:
621 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
622 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
John Zulauf8e3c3e92021-01-06 11:19:36 -0700623 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf7635de32020-05-29 17:14:15 -0600624 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
625 // Ignores validation only arguments...
John Zulauf8e3c3e92021-01-06 11:19:36 -0700626 context_.UpdateAccessState(view, current_usage, ordering_rule, offset, extent, aspect_mask, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600627 }
628
629 private:
630 AccessContext &context_;
631 const ResourceUsageTag &tag_;
632};
633
John Zulauf59e25072020-07-17 10:55:21 -0600634void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700635 const SyncStageAccessFlags &prior_, const ResourceUsageTag &tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600636 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
637 usage_index = usage_index_;
638 hazard = hazard_;
639 prior_access = prior_;
640 tag = tag_;
641}
642
John Zulauf540266b2020-04-06 18:54:53 -0600643AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
644 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600645 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600646 Reset();
647 const auto &subpass_dep = dependencies[subpass];
648 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600649 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600650 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600651 const auto prev_pass = prev_dep.first->pass;
652 const auto &prev_barriers = prev_dep.second;
653 assert(prev_dep.second.size());
654 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
655 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700656 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600657
658 async_.reserve(subpass_dep.async.size());
659 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700660 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600661 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600662 if (subpass_dep.barrier_from_external.size()) {
663 src_external_ = TrackBack(external_context, queue_flags, subpass_dep.barrier_from_external);
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);
712 const auto from = accesses.lower_bound(range);
713 const auto to = accesses.upper_bound(range);
714 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600715
John Zulauf69133422020-05-20 14:55:53 -0600716 for (auto pos = from; pos != to; ++pos) {
717 // 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;
733 }
734
735 if (detect_prev) {
736 // Detect in the trailing empty as needed
737 gap.end = range.end;
738 if (gap.non_empty()) {
739 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600740 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600741 }
742
743 return hazard;
744}
745
746// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
747template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700748HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
749 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600750 auto &accesses = GetAccessStateMap(type);
751 const auto from = accesses.lower_bound(range);
752 const auto to = accesses.upper_bound(range);
753
John Zulauf3d84f1b2020-03-09 13:33:25 -0600754 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600755 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700756 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600757 }
John Zulauf16adfc92020-04-08 10:28:33 -0600758
John Zulauf3d84f1b2020-03-09 13:33:25 -0600759 return hazard;
760}
761
John Zulaufb02c1eb2020-10-06 16:33:36 -0600762struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700763 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600764 void operator()(ResourceAccessState *access) const {
765 assert(access);
766 access->ApplyBarriers(barriers, true);
767 }
768 const std::vector<SyncBarrier> &barriers;
769};
770
771struct ApplyTrackbackBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700772 explicit ApplyTrackbackBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600773 void operator()(ResourceAccessState *access) const {
774 assert(access);
775 assert(!access->HasPendingState());
776 access->ApplyBarriers(barriers, false);
777 access->ApplyPendingBarriers(kCurrentCommandTag);
778 }
779 const std::vector<SyncBarrier> &barriers;
780};
781
782// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
783// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
784// *different* map from dest.
785// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
786// range [first, last)
787template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600788static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
789 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600790 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600791 auto at = entry;
792 for (auto pos = first; pos != last; ++pos) {
793 // Every member of the input iterator range must fit within the remaining portion of entry
794 assert(at->first.includes(pos->first));
795 assert(at != dest->end());
796 // Trim up at to the same size as the entry to resolve
797 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600798 auto access = pos->second; // intentional copy
799 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600800 at->second.Resolve(access);
801 ++at; // Go to the remaining unused section of entry
802 }
803}
804
John Zulaufa0a98292020-09-18 09:30:10 -0600805static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
806 SyncBarrier merged = {};
807 for (const auto &barrier : barriers) {
808 merged.Merge(barrier);
809 }
810 return merged;
811}
812
John Zulaufb02c1eb2020-10-06 16:33:36 -0600813template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700814void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600815 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
816 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600817 if (!range.non_empty()) return;
818
John Zulauf355e49b2020-04-24 15:11:15 -0600819 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
820 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600821 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600822 if (current->pos_B->valid) {
823 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600824 auto access = src_pos->second; // intentional copy
825 barrier_action(&access);
826
John Zulauf16adfc92020-04-08 10:28:33 -0600827 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600828 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
829 trimmed->second.Resolve(access);
830 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600831 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600832 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600833 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600834 }
John Zulauf16adfc92020-04-08 10:28:33 -0600835 } else {
836 // we have to descend to fill this gap
837 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600838 if (current->pos_A->valid) {
839 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
840 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600841 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600842 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -0600843 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600844 // There isn't anything in dest in current)range, so we can accumulate directly into it.
845 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600846 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
847 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
848 barrier_action(&pos->second);
John Zulauf355e49b2020-04-24 15:11:15 -0600849 }
850 }
851 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
852 // iterator of the outer while.
853
854 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
855 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
856 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600857 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
858 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600859 current.seek(seek_to);
860 } else if (!current->pos_A->valid && infill_state) {
861 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
862 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
863 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600864 }
John Zulauf5f13a792020-03-10 07:31:21 -0600865 }
John Zulauf16adfc92020-04-08 10:28:33 -0600866 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600867 }
John Zulauf1a224292020-06-30 14:52:13 -0600868
869 // Infill if range goes passed both the current and resolve map prior contents
870 if (recur_to_infill && (current->range.end < range.end)) {
871 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
872 ResourceAccessRangeMap gap_map;
873 const auto the_end = resolve_map->end();
874 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
875 for (auto &access : gap_map) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600876 barrier_action(&access.second);
John Zulauf1a224292020-06-30 14:52:13 -0600877 resolve_map->insert(the_end, access);
878 }
879 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600880}
881
John Zulauf43cc7462020-12-03 12:33:12 -0700882void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
883 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600884 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600885 if (range.non_empty() && infill_state) {
886 descent_map->insert(std::make_pair(range, *infill_state));
887 }
888 } else {
889 // Look for something to fill the gap further along.
890 for (const auto &prev_dep : prev_) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600891 const ApplyTrackbackBarriersAction barrier_action(prev_dep.barriers);
892 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600893 }
894
John Zulaufe5da6e52020-03-18 15:32:18 -0600895 if (src_external_.context) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600896 const ApplyTrackbackBarriersAction barrier_action(src_external_.barriers);
897 src_external_.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600898 }
899 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600900}
901
John Zulauf4a6105a2020-11-17 15:11:05 -0700902// Non-lazy import of all accesses, WaitEvents needs this.
903void AccessContext::ResolvePreviousAccesses() {
904 ResourceAccessState default_state;
905 for (const auto address_type : kAddressTypes) {
906 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
907 }
908}
909
John Zulauf43cc7462020-12-03 12:33:12 -0700910AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
911 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -0600912}
913
John Zulauf1507ee42020-05-18 11:33:09 -0600914static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
915 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
916 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
917 return stage_access;
918}
919static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
920 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
921 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
922 return stage_access;
923}
924
John Zulauf7635de32020-05-29 17:14:15 -0600925// Caller must manage returned pointer
926static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
927 uint32_t subpass, const VkRect2D &render_area,
928 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
929 auto *proxy = new AccessContext(context);
930 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600931 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600932 return proxy;
933}
934
John Zulaufb02c1eb2020-10-06 16:33:36 -0600935template <typename BarrierAction>
John Zulauf52446eb2020-10-22 16:40:08 -0600936class ResolveAccessRangeFunctor {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600937 public:
John Zulauf43cc7462020-12-03 12:33:12 -0700938 ResolveAccessRangeFunctor(const AccessContext &context, AccessAddressType address_type, ResourceAccessRangeMap *descent_map,
939 const ResourceAccessState *infill_state, BarrierAction &barrier_action)
John Zulauf52446eb2020-10-22 16:40:08 -0600940 : context_(context),
941 address_type_(address_type),
942 descent_map_(descent_map),
943 infill_state_(infill_state),
944 barrier_action_(barrier_action) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600945 ResolveAccessRangeFunctor() = delete;
946 void operator()(const ResourceAccessRange &range) const {
947 context_.ResolveAccessRange(address_type_, range, barrier_action_, descent_map_, infill_state_);
948 }
949
950 private:
John Zulauf52446eb2020-10-22 16:40:08 -0600951 const AccessContext &context_;
John Zulauf43cc7462020-12-03 12:33:12 -0700952 const AccessAddressType address_type_;
John Zulauf52446eb2020-10-22 16:40:08 -0600953 ResourceAccessRangeMap *const descent_map_;
954 const ResourceAccessState *infill_state_;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600955 BarrierAction &barrier_action_;
956};
957
John Zulaufb02c1eb2020-10-06 16:33:36 -0600958template <typename BarrierAction>
959void AccessContext::ResolveAccessRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -0700960 BarrierAction &barrier_action, AccessAddressType address_type,
961 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600962 const ResolveAccessRangeFunctor<BarrierAction> action(*this, address_type, descent_map, infill_state, barrier_action);
963 ApplyOverImageRange(image_state, subresource_range, action);
John Zulauf62f10592020-04-03 12:20:02 -0600964}
965
John Zulauf7635de32020-05-29 17:14:15 -0600966// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf64ffe552021-02-06 10:25:07 -0700967bool AccessContext::ValidateLayoutTransitions(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600968 const VkRect2D &render_area, uint32_t subpass,
969 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
970 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);
986 if (prev_needs_proxy) {
987 if (!proxy_for_prev) {
988 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
989 render_area, attachment_views));
990 proxy_track_back = *track_back;
991 proxy_track_back.context = proxy_for_prev.get();
992 }
993 track_back = &proxy_track_back;
994 }
995 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600996 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -0700997 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700998 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
999 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
1000 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1001 string_VkImageLayout(transition.old_layout),
1002 string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07001003 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001004 }
1005 }
1006 return skip;
1007}
1008
John Zulauf64ffe552021-02-06 10:25:07 -07001009bool AccessContext::ValidateLoadOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001010 const VkRect2D &render_area, uint32_t subpass,
1011 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1012 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;
1015 VkExtent3D extent = CastTo3D(render_area.extent);
1016 VkOffset3D offset = CastTo3D(render_area.offset);
John Zulaufa0a98292020-09-18 09:30:10 -06001017
John Zulauf1507ee42020-05-18 11:33:09 -06001018 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1019 if (subpass == rp_state.attachment_first_subpass[i]) {
1020 if (attachment_views[i] == nullptr) continue;
1021 const IMAGE_VIEW_STATE &view = *attachment_views[i];
1022 const IMAGE_STATE *image = view.image_state.get();
1023 if (image == nullptr) continue;
1024 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001025
1026 // Need check in the following way
1027 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1028 // vs. transition
1029 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1030 // for each aspect loaded.
1031
1032 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001033 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001034 const bool is_color = !(has_depth || has_stencil);
1035
1036 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001037 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001038
John Zulaufaff20662020-06-01 14:07:58 -06001039 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001040 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001041
John Zulaufb02c1eb2020-10-06 16:33:36 -06001042 auto hazard_range = view.normalized_subresource_range;
1043 bool checked_stencil = false;
1044 if (is_color) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001045 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, SyncOrdering::kColorAttachment, offset,
John Zulauf859089b2020-10-29 17:37:03 -06001046 extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001047 aspect = "color";
1048 } else {
1049 if (has_depth) {
1050 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001051 hazard = DetectHazard(*image, load_index, hazard_range, SyncOrdering::kDepthStencilAttachment, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001052 aspect = "depth";
1053 }
1054 if (!hazard.hazard && has_stencil) {
1055 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001056 hazard = DetectHazard(*image, stencil_load_index, hazard_range, SyncOrdering::kDepthStencilAttachment, offset,
1057 extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001058 aspect = "stencil";
1059 checked_stencil = true;
1060 }
1061 }
1062
1063 if (hazard.hazard) {
1064 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001065 const auto &sync_state = ex_context.GetSyncState();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001066 if (hazard.tag == kCurrentCommandTag) {
1067 // Hazard vs. ILT
1068 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1069 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1070 " aspect %s during load with loadOp %s.",
1071 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1072 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06001073 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1074 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001075 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001076 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf64ffe552021-02-06 10:25:07 -07001077 ex_context.FormatUsage(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001078 }
1079 }
1080 }
1081 }
1082 return skip;
1083}
1084
John Zulaufaff20662020-06-01 14:07:58 -06001085// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1086// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1087// store is part of the same Next/End operation.
1088// The latter is handled in layout transistion validation directly
John Zulauf64ffe552021-02-06 10:25:07 -07001089bool AccessContext::ValidateStoreOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufaff20662020-06-01 14:07:58 -06001090 const VkRect2D &render_area, uint32_t subpass,
1091 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1092 const char *func_name) const {
1093 bool skip = false;
1094 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1095 VkExtent3D extent = CastTo3D(render_area.extent);
1096 VkOffset3D offset = CastTo3D(render_area.offset);
1097
1098 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1099 if (subpass == rp_state.attachment_last_subpass[i]) {
1100 if (attachment_views[i] == nullptr) continue;
1101 const IMAGE_VIEW_STATE &view = *attachment_views[i];
1102 const IMAGE_STATE *image = view.image_state.get();
1103 if (image == nullptr) continue;
1104 const auto &ci = attachment_ci[i];
1105
1106 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1107 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1108 // sake, we treat DONT_CARE as writing.
1109 const bool has_depth = FormatHasDepth(ci.format);
1110 const bool has_stencil = FormatHasStencil(ci.format);
1111 const bool is_color = !(has_depth || has_stencil);
1112 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1113 if (!has_stencil && !store_op_stores) continue;
1114
1115 HazardResult hazard;
1116 const char *aspect = nullptr;
1117 bool checked_stencil = false;
1118 if (is_color) {
1119 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001120 view.normalized_subresource_range, SyncOrdering::kRaster, offset, extent);
John Zulaufaff20662020-06-01 14:07:58 -06001121 aspect = "color";
1122 } else {
1123 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1124 auto hazard_range = view.normalized_subresource_range;
1125 if (has_depth && store_op_stores) {
1126 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1127 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001128 SyncOrdering::kRaster, offset, extent);
John Zulaufaff20662020-06-01 14:07:58 -06001129 aspect = "depth";
1130 }
1131 if (!hazard.hazard && has_stencil && stencil_op_stores) {
1132 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1133 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001134 SyncOrdering::kRaster, offset, extent);
John Zulaufaff20662020-06-01 14:07:58 -06001135 aspect = "stencil";
1136 checked_stencil = true;
1137 }
1138 }
1139
1140 if (hazard.hazard) {
1141 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1142 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001143 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001144 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1145 " %s aspect during store with %s %s. Access info %s",
1146 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect,
John Zulauf64ffe552021-02-06 10:25:07 -07001147 op_type_string, store_op_string, ex_context.FormatUsage(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001148 }
1149 }
1150 }
1151 return skip;
1152}
1153
John Zulauf64ffe552021-02-06 10:25:07 -07001154bool AccessContext::ValidateResolveOperations(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufb027cdb2020-05-21 14:25:22 -06001155 const VkRect2D &render_area,
1156 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
1157 uint32_t subpass) const {
John Zulauf64ffe552021-02-06 10:25:07 -07001158 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, ex_context, func_name);
John Zulauf7635de32020-05-29 17:14:15 -06001159 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
1160 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001161}
1162
John Zulauf3d84f1b2020-03-09 13:33:25 -06001163class HazardDetector {
1164 SyncStageAccessIndex usage_index_;
1165
1166 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001167 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001168 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1169 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001170 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001171 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001172};
1173
John Zulauf69133422020-05-20 14:55:53 -06001174class HazardDetectorWithOrdering {
1175 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001176 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001177
1178 public:
1179 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001180 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001181 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001182 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1183 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001184 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001185 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001186};
1187
John Zulauf16adfc92020-04-08 10:28:33 -06001188HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001189 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001190 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001191 const auto base_address = ResourceBaseAddress(buffer);
1192 HazardDetector detector(usage_index);
1193 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001194}
1195
John Zulauf69133422020-05-20 14:55:53 -06001196template <typename Detector>
1197HazardResult 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}
1211
John Zulauf540266b2020-04-06 18:54:53 -06001212HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1213 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1214 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001215 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1216 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -06001217 return DetectHazard(image, current_usage, subresource_range, offset, extent);
1218}
1219
1220HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1221 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1222 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001223 HazardDetector detector(current_usage);
1224 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1225}
1226
1227HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001228 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001229 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001230 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001231 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001232}
1233
John Zulaufb027cdb2020-05-21 14:25:22 -06001234// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1235// should have reported the issue regarding an invalid attachment entry
1236HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001237 SyncOrdering ordering_rule, const VkOffset3D &offset, const VkExtent3D &extent,
John Zulaufb027cdb2020-05-21 14:25:22 -06001238 VkImageAspectFlags aspect_mask) const {
1239 if (view != nullptr) {
1240 const IMAGE_STATE *image = view->image_state.get();
1241 if (image != nullptr) {
1242 auto *detect_range = &view->normalized_subresource_range;
1243 VkImageSubresourceRange masked_range;
1244 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1245 masked_range = view->normalized_subresource_range;
1246 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1247 detect_range = &masked_range;
1248 }
1249
1250 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1251 if (detect_range->aspectMask) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001252 return DetectHazard(*image, current_usage, *detect_range, ordering_rule, offset, extent);
John Zulaufb027cdb2020-05-21 14:25:22 -06001253 }
1254 }
1255 }
1256 return HazardResult();
1257}
John Zulauf43cc7462020-12-03 12:33:12 -07001258
John Zulauf3d84f1b2020-03-09 13:33:25 -06001259class BarrierHazardDetector {
1260 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001261 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001262 SyncStageAccessFlags src_access_scope)
1263 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1264
John Zulauf5f13a792020-03-10 07:31:21 -06001265 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1266 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001267 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001268 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001269 // 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 -07001270 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001271 }
1272
1273 private:
1274 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001275 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001276 SyncStageAccessFlags src_access_scope_;
1277};
1278
John Zulauf4a6105a2020-11-17 15:11:05 -07001279class EventBarrierHazardDetector {
1280 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001281 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001282 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
1283 const ResourceUsageTag &scope_tag)
1284 : usage_index_(usage_index),
1285 src_exec_scope_(src_exec_scope),
1286 src_access_scope_(src_access_scope),
1287 event_scope_(event_scope),
1288 scope_pos_(event_scope.cbegin()),
1289 scope_end_(event_scope.cend()),
1290 scope_tag_(scope_tag) {}
1291
1292 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1293 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1294 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1295 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1296 if (scope_pos_ == scope_end_) return HazardResult();
1297 if (!scope_pos_->first.intersects(pos->first)) {
1298 event_scope_.lower_bound(pos->first);
1299 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1300 }
1301
1302 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1303 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1304 }
1305 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1306 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1307 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1308 }
1309
1310 private:
1311 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001312 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001313 SyncStageAccessFlags src_access_scope_;
1314 const SyncEventState::ScopeMap &event_scope_;
1315 SyncEventState::ScopeMap::const_iterator scope_pos_;
1316 SyncEventState::ScopeMap::const_iterator scope_end_;
1317 const ResourceUsageTag &scope_tag_;
1318};
1319
Jeremy Gebben40a22942020-12-22 14:22:06 -07001320HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001321 const SyncStageAccessFlags &src_access_scope,
1322 const VkImageSubresourceRange &subresource_range,
1323 const SyncEventState &sync_event, DetectOptions options) const {
1324 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1325 // first access scope map to use, and there's no easy way to plumb it in below.
1326 const auto address_type = ImageAddressType(image);
1327 const auto &event_scope = sync_event.FirstScope(address_type);
1328
1329 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1330 event_scope, sync_event.first_scope_tag);
1331 VkOffset3D zero_offset = {0, 0, 0};
1332 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, 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);
1340 VkOffset3D zero_offset = {0, 0, 0};
1341 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001342}
1343
Jeremy Gebben40a22942020-12-22 14:22:06 -07001344HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001345 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001346 const VkImageMemoryBarrier &barrier) const {
1347 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1348 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1349 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1350}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001351HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07001352 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope.exec_scope,
John Zulaufd5115702021-01-18 12:34:33 -07001353 image_barrier.barrier.src_access_scope, image_barrier.range.subresource_range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001354}
John Zulauf355e49b2020-04-24 15:11:15 -06001355
John Zulauf9cb530d2019-09-30 14:14:10 -06001356template <typename Flags, typename Map>
1357SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1358 SyncStageAccessFlags scope = 0;
1359 for (const auto &bit_scope : map) {
1360 if (flag_mask < bit_scope.first) break;
1361
1362 if (flag_mask & bit_scope.first) {
1363 scope |= bit_scope.second;
1364 }
1365 }
1366 return scope;
1367}
1368
Jeremy Gebben40a22942020-12-22 14:22:06 -07001369SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags2KHR stages) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001370 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1371}
1372
Jeremy Gebben40a22942020-12-22 14:22:06 -07001373SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags2KHR accesses) {
1374 return AccessScopeImpl(sync_utils::ExpandAccessFlags(accesses), syncStageAccessMaskByAccessBit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001375}
1376
Jeremy Gebben40a22942020-12-22 14:22:06 -07001377// Getting from stage mask and access mask to stage/access masks is something we need to be good at...
1378SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001379 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1380 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1381 // of the union of all stage/access types for all the stages and the same unions for the access mask...
John Zulauf9cb530d2019-09-30 14:14:10 -06001382 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1383}
1384
1385template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001386void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001387 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1388 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001389 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001390 auto pos = accesses->lower_bound(range);
1391 if (pos == accesses->end() || !pos->first.intersects(range)) {
1392 // The range is empty, fill it with a default value.
1393 pos = action.Infill(accesses, pos, range);
1394 } else if (range.begin < pos->first.begin) {
1395 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001396 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001397 } else if (pos->first.begin < range.begin) {
1398 // Trim the beginning if needed
1399 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1400 ++pos;
1401 }
1402
1403 const auto the_end = accesses->end();
1404 while ((pos != the_end) && pos->first.intersects(range)) {
1405 if (pos->first.end > range.end) {
1406 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1407 }
1408
1409 pos = action(accesses, pos);
1410 if (pos == the_end) break;
1411
1412 auto next = pos;
1413 ++next;
1414 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1415 // Need to infill if next is disjoint
1416 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001417 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001418 next = action.Infill(accesses, next, new_range);
1419 }
1420 pos = next;
1421 }
1422}
John Zulaufd5115702021-01-18 12:34:33 -07001423
1424// Give a comparable interface for range generators and ranges
1425template <typename Action>
1426inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1427 assert(range);
1428 UpdateMemoryAccessState(accesses, *range, action);
1429}
1430
John Zulauf4a6105a2020-11-17 15:11:05 -07001431template <typename Action, typename RangeGen>
1432void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1433 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001434 RangeGen &range_gen = *range_gen_arg; // Non-const references must be * by style requirement but deref-ing * iterator is a pain
John Zulauf4a6105a2020-11-17 15:11:05 -07001435 for (; range_gen->non_empty(); ++range_gen) {
1436 UpdateMemoryAccessState(accesses, *range_gen, action);
1437 }
1438}
John Zulauf9cb530d2019-09-30 14:14:10 -06001439
1440struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001441 using Iterator = ResourceAccessRangeMap::iterator;
1442 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001443 // this is only called on gaps, and never returns a gap.
1444 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001445 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001446 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001447 }
John Zulauf5f13a792020-03-10 07:31:21 -06001448
John Zulauf5c5e88d2019-12-26 11:22:02 -07001449 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001450 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001451 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001452 return pos;
1453 }
1454
John Zulauf43cc7462020-12-03 12:33:12 -07001455 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001456 SyncOrdering ordering_rule_, const ResourceUsageTag &tag_)
1457 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001458 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001459 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001460 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001461 const SyncOrdering ordering_rule;
John Zulauf9cb530d2019-09-30 14:14:10 -06001462 const ResourceUsageTag &tag;
1463};
1464
John Zulauf4a6105a2020-11-17 15:11:05 -07001465// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001466struct PipelineBarrierOp {
1467 SyncBarrier barrier;
1468 bool layout_transition;
1469 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1470 : barrier(barrier_), layout_transition(layout_transition_) {}
1471 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001472 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001473 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1474};
John Zulauf4a6105a2020-11-17 15:11:05 -07001475// The barrier operation for wait events
1476struct WaitEventBarrierOp {
1477 const ResourceUsageTag *scope_tag;
1478 SyncBarrier barrier;
1479 bool layout_transition;
1480 WaitEventBarrierOp(const ResourceUsageTag &scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1481 : scope_tag(&scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
1482 WaitEventBarrierOp() = default;
1483 void operator()(ResourceAccessState *access_state) const {
1484 assert(scope_tag); // Not valid to have a non-scope op executed, default construct included for std::vector support
1485 access_state->ApplyBarrier(*scope_tag, barrier, layout_transition);
1486 }
1487};
John Zulauf1e331ec2020-12-04 18:29:38 -07001488
John Zulauf4a6105a2020-11-17 15:11:05 -07001489// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1490// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1491// of a collection is known/present.
John Zulauf1e331ec2020-12-04 18:29:38 -07001492template <typename BarrierOp>
John Zulauf89311b42020-09-29 16:28:47 -06001493class ApplyBarrierOpsFunctor {
1494 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001495 using Iterator = ResourceAccessRangeMap::iterator;
1496 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001497
John Zulauf5c5e88d2019-12-26 11:22:02 -07001498 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001499 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001500 for (const auto &op : barrier_ops_) {
1501 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001502 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001503
John Zulauf89311b42020-09-29 16:28:47 -06001504 if (resolve_) {
1505 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1506 // another walk
1507 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001508 }
1509 return pos;
1510 }
1511
John Zulauf89311b42020-09-29 16:28:47 -06001512 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulaufd5115702021-01-18 12:34:33 -07001513 ApplyBarrierOpsFunctor(bool resolve, size_t size_hint, const ResourceUsageTag &tag)
1514 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1515 barrier_ops_.reserve(size_hint);
1516 }
1517 void EmplaceBack(const BarrierOp &op) { barrier_ops_.emplace_back(op); }
John Zulauf89311b42020-09-29 16:28:47 -06001518
1519 private:
1520 bool resolve_;
John Zulaufd5115702021-01-18 12:34:33 -07001521 std::vector<BarrierOp> barrier_ops_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001522 const ResourceUsageTag &tag_;
1523};
1524
John Zulauf4a6105a2020-11-17 15:11:05 -07001525// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1526// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1527template <typename BarrierOp>
1528class ApplyBarrierFunctor {
1529 public:
1530 using Iterator = ResourceAccessRangeMap::iterator;
1531 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1532
1533 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1534 auto &access_state = pos->second;
1535 barrier_op_(&access_state);
1536 return pos;
1537 }
1538
1539 ApplyBarrierFunctor(const BarrierOp &barrier_op) : barrier_op_(barrier_op) {}
1540
1541 private:
John Zulaufd5115702021-01-18 12:34:33 -07001542 BarrierOp barrier_op_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001543};
1544
John Zulauf1e331ec2020-12-04 18:29:38 -07001545// This functor resolves the pendinging state.
1546class ResolvePendingBarrierFunctor {
1547 public:
1548 using Iterator = ResourceAccessRangeMap::iterator;
1549 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1550
1551 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1552 auto &access_state = pos->second;
1553 access_state.ApplyPendingBarriers(tag_);
1554 return pos;
1555 }
1556
1557 ResolvePendingBarrierFunctor(const ResourceUsageTag &tag) : tag_(tag) {}
1558
1559 private:
John Zulauf89311b42020-09-29 16:28:47 -06001560 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001561};
1562
John Zulauf8e3c3e92021-01-06 11:19:36 -07001563void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
1564 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
1565 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001566 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001567}
1568
John Zulauf8e3c3e92021-01-06 11:19:36 -07001569void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001570 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001571 if (!SimpleBinding(buffer)) return;
1572 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001573 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001574}
John Zulauf355e49b2020-04-24 15:11:15 -06001575
John Zulauf8e3c3e92021-01-06 11:19:36 -07001576void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001577 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001578 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001579 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001580 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001581 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1582 base_address);
1583 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001584 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001585 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001586 UpdateMemoryAccessState(&GetAccessStateMap(address_type), *range_gen, action);
John Zulauf5f13a792020-03-10 07:31:21 -06001587 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001588}
John Zulauf8e3c3e92021-01-06 11:19:36 -07001589void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
1590 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask,
1591 const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001592 if (view != nullptr) {
1593 const IMAGE_STATE *image = view->image_state.get();
1594 if (image != nullptr) {
1595 auto *update_range = &view->normalized_subresource_range;
1596 VkImageSubresourceRange masked_range;
1597 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1598 masked_range = view->normalized_subresource_range;
1599 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1600 update_range = &masked_range;
1601 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001602 UpdateAccessState(*image, current_usage, ordering_rule, *update_range, offset, extent, tag);
John Zulauf7635de32020-05-29 17:14:15 -06001603 }
1604 }
1605}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001606
John Zulauf8e3c3e92021-01-06 11:19:36 -07001607void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001608 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1609 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001610 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1611 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001612 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001613}
1614
John Zulauf540266b2020-04-06 18:54:53 -06001615template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001616void AccessContext::UpdateResourceAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001617 if (!SimpleBinding(buffer)) return;
1618 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf43cc7462020-12-03 12:33:12 -07001619 UpdateMemoryAccessState(&GetAccessStateMap(AccessAddressType::kLinear), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001620}
1621
1622template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001623void AccessContext::UpdateResourceAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1624 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001625 if (!SimpleBinding(image)) return;
1626 const auto address_type = ImageAddressType(image);
1627 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001628
John Zulauf16adfc92020-04-08 10:28:33 -06001629 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001630 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
1631 image.createInfo.extent, base_address);
1632
John Zulauf540266b2020-04-06 18:54:53 -06001633 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001634 UpdateMemoryAccessState(accesses, *range_gen, action);
John Zulauf540266b2020-04-06 18:54:53 -06001635 }
1636}
1637
John Zulauf7635de32020-05-29 17:14:15 -06001638void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1639 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1640 const ResourceUsageTag &tag) {
1641 UpdateStateResolveAction update(*this, tag);
1642 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1643}
1644
John Zulaufaff20662020-06-01 14:07:58 -06001645void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1646 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1647 const ResourceUsageTag &tag) {
1648 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1649 VkExtent3D extent = CastTo3D(render_area.extent);
1650 VkOffset3D offset = CastTo3D(render_area.offset);
1651
1652 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1653 if (rp_state.attachment_last_subpass[i] == subpass) {
1654 if (attachment_views[i] == nullptr) continue; // UNUSED
1655 const auto &view = *attachment_views[i];
1656 const IMAGE_STATE *image = view.image_state.get();
1657 if (image == nullptr) continue;
1658
1659 const auto &ci = attachment_ci[i];
1660 const bool has_depth = FormatHasDepth(ci.format);
1661 const bool has_stencil = FormatHasStencil(ci.format);
1662 const bool is_color = !(has_depth || has_stencil);
1663 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1664
1665 if (is_color && store_op_stores) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001666 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster,
1667 view.normalized_subresource_range, offset, extent, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001668 } else {
1669 auto update_range = view.normalized_subresource_range;
1670 if (has_depth && store_op_stores) {
1671 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001672 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster,
1673 update_range, offset, extent, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001674 }
1675 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1676 if (has_stencil && stencil_op_stores) {
1677 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001678 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster,
1679 update_range, offset, extent, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001680 }
1681 }
1682 }
1683 }
1684}
1685
John Zulauf540266b2020-04-06 18:54:53 -06001686template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001687void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001688 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001689 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001690 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001691 }
1692}
1693
1694void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001695 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1696 auto &context = contexts[subpass_index];
John Zulaufb02c1eb2020-10-06 16:33:36 -06001697 ApplyTrackbackBarriersAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001698 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001699 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001700 }
1701 }
1702}
1703
John Zulauf355e49b2020-04-24 15:11:15 -06001704// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001705HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001706 if (!attach_view) return HazardResult();
1707 const auto image_state = attach_view->image_state.get();
1708 if (!image_state) return HazardResult();
1709
John Zulauf355e49b2020-04-24 15:11:15 -06001710 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001711 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001712
1713 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001714 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1715 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufc523bf62021-02-16 08:20:34 -07001716 HazardResult hazard = track_back.context->DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope.exec_scope,
1717 merged_barrier.src_access_scope,
1718 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001719 if (!hazard.hazard) {
1720 // The Async hazard check is against the current context's async set.
John Zulaufc523bf62021-02-16 08:20:34 -07001721 hazard = DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope.exec_scope, merged_barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001722 attach_view->normalized_subresource_range, kDetectAsync);
1723 }
John Zulaufa0a98292020-09-18 09:30:10 -06001724
John Zulauf355e49b2020-04-24 15:11:15 -06001725 return hazard;
1726}
1727
John Zulaufb02c1eb2020-10-06 16:33:36 -06001728void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
1729 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1730 const ResourceUsageTag &tag) {
1731 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001732 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001733 for (const auto &transition : transitions) {
1734 const auto prev_pass = transition.prev_pass;
1735 const auto attachment_view = attachment_views[transition.attachment];
1736 if (!attachment_view) continue;
1737 const auto *image = attachment_view->image_state.get();
1738 if (!image) continue;
1739 if (!SimpleBinding(*image)) continue;
1740
1741 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1742 assert(trackback);
1743
1744 // Import the attachments into the current context
1745 const auto *prev_context = trackback->context;
1746 assert(prev_context);
1747 const auto address_type = ImageAddressType(*image);
1748 auto &target_map = GetAccessStateMap(address_type);
1749 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
1750 prev_context->ResolveAccessRange(*image, attachment_view->normalized_subresource_range, barrier_action, address_type,
John Zulauf646cc292020-10-23 09:16:45 -06001751 &target_map, &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001752 }
1753
John Zulauf86356ca2020-10-19 11:46:41 -06001754 // If there were no transitions skip this global map walk
1755 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001756 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001757 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001758 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001759}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001760
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001761void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
1762 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
John Zulauf669dfd52021-01-27 17:15:28 -07001763
1764 auto *events_context = GetCurrentEventsContext();
1765 assert(events_context);
1766 for (auto &event_pair : *events_context) {
John Zulauf4a6105a2020-11-17 15:11:05 -07001767 assert(event_pair.second); // Shouldn't be storing empty
1768 auto &sync_event = *event_pair.second;
1769 // 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 -07001770 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
1771 sync_event.barriers |= dst.exec_scope;
1772 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
John Zulauf4a6105a2020-11-17 15:11:05 -07001773 }
1774 }
1775}
1776
John Zulauf355e49b2020-04-24 15:11:15 -06001777
locke-lunarg61870c22020-06-09 14:51:50 -06001778bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1779 const char *func_name) const {
1780 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001781 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001782 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001783 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1784 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001785 return skip;
1786 }
1787
1788 using DescriptorClass = cvdescriptorset::DescriptorClass;
1789 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1790 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1791 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1792 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1793
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001794 for (const auto &stage_state : pipe->stage_state) {
1795 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1796 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001797 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001798 }
locke-lunarg61870c22020-06-09 14:51:50 -06001799 for (const auto &set_binding : stage_state.descriptor_uses) {
1800 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1801 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1802 set_binding.first.second);
1803 const auto descriptor_type = binding_it.GetType();
1804 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1805 auto array_idx = 0;
1806
1807 if (binding_it.IsVariableDescriptorCount()) {
1808 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1809 }
1810 SyncStageAccessIndex sync_index =
1811 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1812
1813 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1814 uint32_t index = i - index_range.start;
1815 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1816 switch (descriptor->GetClass()) {
1817 case DescriptorClass::ImageSampler:
1818 case DescriptorClass::Image: {
1819 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001820 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001821 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001822 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1823 img_view_state = image_sampler_descriptor->GetImageViewState();
1824 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001825 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001826 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1827 img_view_state = image_descriptor->GetImageViewState();
1828 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001829 }
1830 if (!img_view_state) continue;
1831 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1832 VkExtent3D extent = {};
1833 VkOffset3D offset = {};
1834 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1835 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1836 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1837 } else {
1838 extent = img_state->createInfo.extent;
1839 }
John Zulauf361fb532020-07-22 10:45:39 -06001840 HazardResult hazard;
1841 const auto &subresource_range = img_view_state->normalized_subresource_range;
1842 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1843 // Input attachments are subject to raster ordering rules
1844 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001845 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001846 } else {
1847 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1848 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001849 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001850 skip |= sync_state_->LogError(
1851 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001852 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1853 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001854 func_name, string_SyncHazard(hazard.hazard),
1855 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1856 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001857 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001858 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1859 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
John Zulauffaea0ee2021-01-14 14:01:32 -07001860 set_binding.first.second, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001861 }
1862 break;
1863 }
1864 case DescriptorClass::TexelBuffer: {
1865 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1866 if (!buf_view_state) continue;
1867 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001868 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001869 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001870 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001871 skip |= sync_state_->LogError(
1872 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001873 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1874 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001875 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1876 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001877 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001878 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1879 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001880 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001881 }
1882 break;
1883 }
1884 case DescriptorClass::GeneralBuffer: {
1885 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1886 auto buf_state = buffer_descriptor->GetBufferState();
1887 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001888 const ResourceAccessRange range =
1889 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001890 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001891 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001892 skip |= sync_state_->LogError(
1893 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001894 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1895 func_name, string_SyncHazard(hazard.hazard),
1896 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001897 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001898 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001899 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1900 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001901 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001902 }
1903 break;
1904 }
1905 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1906 default:
1907 break;
1908 }
1909 }
1910 }
1911 }
1912 return skip;
1913}
1914
1915void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1916 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001917 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001918 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001919 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1920 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001921 return;
1922 }
1923
1924 using DescriptorClass = cvdescriptorset::DescriptorClass;
1925 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1926 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1927 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1928 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1929
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001930 for (const auto &stage_state : pipe->stage_state) {
1931 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1932 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001933 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001934 }
locke-lunarg61870c22020-06-09 14:51:50 -06001935 for (const auto &set_binding : stage_state.descriptor_uses) {
1936 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1937 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1938 set_binding.first.second);
1939 const auto descriptor_type = binding_it.GetType();
1940 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1941 auto array_idx = 0;
1942
1943 if (binding_it.IsVariableDescriptorCount()) {
1944 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1945 }
1946 SyncStageAccessIndex sync_index =
1947 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1948
1949 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1950 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1951 switch (descriptor->GetClass()) {
1952 case DescriptorClass::ImageSampler:
1953 case DescriptorClass::Image: {
1954 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1955 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1956 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1957 } else {
1958 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1959 }
1960 if (!img_view_state) continue;
1961 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1962 VkExtent3D extent = {};
1963 VkOffset3D offset = {};
1964 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1965 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1966 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1967 } else {
1968 extent = img_state->createInfo.extent;
1969 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001970 SyncOrdering ordering_rule = (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)
1971 ? SyncOrdering::kRaster
1972 : SyncOrdering::kNonAttachment;
1973 current_context_->UpdateAccessState(*img_state, sync_index, ordering_rule,
1974 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001975 break;
1976 }
1977 case DescriptorClass::TexelBuffer: {
1978 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1979 if (!buf_view_state) continue;
1980 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001981 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001982 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001983 break;
1984 }
1985 case DescriptorClass::GeneralBuffer: {
1986 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1987 auto buf_state = buffer_descriptor->GetBufferState();
1988 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001989 const ResourceAccessRange range =
1990 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07001991 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001992 break;
1993 }
1994 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1995 default:
1996 break;
1997 }
1998 }
1999 }
2000 }
2001}
2002
2003bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2004 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002005 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2006 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002007 return skip;
2008 }
2009
2010 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2011 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002012 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002013
2014 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002015 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002016 if (binding_description.binding < binding_buffers_size) {
2017 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002018 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002019
locke-lunarg1ae57d62020-11-18 10:49:19 -07002020 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002021 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2022 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002023 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002024 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002025 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002026 buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002027 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07002028 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002029 }
2030 }
2031 }
2032 return skip;
2033}
2034
2035void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002036 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2037 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002038 return;
2039 }
2040 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2041 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002042 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002043
2044 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002045 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002046 if (binding_description.binding < binding_buffers_size) {
2047 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002048 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002049
locke-lunarg1ae57d62020-11-18 10:49:19 -07002050 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002051 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2052 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002053 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2054 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002055 }
2056 }
2057}
2058
2059bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2060 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002061 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002062 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002063 }
locke-lunarg61870c22020-06-09 14:51:50 -06002064
locke-lunarg1ae57d62020-11-18 10:49:19 -07002065 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002066 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002067 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2068 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002069 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002070 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002071 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002072 index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002073 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07002074 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002075 }
2076
2077 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2078 // We will detect more accurate range in the future.
2079 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2080 return skip;
2081}
2082
2083void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002084 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 -06002085
locke-lunarg1ae57d62020-11-18 10:49:19 -07002086 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002087 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002088 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2089 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002090 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002091
2092 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2093 // We will detect more accurate range in the future.
2094 RecordDrawVertex(UINT32_MAX, 0, tag);
2095}
2096
2097bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002098 bool skip = false;
2099 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002100 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002101 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002102}
2103
2104void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002105 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002106 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002107 }
locke-lunarg61870c22020-06-09 14:51:50 -06002108}
2109
John Zulauf64ffe552021-02-06 10:25:07 -07002110void CommandBufferAccessContext::RecordBeginRenderPass(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2111 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2112 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002113 // Create an access context the current renderpass.
John Zulauf64ffe552021-02-06 10:25:07 -07002114 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002115 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf64ffe552021-02-06 10:25:07 -07002116 current_renderpass_context_->RecordBeginRenderPass(tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002117 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002118}
2119
John Zulauf64ffe552021-02-06 10:25:07 -07002120void CommandBufferAccessContext::RecordNextSubpass(CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002121 assert(current_renderpass_context_);
John Zulauffaea0ee2021-01-14 14:01:32 -07002122 auto prev_tag = NextCommandTag(command);
2123 auto next_tag = NextSubcommandTag(command);
John Zulauf64ffe552021-02-06 10:25:07 -07002124 current_renderpass_context_->RecordNextSubpass(prev_tag, next_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002125 current_context_ = &current_renderpass_context_->CurrentContext();
2126}
2127
John Zulauf64ffe552021-02-06 10:25:07 -07002128void CommandBufferAccessContext::RecordEndRenderPass(CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002129 assert(current_renderpass_context_);
2130 if (!current_renderpass_context_) return;
2131
John Zulauf64ffe552021-02-06 10:25:07 -07002132 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, NextCommandTag(command));
John Zulauf355e49b2020-04-24 15:11:15 -06002133 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002134 current_renderpass_context_ = nullptr;
2135}
2136
John Zulauf4a6105a2020-11-17 15:11:05 -07002137void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2138 // Erase is okay with the key not being
John Zulauf669dfd52021-01-27 17:15:28 -07002139 const auto *event_state = sync_state_->Get<EVENT_STATE>(event);
2140 if (event_state) {
2141 GetCurrentEventsContext()->Destroy(event_state);
John Zulaufd5115702021-01-18 12:34:33 -07002142 }
2143}
2144
John Zulauf64ffe552021-02-06 10:25:07 -07002145bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
John Zulauffaea0ee2021-01-14 14:01:32 -07002146 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002147 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002148 const auto &sync_state = ex_context.GetSyncState();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002149 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2150 if (!pipe ||
2151 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002152 return skip;
2153 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002154 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002155 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
John Zulauf64ffe552021-02-06 10:25:07 -07002156 VkExtent3D extent = CastTo3D(render_area_.extent);
2157 VkOffset3D offset = CastTo3D(render_area_.offset);
locke-lunarg37047832020-06-12 13:44:45 -06002158
John Zulauf1a224292020-06-30 14:52:13 -06002159 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002160 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002161 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2162 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002163 if (location >= subpass.colorAttachmentCount ||
2164 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002165 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002166 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002167 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002168 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07002169 SyncOrdering::kColorAttachment, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06002170 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002171 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002172 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002173 func_name, string_SyncHazard(hazard.hazard),
2174 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2175 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002176 location, ex_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002177 }
2178 }
2179 }
locke-lunarg37047832020-06-12 13:44:45 -06002180
2181 // PHASE1 TODO: Add layout based read/vs. write selection.
2182 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002183 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002184 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002185 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002186 bool depth_write = false, stencil_write = false;
2187
2188 // PHASE1 TODO: These validation should be in core_checks.
2189 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002190 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2191 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002192 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2193 depth_write = true;
2194 }
2195 // PHASE1 TODO: It needs to check if stencil is writable.
2196 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2197 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2198 // PHASE1 TODO: These validation should be in core_checks.
2199 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002200 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002201 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2202 stencil_write = true;
2203 }
2204
2205 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2206 if (depth_write) {
2207 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002208 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07002209 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002210 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002211 skip |= sync_state.LogError(
2212 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002213 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002214 func_name, string_SyncHazard(hazard.hazard),
2215 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2216 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002217 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002218 }
2219 }
2220 if (stencil_write) {
2221 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002222 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07002223 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002224 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002225 skip |= sync_state.LogError(
2226 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002227 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002228 func_name, string_SyncHazard(hazard.hazard),
2229 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2230 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002231 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002232 }
locke-lunarg61870c22020-06-09 14:51:50 -06002233 }
2234 }
2235 return skip;
2236}
2237
John Zulauf64ffe552021-02-06 10:25:07 -07002238void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002239 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2240 if (!pipe ||
2241 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002242 return;
2243 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002244 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002245 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
John Zulauf64ffe552021-02-06 10:25:07 -07002246 VkExtent3D extent = CastTo3D(render_area_.extent);
2247 VkOffset3D offset = CastTo3D(render_area_.offset);
locke-lunarg61870c22020-06-09 14:51:50 -06002248
John Zulauf1a224292020-06-30 14:52:13 -06002249 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002250 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002251 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2252 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002253 if (location >= subpass.colorAttachmentCount ||
2254 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002255 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002256 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002257 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf8e3c3e92021-01-06 11:19:36 -07002258 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
2259 SyncOrdering::kColorAttachment, offset, extent, 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002260 }
2261 }
locke-lunarg37047832020-06-12 13:44:45 -06002262
2263 // PHASE1 TODO: Add layout based read/vs. write selection.
2264 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002265 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002266 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002267 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002268 bool depth_write = false, stencil_write = false;
2269
2270 // PHASE1 TODO: These validation should be in core_checks.
2271 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002272 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2273 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002274 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2275 depth_write = true;
2276 }
2277 // PHASE1 TODO: It needs to check if stencil is writable.
2278 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2279 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2280 // PHASE1 TODO: These validation should be in core_checks.
2281 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002282 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002283 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2284 stencil_write = true;
2285 }
2286
2287 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2288 if (depth_write) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002289 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2290 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT,
2291 tag);
locke-lunarg37047832020-06-12 13:44:45 -06002292 }
2293 if (stencil_write) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002294 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2295 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT,
2296 tag);
locke-lunarg37047832020-06-12 13:44:45 -06002297 }
locke-lunarg61870c22020-06-09 14:51:50 -06002298 }
2299}
2300
John Zulauf64ffe552021-02-06 10:25:07 -07002301bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002302 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002303 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002304 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002305 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002306 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002307 func_name);
2308
John Zulauf355e49b2020-04-24 15:11:15 -06002309 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002310 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002311 skip |=
2312 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002313 if (!skip) {
2314 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2315 // on a copy of the (empty) next context.
2316 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2317 AccessContext temp_context(next_context);
2318 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002319 skip |=
2320 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002321 }
John Zulauf7635de32020-05-29 17:14:15 -06002322 return skip;
2323}
John Zulauf64ffe552021-02-06 10:25:07 -07002324bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002325 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002326 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002327 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002328 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002329 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002330 func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002331 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002332 return skip;
2333}
2334
John Zulauf64ffe552021-02-06 10:25:07 -07002335AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
2336 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002337}
2338
John Zulauf64ffe552021-02-06 10:25:07 -07002339bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2340 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002341 bool skip = false;
2342
John Zulauf7635de32020-05-29 17:14:15 -06002343 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2344 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2345 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2346 // to apply and only copy then, if this proves a hot spot.
2347 std::unique_ptr<AccessContext> proxy_for_current;
2348
John Zulauf355e49b2020-04-24 15:11:15 -06002349 // Validate the "finalLayout" transitions to external
2350 // Get them from where there we're hidding in the extra entry.
2351 const auto &final_transitions = rp_state_->subpass_transitions.back();
2352 for (const auto &transition : final_transitions) {
2353 const auto &attach_view = attachment_views_[transition.attachment];
2354 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2355 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002356 auto *context = trackback.context;
2357
2358 if (transition.prev_pass == current_subpass_) {
2359 if (!proxy_for_current) {
2360 // 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 -07002361 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002362 }
2363 context = proxy_for_current.get();
2364 }
2365
John Zulaufa0a98292020-09-18 09:30:10 -06002366 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2367 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufc523bf62021-02-16 08:20:34 -07002368 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope.exec_scope,
John Zulaufa0a98292020-09-18 09:30:10 -06002369 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2370 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002371 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002372 skip |= ex_context.GetSyncState().LogError(
John Zulauffaea0ee2021-01-14 14:01:32 -07002373 rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2374 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2375 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2376 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2377 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002378 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002379 }
2380 }
2381 return skip;
2382}
2383
2384void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2385 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002386 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002387}
2388
John Zulauf64ffe552021-02-06 10:25:07 -07002389void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag &tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002390 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2391 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf64ffe552021-02-06 10:25:07 -07002392 VkExtent3D extent = CastTo3D(render_area_.extent);
2393 VkOffset3D offset = CastTo3D(render_area_.offset);
John Zulauf1507ee42020-05-18 11:33:09 -06002394
2395 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2396 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2397 if (attachment_views_[i] == nullptr) continue; // UNUSED
2398 const auto &view = *attachment_views_[i];
2399 const IMAGE_STATE *image = view.image_state.get();
2400 if (image == nullptr) continue;
2401
2402 const auto &ci = attachment_ci[i];
2403 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002404 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002405 const bool is_color = !(has_depth || has_stencil);
2406
2407 if (is_color) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002408 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), SyncOrdering::kColorAttachment,
2409 view.normalized_subresource_range, offset, extent, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002410 } else {
2411 auto update_range = view.normalized_subresource_range;
2412 if (has_depth) {
2413 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07002414 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp),
2415 SyncOrdering::kDepthStencilAttachment, update_range, offset, extent, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002416 }
2417 if (has_stencil) {
2418 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07002419 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp),
2420 SyncOrdering::kDepthStencilAttachment, update_range, offset, extent, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002421 }
2422 }
2423 }
2424 }
2425}
John Zulauf64ffe552021-02-06 10:25:07 -07002426RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2427 VkQueueFlags queue_flags,
2428 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2429 const AccessContext *external_context)
2430 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_(attachment_views) {
John Zulauf355e49b2020-04-24 15:11:15 -06002431 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002432 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002433 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002434 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002435 }
John Zulauf64ffe552021-02-06 10:25:07 -07002436}
2437void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
2438 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002439 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002440 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002441 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002442}
John Zulauf1507ee42020-05-18 11:33:09 -06002443
John Zulauf64ffe552021-02-06 10:25:07 -07002444void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag &prev_subpass_tag,
John Zulauffaea0ee2021-01-14 14:01:32 -07002445 const ResourceUsageTag &next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002446 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulauf64ffe552021-02-06 10:25:07 -07002447 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area_, attachment_views_, current_subpass_, prev_subpass_tag);
2448 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002449
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002450 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2451 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002452 current_subpass_++;
2453 assert(current_subpass_ < subpass_contexts_.size());
John Zulauffaea0ee2021-01-14 14:01:32 -07002454 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2455 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002456 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002457}
2458
John Zulauf64ffe552021-02-06 10:25:07 -07002459void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002460 // Add the resolve and store accesses
John Zulauf64ffe552021-02-06 10:25:07 -07002461 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area_, attachment_views_, current_subpass_, tag);
2462 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002463
John Zulauf355e49b2020-04-24 15:11:15 -06002464 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002465 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002466
2467 // Add the "finalLayout" transitions to external
2468 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002469 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2470 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2471 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002472 const auto &final_transitions = rp_state_->subpass_transitions.back();
2473 for (const auto &transition : final_transitions) {
2474 const auto &attachment = attachment_views_[transition.attachment];
2475 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002476 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002477 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002478 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002479 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002480 }
John Zulauf1e331ec2020-12-04 18:29:38 -07002481 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002482 }
2483}
2484
Jeremy Gebben40a22942020-12-22 14:22:06 -07002485SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002486 SyncExecScope result;
2487 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002488 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2489 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002490 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2491 return result;
2492}
2493
Jeremy Gebben40a22942020-12-22 14:22:06 -07002494SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002495 SyncExecScope result;
2496 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002497 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2498 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002499 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2500 return result;
2501}
2502
2503SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002504 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002505 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002506 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002507 dst_access_scope = 0;
2508}
2509
2510template <typename Barrier>
2511SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002512 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002513 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002514 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002515 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2516}
2517
2518SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002519 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2520 if (barrier) {
2521 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002522 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002523 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002524
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002525 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002526 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002527 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2528
2529 } else {
2530 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002531 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002532 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2533
2534 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002535 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002536 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2537 }
2538}
2539
2540template <typename Barrier>
2541SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2542 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2543 src_exec_scope = src.exec_scope;
2544 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2545
2546 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002547 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002548 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002549}
2550
John Zulaufb02c1eb2020-10-06 16:33:36 -06002551// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2552void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2553 for (const auto &barrier : barriers) {
2554 ApplyBarrier(barrier, layout_transition);
2555 }
2556}
2557
John Zulauf89311b42020-09-29 16:28:47 -06002558// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2559// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2560// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002561void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2562 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002563 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002564 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002565 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002566 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002567 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002568 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002569}
John Zulauf9cb530d2019-09-30 14:14:10 -06002570HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2571 HazardResult hazard;
2572 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002573 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002574 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002575 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002576 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002577 }
2578 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002579 // Write operation:
2580 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2581 // If reads exists -- test only against them because either:
2582 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2583 // * 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
2584 // the current write happens after the reads, so just test the write against the reades
2585 // Otherwise test against last_write
2586 //
2587 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002588 if (last_reads.size()) {
2589 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002590 if (IsReadHazard(usage_stage, read_access)) {
2591 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2592 break;
2593 }
2594 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002595 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002596 // Write-After-Write check -- if we have a previous write to test against
2597 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002598 }
2599 }
2600 return hazard;
2601}
2602
John Zulauf8e3c3e92021-01-06 11:19:36 -07002603HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering &ordering_rule) const {
2604 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06002605 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2606 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002607 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002608 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002609 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2610 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002611 if (IsRead(usage_bit)) {
2612 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2613 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2614 if (is_raw_hazard) {
2615 // NOTE: we know last_write is non-zero
2616 // See if the ordering rules save us from the simple RAW check above
2617 // First check to see if the current usage is covered by the ordering rules
2618 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2619 const bool usage_is_ordered =
2620 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2621 if (usage_is_ordered) {
2622 // Now see of the most recent write (or a subsequent read) are ordered
2623 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2624 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002625 }
2626 }
John Zulauf4285ee92020-09-23 10:20:52 -06002627 if (is_raw_hazard) {
2628 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2629 }
John Zulauf361fb532020-07-22 10:45:39 -06002630 } else {
2631 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002632 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002633 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002634 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002635 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002636 if (usage_write_is_ordered) {
2637 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2638 ordered_stages = GetOrderedStages(ordering);
2639 }
2640 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2641 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002642 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002643 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2644 if (IsReadHazard(usage_stage, read_access)) {
2645 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2646 break;
2647 }
John Zulaufd14743a2020-07-03 09:42:39 -06002648 }
2649 }
John Zulauf4285ee92020-09-23 10:20:52 -06002650 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002651 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002652 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002653 }
John Zulauf69133422020-05-20 14:55:53 -06002654 }
2655 }
2656 return hazard;
2657}
2658
John Zulauf2f952d22020-02-10 11:34:51 -07002659// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002660HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002661 HazardResult hazard;
2662 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002663 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2664 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2665 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002666 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002667 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002668 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002669 }
2670 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002671 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002672 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002673 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002674 // 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 -07002675 for (const auto &read_access : last_reads) {
2676 if (read_access.tag.index >= start_tag.index) {
2677 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002678 break;
2679 }
2680 }
John Zulauf2f952d22020-02-10 11:34:51 -07002681 }
2682 }
2683 return hazard;
2684}
2685
Jeremy Gebben40a22942020-12-22 14:22:06 -07002686HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002687 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002688 // Only supporting image layout transitions for now
2689 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2690 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002691 // only test for WAW if there no intervening read operations.
2692 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002693 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002694 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002695 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002696 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002697 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002698 break;
2699 }
2700 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002701 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2702 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2703 }
2704
2705 return hazard;
2706}
2707
Jeremy Gebben40a22942020-12-22 14:22:06 -07002708HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07002709 const SyncStageAccessFlags &src_access_scope,
2710 const ResourceUsageTag &event_tag) const {
2711 // Only supporting image layout transitions for now
2712 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2713 HazardResult hazard;
2714 // only test for WAW if there no intervening read operations.
2715 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2716
John Zulaufab7756b2020-12-29 16:10:16 -07002717 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002718 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
2719 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07002720 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002721 if (read_access.tag.IsBefore(event_tag)) {
2722 // The read is in the events first synchronization scope, so we use a barrier hazard check
2723 // If the read stage is not in the src sync scope
2724 // *AND* not execution chained with an existing sync barrier (that's the or)
2725 // then the barrier access is unsafe (R/W after R)
2726 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
2727 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2728 break;
2729 }
2730 } else {
2731 // The read not in the event first sync scope and so is a hazard vs. the layout transition
2732 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2733 }
2734 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002735 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002736 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
2737 if (write_tag.IsBefore(event_tag)) {
2738 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
2739 // So do a normal barrier hazard check
2740 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2741 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2742 }
2743 } else {
2744 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06002745 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2746 }
John Zulaufd14743a2020-07-03 09:42:39 -06002747 }
John Zulauf361fb532020-07-22 10:45:39 -06002748
John Zulauf0cb5be22020-01-23 12:18:22 -07002749 return hazard;
2750}
2751
John Zulauf5f13a792020-03-10 07:31:21 -06002752// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2753// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2754// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2755void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2756 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002757 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2758 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002759 *this = other;
2760 } else if (!other.write_tag.IsBefore(write_tag)) {
2761 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2762 // dependency chaining logic or any stage expansion)
2763 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002764 pending_write_barriers |= other.pending_write_barriers;
2765 pending_layout_transition |= other.pending_layout_transition;
2766 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002767
John Zulaufd14743a2020-07-03 09:42:39 -06002768 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07002769 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06002770 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07002771 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002772 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002773 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002774 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002775 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2776 // but we should wait on profiling data for that.
2777 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002778 auto &my_read = last_reads[my_read_index];
2779 if (other_read.stage == my_read.stage) {
2780 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002781 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002782 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002783 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002784 my_read.pending_dep_chain = other_read.pending_dep_chain;
2785 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2786 // May require tracking more than one access per stage.
2787 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002788 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06002789 // Since I'm overwriting the fragement stage read, also update the input attachment info
2790 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002791 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002792 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002793 } else if (other_read.tag.IsBefore(my_read.tag)) {
2794 // The read tags match so merge the barriers
2795 my_read.barriers |= other_read.barriers;
2796 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002797 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002798
John Zulauf5f13a792020-03-10 07:31:21 -06002799 break;
2800 }
2801 }
2802 } else {
2803 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07002804 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06002805 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002806 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002807 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002808 }
John Zulauf5f13a792020-03-10 07:31:21 -06002809 }
2810 }
John Zulauf361fb532020-07-22 10:45:39 -06002811 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002812 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2813 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07002814
2815 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
2816 // of the copy and other into this using the update first logic.
2817 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
2818 // of the other first_accesses... )
2819 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
2820 FirstAccesses firsts(std::move(first_accesses_));
2821 first_accesses_.clear();
2822 first_read_stages_ = 0U;
2823 auto a = firsts.begin();
2824 auto a_end = firsts.end();
2825 for (auto &b : other.first_accesses_) {
2826 // TODO: Determine whether "IsBefore" or "IsGloballyBefore" is needed...
2827 while (a != a_end && a->tag.IsBefore(b.tag)) {
2828 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2829 ++a;
2830 }
2831 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
2832 }
2833 for (; a != a_end; ++a) {
2834 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2835 }
2836 }
John Zulauf5f13a792020-03-10 07:31:21 -06002837}
2838
John Zulauf8e3c3e92021-01-06 11:19:36 -07002839void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag &tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002840 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2841 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002842 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002843 // Mulitple outstanding reads may be of interest and do dependency chains independently
2844 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2845 const auto usage_stage = PipelineStageBit(usage_index);
2846 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002847 for (auto &read_access : last_reads) {
2848 if (read_access.stage == usage_stage) {
2849 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002850 break;
2851 }
2852 }
2853 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07002854 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002855 last_read_stages |= usage_stage;
2856 }
John Zulauf4285ee92020-09-23 10:20:52 -06002857
2858 // 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 -07002859 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002860 // TODO Revisit re: multiple reads for a given stage
2861 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002862 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002863 } else {
2864 // Assume write
2865 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002866 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002867 }
John Zulauffaea0ee2021-01-14 14:01:32 -07002868 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06002869}
John Zulauf5f13a792020-03-10 07:31:21 -06002870
John Zulauf89311b42020-09-29 16:28:47 -06002871// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2872// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2873// We can overwrite them as *this* write is now after them.
2874//
2875// 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 -07002876void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002877 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06002878 last_read_stages = 0;
2879 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002880 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002881
2882 write_barriers = 0;
2883 write_dependency_chain = 0;
2884 write_tag = tag;
2885 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002886}
2887
John Zulauf89311b42020-09-29 16:28:47 -06002888// Apply the memory barrier without updating the existing barriers. The execution barrier
2889// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2890// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2891// replace the current write barriers or add to them, so accumulate to pending as well.
2892void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2893 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2894 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002895 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
2896 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
2897 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
2898 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07002899 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06002900 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07002901 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002902 }
John Zulauf89311b42020-09-29 16:28:47 -06002903 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2904 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002905
John Zulauf89311b42020-09-29 16:28:47 -06002906 if (!pending_layout_transition) {
2907 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2908 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002909 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06002910 // 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 -07002911 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
2912 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002913 }
2914 }
John Zulaufa0a98292020-09-18 09:30:10 -06002915 }
John Zulaufa0a98292020-09-18 09:30:10 -06002916}
2917
John Zulauf4a6105a2020-11-17 15:11:05 -07002918// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
2919// changes the "chaining" state, but to keep barriers independent. See discussion above.
2920void ResourceAccessState::ApplyBarrier(const ResourceUsageTag &scope_tag, const SyncBarrier &barrier, bool layout_transition) {
2921 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
2922 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
2923 // in order to know if it's in the excecution scope
2924 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
2925 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
2926 // errors w.r.t. "most recent" accesses.
2927 if (layout_transition || ((write_tag.IsBefore(scope_tag)) && (barrier.src_access_scope & last_write).any())) {
2928 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07002929 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002930 }
2931 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2932 pending_layout_transition |= layout_transition;
2933
2934 if (!pending_layout_transition) {
2935 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2936 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002937 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002938 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
2939 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
2940 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
2941 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
2942 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
2943 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
2944 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulaufc523bf62021-02-16 08:20:34 -07002945 if (read_access.tag.IsBefore(scope_tag) &&
2946 (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers))) {
2947 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002948 }
2949 }
2950 }
2951}
John Zulauf89311b42020-09-29 16:28:47 -06002952void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
2953 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06002954 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2955 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07002956 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf89311b42020-09-29 16:28:47 -06002957 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002958 }
John Zulauf89311b42020-09-29 16:28:47 -06002959
2960 // Apply the accumulate execution barriers (and thus update chaining information)
2961 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07002962 for (auto &read_access : last_reads) {
2963 read_access.barriers |= read_access.pending_dep_chain;
2964 read_execution_barriers |= read_access.barriers;
2965 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06002966 }
2967
2968 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2969 write_dependency_chain |= pending_write_dep_chain;
2970 write_barriers |= pending_write_barriers;
2971 pending_write_dep_chain = 0;
2972 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002973}
2974
John Zulauf59e25072020-07-17 10:55:21 -06002975// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07002976VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
2977 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002978
John Zulaufab7756b2020-12-29 16:10:16 -07002979 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002980 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06002981 barriers = read_access.barriers;
2982 break;
John Zulauf59e25072020-07-17 10:55:21 -06002983 }
2984 }
John Zulauf4285ee92020-09-23 10:20:52 -06002985
John Zulauf59e25072020-07-17 10:55:21 -06002986 return barriers;
2987}
2988
Jeremy Gebben40a22942020-12-22 14:22:06 -07002989inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002990 assert(IsRead(usage));
2991 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
2992 // * the previous reads are not hazards, and thus last_write must be visible and available to
2993 // any reads that happen after.
2994 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2995 // 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 -07002996 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06002997}
2998
Jeremy Gebben40a22942020-12-22 14:22:06 -07002999VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003000 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07003001 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06003002 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003003 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003004 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003005 // 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 -07003006 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06003007 }
3008
3009 return ordered_stages;
3010}
3011
John Zulauffaea0ee2021-01-14 14:01:32 -07003012void ResourceAccessState::UpdateFirst(const ResourceUsageTag &tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
3013 // Only record until we record a write.
3014 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003015 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003016 if (0 == (usage_stage & first_read_stages_)) {
3017 // If this is a read we haven't seen or a write, record.
3018 first_read_stages_ |= usage_stage;
3019 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3020 }
3021 }
3022}
3023
John Zulaufd1f85d42020-04-15 12:23:15 -06003024void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003025 auto *access_context = GetAccessContextNoInsert(command_buffer);
3026 if (access_context) {
3027 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003028 }
3029}
3030
John Zulaufd1f85d42020-04-15 12:23:15 -06003031void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3032 auto access_found = cb_access_state.find(command_buffer);
3033 if (access_found != cb_access_state.end()) {
3034 access_found->second->Reset();
3035 cb_access_state.erase(access_found);
3036 }
3037}
3038
John Zulauf9cb530d2019-09-30 14:14:10 -06003039bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3040 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3041 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003042 const auto *cb_context = GetAccessContext(commandBuffer);
3043 assert(cb_context);
3044 if (!cb_context) return skip;
3045 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003046
John Zulauf3d84f1b2020-03-09 13:33:25 -06003047 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003048 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003049 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003050
3051 for (uint32_t region = 0; region < regionCount; region++) {
3052 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003053 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003054 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003055 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003056 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003057 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003058 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003059 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003060 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003061 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003062 }
John Zulauf16adfc92020-04-08 10:28:33 -06003063 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003064 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003065 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003066 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003067 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003068 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003069 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003070 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003071 }
3072 }
3073 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003074 }
3075 return skip;
3076}
3077
3078void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3079 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003080 auto *cb_context = GetAccessContext(commandBuffer);
3081 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003082 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003083 auto *context = cb_context->GetCurrentAccessContext();
3084
John Zulauf9cb530d2019-09-30 14:14:10 -06003085 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003086 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003087
3088 for (uint32_t region = 0; region < regionCount; region++) {
3089 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003090 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003091 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003092 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003093 }
John Zulauf16adfc92020-04-08 10:28:33 -06003094 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003095 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003096 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003097 }
3098 }
3099}
3100
John Zulauf4a6105a2020-11-17 15:11:05 -07003101void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3102 // Clear out events from the command buffer contexts
3103 for (auto &cb_context : cb_access_state) {
3104 cb_context.second->RecordDestroyEvent(event);
3105 }
3106}
3107
Jeff Leger178b1e52020-10-05 12:22:23 -04003108bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3109 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3110 bool skip = false;
3111 const auto *cb_context = GetAccessContext(commandBuffer);
3112 assert(cb_context);
3113 if (!cb_context) return skip;
3114 const auto *context = cb_context->GetCurrentAccessContext();
3115
3116 // If we have no previous accesses, we have no hazards
3117 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3118 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3119
3120 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3121 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3122 if (src_buffer) {
3123 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003124 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003125 if (hazard.hazard) {
3126 // TODO -- add tag information to log msg when useful.
3127 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3128 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3129 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003130 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003131 }
3132 }
3133 if (dst_buffer && !skip) {
3134 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003135 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003136 if (hazard.hazard) {
3137 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3138 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3139 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003140 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003141 }
3142 }
3143 if (skip) break;
3144 }
3145 return skip;
3146}
3147
3148void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3149 auto *cb_context = GetAccessContext(commandBuffer);
3150 assert(cb_context);
3151 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3152 auto *context = cb_context->GetCurrentAccessContext();
3153
3154 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3155 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3156
3157 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3158 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3159 if (src_buffer) {
3160 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003161 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003162 }
3163 if (dst_buffer) {
3164 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003165 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003166 }
3167 }
3168}
3169
John Zulauf5c5e88d2019-12-26 11:22:02 -07003170bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3171 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3172 const VkImageCopy *pRegions) const {
3173 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003174 const auto *cb_access_context = GetAccessContext(commandBuffer);
3175 assert(cb_access_context);
3176 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003177
John Zulauf3d84f1b2020-03-09 13:33:25 -06003178 const auto *context = cb_access_context->GetCurrentAccessContext();
3179 assert(context);
3180 if (!context) return skip;
3181
3182 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3183 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003184 for (uint32_t region = 0; region < regionCount; region++) {
3185 const auto &copy_region = pRegions[region];
3186 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003187 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003188 copy_region.srcOffset, copy_region.extent);
3189 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003190 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003191 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003192 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003193 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003194 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003195 }
3196
3197 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003198 VkExtent3D dst_copy_extent =
3199 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003200 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003201 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003202 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003203 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003204 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003205 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003206 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003207 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003208 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003209 }
3210 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003211
John Zulauf5c5e88d2019-12-26 11:22:02 -07003212 return skip;
3213}
3214
3215void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3216 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3217 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003218 auto *cb_access_context = GetAccessContext(commandBuffer);
3219 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003220 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003221 auto *context = cb_access_context->GetCurrentAccessContext();
3222 assert(context);
3223
John Zulauf5c5e88d2019-12-26 11:22:02 -07003224 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003225 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003226
3227 for (uint32_t region = 0; region < regionCount; region++) {
3228 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003229 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003230 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003231 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003232 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003233 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003234 VkExtent3D dst_copy_extent =
3235 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003236 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003237 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003238 }
3239 }
3240}
3241
Jeff Leger178b1e52020-10-05 12:22:23 -04003242bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3243 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3244 bool skip = false;
3245 const auto *cb_access_context = GetAccessContext(commandBuffer);
3246 assert(cb_access_context);
3247 if (!cb_access_context) return skip;
3248
3249 const auto *context = cb_access_context->GetCurrentAccessContext();
3250 assert(context);
3251 if (!context) return skip;
3252
3253 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3254 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3255 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3256 const auto &copy_region = pCopyImageInfo->pRegions[region];
3257 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003258 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003259 copy_region.srcOffset, copy_region.extent);
3260 if (hazard.hazard) {
3261 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3262 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3263 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003264 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003265 }
3266 }
3267
3268 if (dst_image) {
3269 VkExtent3D dst_copy_extent =
3270 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003271 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003272 copy_region.dstOffset, dst_copy_extent);
3273 if (hazard.hazard) {
3274 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3275 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3276 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003277 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003278 }
3279 if (skip) break;
3280 }
3281 }
3282
3283 return skip;
3284}
3285
3286void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3287 auto *cb_access_context = GetAccessContext(commandBuffer);
3288 assert(cb_access_context);
3289 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3290 auto *context = cb_access_context->GetCurrentAccessContext();
3291 assert(context);
3292
3293 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3294 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3295
3296 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3297 const auto &copy_region = pCopyImageInfo->pRegions[region];
3298 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003299 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003300 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003301 }
3302 if (dst_image) {
3303 VkExtent3D dst_copy_extent =
3304 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003305 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003306 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003307 }
3308 }
3309}
3310
John Zulauf9cb530d2019-09-30 14:14:10 -06003311bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3312 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3313 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3314 uint32_t bufferMemoryBarrierCount,
3315 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3316 uint32_t imageMemoryBarrierCount,
3317 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3318 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003319 const auto *cb_access_context = GetAccessContext(commandBuffer);
3320 assert(cb_access_context);
3321 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003322
John Zulauf36ef9282021-02-02 11:47:24 -07003323 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3324 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3325 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3326 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003327 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003328 return skip;
3329}
3330
3331void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3332 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3333 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3334 uint32_t bufferMemoryBarrierCount,
3335 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3336 uint32_t imageMemoryBarrierCount,
3337 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003338 auto *cb_access_context = GetAccessContext(commandBuffer);
3339 assert(cb_access_context);
3340 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003341
John Zulauf36ef9282021-02-02 11:47:24 -07003342 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3343 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3344 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3345 pImageMemoryBarriers);
3346 pipeline_barrier.Record(cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003347}
3348
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003349bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3350 const VkDependencyInfoKHR *pDependencyInfo) const {
3351 bool skip = false;
3352 const auto *cb_access_context = GetAccessContext(commandBuffer);
3353 assert(cb_access_context);
3354 if (!cb_access_context) return skip;
3355
3356 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3357 skip = pipeline_barrier.Validate(*cb_access_context);
3358 return skip;
3359}
3360
3361void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3362 auto *cb_access_context = GetAccessContext(commandBuffer);
3363 assert(cb_access_context);
3364 if (!cb_access_context) return;
3365
3366 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3367 pipeline_barrier.Record(cb_access_context);
3368}
3369
John Zulauf9cb530d2019-09-30 14:14:10 -06003370void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3371 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3372 // The state tracker sets up the device state
3373 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3374
John Zulauf5f13a792020-03-10 07:31:21 -06003375 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3376 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003377 // TODO: Find a good way to do this hooklessly.
3378 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3379 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3380 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3381
John Zulaufd1f85d42020-04-15 12:23:15 -06003382 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3383 sync_device_state->ResetCommandBufferCallback(command_buffer);
3384 });
3385 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3386 sync_device_state->FreeCommandBufferCallback(command_buffer);
3387 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003388}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003389
John Zulauf355e49b2020-04-24 15:11:15 -06003390bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf64ffe552021-02-06 10:25:07 -07003391 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd, const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003392 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003393 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003394 if (cb_context) {
3395 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo, cmd_name);
3396 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003397 }
John Zulauf355e49b2020-04-24 15:11:15 -06003398 return skip;
3399}
3400
3401bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3402 VkSubpassContents contents) const {
3403 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003404 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003405 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003406 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003407 return skip;
3408}
3409
3410bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003411 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003412 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003413 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003414 return skip;
3415}
3416
John Zulauf64ffe552021-02-06 10:25:07 -07003417static const char *kBeginRenderPass2KhrName = "vkCmdBeginRenderPass2KHR";
John Zulauf355e49b2020-04-24 15:11:15 -06003418bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3419 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003420 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003421 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003422 skip |=
3423 ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2, kBeginRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003424 return skip;
3425}
3426
John Zulauf3d84f1b2020-03-09 13:33:25 -06003427void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3428 VkResult result) {
3429 // The state tracker sets up the command buffer state
3430 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3431
3432 // Create/initialize the structure that trackers accesses at the command buffer scope.
3433 auto cb_access_context = GetAccessContext(commandBuffer);
3434 assert(cb_access_context);
3435 cb_access_context->Reset();
3436}
3437
3438void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf64ffe552021-02-06 10:25:07 -07003439 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd, const char *cmd_name) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003440 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003441 if (cb_context) {
John Zulauf64ffe552021-02-06 10:25:07 -07003442 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo, cmd_name);
3443 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003444 }
3445}
3446
3447void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3448 VkSubpassContents contents) {
3449 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003450 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003451 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003452 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003453}
3454
3455void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3456 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3457 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003458 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003459}
3460
3461void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3462 const VkRenderPassBeginInfo *pRenderPassBegin,
3463 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3464 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003465 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2, kBeginRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003466}
3467
Mike Schuchardt2df08912020-12-15 16:28:09 -08003468bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf64ffe552021-02-06 10:25:07 -07003469 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd, const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003470 bool skip = false;
3471
3472 auto cb_context = GetAccessContext(commandBuffer);
3473 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003474 if (!cb_context) return skip;
3475 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo, cmd_name);
3476 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003477}
3478
3479bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3480 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003481 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003482 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003483 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003484 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3485 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003486 return skip;
3487}
3488
John Zulauf64ffe552021-02-06 10:25:07 -07003489static const char *kNextSubpass2KhrName = "vkCmdNextSubpass2KHR";
Mike Schuchardt2df08912020-12-15 16:28:09 -08003490bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3491 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003492 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003493 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2, kNextSubpass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003494 return skip;
3495}
3496
3497bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3498 const VkSubpassEndInfo *pSubpassEndInfo) const {
3499 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003500 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003501 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003502}
3503
3504void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf64ffe552021-02-06 10:25:07 -07003505 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd, const char *cmd_name) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003506 auto cb_context = GetAccessContext(commandBuffer);
3507 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003508 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003509
John Zulauf64ffe552021-02-06 10:25:07 -07003510 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo, cmd_name);
3511 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003512}
3513
3514void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3515 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003516 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003517 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003518 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003519}
3520
3521void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3522 const VkSubpassEndInfo *pSubpassEndInfo) {
3523 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003524 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003525}
3526
3527void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3528 const VkSubpassEndInfo *pSubpassEndInfo) {
3529 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003530 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2, kNextSubpass2KhrName);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003531}
3532
John Zulauf64ffe552021-02-06 10:25:07 -07003533bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd,
3534 const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003535 bool skip = false;
3536
3537 auto cb_context = GetAccessContext(commandBuffer);
3538 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003539 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003540
John Zulauf64ffe552021-02-06 10:25:07 -07003541 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo, cmd_name);
3542 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003543 return skip;
3544}
3545
3546bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3547 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003548 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003549 return skip;
3550}
3551
Mike Schuchardt2df08912020-12-15 16:28:09 -08003552bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003553 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003554 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003555 return skip;
3556}
3557
John Zulauf64ffe552021-02-06 10:25:07 -07003558const static char *kEndRenderPass2KhrName = "vkEndRenderPass2KHR";
John Zulauf355e49b2020-04-24 15:11:15 -06003559bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003560 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003561 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003562 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2, kEndRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003563 return skip;
3564}
3565
John Zulauf64ffe552021-02-06 10:25:07 -07003566void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd,
3567 const char *cmd_name) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003568 // Resolve the all subpass contexts to the command buffer contexts
3569 auto cb_context = GetAccessContext(commandBuffer);
3570 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003571 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003572
John Zulauf64ffe552021-02-06 10:25:07 -07003573 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo, cmd_name);
3574 sync_op.Record(cb_context);
3575 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003576}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003577
John Zulauf33fc1d52020-07-17 11:01:10 -06003578// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3579// updates to a resource which do not conflict at the byte level.
3580// TODO: Revisit this rule to see if it needs to be tighter or looser
3581// TODO: Add programatic control over suppression heuristics
3582bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3583 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3584}
3585
John Zulauf3d84f1b2020-03-09 13:33:25 -06003586void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003587 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003588 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003589}
3590
3591void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003592 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003593 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003594}
3595
3596void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf64ffe552021-02-06 10:25:07 -07003597 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2, kEndRenderPass2KhrName);
John Zulauf5a1a5382020-06-22 17:23:25 -06003598 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003599}
locke-lunarga19c71d2020-03-02 18:17:04 -07003600
Jeff Leger178b1e52020-10-05 12:22:23 -04003601template <typename BufferImageCopyRegionType>
3602bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3603 VkImageLayout dstImageLayout, uint32_t regionCount,
3604 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003605 bool skip = false;
3606 const auto *cb_access_context = GetAccessContext(commandBuffer);
3607 assert(cb_access_context);
3608 if (!cb_access_context) return skip;
3609
Jeff Leger178b1e52020-10-05 12:22:23 -04003610 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3611 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3612
locke-lunarga19c71d2020-03-02 18:17:04 -07003613 const auto *context = cb_access_context->GetCurrentAccessContext();
3614 assert(context);
3615 if (!context) return skip;
3616
3617 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003618 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3619
3620 for (uint32_t region = 0; region < regionCount; region++) {
3621 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003622 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003623 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003624 if (src_buffer) {
3625 ResourceAccessRange src_range =
3626 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003627 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07003628 if (hazard.hazard) {
3629 // PHASE1 TODO -- add tag information to log msg when useful.
3630 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3631 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3632 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003633 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003634 }
3635 }
3636
Jeremy Gebben40a22942020-12-22 14:22:06 -07003637 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07003638 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003639 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003640 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003641 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003642 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003643 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003644 }
3645 if (skip) break;
3646 }
3647 if (skip) break;
3648 }
3649 return skip;
3650}
3651
Jeff Leger178b1e52020-10-05 12:22:23 -04003652bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3653 VkImageLayout dstImageLayout, uint32_t regionCount,
3654 const VkBufferImageCopy *pRegions) const {
3655 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3656 COPY_COMMAND_VERSION_1);
3657}
3658
3659bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3660 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3661 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3662 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3663 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3664}
3665
3666template <typename BufferImageCopyRegionType>
3667void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3668 VkImageLayout dstImageLayout, uint32_t regionCount,
3669 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003670 auto *cb_access_context = GetAccessContext(commandBuffer);
3671 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003672
3673 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3674 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3675
3676 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003677 auto *context = cb_access_context->GetCurrentAccessContext();
3678 assert(context);
3679
3680 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003681 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003682
3683 for (uint32_t region = 0; region < regionCount; region++) {
3684 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07003685 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003686 if (src_buffer) {
3687 ResourceAccessRange src_range =
3688 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003689 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003690 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07003691 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003692 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003693 }
3694 }
3695}
3696
Jeff Leger178b1e52020-10-05 12:22:23 -04003697void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3698 VkImageLayout dstImageLayout, uint32_t regionCount,
3699 const VkBufferImageCopy *pRegions) {
3700 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3701 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3702}
3703
3704void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3705 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3706 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3707 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3708 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3709 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3710}
3711
3712template <typename BufferImageCopyRegionType>
3713bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3714 VkBuffer dstBuffer, uint32_t regionCount,
3715 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003716 bool skip = false;
3717 const auto *cb_access_context = GetAccessContext(commandBuffer);
3718 assert(cb_access_context);
3719 if (!cb_access_context) return skip;
3720
Jeff Leger178b1e52020-10-05 12:22:23 -04003721 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3722 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3723
locke-lunarga19c71d2020-03-02 18:17:04 -07003724 const auto *context = cb_access_context->GetCurrentAccessContext();
3725 assert(context);
3726 if (!context) return skip;
3727
3728 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3729 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3730 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3731 for (uint32_t region = 0; region < regionCount; region++) {
3732 const auto &copy_region = pRegions[region];
3733 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003734 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003735 copy_region.imageOffset, copy_region.imageExtent);
3736 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003737 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003738 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003739 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003740 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003741 }
John Zulauf477700e2021-01-06 11:41:49 -07003742 if (dst_mem) {
3743 ResourceAccessRange dst_range =
3744 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003745 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07003746 if (hazard.hazard) {
3747 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3748 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3749 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003750 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003751 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003752 }
3753 }
3754 if (skip) break;
3755 }
3756 return skip;
3757}
3758
Jeff Leger178b1e52020-10-05 12:22:23 -04003759bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3760 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3761 const VkBufferImageCopy *pRegions) const {
3762 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3763 COPY_COMMAND_VERSION_1);
3764}
3765
3766bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3767 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3768 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3769 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3770 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3771}
3772
3773template <typename BufferImageCopyRegionType>
3774void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3775 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3776 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003777 auto *cb_access_context = GetAccessContext(commandBuffer);
3778 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003779
3780 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3781 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3782
3783 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003784 auto *context = cb_access_context->GetCurrentAccessContext();
3785 assert(context);
3786
3787 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003788 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3789 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 -06003790 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003791
3792 for (uint32_t region = 0; region < regionCount; region++) {
3793 const auto &copy_region = pRegions[region];
3794 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003795 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003796 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003797 if (dst_buffer) {
3798 ResourceAccessRange dst_range =
3799 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003800 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003801 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003802 }
3803 }
3804}
3805
Jeff Leger178b1e52020-10-05 12:22:23 -04003806void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3807 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3808 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3809 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3810}
3811
3812void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3813 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3814 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3815 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3816 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3817 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3818}
3819
3820template <typename RegionType>
3821bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3822 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3823 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003824 bool skip = false;
3825 const auto *cb_access_context = GetAccessContext(commandBuffer);
3826 assert(cb_access_context);
3827 if (!cb_access_context) return skip;
3828
3829 const auto *context = cb_access_context->GetCurrentAccessContext();
3830 assert(context);
3831 if (!context) return skip;
3832
3833 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3834 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3835
3836 for (uint32_t region = 0; region < regionCount; region++) {
3837 const auto &blit_region = pRegions[region];
3838 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003839 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3840 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3841 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3842 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3843 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3844 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003845 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003846 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003847 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003848 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003849 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003850 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003851 }
3852 }
3853
3854 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003855 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3856 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3857 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3858 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3859 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3860 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003861 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003862 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003863 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003864 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003865 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003866 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003867 }
3868 if (skip) break;
3869 }
3870 }
3871
3872 return skip;
3873}
3874
Jeff Leger178b1e52020-10-05 12:22:23 -04003875bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3876 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3877 const VkImageBlit *pRegions, VkFilter filter) const {
3878 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3879 "vkCmdBlitImage");
3880}
3881
3882bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3883 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3884 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3885 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3886 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3887}
3888
3889template <typename RegionType>
3890void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3891 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3892 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003893 auto *cb_access_context = GetAccessContext(commandBuffer);
3894 assert(cb_access_context);
3895 auto *context = cb_access_context->GetCurrentAccessContext();
3896 assert(context);
3897
3898 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003899 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003900
3901 for (uint32_t region = 0; region < regionCount; region++) {
3902 const auto &blit_region = pRegions[region];
3903 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003904 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3905 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3906 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3907 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3908 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3909 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003910 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003911 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003912 }
3913 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003914 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3915 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3916 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3917 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3918 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3919 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003920 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003921 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003922 }
3923 }
3924}
locke-lunarg36ba2592020-04-03 09:42:04 -06003925
Jeff Leger178b1e52020-10-05 12:22:23 -04003926void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3927 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3928 const VkImageBlit *pRegions, VkFilter filter) {
3929 auto *cb_access_context = GetAccessContext(commandBuffer);
3930 assert(cb_access_context);
3931 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3932 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3933 pRegions, filter);
3934 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3935}
3936
3937void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3938 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3939 auto *cb_access_context = GetAccessContext(commandBuffer);
3940 assert(cb_access_context);
3941 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3942 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3943 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3944 pBlitImageInfo->filter, tag);
3945}
3946
John Zulauffaea0ee2021-01-14 14:01:32 -07003947bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
3948 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
3949 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
3950 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003951 bool skip = false;
3952 if (drawCount == 0) return skip;
3953
3954 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3955 VkDeviceSize size = struct_size;
3956 if (drawCount == 1 || stride == size) {
3957 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003958 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003959 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3960 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003961 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003962 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003963 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003964 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003965 }
3966 } else {
3967 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003968 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003969 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3970 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003971 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003972 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3973 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003974 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003975 break;
3976 }
3977 }
3978 }
3979 return skip;
3980}
3981
locke-lunarg61870c22020-06-09 14:51:50 -06003982void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3983 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3984 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003985 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3986 VkDeviceSize size = struct_size;
3987 if (drawCount == 1 || stride == size) {
3988 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003989 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003990 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003991 } else {
3992 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003993 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003994 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
3995 tag);
locke-lunargff255f92020-05-13 18:53:52 -06003996 }
3997 }
3998}
3999
John Zulauffaea0ee2021-01-14 14:01:32 -07004000bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4001 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4002 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004003 bool skip = false;
4004
4005 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004006 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004007 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4008 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004009 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004010 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004011 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004012 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004013 }
4014 return skip;
4015}
4016
locke-lunarg61870c22020-06-09 14:51:50 -06004017void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06004018 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004019 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004020 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004021}
4022
locke-lunarg36ba2592020-04-03 09:42:04 -06004023bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004024 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004025 const auto *cb_access_context = GetAccessContext(commandBuffer);
4026 assert(cb_access_context);
4027 if (!cb_access_context) return skip;
4028
locke-lunarg61870c22020-06-09 14:51:50 -06004029 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004030 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004031}
4032
4033void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004034 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004035 auto *cb_access_context = GetAccessContext(commandBuffer);
4036 assert(cb_access_context);
4037 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004038
locke-lunarg61870c22020-06-09 14:51:50 -06004039 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004040}
locke-lunarge1a67022020-04-29 00:15:36 -06004041
4042bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004043 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004044 const auto *cb_access_context = GetAccessContext(commandBuffer);
4045 assert(cb_access_context);
4046 if (!cb_access_context) return skip;
4047
4048 const auto *context = cb_access_context->GetCurrentAccessContext();
4049 assert(context);
4050 if (!context) return skip;
4051
locke-lunarg61870c22020-06-09 14:51:50 -06004052 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004053 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4054 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004055 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004056}
4057
4058void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004059 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004060 auto *cb_access_context = GetAccessContext(commandBuffer);
4061 assert(cb_access_context);
4062 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4063 auto *context = cb_access_context->GetCurrentAccessContext();
4064 assert(context);
4065
locke-lunarg61870c22020-06-09 14:51:50 -06004066 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4067 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004068}
4069
4070bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4071 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004072 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004073 const auto *cb_access_context = GetAccessContext(commandBuffer);
4074 assert(cb_access_context);
4075 if (!cb_access_context) return skip;
4076
locke-lunarg61870c22020-06-09 14:51:50 -06004077 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4078 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4079 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004080 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004081}
4082
4083void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4084 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004085 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004086 auto *cb_access_context = GetAccessContext(commandBuffer);
4087 assert(cb_access_context);
4088 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004089
locke-lunarg61870c22020-06-09 14:51:50 -06004090 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4091 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4092 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004093}
4094
4095bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4096 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004097 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004098 const auto *cb_access_context = GetAccessContext(commandBuffer);
4099 assert(cb_access_context);
4100 if (!cb_access_context) return skip;
4101
locke-lunarg61870c22020-06-09 14:51:50 -06004102 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4103 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4104 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004105 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004106}
4107
4108void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4109 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004110 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004111 auto *cb_access_context = GetAccessContext(commandBuffer);
4112 assert(cb_access_context);
4113 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004114
locke-lunarg61870c22020-06-09 14:51:50 -06004115 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4116 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4117 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004118}
4119
4120bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4121 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004122 bool skip = false;
4123 if (drawCount == 0) return skip;
4124
locke-lunargff255f92020-05-13 18:53:52 -06004125 const auto *cb_access_context = GetAccessContext(commandBuffer);
4126 assert(cb_access_context);
4127 if (!cb_access_context) return skip;
4128
4129 const auto *context = cb_access_context->GetCurrentAccessContext();
4130 assert(context);
4131 if (!context) return skip;
4132
locke-lunarg61870c22020-06-09 14:51:50 -06004133 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4134 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004135 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4136 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004137
4138 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4139 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4140 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004141 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004142 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004143}
4144
4145void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4146 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004147 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004148 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004149 auto *cb_access_context = GetAccessContext(commandBuffer);
4150 assert(cb_access_context);
4151 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4152 auto *context = cb_access_context->GetCurrentAccessContext();
4153 assert(context);
4154
locke-lunarg61870c22020-06-09 14:51:50 -06004155 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4156 cb_access_context->RecordDrawSubpassAttachment(tag);
4157 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004158
4159 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4160 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4161 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004162 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004163}
4164
4165bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4166 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004167 bool skip = false;
4168 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004169 const auto *cb_access_context = GetAccessContext(commandBuffer);
4170 assert(cb_access_context);
4171 if (!cb_access_context) return skip;
4172
4173 const auto *context = cb_access_context->GetCurrentAccessContext();
4174 assert(context);
4175 if (!context) return skip;
4176
locke-lunarg61870c22020-06-09 14:51:50 -06004177 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4178 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004179 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4180 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004181
4182 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4183 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4184 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004185 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004186 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004187}
4188
4189void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4190 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004191 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004192 auto *cb_access_context = GetAccessContext(commandBuffer);
4193 assert(cb_access_context);
4194 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4195 auto *context = cb_access_context->GetCurrentAccessContext();
4196 assert(context);
4197
locke-lunarg61870c22020-06-09 14:51:50 -06004198 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4199 cb_access_context->RecordDrawSubpassAttachment(tag);
4200 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004201
4202 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4203 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4204 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004205 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004206}
4207
4208bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4209 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4210 uint32_t stride, const char *function) const {
4211 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004212 const auto *cb_access_context = GetAccessContext(commandBuffer);
4213 assert(cb_access_context);
4214 if (!cb_access_context) return skip;
4215
4216 const auto *context = cb_access_context->GetCurrentAccessContext();
4217 assert(context);
4218 if (!context) return skip;
4219
locke-lunarg61870c22020-06-09 14:51:50 -06004220 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4221 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004222 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4223 maxDrawCount, stride, function);
4224 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004225
4226 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4227 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4228 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004229 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004230 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004231}
4232
4233bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4234 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4235 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004236 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4237 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004238}
4239
4240void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4241 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4242 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004243 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4244 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004245 auto *cb_access_context = GetAccessContext(commandBuffer);
4246 assert(cb_access_context);
4247 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
4248 auto *context = cb_access_context->GetCurrentAccessContext();
4249 assert(context);
4250
locke-lunarg61870c22020-06-09 14:51:50 -06004251 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4252 cb_access_context->RecordDrawSubpassAttachment(tag);
4253 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4254 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004255
4256 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4257 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4258 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004259 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004260}
4261
4262bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4263 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4264 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004265 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4266 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004267}
4268
4269void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4270 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4271 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004272 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4273 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004274 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004275}
4276
4277bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4278 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4279 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004280 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4281 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004282}
4283
4284void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4285 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4286 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004287 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4288 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004289 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4290}
4291
4292bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4293 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4294 uint32_t stride, const char *function) const {
4295 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004296 const auto *cb_access_context = GetAccessContext(commandBuffer);
4297 assert(cb_access_context);
4298 if (!cb_access_context) return skip;
4299
4300 const auto *context = cb_access_context->GetCurrentAccessContext();
4301 assert(context);
4302 if (!context) return skip;
4303
locke-lunarg61870c22020-06-09 14:51:50 -06004304 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4305 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004306 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4307 offset, maxDrawCount, stride, function);
4308 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004309
4310 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4311 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4312 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004313 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004314 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004315}
4316
4317bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4318 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4319 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004320 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4321 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004322}
4323
4324void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4325 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4326 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004327 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4328 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004329 auto *cb_access_context = GetAccessContext(commandBuffer);
4330 assert(cb_access_context);
4331 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4332 auto *context = cb_access_context->GetCurrentAccessContext();
4333 assert(context);
4334
locke-lunarg61870c22020-06-09 14:51:50 -06004335 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4336 cb_access_context->RecordDrawSubpassAttachment(tag);
4337 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4338 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004339
4340 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4341 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004342 // We will update the index and vertex buffer in SubmitQueue in the future.
4343 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004344}
4345
4346bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4347 VkDeviceSize offset, VkBuffer countBuffer,
4348 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4349 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004350 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4351 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004352}
4353
4354void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4355 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4356 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004357 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4358 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004359 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4360}
4361
4362bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4363 VkDeviceSize offset, VkBuffer countBuffer,
4364 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4365 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004366 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4367 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004368}
4369
4370void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4371 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4372 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004373 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4374 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004375 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4376}
4377
4378bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4379 const VkClearColorValue *pColor, uint32_t rangeCount,
4380 const VkImageSubresourceRange *pRanges) const {
4381 bool skip = false;
4382 const auto *cb_access_context = GetAccessContext(commandBuffer);
4383 assert(cb_access_context);
4384 if (!cb_access_context) return skip;
4385
4386 const auto *context = cb_access_context->GetCurrentAccessContext();
4387 assert(context);
4388 if (!context) return skip;
4389
4390 const auto *image_state = Get<IMAGE_STATE>(image);
4391
4392 for (uint32_t index = 0; index < rangeCount; index++) {
4393 const auto &range = pRanges[index];
4394 if (image_state) {
4395 auto hazard =
Jeremy Gebben40a22942020-12-22 14:22:06 -07004396 context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
locke-lunarge1a67022020-04-29 00:15:36 -06004397 if (hazard.hazard) {
4398 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004399 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004400 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004401 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004402 }
4403 }
4404 }
4405 return skip;
4406}
4407
4408void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4409 const VkClearColorValue *pColor, uint32_t rangeCount,
4410 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004411 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004412 auto *cb_access_context = GetAccessContext(commandBuffer);
4413 assert(cb_access_context);
4414 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4415 auto *context = cb_access_context->GetCurrentAccessContext();
4416 assert(context);
4417
4418 const auto *image_state = Get<IMAGE_STATE>(image);
4419
4420 for (uint32_t index = 0; index < rangeCount; index++) {
4421 const auto &range = pRanges[index];
4422 if (image_state) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004423 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, {0, 0, 0},
John Zulauf8e3c3e92021-01-06 11:19:36 -07004424 image_state->createInfo.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004425 }
4426 }
4427}
4428
4429bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4430 VkImageLayout imageLayout,
4431 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4432 const VkImageSubresourceRange *pRanges) const {
4433 bool skip = false;
4434 const auto *cb_access_context = GetAccessContext(commandBuffer);
4435 assert(cb_access_context);
4436 if (!cb_access_context) return skip;
4437
4438 const auto *context = cb_access_context->GetCurrentAccessContext();
4439 assert(context);
4440 if (!context) return skip;
4441
4442 const auto *image_state = Get<IMAGE_STATE>(image);
4443
4444 for (uint32_t index = 0; index < rangeCount; index++) {
4445 const auto &range = pRanges[index];
4446 if (image_state) {
4447 auto hazard =
Jeremy Gebben40a22942020-12-22 14:22:06 -07004448 context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
locke-lunarge1a67022020-04-29 00:15:36 -06004449 if (hazard.hazard) {
4450 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004451 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004452 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004453 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004454 }
4455 }
4456 }
4457 return skip;
4458}
4459
4460void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4461 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4462 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004463 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004464 auto *cb_access_context = GetAccessContext(commandBuffer);
4465 assert(cb_access_context);
4466 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4467 auto *context = cb_access_context->GetCurrentAccessContext();
4468 assert(context);
4469
4470 const auto *image_state = Get<IMAGE_STATE>(image);
4471
4472 for (uint32_t index = 0; index < rangeCount; index++) {
4473 const auto &range = pRanges[index];
4474 if (image_state) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004475 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, {0, 0, 0},
John Zulauf8e3c3e92021-01-06 11:19:36 -07004476 image_state->createInfo.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004477 }
4478 }
4479}
4480
4481bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4482 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4483 VkDeviceSize dstOffset, VkDeviceSize stride,
4484 VkQueryResultFlags flags) const {
4485 bool skip = false;
4486 const auto *cb_access_context = GetAccessContext(commandBuffer);
4487 assert(cb_access_context);
4488 if (!cb_access_context) return skip;
4489
4490 const auto *context = cb_access_context->GetCurrentAccessContext();
4491 assert(context);
4492 if (!context) return skip;
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 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004499 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004500 skip |=
4501 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4502 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004503 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004504 }
4505 }
locke-lunargff255f92020-05-13 18:53:52 -06004506
4507 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004508 return skip;
4509}
4510
4511void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4512 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4513 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004514 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4515 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004516 auto *cb_access_context = GetAccessContext(commandBuffer);
4517 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004518 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004519 auto *context = cb_access_context->GetCurrentAccessContext();
4520 assert(context);
4521
4522 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4523
4524 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004525 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004526 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004527 }
locke-lunargff255f92020-05-13 18:53:52 -06004528
4529 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004530}
4531
4532bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4533 VkDeviceSize size, uint32_t data) const {
4534 bool skip = false;
4535 const auto *cb_access_context = GetAccessContext(commandBuffer);
4536 assert(cb_access_context);
4537 if (!cb_access_context) return skip;
4538
4539 const auto *context = cb_access_context->GetCurrentAccessContext();
4540 assert(context);
4541 if (!context) return skip;
4542
4543 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4544
4545 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004546 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004547 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004548 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004549 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004550 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004551 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004552 }
4553 }
4554 return skip;
4555}
4556
4557void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4558 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004559 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004560 auto *cb_access_context = GetAccessContext(commandBuffer);
4561 assert(cb_access_context);
4562 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4563 auto *context = cb_access_context->GetCurrentAccessContext();
4564 assert(context);
4565
4566 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4567
4568 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004569 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004570 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004571 }
4572}
4573
4574bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4575 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4576 const VkImageResolve *pRegions) const {
4577 bool skip = false;
4578 const auto *cb_access_context = GetAccessContext(commandBuffer);
4579 assert(cb_access_context);
4580 if (!cb_access_context) return skip;
4581
4582 const auto *context = cb_access_context->GetCurrentAccessContext();
4583 assert(context);
4584 if (!context) return skip;
4585
4586 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4587 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4588
4589 for (uint32_t region = 0; region < regionCount; region++) {
4590 const auto &resolve_region = pRegions[region];
4591 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004592 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004593 resolve_region.srcOffset, resolve_region.extent);
4594 if (hazard.hazard) {
4595 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004596 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004597 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004598 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004599 }
4600 }
4601
4602 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004603 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004604 resolve_region.dstOffset, resolve_region.extent);
4605 if (hazard.hazard) {
4606 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004607 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004608 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004609 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004610 }
4611 if (skip) break;
4612 }
4613 }
4614
4615 return skip;
4616}
4617
4618void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4619 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4620 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004621 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4622 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004623 auto *cb_access_context = GetAccessContext(commandBuffer);
4624 assert(cb_access_context);
4625 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4626 auto *context = cb_access_context->GetCurrentAccessContext();
4627 assert(context);
4628
4629 auto *src_image = Get<IMAGE_STATE>(srcImage);
4630 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4631
4632 for (uint32_t region = 0; region < regionCount; region++) {
4633 const auto &resolve_region = pRegions[region];
4634 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004635 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004636 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004637 }
4638 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004639 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004640 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004641 }
4642 }
4643}
4644
Jeff Leger178b1e52020-10-05 12:22:23 -04004645bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4646 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4647 bool skip = false;
4648 const auto *cb_access_context = GetAccessContext(commandBuffer);
4649 assert(cb_access_context);
4650 if (!cb_access_context) return skip;
4651
4652 const auto *context = cb_access_context->GetCurrentAccessContext();
4653 assert(context);
4654 if (!context) return skip;
4655
4656 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4657 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4658
4659 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4660 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4661 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004662 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004663 resolve_region.srcOffset, resolve_region.extent);
4664 if (hazard.hazard) {
4665 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4666 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4667 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004668 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004669 }
4670 }
4671
4672 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004673 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004674 resolve_region.dstOffset, resolve_region.extent);
4675 if (hazard.hazard) {
4676 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4677 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4678 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004679 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004680 }
4681 if (skip) break;
4682 }
4683 }
4684
4685 return skip;
4686}
4687
4688void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4689 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4690 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4691 auto *cb_access_context = GetAccessContext(commandBuffer);
4692 assert(cb_access_context);
4693 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4694 auto *context = cb_access_context->GetCurrentAccessContext();
4695 assert(context);
4696
4697 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4698 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4699
4700 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4701 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4702 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004703 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004704 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004705 }
4706 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004707 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004708 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004709 }
4710 }
4711}
4712
locke-lunarge1a67022020-04-29 00:15:36 -06004713bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4714 VkDeviceSize dataSize, const void *pData) const {
4715 bool skip = false;
4716 const auto *cb_access_context = GetAccessContext(commandBuffer);
4717 assert(cb_access_context);
4718 if (!cb_access_context) return skip;
4719
4720 const auto *context = cb_access_context->GetCurrentAccessContext();
4721 assert(context);
4722 if (!context) return skip;
4723
4724 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4725
4726 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004727 // VK_WHOLE_SIZE not allowed
4728 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004729 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004730 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004731 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004732 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004733 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004734 }
4735 }
4736 return skip;
4737}
4738
4739void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4740 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004741 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004742 auto *cb_access_context = GetAccessContext(commandBuffer);
4743 assert(cb_access_context);
4744 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4745 auto *context = cb_access_context->GetCurrentAccessContext();
4746 assert(context);
4747
4748 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4749
4750 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004751 // VK_WHOLE_SIZE not allowed
4752 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004753 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004754 }
4755}
locke-lunargff255f92020-05-13 18:53:52 -06004756
4757bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4758 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4759 bool skip = false;
4760 const auto *cb_access_context = GetAccessContext(commandBuffer);
4761 assert(cb_access_context);
4762 if (!cb_access_context) return skip;
4763
4764 const auto *context = cb_access_context->GetCurrentAccessContext();
4765 assert(context);
4766 if (!context) return skip;
4767
4768 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4769
4770 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004771 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004772 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06004773 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004774 skip |=
4775 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4776 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004777 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004778 }
4779 }
4780 return skip;
4781}
4782
4783void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4784 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004785 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004786 auto *cb_access_context = GetAccessContext(commandBuffer);
4787 assert(cb_access_context);
4788 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4789 auto *context = cb_access_context->GetCurrentAccessContext();
4790 assert(context);
4791
4792 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4793
4794 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004795 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004796 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004797 }
4798}
John Zulauf49beb112020-11-04 16:06:31 -07004799
4800bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
4801 bool skip = false;
4802 const auto *cb_context = GetAccessContext(commandBuffer);
4803 assert(cb_context);
4804 if (!cb_context) return skip;
4805
John Zulauf36ef9282021-02-02 11:47:24 -07004806 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004807 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004808}
4809
4810void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4811 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
4812 auto *cb_context = GetAccessContext(commandBuffer);
4813 assert(cb_context);
4814 if (!cb_context) return;
John Zulauf36ef9282021-02-02 11:47:24 -07004815 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4816 set_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004817}
4818
John Zulauf4edde622021-02-15 08:54:50 -07004819bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4820 const VkDependencyInfoKHR *pDependencyInfo) const {
4821 bool skip = false;
4822 const auto *cb_context = GetAccessContext(commandBuffer);
4823 assert(cb_context);
4824 if (!cb_context || !pDependencyInfo) return skip;
4825
4826 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4827 return set_event_op.Validate(*cb_context);
4828}
4829
4830void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4831 const VkDependencyInfoKHR *pDependencyInfo) {
4832 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
4833 auto *cb_context = GetAccessContext(commandBuffer);
4834 assert(cb_context);
4835 if (!cb_context || !pDependencyInfo) return;
4836
4837 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4838 set_event_op.Record(cb_context);
4839}
4840
John Zulauf49beb112020-11-04 16:06:31 -07004841bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
4842 VkPipelineStageFlags stageMask) const {
4843 bool skip = false;
4844 const auto *cb_context = GetAccessContext(commandBuffer);
4845 assert(cb_context);
4846 if (!cb_context) return skip;
4847
John Zulauf36ef9282021-02-02 11:47:24 -07004848 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004849 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004850}
4851
4852void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4853 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
4854 auto *cb_context = GetAccessContext(commandBuffer);
4855 assert(cb_context);
4856 if (!cb_context) return;
4857
John Zulauf36ef9282021-02-02 11:47:24 -07004858 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4859 reset_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004860}
4861
John Zulauf4edde622021-02-15 08:54:50 -07004862bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4863 VkPipelineStageFlags2KHR stageMask) const {
4864 bool skip = false;
4865 const auto *cb_context = GetAccessContext(commandBuffer);
4866 assert(cb_context);
4867 if (!cb_context) return skip;
4868
4869 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4870 return reset_event_op.Validate(*cb_context);
4871}
4872
4873void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4874 VkPipelineStageFlags2KHR stageMask) {
4875 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
4876 auto *cb_context = GetAccessContext(commandBuffer);
4877 assert(cb_context);
4878 if (!cb_context) return;
4879
4880 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4881 reset_event_op.Record(cb_context);
4882}
4883
John Zulauf49beb112020-11-04 16:06:31 -07004884bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4885 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4886 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4887 uint32_t bufferMemoryBarrierCount,
4888 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4889 uint32_t imageMemoryBarrierCount,
4890 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
4891 bool skip = false;
4892 const auto *cb_context = GetAccessContext(commandBuffer);
4893 assert(cb_context);
4894 if (!cb_context) return skip;
4895
John Zulauf36ef9282021-02-02 11:47:24 -07004896 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4897 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4898 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07004899 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004900}
4901
4902void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4903 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4904 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4905 uint32_t bufferMemoryBarrierCount,
4906 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4907 uint32_t imageMemoryBarrierCount,
4908 const VkImageMemoryBarrier *pImageMemoryBarriers) {
4909 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
4910 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
4911 imageMemoryBarrierCount, pImageMemoryBarriers);
4912
4913 auto *cb_context = GetAccessContext(commandBuffer);
4914 assert(cb_context);
4915 if (!cb_context) return;
4916
John Zulauf36ef9282021-02-02 11:47:24 -07004917 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4918 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4919 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
4920 return wait_events_op.Record(cb_context);
John Zulauf4a6105a2020-11-17 15:11:05 -07004921}
4922
John Zulauf4edde622021-02-15 08:54:50 -07004923bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4924 const VkDependencyInfoKHR *pDependencyInfos) const {
4925 bool skip = false;
4926 const auto *cb_context = GetAccessContext(commandBuffer);
4927 assert(cb_context);
4928 if (!cb_context) return skip;
4929
4930 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
4931 skip |= wait_events_op.Validate(*cb_context);
4932 return skip;
4933}
4934
4935void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4936 const VkDependencyInfoKHR *pDependencyInfos) {
4937 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
4938
4939 auto *cb_context = GetAccessContext(commandBuffer);
4940 assert(cb_context);
4941 if (!cb_context) return;
4942
4943 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
4944 wait_events_op.Record(cb_context);
4945}
4946
John Zulauf4a6105a2020-11-17 15:11:05 -07004947void SyncEventState::ResetFirstScope() {
4948 for (const auto address_type : kAddressTypes) {
4949 first_scope[static_cast<size_t>(address_type)].clear();
4950 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07004951 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07004952}
4953
4954// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07004955SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07004956 IgnoreReason reason = NotIgnored;
4957
John Zulauf4edde622021-02-15 08:54:50 -07004958 if ((CMD_WAITEVENTS2KHR == cmd) && (CMD_SETEVENT == last_command)) {
4959 reason = SetVsWait2;
4960 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
4961 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07004962 } else if (unsynchronized_set) {
4963 reason = SetRace;
4964 } else {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004965 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07004966 if (missing_bits) reason = MissingStageBits;
4967 }
4968
4969 return reason;
4970}
4971
Jeremy Gebben40a22942020-12-22 14:22:06 -07004972bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07004973 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
4974 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
4975 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07004976}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004977
John Zulauf36ef9282021-02-02 11:47:24 -07004978SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
4979 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4980 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07004981 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
4982 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
4983 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07004984 : SyncOpBase(cmd), barriers_(1) {
4985 auto &barrier_set = barriers_[0];
4986 barrier_set.dependency_flags = dependencyFlags;
4987 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
4988 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004989 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07004990 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
4991 pMemoryBarriers);
4992 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
4993 bufferMemoryBarrierCount, pBufferMemoryBarriers);
4994 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
4995 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004996}
4997
John Zulauf4edde622021-02-15 08:54:50 -07004998SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
4999 const VkDependencyInfoKHR *dep_infos)
5000 : SyncOpBase(cmd), barriers_(event_count) {
5001 for (uint32_t i = 0; i < event_count; i++) {
5002 const auto &dep_info = dep_infos[i];
5003 auto &barrier_set = barriers_[i];
5004 barrier_set.dependency_flags = dep_info.dependencyFlags;
5005 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5006 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5007 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5008 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5009 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5010 dep_info.pMemoryBarriers);
5011 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5012 dep_info.pBufferMemoryBarriers);
5013 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5014 dep_info.pImageMemoryBarriers);
5015 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005016}
5017
John Zulauf36ef9282021-02-02 11:47:24 -07005018SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005019 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5020 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5021 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5022 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5023 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005024 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005025 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5026
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005027SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5028 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005029 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005030
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005031bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5032 bool skip = false;
5033 const auto *context = cb_context.GetCurrentAccessContext();
5034 assert(context);
5035 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005036 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5037
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005038 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005039 const auto &barrier_set = barriers_[0];
5040 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5041 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5042 const auto *image_state = image_barrier.image.get();
5043 if (!image_state) continue;
5044 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5045 if (hazard.hazard) {
5046 // PHASE1 TODO -- add tag information to log msg when useful.
5047 const auto &sync_state = cb_context.GetSyncState();
5048 const auto image_handle = image_state->image;
5049 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5050 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5051 string_SyncHazard(hazard.hazard), image_barrier.index,
5052 sync_state.report_data->FormatHandle(image_handle).c_str(),
5053 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005054 }
5055 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005056 return skip;
5057}
5058
John Zulaufd5115702021-01-18 12:34:33 -07005059struct SyncOpPipelineBarrierFunctorFactory {
5060 using BarrierOpFunctor = PipelineBarrierOp;
5061 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5062 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5063 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5064 using BufferRange = ResourceAccessRange;
5065 using ImageRange = subresource_adapter::ImageRangeGenerator;
5066 using GlobalRange = ResourceAccessRange;
5067
5068 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5069 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5070 }
5071 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
5072 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5073 }
5074 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5075 return GlobalBarrierOpFunctor(barrier, false);
5076 }
5077
5078 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5079 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5080 const auto base_address = ResourceBaseAddress(buffer);
5081 return (range + base_address);
5082 }
5083 ImageRange MakeRangeGen(const IMAGE_STATE &image, const SyncImageMemoryBarrier::SubImageRange &range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005084 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005085
5086 const auto base_address = ResourceBaseAddress(image);
5087 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), range.subresource_range, range.offset,
5088 range.extent, base_address);
5089 return range_gen;
5090 }
5091 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5092};
5093
5094template <typename Barriers, typename FunctorFactory>
5095void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
5096 AccessContext *context) {
5097 for (const auto &barrier : barriers) {
5098 const auto *state = barrier.GetState();
5099 if (state) {
5100 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5101 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5102 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5103 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5104 }
5105 }
5106}
5107
5108template <typename Barriers, typename FunctorFactory>
5109void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
5110 AccessContext *access_context) {
5111 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5112 for (const auto &barrier : barriers) {
5113 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5114 }
5115 for (const auto address_type : kAddressTypes) {
5116 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5117 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5118 }
5119}
5120
John Zulauf36ef9282021-02-02 11:47:24 -07005121void SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005122 SyncOpPipelineBarrierFunctorFactory factory;
5123 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005124 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005125
John Zulauf4edde622021-02-15 08:54:50 -07005126 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5127 assert(barriers_.size() == 1);
5128 const auto &barrier_set = barriers_[0];
5129 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5130 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5131 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
5132
5133 if (barrier_set.single_exec_scope) {
5134 cb_context->ApplyGlobalBarriersToEvents(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
5135 } else {
5136 for (const auto &barrier : barrier_set.memory_barriers) {
5137 cb_context->ApplyGlobalBarriersToEvents(barrier.src_exec_scope, barrier.dst_exec_scope);
5138 }
5139 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005140}
5141
John Zulauf4edde622021-02-15 08:54:50 -07005142void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5143 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5144 const VkMemoryBarrier *barriers) {
5145 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005146 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005147 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005148 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005149 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005150 }
5151 if (0 == memory_barrier_count) {
5152 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005153 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005154 }
John Zulauf4edde622021-02-15 08:54:50 -07005155 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005156}
5157
John Zulauf4edde622021-02-15 08:54:50 -07005158void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5159 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5160 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5161 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005162 for (uint32_t index = 0; index < barrier_count; index++) {
5163 const auto &barrier = barriers[index];
5164 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5165 if (buffer) {
5166 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5167 const auto range = MakeRange(barrier.offset, barrier_size);
5168 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005169 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005170 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005171 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005172 }
5173 }
5174}
5175
John Zulauf4edde622021-02-15 08:54:50 -07005176void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
5177 uint32_t memory_barrier_count, const VkMemoryBarrier2KHR *barriers) {
5178 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005179 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005180 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005181 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5182 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5183 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005184 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005185 }
John Zulauf4edde622021-02-15 08:54:50 -07005186 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005187}
5188
John Zulauf4edde622021-02-15 08:54:50 -07005189void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5190 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5191 const VkBufferMemoryBarrier2KHR *barriers) {
5192 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005193 for (uint32_t index = 0; index < barrier_count; index++) {
5194 const auto &barrier = barriers[index];
5195 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5196 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5197 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5198 if (buffer) {
5199 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5200 const auto range = MakeRange(barrier.offset, barrier_size);
5201 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005202 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005203 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005204 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005205 }
5206 }
5207}
5208
John Zulauf4edde622021-02-15 08:54:50 -07005209void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5210 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5211 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5212 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005213 for (uint32_t index = 0; index < barrier_count; index++) {
5214 const auto &barrier = barriers[index];
5215 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5216 if (image) {
5217 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5218 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005219 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005220 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005221 image_memory_barriers.emplace_back();
5222 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005223 }
5224 }
5225}
John Zulaufd5115702021-01-18 12:34:33 -07005226
John Zulauf4edde622021-02-15 08:54:50 -07005227void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5228 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5229 const VkImageMemoryBarrier2KHR *barriers) {
5230 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005231 for (uint32_t index = 0; index < barrier_count; index++) {
5232 const auto &barrier = barriers[index];
5233 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5234 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5235 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5236 if (image) {
5237 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5238 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005239 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005240 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005241 image_memory_barriers.emplace_back();
5242 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005243 }
5244 }
5245}
5246
John Zulauf36ef9282021-02-02 11:47:24 -07005247SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005248 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5249 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5250 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5251 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005252 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005253 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5254 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005255 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005256}
5257
John Zulauf4edde622021-02-15 08:54:50 -07005258SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5259 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5260 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5261 MakeEventsList(sync_state, eventCount, pEvents);
5262 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5263}
5264
John Zulaufd5115702021-01-18 12:34:33 -07005265bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005266 const char *const ignored = "Wait operation is ignored for this event.";
5267 bool skip = false;
5268 const auto &sync_state = cb_context.GetSyncState();
5269 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer;
5270
John Zulauf4edde622021-02-15 08:54:50 -07005271 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5272 const auto &barrier_set = barriers_[barrier_set_index];
5273 if (barrier_set.single_exec_scope) {
5274 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5275 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5276 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5277 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5278 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5279 } else {
5280 const auto &barriers = barrier_set.memory_barriers;
5281 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5282 const auto &barrier = barriers[barrier_index];
5283 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5284 const std::string vuid =
5285 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5286 skip =
5287 sync_state.LogInfo(command_buffer_handle, vuid,
5288 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5289 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5290 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5291 }
5292 }
5293 }
5294 }
John Zulaufd5115702021-01-18 12:34:33 -07005295 }
5296
Jeremy Gebben40a22942020-12-22 14:22:06 -07005297 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005298 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005299 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005300 const auto *events_context = cb_context.GetCurrentEventsContext();
5301 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005302 size_t barrier_set_index = 0;
5303 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5304 for (size_t event_index = 0; event_index < events_.size(); event_index++)
5305 for (const auto &event : events_) {
5306 const auto *sync_event = events_context->Get(event.get());
5307 const auto &barrier_set = barriers_[barrier_set_index];
5308 if (!sync_event) {
5309 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5310 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5311 // new validation error... wait without previously submitted set event...
5312 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives at *record time*
5313 barrier_set_index += barrier_set_incr;
5314 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005315 }
John Zulauf4edde622021-02-15 08:54:50 -07005316 const auto event_handle = sync_event->event->event;
5317 // TODO add "destroyed" checks
5318
5319 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5320 const auto &src_exec_scope = barrier_set.src_exec_scope;
5321 event_stage_masks |= sync_event->scope.mask_param;
5322 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5323 if (ignore_reason) {
5324 switch (ignore_reason) {
5325 case SyncEventState::ResetWaitRace:
5326 case SyncEventState::Reset2WaitRace: {
5327 // Four permuations of Reset and Wait calls...
5328 const char *vuid =
5329 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5330 if (ignore_reason == SyncEventState::Reset2WaitRace) {
5331 vuid =
Jeremy Gebben476f5e22021-03-01 15:27:20 -07005332 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2KHR-event-03831" : "VUID-vkCmdResetEvent2KHR-event-03832";
John Zulauf4edde622021-02-15 08:54:50 -07005333 }
5334 const char *const message =
5335 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5336 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5337 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
5338 CommandTypeString(sync_event->last_command), ignored);
5339 break;
5340 }
5341 case SyncEventState::SetRace: {
5342 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5343 // this event
5344 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5345 const char *const message =
5346 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5347 const char *const reason = "First synchronization scope is undefined.";
5348 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5349 sync_state.report_data->FormatHandle(event_handle).c_str(),
5350 CommandTypeString(sync_event->last_command), reason, ignored);
5351 break;
5352 }
5353 case SyncEventState::MissingStageBits: {
5354 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5355 // Issue error message that event waited for is not in wait events scope
5356 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5357 const char *const message =
5358 "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5359 ". Bits missing from srcStageMask %s. %s";
5360 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5361 sync_state.report_data->FormatHandle(event_handle).c_str(),
5362 sync_event->scope.mask_param, src_exec_scope.mask_param,
5363 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), ignored);
5364 break;
5365 }
5366 case SyncEventState::SetVsWait2: {
5367 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2KHR-pEvents-03837",
5368 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5369 sync_state.report_data->FormatHandle(event_handle).c_str(),
5370 CommandTypeString(sync_event->last_command));
5371 break;
5372 }
5373 default:
5374 assert(ignore_reason == SyncEventState::NotIgnored);
5375 }
5376 } else if (barrier_set.image_memory_barriers.size()) {
5377 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5378 const auto *context = cb_context.GetCurrentAccessContext();
5379 assert(context);
5380 for (const auto &image_memory_barrier : image_memory_barriers) {
5381 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5382 const auto *image_state = image_memory_barrier.image.get();
5383 if (!image_state) continue;
5384 const auto &subresource_range = image_memory_barrier.range.subresource_range;
5385 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5386 const auto hazard =
5387 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5388 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5389 if (hazard.hazard) {
5390 skip |= sync_state.LogError(image_state->image, string_SyncHazardVUID(hazard.hazard),
5391 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5392 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
5393 sync_state.report_data->FormatHandle(image_state->image).c_str(),
5394 cb_context.FormatUsage(hazard).c_str());
5395 break;
5396 }
John Zulaufd5115702021-01-18 12:34:33 -07005397 }
5398 }
John Zulauf4edde622021-02-15 08:54:50 -07005399 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5400 // 03839
5401 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005402 }
John Zulaufd5115702021-01-18 12:34:33 -07005403
5404 // 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 -07005405 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 -07005406 if (extra_stage_bits) {
5407 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005408 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5409 const char *const vuid =
5410 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2KHR-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005411 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005412 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulaufd5115702021-01-18 12:34:33 -07005413 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005414 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005415 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005416 " vkCmdSetEvent may be in previously submitted command buffer.");
5417 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005418 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005419 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005420 }
5421 }
5422 return skip;
5423}
5424
5425struct SyncOpWaitEventsFunctorFactory {
5426 using BarrierOpFunctor = WaitEventBarrierOp;
5427 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5428 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5429 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5430 using BufferRange = EventSimpleRangeGenerator;
5431 using ImageRange = EventImageRangeGenerator;
5432 using GlobalRange = EventSimpleRangeGenerator;
5433
5434 // Need to restrict to only valid exec and access scope for this event
5435 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5436 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005437 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005438 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5439 return barrier;
5440 }
5441 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5442 auto barrier = RestrictToEvent(barrier_arg);
5443 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5444 }
5445 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
5446 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5447 }
5448 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5449 auto barrier = RestrictToEvent(barrier_arg);
5450 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5451 }
5452
5453 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5454 const AccessAddressType address_type = GetAccessAddressType(buffer);
5455 const auto base_address = ResourceBaseAddress(buffer);
5456 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5457 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5458 return filtered_range_gen;
5459 }
5460 ImageRange MakeRangeGen(const IMAGE_STATE &image, const SyncImageMemoryBarrier::SubImageRange &range) const {
5461 if (!SimpleBinding(image)) return ImageRange();
5462 const auto address_type = GetAccessAddressType(image);
5463 const auto base_address = ResourceBaseAddress(image);
5464 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), range.subresource_range,
5465 range.offset, range.extent, base_address);
5466 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5467
5468 return filtered_range_gen;
5469 }
5470 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5471 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5472 }
5473 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5474 SyncEventState *sync_event;
5475};
5476
John Zulauf36ef9282021-02-02 11:47:24 -07005477void SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
5478 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07005479 auto *access_context = cb_context->GetCurrentAccessContext();
5480 assert(access_context);
5481 if (!access_context) return;
John Zulauf669dfd52021-01-27 17:15:28 -07005482 auto *events_context = cb_context->GetCurrentEventsContext();
5483 assert(events_context);
5484 if (!events_context) return;
John Zulaufd5115702021-01-18 12:34:33 -07005485
5486 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
5487 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
5488 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
5489 access_context->ResolvePreviousAccesses();
5490
John Zulaufd5115702021-01-18 12:34:33 -07005491 // TODO... this needs change the SyncEventContext it's using depending on whether this is replay... the recorded
5492 // 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 -07005493 size_t barrier_set_index = 0;
5494 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5495 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07005496 for (auto &event_shared : events_) {
5497 if (!event_shared.get()) continue;
5498 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07005499
John Zulauf4edde622021-02-15 08:54:50 -07005500 sync_event->last_command = cmd_;
John Zulaufd5115702021-01-18 12:34:33 -07005501
John Zulauf4edde622021-02-15 08:54:50 -07005502 const auto &barrier_set = barriers_[barrier_set_index];
5503 const auto &dst = barrier_set.dst_exec_scope;
5504 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07005505 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
5506 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
5507 // of the barriers is maintained.
5508 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07005509 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5510 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5511 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07005512
5513 // Apply the global barrier to the event itself (for race condition tracking)
5514 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
5515 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
5516 sync_event->barriers |= dst.exec_scope;
5517 } else {
5518 // We ignored this wait, so we don't have any effective synchronization barriers for it.
5519 sync_event->barriers = 0U;
5520 }
John Zulauf4edde622021-02-15 08:54:50 -07005521 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005522 }
5523
5524 // Apply the pending barriers
5525 ResolvePendingBarrierFunctor apply_pending_action(tag);
5526 access_context->ApplyToContext(apply_pending_action);
5527}
5528
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005529bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5530 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5531 bool skip = false;
5532 const auto *cb_access_context = GetAccessContext(commandBuffer);
5533 assert(cb_access_context);
5534 if (!cb_access_context) return skip;
5535
5536 const auto *context = cb_access_context->GetCurrentAccessContext();
5537 assert(context);
5538 if (!context) return skip;
5539
5540 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5541
5542 if (dst_buffer) {
5543 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5544 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
5545 if (hazard.hazard) {
5546 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5547 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
5548 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
5549 string_UsageTag(hazard.tag).c_str());
5550 }
5551 }
5552 return skip;
5553}
5554
John Zulauf669dfd52021-01-27 17:15:28 -07005555void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07005556 events_.reserve(event_count);
5557 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
John Zulauf669dfd52021-01-27 17:15:28 -07005558 events_.emplace_back(sync_state.GetShared<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07005559 }
5560}
John Zulauf6ce24372021-01-30 05:56:25 -07005561
John Zulauf36ef9282021-02-02 11:47:24 -07005562SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005563 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005564 : SyncOpBase(cmd),
5565 event_(sync_state.GetShared<EVENT_STATE>(event)),
5566 exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005567
5568bool SyncOpResetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005569 auto *events_context = cb_context.GetCurrentEventsContext();
5570 assert(events_context);
5571 bool skip = false;
5572 if (!events_context) return skip;
5573
5574 const auto &sync_state = cb_context.GetSyncState();
5575 const auto *sync_event = events_context->Get(event_);
5576 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5577
5578 const char *const set_wait =
5579 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5580 "hazards.";
5581 const char *message = set_wait; // Only one message this call.
5582 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
5583 const char *vuid = nullptr;
5584 switch (sync_event->last_command) {
5585 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005586 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005587 // Needs a barrier between set and reset
5588 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
5589 break;
John Zulauf4edde622021-02-15 08:54:50 -07005590 case CMD_WAITEVENTS:
5591 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07005592 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
5593 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
5594 break;
5595 }
5596 default:
5597 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07005598 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
5599 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07005600 break;
5601 }
5602 if (vuid) {
John Zulauf36ef9282021-02-02 11:47:24 -07005603 skip |= sync_state.LogError(event_->event, vuid, message, CmdName(),
5604 sync_state.report_data->FormatHandle(event_->event).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005605 CommandTypeString(sync_event->last_command));
5606 }
5607 }
5608 return skip;
5609}
5610
John Zulauf36ef9282021-02-02 11:47:24 -07005611void SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005612 auto *events_context = cb_context->GetCurrentEventsContext();
5613 assert(events_context);
5614 if (!events_context) return;
5615
5616 auto *sync_event = events_context->GetFromShared(event_);
5617 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5618
5619 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07005620 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005621 sync_event->unsynchronized_set = CMD_NONE;
5622 sync_event->ResetFirstScope();
5623 sync_event->barriers = 0U;
5624}
5625
John Zulauf36ef9282021-02-02 11:47:24 -07005626SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005627 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005628 : SyncOpBase(cmd),
5629 event_(sync_state.GetShared<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07005630 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
5631 dep_info_() {}
5632
5633SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
5634 const VkDependencyInfoKHR &dep_info)
5635 : SyncOpBase(cmd),
5636 event_(sync_state.GetShared<EVENT_STATE>(event)),
5637 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
5638 dep_info_(new safe_VkDependencyInfoKHR(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005639
5640bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
5641 // 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 -07005642 bool skip = false;
5643
5644 const auto &sync_state = cb_context.GetSyncState();
5645 auto *events_context = cb_context.GetCurrentEventsContext();
5646 assert(events_context);
5647 if (!events_context) return skip;
5648
5649 const auto *sync_event = events_context->Get(event_);
5650 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5651
5652 const char *const reset_set =
5653 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5654 "hazards.";
5655 const char *const wait =
5656 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
5657
5658 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07005659 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07005660 const char *message = nullptr;
5661 switch (sync_event->last_command) {
5662 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005663 case CMD_RESETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005664 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07005665 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07005666 message = reset_set;
5667 break;
5668 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005669 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005670 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07005671 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07005672 message = reset_set;
5673 break;
5674 case CMD_WAITEVENTS:
John Zulauf4edde622021-02-15 08:54:50 -07005675 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005676 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07005677 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07005678 message = wait;
5679 break;
5680 default:
5681 // The only other valid last command that wasn't one.
5682 assert(sync_event->last_command == CMD_NONE);
5683 break;
5684 }
John Zulauf4edde622021-02-15 08:54:50 -07005685 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07005686 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07005687 std::string vuid("SYNC-");
5688 vuid.append(CmdName()).append(vuid_stem);
5689 skip |= sync_state.LogError(event_->event, vuid.c_str(), message, CmdName(),
John Zulauf36ef9282021-02-02 11:47:24 -07005690 sync_state.report_data->FormatHandle(event_->event).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005691 CommandTypeString(sync_event->last_command));
5692 }
5693 }
5694
5695 return skip;
5696}
5697
John Zulauf36ef9282021-02-02 11:47:24 -07005698void SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
5699 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07005700 auto *events_context = cb_context->GetCurrentEventsContext();
5701 auto *access_context = cb_context->GetCurrentAccessContext();
5702 assert(events_context);
5703 if (!events_context) return;
5704
5705 auto *sync_event = events_context->GetFromShared(event_);
5706 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5707
5708 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
5709 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
5710 // any issues caused by naive scope setting here.
5711
5712 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
5713 // Given:
5714 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
5715 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
5716
5717 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
5718 sync_event->unsynchronized_set = sync_event->last_command;
5719 sync_event->ResetFirstScope();
5720 } else if (sync_event->scope.exec_scope == 0) {
5721 // We only set the scope if there isn't one
5722 sync_event->scope = src_exec_scope_;
5723
5724 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
5725 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
5726 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
5727 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
5728 }
5729 };
5730 access_context->ForAll(set_scope);
5731 sync_event->unsynchronized_set = CMD_NONE;
5732 sync_event->first_scope_tag = tag;
5733 }
John Zulauf4edde622021-02-15 08:54:50 -07005734 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
5735 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005736 sync_event->barriers = 0U;
5737}
John Zulauf64ffe552021-02-06 10:25:07 -07005738
5739SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
5740 const VkRenderPassBeginInfo *pRenderPassBegin,
5741 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *cmd_name)
5742 : SyncOpBase(cmd, cmd_name) {
5743 if (pRenderPassBegin) {
5744 rp_state_ = sync_state.GetShared<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
5745 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
5746 const auto *fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
5747 if (fb_state) {
5748 shared_attachments_ = sync_state.GetSharedAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
5749 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
5750 // Note that this a safe to presist as long as shared_attachments is not cleared
5751 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08005752 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07005753 attachments_.emplace_back(attachment.get());
5754 }
5755 }
5756 if (pSubpassBeginInfo) {
5757 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
5758 }
5759 }
5760}
5761
5762bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5763 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
5764 bool skip = false;
5765
5766 assert(rp_state_.get());
5767 if (nullptr == rp_state_.get()) return skip;
5768 auto &rp_state = *rp_state_.get();
5769
5770 const uint32_t subpass = 0;
5771
5772 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
5773 // hasn't happened yet)
5774 const std::vector<AccessContext> empty_context_vector;
5775 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
5776 cb_context.GetCurrentAccessContext());
5777
5778 // Validate attachment operations
5779 if (attachments_.size() == 0) return skip;
5780 const auto &render_area = renderpass_begin_info_.renderArea;
5781 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, attachments_, CmdName());
5782
5783 // Validate load operations if there were no layout transition hazards
5784 if (!skip) {
5785 temp_context.RecordLayoutTransitions(rp_state, subpass, attachments_, kCurrentCommandTag);
5786 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, attachments_, CmdName());
5787 }
5788
5789 return skip;
5790}
5791
5792void SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5793 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5794 assert(rp_state_.get());
5795 if (nullptr == rp_state_.get()) return;
5796 const auto tag = cb_context->NextCommandTag(cmd_);
5797 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
5798}
5799
5800SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
5801 const VkSubpassEndInfo *pSubpassEndInfo, const char *name_override)
5802 : SyncOpBase(cmd, name_override) {
5803 if (pSubpassBeginInfo) {
5804 subpass_begin_info_.initialize(pSubpassBeginInfo);
5805 }
5806 if (pSubpassEndInfo) {
5807 subpass_end_info_.initialize(pSubpassEndInfo);
5808 }
5809}
5810
5811bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
5812 bool skip = false;
5813 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5814 if (!renderpass_context) return skip;
5815
5816 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
5817 return skip;
5818}
5819
5820void SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
5821 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5822 cb_context->RecordNextSubpass(cmd_);
5823}
5824
5825SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo,
5826 const char *name_override)
5827 : SyncOpBase(cmd, name_override) {
5828 if (pSubpassEndInfo) {
5829 subpass_end_info_.initialize(pSubpassEndInfo);
5830 }
5831}
5832
5833bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5834 bool skip = false;
5835 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5836
5837 if (!renderpass_context) return skip;
5838 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
5839 return skip;
5840}
5841
5842void SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5843 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5844 cb_context->RecordEndRenderPass(cmd_);
5845}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005846
5847void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5848 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
5849 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
5850 auto *cb_access_context = GetAccessContext(commandBuffer);
5851 assert(cb_access_context);
5852 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5853 auto *context = cb_access_context->GetCurrentAccessContext();
5854 assert(context);
5855
5856 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5857
5858 if (dst_buffer) {
5859 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5860 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
5861 }
5862}