blob: 44e71aa09b36fa39e8c11164c46e39969c314a47 [file] [log] [blame]
John Zulaufab7756b2020-12-29 16:10:16 -07001/* Copyright (c) 2019-2021 The Khronos Group Inc.
2 * Copyright (c) 2019-2021 Valve Corporation
3 * Copyright (c) 2019-2021 LunarG, Inc.
John Zulauf9cb530d2019-09-30 14:14:10 -06004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: John Zulauf <jzulauf@lunarg.com>
John Zulaufab7756b2020-12-29 16:10:16 -070018 * Author: Locke Lin <locke@lunarg.com>
19 * Author: Jeremy Gebben <jeremyg@lunarg.com>
John Zulauf9cb530d2019-09-30 14:14:10 -060020 */
21
22#include <limits>
23#include <vector>
locke-lunarg296a3c92020-03-25 01:04:29 -060024#include <memory>
25#include <bitset>
John Zulauf9cb530d2019-09-30 14:14:10 -060026#include "synchronization_validation.h"
Jeremy Gebben5f585ae2021-02-02 09:03:06 -070027#include "sync_utils.h"
John Zulauf9cb530d2019-09-30 14:14:10 -060028
John Zulauf264cce02021-02-05 14:40:47 -070029static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
30
John Zulauf29d00532021-03-04 13:28:54 -070031static bool SimpleBinding(const IMAGE_STATE &image_state) {
32 bool simple = SimpleBinding(static_cast<const BINDABLE &>(image_state)) || image_state.is_swapchain_image ||
33 (VK_NULL_HANDLE != image_state.bind_swapchain);
34
35 // If it's not simple we must have an encoder.
36 assert(!simple || image_state.fragment_encoder.get());
37 return simple;
38}
39
John Zulauf43cc7462020-12-03 12:33:12 -070040const static std::array<AccessAddressType, static_cast<size_t>(AccessAddressType::kTypeCount)> kAddressTypes = {
41 AccessAddressType::kLinear, AccessAddressType::kIdealized};
42
John Zulaufd5115702021-01-18 12:34:33 -070043static constexpr AccessAddressType GetAccessAddressType(const BUFFER_STATE &) { return AccessAddressType::kLinear; };
John Zulauf264cce02021-02-05 14:40:47 -070044static AccessAddressType GetAccessAddressType(const IMAGE_STATE &image) {
45 return SimpleBinding(image) ? AccessContext::ImageAddressType(image) : AccessAddressType::kIdealized;
46}
John Zulaufd5115702021-01-18 12:34:33 -070047
John Zulauf9cb530d2019-09-30 14:14:10 -060048static const char *string_SyncHazardVUID(SyncHazard hazard) {
49 switch (hazard) {
50 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070051 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060052 break;
53 case SyncHazard::READ_AFTER_WRITE:
54 return "SYNC-HAZARD-READ_AFTER_WRITE";
55 break;
56 case SyncHazard::WRITE_AFTER_READ:
57 return "SYNC-HAZARD-WRITE_AFTER_READ";
58 break;
59 case SyncHazard::WRITE_AFTER_WRITE:
60 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
61 break;
John Zulauf2f952d22020-02-10 11:34:51 -070062 case SyncHazard::READ_RACING_WRITE:
63 return "SYNC-HAZARD-READ-RACING-WRITE";
64 break;
65 case SyncHazard::WRITE_RACING_WRITE:
66 return "SYNC-HAZARD-WRITE-RACING-WRITE";
67 break;
68 case SyncHazard::WRITE_RACING_READ:
69 return "SYNC-HAZARD-WRITE-RACING-READ";
70 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060071 default:
72 assert(0);
73 }
74 return "SYNC-HAZARD-INVALID";
75}
76
John Zulauf59e25072020-07-17 10:55:21 -060077static bool IsHazardVsRead(SyncHazard hazard) {
78 switch (hazard) {
79 case SyncHazard::NONE:
80 return false;
81 break;
82 case SyncHazard::READ_AFTER_WRITE:
83 return false;
84 break;
85 case SyncHazard::WRITE_AFTER_READ:
86 return true;
87 break;
88 case SyncHazard::WRITE_AFTER_WRITE:
89 return false;
90 break;
91 case SyncHazard::READ_RACING_WRITE:
92 return false;
93 break;
94 case SyncHazard::WRITE_RACING_WRITE:
95 return false;
96 break;
97 case SyncHazard::WRITE_RACING_READ:
98 return true;
99 break;
100 default:
101 assert(0);
102 }
103 return false;
104}
105
John Zulauf9cb530d2019-09-30 14:14:10 -0600106static const char *string_SyncHazard(SyncHazard hazard) {
107 switch (hazard) {
108 case SyncHazard::NONE:
109 return "NONR";
110 break;
111 case SyncHazard::READ_AFTER_WRITE:
112 return "READ_AFTER_WRITE";
113 break;
114 case SyncHazard::WRITE_AFTER_READ:
115 return "WRITE_AFTER_READ";
116 break;
117 case SyncHazard::WRITE_AFTER_WRITE:
118 return "WRITE_AFTER_WRITE";
119 break;
John Zulauf2f952d22020-02-10 11:34:51 -0700120 case SyncHazard::READ_RACING_WRITE:
121 return "READ_RACING_WRITE";
122 break;
123 case SyncHazard::WRITE_RACING_WRITE:
124 return "WRITE_RACING_WRITE";
125 break;
126 case SyncHazard::WRITE_RACING_READ:
127 return "WRITE_RACING_READ";
128 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600129 default:
130 assert(0);
131 }
132 return "INVALID HAZARD";
133}
134
John Zulauf37ceaed2020-07-03 16:18:15 -0600135static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
136 // Return the info for the first bit found
137 const SyncStageAccessInfoType *info = nullptr;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700138 for (size_t i = 0; i < flags.size(); i++) {
139 if (flags.test(i)) {
140 info = &syncStageAccessInfoByStageAccessIndex[i];
141 break;
John Zulauf37ceaed2020-07-03 16:18:15 -0600142 }
143 }
144 return info;
145}
146
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700147static std::string string_SyncStageAccessFlags(const SyncStageAccessFlags &flags, const char *sep = "|") {
John Zulauf59e25072020-07-17 10:55:21 -0600148 std::string out_str;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700149 if (flags.none()) {
John Zulauf389c34b2020-07-28 11:19:35 -0600150 out_str = "0";
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700151 } else {
152 for (size_t i = 0; i < syncStageAccessInfoByStageAccessIndex.size(); i++) {
153 const auto &info = syncStageAccessInfoByStageAccessIndex[i];
154 if ((flags & info.stage_access_bit).any()) {
155 if (!out_str.empty()) {
156 out_str.append(sep);
157 }
158 out_str.append(info.name);
John Zulauf59e25072020-07-17 10:55:21 -0600159 }
John Zulauf59e25072020-07-17 10:55:21 -0600160 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700161 if (out_str.length() == 0) {
162 out_str.append("Unhandled SyncStageAccess");
163 }
John Zulauf59e25072020-07-17 10:55:21 -0600164 }
165 return out_str;
166}
167
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700168static std::string string_UsageTag(const ResourceUsageTag &tag) {
169 std::stringstream out;
170
John Zulauffaea0ee2021-01-14 14:01:32 -0700171 out << "command: " << CommandTypeString(tag.command);
172 out << ", seq_no: " << tag.seq_num;
173 if (tag.sub_command != 0) {
174 out << ", subcmd: " << tag.sub_command;
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700175 }
176 return out.str();
177}
178
John Zulauffaea0ee2021-01-14 14:01:32 -0700179std::string CommandBufferAccessContext::FormatUsage(const HazardResult &hazard) const {
John Zulauf37ceaed2020-07-03 16:18:15 -0600180 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600181 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
182 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600183 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600184 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
185 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf59e25072020-07-17 10:55:21 -0600186 out << "(usage: " << usage_info.name << ", prior_usage: " << stage_access_name;
187 if (IsHazardVsRead(hazard.hazard)) {
188 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
Jeremy Gebben40a22942020-12-22 14:22:06 -0700189 out << ", read_barriers: " << string_VkPipelineStageFlags2KHR(barriers);
John Zulauf59e25072020-07-17 10:55:21 -0600190 } else {
191 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
192 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
193 }
194
John Zulauffaea0ee2021-01-14 14:01:32 -0700195 // PHASE2 TODO -- add comand buffer and reset from secondary if applicable
ZaOniRinku56b86472021-03-23 20:25:05 +0100196 out << ", " << string_UsageTag(tag) << ", reset_no: " << reset_count_ << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600197 return out.str();
198}
199
John Zulaufd14743a2020-07-03 09:42:39 -0600200// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
201// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
202// also reflects this special case for read hazard detection (using access instead of exec scope)
Jeremy Gebben40a22942020-12-22 14:22:06 -0700203static constexpr VkPipelineStageFlags2KHR kColorAttachmentExecScope = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700204static const SyncStageAccessFlags kColorAttachmentAccessScope =
205 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
206 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
207 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
208 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebben40a22942020-12-22 14:22:06 -0700209static constexpr VkPipelineStageFlags2KHR kDepthStencilAttachmentExecScope =
210 VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT_KHR | VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700211static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
212 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
213 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
214 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -0700215static constexpr VkPipelineStageFlags2KHR kRasterAttachmentExecScope = kDepthStencilAttachmentExecScope | kColorAttachmentExecScope;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700216static const SyncStageAccessFlags kRasterAttachmentAccessScope = kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope;
John Zulaufb027cdb2020-05-21 14:25:22 -0600217
John Zulauf8e3c3e92021-01-06 11:19:36 -0700218ResourceAccessState::OrderingBarriers ResourceAccessState::kOrderingRules = {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700219 {{VK_PIPELINE_STAGE_2_NONE_KHR, SyncStageAccessFlags()},
John Zulauf8e3c3e92021-01-06 11:19:36 -0700220 {kColorAttachmentExecScope, kColorAttachmentAccessScope},
221 {kDepthStencilAttachmentExecScope, kDepthStencilAttachmentAccessScope},
222 {kRasterAttachmentExecScope, kRasterAttachmentAccessScope}}};
223
John Zulauf7635de32020-05-29 17:14:15 -0600224// Sometimes we have an internal access conflict, and we using the kCurrentCommandTag to set and detect in temporary/proxy contexts
John Zulauffaea0ee2021-01-14 14:01:32 -0700225static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, ResourceUsageTag::kMaxCount,
226 ResourceUsageTag::kMaxCount, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600227
John Zulaufb02c1eb2020-10-06 16:33:36 -0600228static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) {
229 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
230}
John Zulauf29d00532021-03-04 13:28:54 -0700231static VkDeviceSize ResourceBaseAddress(const IMAGE_STATE &image_state) {
232 VkDeviceSize base_address;
233 if (image_state.is_swapchain_image || (VK_NULL_HANDLE != image_state.bind_swapchain)) {
234 base_address = image_state.swapchain_fake_address;
235 } else {
236 base_address = ResourceBaseAddress(static_cast<const BINDABLE &>(image_state));
237 }
238 return base_address;
239}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600240
locke-lunarg3c038002020-04-30 23:08:08 -0600241inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
242 if (size == VK_WHOLE_SIZE) {
243 return (whole_size - offset);
244 }
245 return size;
246}
247
John Zulauf3e86bf02020-09-12 10:47:57 -0600248static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
249 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
250}
251
John Zulauf16adfc92020-04-08 10:28:33 -0600252template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600253static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600254 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
255}
256
John Zulauf355e49b2020-04-24 15:11:15 -0600257static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600258
John Zulauf3e86bf02020-09-12 10:47:57 -0600259static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
260 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
261}
262
263static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
264 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
265}
266
John Zulauf4a6105a2020-11-17 15:11:05 -0700267// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
268//
John Zulauf10f1f522020-12-18 12:00:35 -0700269// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
270//
John Zulauf4a6105a2020-11-17 15:11:05 -0700271// Usage:
272// Constructor() -- initializes the generator to point to the begin of the space declared.
273// * -- the current range of the generator empty signfies end
274// ++ -- advance to the next non-empty range (or end)
275
276// A wrapper for a single range with the same semantics as the actual generators below
277template <typename KeyType>
278class SingleRangeGenerator {
279 public:
280 SingleRangeGenerator(const KeyType &range) : current_(range) {}
John Zulaufd5115702021-01-18 12:34:33 -0700281 const KeyType &operator*() const { return current_; }
282 const KeyType *operator->() const { return &current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700283 SingleRangeGenerator &operator++() {
284 current_ = KeyType(); // just one real range
285 return *this;
286 }
287
288 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
289
290 private:
291 SingleRangeGenerator() = default;
292 const KeyType range_;
293 KeyType current_;
294};
295
296// Generate the ranges that are the intersection of range and the entries in the FilterMap
297template <typename FilterMap, typename KeyType = typename FilterMap::key_type>
298class FilteredRangeGenerator {
299 public:
John Zulaufd5115702021-01-18 12:34:33 -0700300 // Default constructed is safe to dereference for "empty" test, but for no other operation.
301 FilteredRangeGenerator() : range_(), filter_(nullptr), filter_pos_(), current_() {
302 // Default construction for KeyType *must* be empty range
303 assert(current_.empty());
304 }
John Zulauf4a6105a2020-11-17 15:11:05 -0700305 FilteredRangeGenerator(const FilterMap &filter, const KeyType &range)
306 : range_(range), filter_(&filter), filter_pos_(), current_() {
307 SeekBegin();
308 }
John Zulaufd5115702021-01-18 12:34:33 -0700309 FilteredRangeGenerator(const FilteredRangeGenerator &from) = default;
310
John Zulauf4a6105a2020-11-17 15:11:05 -0700311 const KeyType &operator*() const { return current_; }
312 const KeyType *operator->() const { return &current_; }
313 FilteredRangeGenerator &operator++() {
314 ++filter_pos_;
315 UpdateCurrent();
316 return *this;
317 }
318
319 bool operator==(const FilteredRangeGenerator &other) const { return current_ == other.current_; }
320
321 private:
John Zulauf4a6105a2020-11-17 15:11:05 -0700322 void UpdateCurrent() {
323 if (filter_pos_ != filter_->cend()) {
324 current_ = range_ & filter_pos_->first;
325 } else {
326 current_ = KeyType();
327 }
328 }
329 void SeekBegin() {
330 filter_pos_ = filter_->lower_bound(range_);
331 UpdateCurrent();
332 }
333 const KeyType range_;
334 const FilterMap *filter_;
335 typename FilterMap::const_iterator filter_pos_;
336 KeyType current_;
337};
John Zulaufd5115702021-01-18 12:34:33 -0700338using SingleAccessRangeGenerator = SingleRangeGenerator<ResourceAccessRange>;
John Zulauf4a6105a2020-11-17 15:11:05 -0700339using EventSimpleRangeGenerator = FilteredRangeGenerator<SyncEventState::ScopeMap>;
340
341// Templated to allow for different Range generators or map sources...
342
343// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulauf4a6105a2020-11-17 15:11:05 -0700344template <typename FilterMap, typename RangeGen, typename KeyType = typename FilterMap::key_type>
345class FilteredGeneratorGenerator {
346 public:
John Zulaufd5115702021-01-18 12:34:33 -0700347 // Default constructed is safe to dereference for "empty" test, but for no other operation.
348 FilteredGeneratorGenerator() : filter_(nullptr), gen_(), filter_pos_(), current_() {
349 // Default construction for KeyType *must* be empty range
350 assert(current_.empty());
351 }
352 FilteredGeneratorGenerator(const FilterMap &filter, RangeGen &gen) : filter_(&filter), gen_(gen), filter_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700353 SeekBegin();
354 }
John Zulaufd5115702021-01-18 12:34:33 -0700355 FilteredGeneratorGenerator(const FilteredGeneratorGenerator &from) = default;
John Zulauf4a6105a2020-11-17 15:11:05 -0700356 const KeyType &operator*() const { return current_; }
357 const KeyType *operator->() const { return &current_; }
358 FilteredGeneratorGenerator &operator++() {
359 KeyType gen_range = GenRange();
360 KeyType filter_range = FilterRange();
361 current_ = KeyType();
362 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
363 if (gen_range.end > filter_range.end) {
364 // if the generated range is beyond the filter_range, advance the filter range
365 filter_range = AdvanceFilter();
366 } else {
367 gen_range = AdvanceGen();
368 }
369 current_ = gen_range & filter_range;
370 }
371 return *this;
372 }
373
374 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
375
376 private:
377 KeyType AdvanceFilter() {
378 ++filter_pos_;
379 auto filter_range = FilterRange();
380 if (filter_range.valid()) {
381 FastForwardGen(filter_range);
382 }
383 return filter_range;
384 }
385 KeyType AdvanceGen() {
John Zulaufd5115702021-01-18 12:34:33 -0700386 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700387 auto gen_range = GenRange();
388 if (gen_range.valid()) {
389 FastForwardFilter(gen_range);
390 }
391 return gen_range;
392 }
393
394 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
John Zulaufd5115702021-01-18 12:34:33 -0700395 KeyType GenRange() const { return *gen_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700396
397 KeyType FastForwardFilter(const KeyType &range) {
398 auto filter_range = FilterRange();
399 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700400 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700401 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
402 if (retry_count < kRetryLimit) {
403 ++filter_pos_;
404 filter_range = FilterRange();
405 retry_count++;
406 } else {
407 // Okay we've tried walking, do a seek.
408 filter_pos_ = filter_->lower_bound(range);
409 break;
410 }
411 }
412 return FilterRange();
413 }
414
415 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
416 // faster.
417 KeyType FastForwardGen(const KeyType &range) {
418 auto gen_range = GenRange();
419 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
John Zulaufd5115702021-01-18 12:34:33 -0700420 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700421 gen_range = GenRange();
422 }
423 return gen_range;
424 }
425
426 void SeekBegin() {
427 auto gen_range = GenRange();
428 if (gen_range.empty()) {
429 current_ = KeyType();
430 filter_pos_ = filter_->cend();
431 } else {
432 filter_pos_ = filter_->lower_bound(gen_range);
433 current_ = gen_range & FilterRange();
434 }
435 }
436
John Zulauf4a6105a2020-11-17 15:11:05 -0700437 const FilterMap *filter_;
John Zulaufd5115702021-01-18 12:34:33 -0700438 RangeGen gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700439 typename FilterMap::const_iterator filter_pos_;
440 KeyType current_;
441};
442
443using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
444
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700445static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700446
John Zulauf3e86bf02020-09-12 10:47:57 -0600447ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
448 VkDeviceSize stride) {
449 VkDeviceSize range_start = offset + first_index * stride;
450 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600451 if (count == UINT32_MAX) {
452 range_size = buf_whole_size - range_start;
453 } else {
454 range_size = count * stride;
455 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600456 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600457}
458
locke-lunarg654e3692020-06-04 17:19:15 -0600459SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
460 VkShaderStageFlagBits stage_flag) {
461 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
462 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
463 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
464 }
465 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
466 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
467 assert(0);
468 }
469 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
470 return stage_access->second.uniform_read;
471 }
472
473 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
474 // Because if write hazard happens, read hazard might or might not happen.
475 // But if write hazard doesn't happen, read hazard is impossible to happen.
476 if (descriptor_data.is_writable) {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700477 return stage_access->second.storage_write;
locke-lunarg654e3692020-06-04 17:19:15 -0600478 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700479 // TODO: sampled_read
480 return stage_access->second.storage_read;
locke-lunarg654e3692020-06-04 17:19:15 -0600481}
482
locke-lunarg37047832020-06-12 13:44:45 -0600483bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
484 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
485 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
486 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
487 ? true
488 : false;
489}
490
491bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
492 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
493 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
494 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
495 ? true
496 : false;
497}
498
John Zulauf355e49b2020-04-24 15:11:15 -0600499// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600500template <typename Action>
501static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
502 Action &action) {
503 // At this point the "apply over range" logic only supports a single memory binding
504 if (!SimpleBinding(image_state)) return;
505 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600506 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700507 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
508 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600509 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700510 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600511 }
512}
513
John Zulauf7635de32020-05-29 17:14:15 -0600514// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
515// Used by both validation and record operations
516//
517// The signature for Action() reflect the needs of both uses.
518template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700519void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
520 uint32_t subpass) {
John Zulauf7635de32020-05-29 17:14:15 -0600521 const auto &rp_ci = rp_state.createInfo;
522 const auto *attachment_ci = rp_ci.pAttachments;
523 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
524
525 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
526 const auto *color_attachments = subpass_ci.pColorAttachments;
527 const auto *color_resolve = subpass_ci.pResolveAttachments;
528 if (color_resolve && color_attachments) {
529 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
530 const auto &color_attach = color_attachments[i].attachment;
531 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
532 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
533 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700534 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ,
535 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600536 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700537 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
538 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600539 }
540 }
541 }
542
543 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700544 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600545 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
546 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
547 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
548 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
549 const auto src_ci = attachment_ci[src_at];
550 // The formats are required to match so we can pick either
551 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
552 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
553 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
John Zulauf7635de32020-05-29 17:14:15 -0600554
555 // Figure out which aspects are actually touched during resolve operations
556 const char *aspect_string = nullptr;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700557 AttachmentViewGen::Gen gen_type = AttachmentViewGen::Gen::kRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600558 if (resolve_depth && resolve_stencil) {
John Zulauf7635de32020-05-29 17:14:15 -0600559 aspect_string = "depth/stencil";
560 } else if (resolve_depth) {
561 // Validate depth only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700562 gen_type = AttachmentViewGen::Gen::kDepthOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600563 aspect_string = "depth";
564 } else if (resolve_stencil) {
565 // Validate all stencil only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700566 gen_type = AttachmentViewGen::Gen::kStencilOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600567 aspect_string = "stencil";
568 }
569
John Zulaufd0ec59f2021-03-13 14:25:08 -0700570 if (aspect_string) {
571 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at], gen_type,
572 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster);
573 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at], gen_type,
574 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulauf7635de32020-05-29 17:14:15 -0600575 }
576 }
577}
578
579// Action for validating resolve operations
580class ValidateResolveAction {
581 public:
John Zulauffaea0ee2021-01-14 14:01:32 -0700582 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context,
John Zulauf64ffe552021-02-06 10:25:07 -0700583 const CommandExecutionContext &ex_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600584 : render_pass_(render_pass),
585 subpass_(subpass),
586 context_(context),
John Zulauf64ffe552021-02-06 10:25:07 -0700587 ex_context_(ex_context),
John Zulauf7635de32020-05-29 17:14:15 -0600588 func_name_(func_name),
589 skip_(false) {}
590 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
John Zulaufd0ec59f2021-03-13 14:25:08 -0700591 const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage,
592 SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600593 HazardResult hazard;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700594 hazard = context_.DetectHazard(view_gen, gen_type, current_usage, ordering_rule);
John Zulauf7635de32020-05-29 17:14:15 -0600595 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -0700596 skip_ |=
John Zulauf64ffe552021-02-06 10:25:07 -0700597 ex_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700598 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
599 " to resolve attachment %" PRIu32 ". Access info %s.",
600 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name,
John Zulauf64ffe552021-02-06 10:25:07 -0700601 attachment_name, src_at, dst_at, ex_context_.FormatUsage(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600602 }
603 }
604 // Providing a mechanism for the constructing caller to get the result of the validation
605 bool GetSkip() const { return skip_; }
606
607 private:
608 VkRenderPass render_pass_;
609 const uint32_t subpass_;
610 const AccessContext &context_;
John Zulauf64ffe552021-02-06 10:25:07 -0700611 const CommandExecutionContext &ex_context_;
John Zulauf7635de32020-05-29 17:14:15 -0600612 const char *func_name_;
613 bool skip_;
614};
615
616// Update action for resolve operations
617class UpdateStateResolveAction {
618 public:
619 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
John Zulaufd0ec59f2021-03-13 14:25:08 -0700620 void operator()(const char *, const char *, uint32_t, uint32_t, const AttachmentViewGen &view_gen,
621 AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600622 // Ignores validation only arguments...
John Zulaufd0ec59f2021-03-13 14:25:08 -0700623 context_.UpdateAccessState(view_gen, gen_type, current_usage, ordering_rule, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600624 }
625
626 private:
627 AccessContext &context_;
628 const ResourceUsageTag &tag_;
629};
630
John Zulauf59e25072020-07-17 10:55:21 -0600631void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700632 const SyncStageAccessFlags &prior_, const ResourceUsageTag &tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600633 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
634 usage_index = usage_index_;
635 hazard = hazard_;
636 prior_access = prior_;
637 tag = tag_;
638}
639
John Zulauf540266b2020-04-06 18:54:53 -0600640AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
641 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600642 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600643 Reset();
644 const auto &subpass_dep = dependencies[subpass];
John Zulauf22aefed2021-03-11 18:14:35 -0700645 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
646 prev_.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
John Zulauf355e49b2020-04-24 15:11:15 -0600647 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600648 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600649 const auto prev_pass = prev_dep.first->pass;
650 const auto &prev_barriers = prev_dep.second;
651 assert(prev_dep.second.size());
652 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
653 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700654 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600655
656 async_.reserve(subpass_dep.async.size());
657 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700658 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600659 }
John Zulauf22aefed2021-03-11 18:14:35 -0700660 if (has_barrier_from_external) {
661 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
662 prev_.emplace_back(external_context, queue_flags, subpass_dep.barrier_from_external);
663 src_external_ = &prev_.back();
John Zulaufe5da6e52020-03-18 15:32:18 -0600664 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600665 if (subpass_dep.barrier_to_external.size()) {
666 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600667 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700668}
669
John Zulauf5f13a792020-03-10 07:31:21 -0600670template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700671HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600672 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600673 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600674 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600675
676 HazardResult hazard;
677 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
678 hazard = detector.Detect(prev);
679 }
680 return hazard;
681}
682
John Zulauf4a6105a2020-11-17 15:11:05 -0700683template <typename Action>
684void AccessContext::ForAll(Action &&action) {
685 for (const auto address_type : kAddressTypes) {
686 auto &accesses = GetAccessStateMap(address_type);
687 for (const auto &access : accesses) {
688 action(address_type, access);
689 }
690 }
691}
692
John Zulauf3d84f1b2020-03-09 13:33:25 -0600693// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
694// the DAG of the contexts (for example subpasses)
695template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700696HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600697 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600698 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600699
John Zulauf1a224292020-06-30 14:52:13 -0600700 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600701 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
702 // so we'll check these first
703 for (const auto &async_context : async_) {
704 hazard = async_context->DetectAsyncHazard(type, detector, range);
705 if (hazard.hazard) return hazard;
706 }
John Zulauf5f13a792020-03-10 07:31:21 -0600707 }
708
John Zulauf1a224292020-06-30 14:52:13 -0600709 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600710
John Zulauf69133422020-05-20 14:55:53 -0600711 const auto &accesses = GetAccessStateMap(type);
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
John Zulauf22aefed2021-03-11 18:14:35 -0700771struct ApplyTrackbackStackAction {
772 explicit ApplyTrackbackStackAction(const std::vector<SyncBarrier> &barriers_,
773 const ResourceAccessStateFunction *previous_barrier_ = nullptr)
774 : barriers(barriers_), previous_barrier(previous_barrier_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600775 void operator()(ResourceAccessState *access) const {
776 assert(access);
777 assert(!access->HasPendingState());
778 access->ApplyBarriers(barriers, false);
779 access->ApplyPendingBarriers(kCurrentCommandTag);
John Zulauf22aefed2021-03-11 18:14:35 -0700780 if (previous_barrier) {
781 assert(bool(*previous_barrier));
782 (*previous_barrier)(access);
783 }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600784 }
785 const std::vector<SyncBarrier> &barriers;
John Zulauf22aefed2021-03-11 18:14:35 -0700786 const ResourceAccessStateFunction *previous_barrier;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600787};
788
789// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
790// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
791// *different* map from dest.
792// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
793// range [first, last)
794template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600795static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
796 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600797 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600798 auto at = entry;
799 for (auto pos = first; pos != last; ++pos) {
800 // Every member of the input iterator range must fit within the remaining portion of entry
801 assert(at->first.includes(pos->first));
802 assert(at != dest->end());
803 // Trim up at to the same size as the entry to resolve
804 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600805 auto access = pos->second; // intentional copy
806 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600807 at->second.Resolve(access);
808 ++at; // Go to the remaining unused section of entry
809 }
810}
811
John Zulaufa0a98292020-09-18 09:30:10 -0600812static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
813 SyncBarrier merged = {};
814 for (const auto &barrier : barriers) {
815 merged.Merge(barrier);
816 }
817 return merged;
818}
819
John Zulaufb02c1eb2020-10-06 16:33:36 -0600820template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700821void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600822 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
823 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600824 if (!range.non_empty()) return;
825
John Zulauf355e49b2020-04-24 15:11:15 -0600826 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
827 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600828 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600829 if (current->pos_B->valid) {
830 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600831 auto access = src_pos->second; // intentional copy
832 barrier_action(&access);
833
John Zulauf16adfc92020-04-08 10:28:33 -0600834 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600835 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
836 trimmed->second.Resolve(access);
837 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600838 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600839 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600840 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600841 }
John Zulauf16adfc92020-04-08 10:28:33 -0600842 } else {
843 // we have to descend to fill this gap
844 if (recur_to_infill) {
John Zulauf22aefed2021-03-11 18:14:35 -0700845 ResourceAccessRange recurrence_range = current_range;
846 // The current context is empty for the current range, so recur to fill the gap.
847 // Since we will be recurring back up the DAG, expand the gap descent to cover the full range for which B
848 // is not valid, to minimize that recurrence
849 if (current->pos_B.at_end()) {
850 // Do the remainder here....
851 recurrence_range.end = range.end;
John Zulauf355e49b2020-04-24 15:11:15 -0600852 } else {
John Zulauf22aefed2021-03-11 18:14:35 -0700853 // Recur only over the range until B becomes valid (within the limits of range).
854 recurrence_range.end = std::min(range.end, current->pos_B->lower_bound->first.begin);
John Zulauf355e49b2020-04-24 15:11:15 -0600855 }
John Zulauf22aefed2021-03-11 18:14:35 -0700856 ResolvePreviousAccessStack(type, recurrence_range, resolve_map, infill_state, barrier_action);
857
John Zulauf355e49b2020-04-24 15:11:15 -0600858 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
859 // iterator of the outer while.
860
861 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
862 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
863 // we stepped on the dest map
John Zulauf22aefed2021-03-11 18:14:35 -0700864 const auto seek_to = recurrence_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
locke-lunarg88dbb542020-06-23 22:05:42 -0600865 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600866 current.seek(seek_to);
867 } else if (!current->pos_A->valid && infill_state) {
868 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
869 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
870 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600871 }
John Zulauf5f13a792020-03-10 07:31:21 -0600872 }
John Zulauf16adfc92020-04-08 10:28:33 -0600873 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600874 }
John Zulauf1a224292020-06-30 14:52:13 -0600875
876 // Infill if range goes passed both the current and resolve map prior contents
877 if (recur_to_infill && (current->range.end < range.end)) {
878 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
John Zulauf22aefed2021-03-11 18:14:35 -0700879 ResolvePreviousAccessStack<BarrierAction>(type, trailing_fill_range, resolve_map, infill_state, barrier_action);
John Zulauf1a224292020-06-30 14:52:13 -0600880 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600881}
882
John Zulauf22aefed2021-03-11 18:14:35 -0700883template <typename BarrierAction>
884void AccessContext::ResolvePreviousAccessStack(AccessAddressType type, const ResourceAccessRange &range,
885 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
886 const BarrierAction &previous_barrier) const {
887 ResourceAccessStateFunction stacked_barrier(std::ref(previous_barrier));
888 ResolvePreviousAccess(type, range, descent_map, infill_state, &stacked_barrier);
889}
890
John Zulauf43cc7462020-12-03 12:33:12 -0700891void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
John Zulauf22aefed2021-03-11 18:14:35 -0700892 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
893 const ResourceAccessStateFunction *previous_barrier) const {
894 if (prev_.size() == 0) {
John Zulauf5f13a792020-03-10 07:31:21 -0600895 if (range.non_empty() && infill_state) {
John Zulauf22aefed2021-03-11 18:14:35 -0700896 // Fill the empty poritions of descent_map with the default_state with the barrier function applied (iff present)
897 ResourceAccessState state_copy;
898 if (previous_barrier) {
899 assert(bool(*previous_barrier));
900 state_copy = *infill_state;
901 (*previous_barrier)(&state_copy);
902 infill_state = &state_copy;
903 }
904 sparse_container::update_range_value(*descent_map, range, *infill_state,
905 sparse_container::value_precedence::prefer_dest);
John Zulauf5f13a792020-03-10 07:31:21 -0600906 }
907 } else {
908 // Look for something to fill the gap further along.
909 for (const auto &prev_dep : prev_) {
John Zulauf22aefed2021-03-11 18:14:35 -0700910 const ApplyTrackbackStackAction barrier_action(prev_dep.barriers, previous_barrier);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600911 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600912 }
John Zulauf5f13a792020-03-10 07:31:21 -0600913 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600914}
915
John Zulauf4a6105a2020-11-17 15:11:05 -0700916// Non-lazy import of all accesses, WaitEvents needs this.
917void AccessContext::ResolvePreviousAccesses() {
918 ResourceAccessState default_state;
John Zulauf22aefed2021-03-11 18:14:35 -0700919 if (!prev_.size()) return; // If no previous contexts, nothing to do
920
John Zulauf4a6105a2020-11-17 15:11:05 -0700921 for (const auto address_type : kAddressTypes) {
922 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
923 }
924}
925
John Zulauf43cc7462020-12-03 12:33:12 -0700926AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
927 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -0600928}
929
John Zulauf1507ee42020-05-18 11:33:09 -0600930static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
931 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
932 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
933 return stage_access;
934}
935static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
936 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
937 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
938 return stage_access;
939}
940
John Zulauf7635de32020-05-29 17:14:15 -0600941// Caller must manage returned pointer
942static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -0700943 uint32_t subpass, const AttachmentViewGenVector &attachment_views) {
John Zulauf7635de32020-05-29 17:14:15 -0600944 auto *proxy = new AccessContext(context);
John Zulaufd0ec59f2021-03-13 14:25:08 -0700945 proxy->UpdateAttachmentResolveAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
946 proxy->UpdateAttachmentStoreAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600947 return proxy;
948}
949
John Zulaufb02c1eb2020-10-06 16:33:36 -0600950template <typename BarrierAction>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700951void AccessContext::ResolveAccessRange(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
952 BarrierAction &barrier_action, ResourceAccessRangeMap *descent_map,
953 const ResourceAccessState *infill_state) const {
954 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
955 if (!attachment_gen) return;
956
957 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
958 const AccessAddressType address_type = view_gen.GetAddressType();
959 for (; range_gen->non_empty(); ++range_gen) {
960 ResolveAccessRange(address_type, *range_gen, barrier_action, descent_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600961 }
John Zulauf62f10592020-04-03 12:20:02 -0600962}
963
John Zulauf7635de32020-05-29 17:14:15 -0600964// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf64ffe552021-02-06 10:25:07 -0700965bool AccessContext::ValidateLayoutTransitions(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600966 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -0700967 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600968 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600969 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
970 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
971 // those affects have not been recorded yet.
972 //
973 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
974 // to apply and only copy then, if this proves a hot spot.
975 std::unique_ptr<AccessContext> proxy_for_prev;
976 TrackBack proxy_track_back;
977
John Zulauf355e49b2020-04-24 15:11:15 -0600978 const auto &transitions = rp_state.subpass_transitions[subpass];
979 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600980 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
981
982 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
John Zulauf22aefed2021-03-11 18:14:35 -0700983 assert(track_back);
John Zulauf7635de32020-05-29 17:14:15 -0600984 if (prev_needs_proxy) {
985 if (!proxy_for_prev) {
John Zulaufd0ec59f2021-03-13 14:25:08 -0700986 proxy_for_prev.reset(
987 CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass, attachment_views));
John Zulauf7635de32020-05-29 17:14:15 -0600988 proxy_track_back = *track_back;
989 proxy_track_back.context = proxy_for_prev.get();
990 }
991 track_back = &proxy_track_back;
992 }
993 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600994 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -0700995 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700996 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
997 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
998 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
999 string_VkImageLayout(transition.old_layout),
1000 string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07001001 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001002 }
1003 }
1004 return skip;
1005}
1006
John Zulauf64ffe552021-02-06 10:25:07 -07001007bool AccessContext::ValidateLoadOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001008 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001009 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001010 bool skip = false;
1011 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufa0a98292020-09-18 09:30:10 -06001012
John Zulauf1507ee42020-05-18 11:33:09 -06001013 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1014 if (subpass == rp_state.attachment_first_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001015 const auto &view_gen = attachment_views[i];
1016 if (!view_gen.IsValid()) continue;
John Zulauf1507ee42020-05-18 11:33:09 -06001017 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001018
1019 // Need check in the following way
1020 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1021 // vs. transition
1022 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1023 // for each aspect loaded.
1024
1025 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001026 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001027 const bool is_color = !(has_depth || has_stencil);
1028
1029 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001030 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001031
John Zulaufaff20662020-06-01 14:07:58 -06001032 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001033 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001034
John Zulaufb02c1eb2020-10-06 16:33:36 -06001035 bool checked_stencil = false;
1036 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001037 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea, load_index, SyncOrdering::kColorAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001038 aspect = "color";
1039 } else {
1040 if (has_depth) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001041 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_index,
1042 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001043 aspect = "depth";
1044 }
1045 if (!hazard.hazard && has_stencil) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001046 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, stencil_load_index,
1047 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001048 aspect = "stencil";
1049 checked_stencil = true;
1050 }
1051 }
1052
1053 if (hazard.hazard) {
1054 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001055 const auto &sync_state = ex_context.GetSyncState();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001056 if (hazard.tag == kCurrentCommandTag) {
1057 // Hazard vs. ILT
1058 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1059 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1060 " aspect %s during load with loadOp %s.",
1061 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1062 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06001063 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1064 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001065 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001066 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf64ffe552021-02-06 10:25:07 -07001067 ex_context.FormatUsage(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001068 }
1069 }
1070 }
1071 }
1072 return skip;
1073}
1074
John Zulaufaff20662020-06-01 14:07:58 -06001075// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1076// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1077// store is part of the same Next/End operation.
1078// The latter is handled in layout transistion validation directly
John Zulauf64ffe552021-02-06 10:25:07 -07001079bool AccessContext::ValidateStoreOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufaff20662020-06-01 14:07:58 -06001080 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001081 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001082 bool skip = false;
1083 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001084
1085 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1086 if (subpass == rp_state.attachment_last_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001087 const AttachmentViewGen &view_gen = attachment_views[i];
1088 if (!view_gen.IsValid()) continue;
John Zulaufaff20662020-06-01 14:07:58 -06001089 const auto &ci = attachment_ci[i];
1090
1091 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1092 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1093 // sake, we treat DONT_CARE as writing.
1094 const bool has_depth = FormatHasDepth(ci.format);
1095 const bool has_stencil = FormatHasStencil(ci.format);
1096 const bool is_color = !(has_depth || has_stencil);
1097 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1098 if (!has_stencil && !store_op_stores) continue;
1099
1100 HazardResult hazard;
1101 const char *aspect = nullptr;
1102 bool checked_stencil = false;
1103 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001104 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
1105 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001106 aspect = "color";
1107 } else {
1108 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
John Zulaufaff20662020-06-01 14:07:58 -06001109 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001110 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1111 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001112 aspect = "depth";
1113 }
1114 if (!hazard.hazard && has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001115 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1116 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001117 aspect = "stencil";
1118 checked_stencil = true;
1119 }
1120 }
1121
1122 if (hazard.hazard) {
1123 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1124 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001125 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001126 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1127 " %s aspect during store with %s %s. Access info %s",
1128 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect,
John Zulauf64ffe552021-02-06 10:25:07 -07001129 op_type_string, store_op_string, ex_context.FormatUsage(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001130 }
1131 }
1132 }
1133 return skip;
1134}
1135
John Zulauf64ffe552021-02-06 10:25:07 -07001136bool AccessContext::ValidateResolveOperations(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001137 const VkRect2D &render_area, const AttachmentViewGenVector &attachment_views,
1138 const char *func_name, uint32_t subpass) const {
John Zulauf64ffe552021-02-06 10:25:07 -07001139 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, ex_context, func_name);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001140 ResolveOperation(validate_action, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001141 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001142}
1143
John Zulauf3d84f1b2020-03-09 13:33:25 -06001144class HazardDetector {
1145 SyncStageAccessIndex usage_index_;
1146
1147 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001148 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001149 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1150 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001151 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001152 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001153};
1154
John Zulauf69133422020-05-20 14:55:53 -06001155class HazardDetectorWithOrdering {
1156 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001157 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001158
1159 public:
1160 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001161 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001162 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001163 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1164 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001165 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001166 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001167};
1168
John Zulauf16adfc92020-04-08 10:28:33 -06001169HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001170 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001171 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001172 const auto base_address = ResourceBaseAddress(buffer);
1173 HazardDetector detector(usage_index);
1174 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001175}
1176
John Zulauf69133422020-05-20 14:55:53 -06001177template <typename Detector>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001178HazardResult AccessContext::DetectHazard(Detector &detector, const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1179 DetectOptions options) const {
1180 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1181 if (!attachment_gen) return HazardResult();
1182
1183 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1184 const auto address_type = view_gen.GetAddressType();
1185 for (; range_gen->non_empty(); ++range_gen) {
1186 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1187 if (hazard.hazard) return hazard;
1188 }
1189
1190 return HazardResult();
1191}
1192
1193template <typename Detector>
John Zulauf69133422020-05-20 14:55:53 -06001194HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1195 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1196 const VkExtent3D &extent, DetectOptions options) const {
1197 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001198 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001199 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1200 base_address);
1201 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001202 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001203 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001204 if (hazard.hazard) return hazard;
1205 }
1206 return HazardResult();
1207}
John Zulauf110413c2021-03-20 05:38:38 -06001208template <typename Detector>
1209HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1210 const VkImageSubresourceRange &subresource_range, DetectOptions options) const {
1211 if (!SimpleBinding(image)) return HazardResult();
1212 const auto base_address = ResourceBaseAddress(image);
1213 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1214 const auto address_type = ImageAddressType(image);
1215 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf110413c2021-03-20 05:38:38 -06001216 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1217 if (hazard.hazard) return hazard;
1218 }
1219 return HazardResult();
1220}
John Zulauf69133422020-05-20 14:55:53 -06001221
John Zulauf540266b2020-04-06 18:54:53 -06001222HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1223 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1224 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001225 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1226 subresource.layerCount};
John Zulauf110413c2021-03-20 05:38:38 -06001227 HazardDetector detector(current_usage);
1228 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf1507ee42020-05-18 11:33:09 -06001229}
1230
1231HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf110413c2021-03-20 05:38:38 -06001232 const VkImageSubresourceRange &subresource_range) const {
John Zulauf69133422020-05-20 14:55:53 -06001233 HazardDetector detector(current_usage);
John Zulauf110413c2021-03-20 05:38:38 -06001234 return DetectHazard(detector, image, subresource_range, DetectOptions::kDetectAll);
John Zulauf69133422020-05-20 14:55:53 -06001235}
1236
John Zulaufd0ec59f2021-03-13 14:25:08 -07001237HazardResult AccessContext::DetectHazard(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1238 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) const {
1239 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
1240 return DetectHazard(detector, view_gen, gen_type, DetectOptions::kDetectAll);
1241}
1242
John Zulauf69133422020-05-20 14:55:53 -06001243HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001244 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001245 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001246 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001247 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001248}
1249
John Zulauf3d84f1b2020-03-09 13:33:25 -06001250class BarrierHazardDetector {
1251 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001252 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001253 SyncStageAccessFlags src_access_scope)
1254 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1255
John Zulauf5f13a792020-03-10 07:31:21 -06001256 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1257 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001258 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001259 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001260 // 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 -07001261 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001262 }
1263
1264 private:
1265 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001266 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001267 SyncStageAccessFlags src_access_scope_;
1268};
1269
John Zulauf4a6105a2020-11-17 15:11:05 -07001270class EventBarrierHazardDetector {
1271 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001272 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001273 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
1274 const ResourceUsageTag &scope_tag)
1275 : usage_index_(usage_index),
1276 src_exec_scope_(src_exec_scope),
1277 src_access_scope_(src_access_scope),
1278 event_scope_(event_scope),
1279 scope_pos_(event_scope.cbegin()),
1280 scope_end_(event_scope.cend()),
1281 scope_tag_(scope_tag) {}
1282
1283 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1284 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1285 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1286 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1287 if (scope_pos_ == scope_end_) return HazardResult();
1288 if (!scope_pos_->first.intersects(pos->first)) {
1289 event_scope_.lower_bound(pos->first);
1290 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1291 }
1292
1293 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1294 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1295 }
1296 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1297 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1298 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1299 }
1300
1301 private:
1302 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001303 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001304 SyncStageAccessFlags src_access_scope_;
1305 const SyncEventState::ScopeMap &event_scope_;
1306 SyncEventState::ScopeMap::const_iterator scope_pos_;
1307 SyncEventState::ScopeMap::const_iterator scope_end_;
1308 const ResourceUsageTag &scope_tag_;
1309};
1310
Jeremy Gebben40a22942020-12-22 14:22:06 -07001311HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001312 const SyncStageAccessFlags &src_access_scope,
1313 const VkImageSubresourceRange &subresource_range,
1314 const SyncEventState &sync_event, DetectOptions options) const {
1315 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1316 // first access scope map to use, and there's no easy way to plumb it in below.
1317 const auto address_type = ImageAddressType(image);
1318 const auto &event_scope = sync_event.FirstScope(address_type);
1319
1320 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1321 event_scope, sync_event.first_scope_tag);
John Zulauf110413c2021-03-20 05:38:38 -06001322 return DetectHazard(detector, image, subresource_range, options);
John Zulauf4a6105a2020-11-17 15:11:05 -07001323}
1324
John Zulaufd0ec59f2021-03-13 14:25:08 -07001325HazardResult AccessContext::DetectImageBarrierHazard(const AttachmentViewGen &view_gen, const SyncBarrier &barrier,
1326 DetectOptions options) const {
1327 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, barrier.src_exec_scope.exec_scope,
1328 barrier.src_access_scope);
1329 return DetectHazard(detector, view_gen, AttachmentViewGen::Gen::kViewSubresource, options);
1330}
1331
Jeremy Gebben40a22942020-12-22 14:22:06 -07001332HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001333 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001334 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001335 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001336 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
John Zulauf110413c2021-03-20 05:38:38 -06001337 return DetectHazard(detector, image, subresource_range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001338}
1339
Jeremy Gebben40a22942020-12-22 14:22:06 -07001340HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001341 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001342 const VkImageMemoryBarrier &barrier) const {
1343 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1344 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1345 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1346}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001347HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07001348 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope.exec_scope,
John Zulauf110413c2021-03-20 05:38:38 -06001349 image_barrier.barrier.src_access_scope, image_barrier.range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001350}
John Zulauf355e49b2020-04-24 15:11:15 -06001351
John Zulauf9cb530d2019-09-30 14:14:10 -06001352template <typename Flags, typename Map>
1353SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1354 SyncStageAccessFlags scope = 0;
1355 for (const auto &bit_scope : map) {
1356 if (flag_mask < bit_scope.first) break;
1357
1358 if (flag_mask & bit_scope.first) {
1359 scope |= bit_scope.second;
1360 }
1361 }
1362 return scope;
1363}
1364
Jeremy Gebben40a22942020-12-22 14:22:06 -07001365SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags2KHR stages) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001366 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1367}
1368
Jeremy Gebben40a22942020-12-22 14:22:06 -07001369SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags2KHR accesses) {
1370 return AccessScopeImpl(sync_utils::ExpandAccessFlags(accesses), syncStageAccessMaskByAccessBit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001371}
1372
Jeremy Gebben40a22942020-12-22 14:22:06 -07001373// Getting from stage mask and access mask to stage/access masks is something we need to be good at...
1374SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001375 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1376 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1377 // 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 -06001378 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1379}
1380
1381template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001382void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001383 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1384 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001385 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001386 auto pos = accesses->lower_bound(range);
1387 if (pos == accesses->end() || !pos->first.intersects(range)) {
1388 // The range is empty, fill it with a default value.
1389 pos = action.Infill(accesses, pos, range);
1390 } else if (range.begin < pos->first.begin) {
1391 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001392 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001393 } else if (pos->first.begin < range.begin) {
1394 // Trim the beginning if needed
1395 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1396 ++pos;
1397 }
1398
1399 const auto the_end = accesses->end();
1400 while ((pos != the_end) && pos->first.intersects(range)) {
1401 if (pos->first.end > range.end) {
1402 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1403 }
1404
1405 pos = action(accesses, pos);
1406 if (pos == the_end) break;
1407
1408 auto next = pos;
1409 ++next;
1410 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1411 // Need to infill if next is disjoint
1412 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001413 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001414 next = action.Infill(accesses, next, new_range);
1415 }
1416 pos = next;
1417 }
1418}
John Zulaufd5115702021-01-18 12:34:33 -07001419
1420// Give a comparable interface for range generators and ranges
1421template <typename Action>
1422inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1423 assert(range);
1424 UpdateMemoryAccessState(accesses, *range, action);
1425}
1426
John Zulauf4a6105a2020-11-17 15:11:05 -07001427template <typename Action, typename RangeGen>
1428void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1429 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001430 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 -07001431 for (; range_gen->non_empty(); ++range_gen) {
1432 UpdateMemoryAccessState(accesses, *range_gen, action);
1433 }
1434}
John Zulauf9cb530d2019-09-30 14:14:10 -06001435
John Zulaufd0ec59f2021-03-13 14:25:08 -07001436template <typename Action, typename RangeGen>
1437void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, const RangeGen &range_gen_prebuilt) {
1438 RangeGen range_gen(range_gen_prebuilt); // RangeGenerators can be expensive to create from scratch... initialize from built
1439 for (; range_gen->non_empty(); ++range_gen) {
1440 UpdateMemoryAccessState(accesses, *range_gen, action);
1441 }
1442}
John Zulauf9cb530d2019-09-30 14:14:10 -06001443struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001444 using Iterator = ResourceAccessRangeMap::iterator;
1445 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001446 // this is only called on gaps, and never returns a gap.
1447 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001448 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001449 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001450 }
John Zulauf5f13a792020-03-10 07:31:21 -06001451
John Zulauf5c5e88d2019-12-26 11:22:02 -07001452 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001453 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001454 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001455 return pos;
1456 }
1457
John Zulauf43cc7462020-12-03 12:33:12 -07001458 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001459 SyncOrdering ordering_rule_, const ResourceUsageTag &tag_)
1460 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001461 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001462 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001463 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001464 const SyncOrdering ordering_rule;
John Zulauf9cb530d2019-09-30 14:14:10 -06001465 const ResourceUsageTag &tag;
1466};
1467
John Zulauf4a6105a2020-11-17 15:11:05 -07001468// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001469struct PipelineBarrierOp {
1470 SyncBarrier barrier;
1471 bool layout_transition;
1472 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1473 : barrier(barrier_), layout_transition(layout_transition_) {}
1474 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001475 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001476 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1477};
John Zulauf4a6105a2020-11-17 15:11:05 -07001478// The barrier operation for wait events
1479struct WaitEventBarrierOp {
1480 const ResourceUsageTag *scope_tag;
1481 SyncBarrier barrier;
1482 bool layout_transition;
1483 WaitEventBarrierOp(const ResourceUsageTag &scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1484 : scope_tag(&scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
1485 WaitEventBarrierOp() = default;
1486 void operator()(ResourceAccessState *access_state) const {
1487 assert(scope_tag); // Not valid to have a non-scope op executed, default construct included for std::vector support
1488 access_state->ApplyBarrier(*scope_tag, barrier, layout_transition);
1489 }
1490};
John Zulauf1e331ec2020-12-04 18:29:38 -07001491
John Zulauf4a6105a2020-11-17 15:11:05 -07001492// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1493// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1494// of a collection is known/present.
John Zulauf1e331ec2020-12-04 18:29:38 -07001495template <typename BarrierOp>
John Zulauf89311b42020-09-29 16:28:47 -06001496class ApplyBarrierOpsFunctor {
1497 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001498 using Iterator = ResourceAccessRangeMap::iterator;
1499 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001500
John Zulauf5c5e88d2019-12-26 11:22:02 -07001501 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001502 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001503 for (const auto &op : barrier_ops_) {
1504 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001505 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001506
John Zulauf89311b42020-09-29 16:28:47 -06001507 if (resolve_) {
1508 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1509 // another walk
1510 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001511 }
1512 return pos;
1513 }
1514
John Zulauf89311b42020-09-29 16:28:47 -06001515 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulaufd5115702021-01-18 12:34:33 -07001516 ApplyBarrierOpsFunctor(bool resolve, size_t size_hint, const ResourceUsageTag &tag)
1517 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1518 barrier_ops_.reserve(size_hint);
1519 }
1520 void EmplaceBack(const BarrierOp &op) { barrier_ops_.emplace_back(op); }
John Zulauf89311b42020-09-29 16:28:47 -06001521
1522 private:
1523 bool resolve_;
John Zulaufd5115702021-01-18 12:34:33 -07001524 std::vector<BarrierOp> barrier_ops_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001525 const ResourceUsageTag &tag_;
1526};
1527
John Zulauf4a6105a2020-11-17 15:11:05 -07001528// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1529// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1530template <typename BarrierOp>
1531class ApplyBarrierFunctor {
1532 public:
1533 using Iterator = ResourceAccessRangeMap::iterator;
1534 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1535
1536 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1537 auto &access_state = pos->second;
1538 barrier_op_(&access_state);
1539 return pos;
1540 }
1541
1542 ApplyBarrierFunctor(const BarrierOp &barrier_op) : barrier_op_(barrier_op) {}
1543
1544 private:
John Zulaufd5115702021-01-18 12:34:33 -07001545 BarrierOp barrier_op_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001546};
1547
John Zulauf1e331ec2020-12-04 18:29:38 -07001548// This functor resolves the pendinging state.
1549class ResolvePendingBarrierFunctor {
1550 public:
1551 using Iterator = ResourceAccessRangeMap::iterator;
1552 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1553
1554 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1555 auto &access_state = pos->second;
1556 access_state.ApplyPendingBarriers(tag_);
1557 return pos;
1558 }
1559
1560 ResolvePendingBarrierFunctor(const ResourceUsageTag &tag) : tag_(tag) {}
1561
1562 private:
John Zulauf89311b42020-09-29 16:28:47 -06001563 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001564};
1565
John Zulauf8e3c3e92021-01-06 11:19:36 -07001566void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
1567 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
1568 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001569 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001570}
1571
John Zulauf8e3c3e92021-01-06 11:19:36 -07001572void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001573 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001574 if (!SimpleBinding(buffer)) return;
1575 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001576 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001577}
John Zulauf355e49b2020-04-24 15:11:15 -06001578
John Zulauf8e3c3e92021-01-06 11:19:36 -07001579void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf110413c2021-03-20 05:38:38 -06001580 const VkImageSubresourceRange &subresource_range, const ResourceUsageTag &tag) {
1581 if (!SimpleBinding(image)) return;
1582 const auto base_address = ResourceBaseAddress(image);
1583 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1584 const auto address_type = ImageAddressType(image);
1585 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1586 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
1587}
1588void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001589 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001590 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001591 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001592 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001593 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1594 base_address);
1595 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001596 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf110413c2021-03-20 05:38:38 -06001597 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001598}
John Zulaufd0ec59f2021-03-13 14:25:08 -07001599
1600void AccessContext::UpdateAccessState(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1601 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule, const ResourceUsageTag &tag) {
1602 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1603 if (!gen) return;
1604 subresource_adapter::ImageRangeGenerator range_gen(*gen);
1605 const auto address_type = view_gen.GetAddressType();
1606 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1607 ApplyUpdateAction(address_type, action, &range_gen);
John Zulauf7635de32020-05-29 17:14:15 -06001608}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001609
John Zulauf8e3c3e92021-01-06 11:19:36 -07001610void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001611 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1612 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001613 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1614 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001615 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001616}
1617
John Zulaufd0ec59f2021-03-13 14:25:08 -07001618template <typename Action, typename RangeGen>
1619void AccessContext::ApplyUpdateAction(AccessAddressType address_type, const Action &action, RangeGen *range_gen_arg) {
1620 assert(range_gen_arg); // Old Google C++ styleguide require non-const object pass by * not &, but this isn't an optional arg.
1621 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, range_gen_arg);
John Zulauf540266b2020-04-06 18:54:53 -06001622}
1623
1624template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001625void AccessContext::ApplyUpdateAction(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, const Action &action) {
1626 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1627 if (!gen) return;
1628 UpdateMemoryAccessState(&GetAccessStateMap(view_gen.GetAddressType()), action, *gen);
John Zulauf540266b2020-04-06 18:54:53 -06001629}
1630
John Zulaufd0ec59f2021-03-13 14:25:08 -07001631void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state,
1632 const AttachmentViewGenVector &attachment_views, uint32_t subpass,
John Zulauf7635de32020-05-29 17:14:15 -06001633 const ResourceUsageTag &tag) {
1634 UpdateStateResolveAction update(*this, tag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001635 ResolveOperation(update, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001636}
1637
John Zulaufd0ec59f2021-03-13 14:25:08 -07001638void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
1639 uint32_t subpass, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001640 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001641
1642 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1643 if (rp_state.attachment_last_subpass[i] == subpass) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001644 const auto &view_gen = attachment_views[i];
1645 if (!view_gen.IsValid()) continue; // UNUSED
John Zulaufaff20662020-06-01 14:07:58 -06001646
1647 const auto &ci = attachment_ci[i];
1648 const bool has_depth = FormatHasDepth(ci.format);
1649 const bool has_stencil = FormatHasStencil(ci.format);
1650 const bool is_color = !(has_depth || has_stencil);
1651 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1652
1653 if (is_color && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001654 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
1655 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001656 } else {
John Zulaufaff20662020-06-01 14:07:58 -06001657 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001658 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1659 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001660 }
1661 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1662 if (has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001663 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1664 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001665 }
1666 }
1667 }
1668 }
1669}
1670
John Zulauf540266b2020-04-06 18:54:53 -06001671template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001672void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001673 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001674 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001675 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001676 }
1677}
1678
1679void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001680 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1681 auto &context = contexts[subpass_index];
John Zulauf22aefed2021-03-11 18:14:35 -07001682 ApplyTrackbackStackAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001683 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001684 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001685 }
1686 }
1687}
1688
John Zulauf355e49b2020-04-24 15:11:15 -06001689// Suitable only for *subpass* access contexts
John Zulaufd0ec59f2021-03-13 14:25:08 -07001690HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const AttachmentViewGen &attach_view) const {
1691 if (!attach_view.IsValid()) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001692
John Zulauf355e49b2020-04-24 15:11:15 -06001693 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001694 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001695
1696 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001697 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1698 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001699 HazardResult hazard = track_back.context->DetectImageBarrierHazard(attach_view, merged_barrier, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001700 if (!hazard.hazard) {
1701 // The Async hazard check is against the current context's async set.
John Zulaufd0ec59f2021-03-13 14:25:08 -07001702 hazard = DetectImageBarrierHazard(attach_view, merged_barrier, kDetectAsync);
John Zulauf355e49b2020-04-24 15:11:15 -06001703 }
John Zulaufa0a98292020-09-18 09:30:10 -06001704
John Zulauf355e49b2020-04-24 15:11:15 -06001705 return hazard;
1706}
1707
John Zulaufb02c1eb2020-10-06 16:33:36 -06001708void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001709 const AttachmentViewGenVector &attachment_views, const ResourceUsageTag &tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001710 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001711 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001712 for (const auto &transition : transitions) {
1713 const auto prev_pass = transition.prev_pass;
John Zulaufd0ec59f2021-03-13 14:25:08 -07001714 const auto &view_gen = attachment_views[transition.attachment];
1715 if (!view_gen.IsValid()) continue;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001716
1717 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1718 assert(trackback);
1719
1720 // Import the attachments into the current context
1721 const auto *prev_context = trackback->context;
1722 assert(prev_context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001723 const auto address_type = view_gen.GetAddressType();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001724 auto &target_map = GetAccessStateMap(address_type);
1725 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001726 prev_context->ResolveAccessRange(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action, &target_map,
1727 &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001728 }
1729
John Zulauf86356ca2020-10-19 11:46:41 -06001730 // If there were no transitions skip this global map walk
1731 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001732 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001733 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001734 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001735}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001736
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001737void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
1738 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
John Zulauf669dfd52021-01-27 17:15:28 -07001739
1740 auto *events_context = GetCurrentEventsContext();
1741 assert(events_context);
1742 for (auto &event_pair : *events_context) {
John Zulauf4a6105a2020-11-17 15:11:05 -07001743 assert(event_pair.second); // Shouldn't be storing empty
1744 auto &sync_event = *event_pair.second;
1745 // 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 -07001746 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
1747 sync_event.barriers |= dst.exec_scope;
1748 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
John Zulauf4a6105a2020-11-17 15:11:05 -07001749 }
1750 }
1751}
1752
John Zulauf355e49b2020-04-24 15:11:15 -06001753
locke-lunarg61870c22020-06-09 14:51:50 -06001754bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1755 const char *func_name) const {
1756 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001757 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001758 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001759 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1760 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001761 return skip;
1762 }
1763
1764 using DescriptorClass = cvdescriptorset::DescriptorClass;
1765 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1766 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1767 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1768 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1769
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001770 for (const auto &stage_state : pipe->stage_state) {
1771 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1772 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001773 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001774 }
locke-lunarg61870c22020-06-09 14:51:50 -06001775 for (const auto &set_binding : stage_state.descriptor_uses) {
1776 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1777 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1778 set_binding.first.second);
1779 const auto descriptor_type = binding_it.GetType();
1780 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1781 auto array_idx = 0;
1782
1783 if (binding_it.IsVariableDescriptorCount()) {
1784 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1785 }
1786 SyncStageAccessIndex sync_index =
1787 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1788
1789 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1790 uint32_t index = i - index_range.start;
1791 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1792 switch (descriptor->GetClass()) {
1793 case DescriptorClass::ImageSampler:
1794 case DescriptorClass::Image: {
1795 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001796 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001797 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001798 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1799 img_view_state = image_sampler_descriptor->GetImageViewState();
1800 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001801 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001802 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1803 img_view_state = image_descriptor->GetImageViewState();
1804 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001805 }
1806 if (!img_view_state) continue;
John Zulauf361fb532020-07-22 10:45:39 -06001807 HazardResult hazard;
John Zulauf110413c2021-03-20 05:38:38 -06001808 const IMAGE_STATE *img_state = img_view_state->image_state.get();
John Zulauf361fb532020-07-22 10:45:39 -06001809 const auto &subresource_range = img_view_state->normalized_subresource_range;
John Zulauf110413c2021-03-20 05:38:38 -06001810
1811 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1812 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1813 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
John Zulauf361fb532020-07-22 10:45:39 -06001814 // Input attachments are subject to raster ordering rules
1815 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001816 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001817 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001818 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range);
John Zulauf361fb532020-07-22 10:45:39 -06001819 }
John Zulauf110413c2021-03-20 05:38:38 -06001820
John Zulauf33fc1d52020-07-17 11:01:10 -06001821 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001822 skip |= sync_state_->LogError(
1823 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001824 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1825 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001826 func_name, string_SyncHazard(hazard.hazard),
1827 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1828 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001829 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001830 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1831 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
John Zulauffaea0ee2021-01-14 14:01:32 -07001832 set_binding.first.second, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001833 }
1834 break;
1835 }
1836 case DescriptorClass::TexelBuffer: {
1837 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1838 if (!buf_view_state) continue;
1839 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001840 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001841 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001842 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001843 skip |= sync_state_->LogError(
1844 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001845 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1846 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001847 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1848 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001849 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001850 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1851 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001852 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001853 }
1854 break;
1855 }
1856 case DescriptorClass::GeneralBuffer: {
1857 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1858 auto buf_state = buffer_descriptor->GetBufferState();
1859 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001860 const ResourceAccessRange range =
1861 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001862 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001863 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001864 skip |= sync_state_->LogError(
1865 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001866 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1867 func_name, string_SyncHazard(hazard.hazard),
1868 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001869 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001870 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001871 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1872 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001873 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001874 }
1875 break;
1876 }
1877 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1878 default:
1879 break;
1880 }
1881 }
1882 }
1883 }
1884 return skip;
1885}
1886
1887void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1888 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001889 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001890 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001891 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1892 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001893 return;
1894 }
1895
1896 using DescriptorClass = cvdescriptorset::DescriptorClass;
1897 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1898 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1899 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1900 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1901
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001902 for (const auto &stage_state : pipe->stage_state) {
1903 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1904 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001905 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001906 }
locke-lunarg61870c22020-06-09 14:51:50 -06001907 for (const auto &set_binding : stage_state.descriptor_uses) {
1908 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1909 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1910 set_binding.first.second);
1911 const auto descriptor_type = binding_it.GetType();
1912 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1913 auto array_idx = 0;
1914
1915 if (binding_it.IsVariableDescriptorCount()) {
1916 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1917 }
1918 SyncStageAccessIndex sync_index =
1919 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1920
1921 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1922 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1923 switch (descriptor->GetClass()) {
1924 case DescriptorClass::ImageSampler:
1925 case DescriptorClass::Image: {
1926 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1927 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1928 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1929 } else {
1930 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1931 }
1932 if (!img_view_state) continue;
1933 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001934 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
John Zulauf110413c2021-03-20 05:38:38 -06001935 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1936 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1937 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kRaster,
1938 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001939 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001940 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kNonAttachment,
1941 img_view_state->normalized_subresource_range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001942 }
locke-lunarg61870c22020-06-09 14:51:50 -06001943 break;
1944 }
1945 case DescriptorClass::TexelBuffer: {
1946 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1947 if (!buf_view_state) continue;
1948 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001949 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001950 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001951 break;
1952 }
1953 case DescriptorClass::GeneralBuffer: {
1954 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1955 auto buf_state = buffer_descriptor->GetBufferState();
1956 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001957 const ResourceAccessRange range =
1958 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07001959 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001960 break;
1961 }
1962 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1963 default:
1964 break;
1965 }
1966 }
1967 }
1968 }
1969}
1970
1971bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1972 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001973 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1974 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06001975 return skip;
1976 }
1977
1978 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1979 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001980 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06001981
1982 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001983 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06001984 if (binding_description.binding < binding_buffers_size) {
1985 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07001986 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001987
locke-lunarg1ae57d62020-11-18 10:49:19 -07001988 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001989 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1990 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07001991 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06001992 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001993 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06001994 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 -06001995 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07001996 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001997 }
1998 }
1999 }
2000 return skip;
2001}
2002
2003void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002004 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2005 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002006 return;
2007 }
2008 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2009 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002010 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002011
2012 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002013 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002014 if (binding_description.binding < binding_buffers_size) {
2015 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002016 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002017
locke-lunarg1ae57d62020-11-18 10:49:19 -07002018 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002019 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2020 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002021 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2022 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002023 }
2024 }
2025}
2026
2027bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2028 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002029 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002030 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002031 }
locke-lunarg61870c22020-06-09 14:51:50 -06002032
locke-lunarg1ae57d62020-11-18 10:49:19 -07002033 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002034 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002035 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2036 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002037 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002038 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002039 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002040 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 -06002041 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07002042 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002043 }
2044
2045 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2046 // We will detect more accurate range in the future.
2047 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2048 return skip;
2049}
2050
2051void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002052 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 -06002053
locke-lunarg1ae57d62020-11-18 10:49:19 -07002054 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002055 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002056 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2057 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002058 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002059
2060 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2061 // We will detect more accurate range in the future.
2062 RecordDrawVertex(UINT32_MAX, 0, tag);
2063}
2064
2065bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002066 bool skip = false;
2067 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002068 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002069 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002070}
2071
2072void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002073 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002074 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002075 }
locke-lunarg61870c22020-06-09 14:51:50 -06002076}
2077
John Zulauf64ffe552021-02-06 10:25:07 -07002078void CommandBufferAccessContext::RecordBeginRenderPass(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2079 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2080 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002081 // Create an access context the current renderpass.
John Zulauf64ffe552021-02-06 10:25:07 -07002082 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002083 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf64ffe552021-02-06 10:25:07 -07002084 current_renderpass_context_->RecordBeginRenderPass(tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002085 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002086}
2087
John Zulauf64ffe552021-02-06 10:25:07 -07002088void CommandBufferAccessContext::RecordNextSubpass(CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002089 assert(current_renderpass_context_);
John Zulauffaea0ee2021-01-14 14:01:32 -07002090 auto prev_tag = NextCommandTag(command);
2091 auto next_tag = NextSubcommandTag(command);
John Zulauf64ffe552021-02-06 10:25:07 -07002092 current_renderpass_context_->RecordNextSubpass(prev_tag, next_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002093 current_context_ = &current_renderpass_context_->CurrentContext();
2094}
2095
John Zulauf64ffe552021-02-06 10:25:07 -07002096void CommandBufferAccessContext::RecordEndRenderPass(CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002097 assert(current_renderpass_context_);
2098 if (!current_renderpass_context_) return;
2099
John Zulauf64ffe552021-02-06 10:25:07 -07002100 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, NextCommandTag(command));
John Zulauf355e49b2020-04-24 15:11:15 -06002101 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002102 current_renderpass_context_ = nullptr;
2103}
2104
John Zulauf4a6105a2020-11-17 15:11:05 -07002105void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2106 // Erase is okay with the key not being
John Zulauf669dfd52021-01-27 17:15:28 -07002107 const auto *event_state = sync_state_->Get<EVENT_STATE>(event);
2108 if (event_state) {
2109 GetCurrentEventsContext()->Destroy(event_state);
John Zulaufd5115702021-01-18 12:34:33 -07002110 }
2111}
2112
John Zulauf64ffe552021-02-06 10:25:07 -07002113bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
John Zulauffaea0ee2021-01-14 14:01:32 -07002114 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002115 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002116 const auto &sync_state = ex_context.GetSyncState();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002117 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2118 if (!pipe ||
2119 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002120 return skip;
2121 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002122 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002123 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg37047832020-06-12 13:44:45 -06002124
John Zulauf1a224292020-06-30 14:52:13 -06002125 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002126 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002127 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2128 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002129 if (location >= subpass.colorAttachmentCount ||
2130 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002131 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002132 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002133 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2134 if (!view_gen.IsValid()) continue;
2135 HazardResult hazard =
2136 current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
2137 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment);
locke-lunarg96dc9632020-06-10 17:22:18 -06002138 if (hazard.hazard) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002139 const VkImageView view_handle = view_gen.GetViewState()->image_view;
2140 skip |= sync_state.LogError(view_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002141 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002142 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002143 sync_state.report_data->FormatHandle(view_handle).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06002144 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002145 location, ex_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002146 }
2147 }
2148 }
locke-lunarg37047832020-06-12 13:44:45 -06002149
2150 // PHASE1 TODO: Add layout based read/vs. write selection.
2151 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002152 const uint32_t depth_stencil_attachment =
2153 GetSubpassDepthStencilAttachmentIndex(pipe->graphicsPipelineCI.pDepthStencilState, subpass.pDepthStencilAttachment);
2154
2155 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2156 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2157 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002158 bool depth_write = false, stencil_write = false;
2159
2160 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002161 if (!FormatIsStencilOnly(view_state.create_info.format) && pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002162 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002163 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2164 depth_write = true;
2165 }
2166 // PHASE1 TODO: It needs to check if stencil is writable.
2167 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2168 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2169 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002170 if (!FormatIsDepthOnly(view_state.create_info.format) && pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002171 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2172 stencil_write = true;
2173 }
2174
2175 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2176 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002177 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2178 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2179 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002180 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002181 skip |= sync_state.LogError(
John Zulaufd0ec59f2021-03-13 14:25:08 -07002182 view_state.image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002183 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002184 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002185 sync_state.report_data->FormatHandle(view_state.image_view).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06002186 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002187 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002188 }
2189 }
2190 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002191 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2192 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2193 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002194 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002195 skip |= sync_state.LogError(
John Zulaufd0ec59f2021-03-13 14:25:08 -07002196 view_state.image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002197 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002198 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002199 sync_state.report_data->FormatHandle(view_state.image_view).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06002200 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002201 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002202 }
locke-lunarg61870c22020-06-09 14:51:50 -06002203 }
2204 }
2205 return skip;
2206}
2207
John Zulauf64ffe552021-02-06 10:25:07 -07002208void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002209 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2210 if (!pipe ||
2211 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002212 return;
2213 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002214 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002215 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002216
John Zulauf1a224292020-06-30 14:52:13 -06002217 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002218 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002219 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2220 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002221 if (location >= subpass.colorAttachmentCount ||
2222 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002223 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002224 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002225 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2226 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2227 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2228 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002229 }
2230 }
locke-lunarg37047832020-06-12 13:44:45 -06002231
2232 // PHASE1 TODO: Add layout based read/vs. write selection.
2233 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002234 const uint32_t depth_stencil_attachment =
2235 GetSubpassDepthStencilAttachmentIndex(pipe->graphicsPipelineCI.pDepthStencilState, subpass.pDepthStencilAttachment);
2236 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2237 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2238 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002239 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002240 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2241 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002242
2243 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002244 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002245 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2246 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002247 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2248 depth_write = true;
2249 }
2250 // PHASE1 TODO: It needs to check if stencil is writable.
2251 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2252 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2253 // PHASE1 TODO: These validation should be in core_checks.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002254 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002255 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002256 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2257 stencil_write = true;
2258 }
2259
John Zulaufd0ec59f2021-03-13 14:25:08 -07002260 if (depth_write || stencil_write) {
2261 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2262 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2263 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2264 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002265 }
locke-lunarg61870c22020-06-09 14:51:50 -06002266 }
2267}
2268
John Zulauf64ffe552021-02-06 10:25:07 -07002269bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002270 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002271 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002272 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002273 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002274 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002275 func_name);
2276
John Zulauf355e49b2020-04-24 15:11:15 -06002277 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002278 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002279 skip |=
2280 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002281 if (!skip) {
2282 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2283 // on a copy of the (empty) next context.
2284 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2285 AccessContext temp_context(next_context);
2286 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002287 skip |=
2288 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002289 }
John Zulauf7635de32020-05-29 17:14:15 -06002290 return skip;
2291}
John Zulauf64ffe552021-02-06 10:25:07 -07002292bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002293 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002294 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002295 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002296 current_subpass_);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002297 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_,
2298
2299 attachment_views_, func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002300 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002301 return skip;
2302}
2303
John Zulauf64ffe552021-02-06 10:25:07 -07002304AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002305 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002306}
2307
John Zulauf64ffe552021-02-06 10:25:07 -07002308bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2309 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002310 bool skip = false;
2311
John Zulauf7635de32020-05-29 17:14:15 -06002312 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2313 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2314 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2315 // to apply and only copy then, if this proves a hot spot.
2316 std::unique_ptr<AccessContext> proxy_for_current;
2317
John Zulauf355e49b2020-04-24 15:11:15 -06002318 // Validate the "finalLayout" transitions to external
2319 // Get them from where there we're hidding in the extra entry.
2320 const auto &final_transitions = rp_state_->subpass_transitions.back();
2321 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002322 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002323 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2324 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002325 auto *context = trackback.context;
2326
2327 if (transition.prev_pass == current_subpass_) {
2328 if (!proxy_for_current) {
2329 // 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 -07002330 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002331 }
2332 context = proxy_for_current.get();
2333 }
2334
John Zulaufa0a98292020-09-18 09:30:10 -06002335 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2336 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002337 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002338 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002339 skip |= ex_context.GetSyncState().LogError(
John Zulauffaea0ee2021-01-14 14:01:32 -07002340 rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2341 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2342 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2343 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2344 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002345 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002346 }
2347 }
2348 return skip;
2349}
2350
2351void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2352 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002353 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002354}
2355
John Zulauf64ffe552021-02-06 10:25:07 -07002356void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag &tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002357 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2358 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002359
2360 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2361 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002362 const AttachmentViewGen &view_gen = attachment_views_[i];
2363 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002364
2365 const auto &ci = attachment_ci[i];
2366 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002367 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002368 const bool is_color = !(has_depth || has_stencil);
2369
2370 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002371 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, ColorLoadUsage(ci.loadOp),
2372 SyncOrdering::kColorAttachment, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002373 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002374 if (has_depth) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002375 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2376 DepthStencilLoadUsage(ci.loadOp), SyncOrdering::kDepthStencilAttachment, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002377 }
2378 if (has_stencil) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002379 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2380 DepthStencilLoadUsage(ci.stencilLoadOp),
2381 SyncOrdering::kDepthStencilAttachment, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002382 }
2383 }
2384 }
2385 }
2386}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002387AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2388 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2389 AttachmentViewGenVector view_gens;
2390 VkExtent3D extent = CastTo3D(render_area.extent);
2391 VkOffset3D offset = CastTo3D(render_area.offset);
2392 view_gens.reserve(attachment_views.size());
2393 for (const auto *view : attachment_views) {
2394 view_gens.emplace_back(view, offset, extent);
2395 }
2396 return view_gens;
2397}
John Zulauf64ffe552021-02-06 10:25:07 -07002398RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2399 VkQueueFlags queue_flags,
2400 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2401 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002402 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002403 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002404 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002405 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002406 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002407 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002408 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002409}
2410void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
2411 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002412 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002413 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002414 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002415}
John Zulauf1507ee42020-05-18 11:33:09 -06002416
John Zulauf64ffe552021-02-06 10:25:07 -07002417void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag &prev_subpass_tag,
John Zulauffaea0ee2021-01-14 14:01:32 -07002418 const ResourceUsageTag &next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002419 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulaufd0ec59f2021-03-13 14:25:08 -07002420 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
2421 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002422
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002423 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2424 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002425 current_subpass_++;
2426 assert(current_subpass_ < subpass_contexts_.size());
John Zulauffaea0ee2021-01-14 14:01:32 -07002427 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2428 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002429 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002430}
2431
John Zulauf64ffe552021-02-06 10:25:07 -07002432void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002433 // Add the resolve and store accesses
John Zulaufd0ec59f2021-03-13 14:25:08 -07002434 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, tag);
2435 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002436
John Zulauf355e49b2020-04-24 15:11:15 -06002437 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002438 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002439
2440 // Add the "finalLayout" transitions to external
2441 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002442 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2443 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2444 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002445 const auto &final_transitions = rp_state_->subpass_transitions.back();
2446 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002447 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002448 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002449 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002450 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002451 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002452 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002453 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002454 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002455 }
2456}
2457
Jeremy Gebben40a22942020-12-22 14:22:06 -07002458SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002459 SyncExecScope result;
2460 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002461 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2462 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002463 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2464 return result;
2465}
2466
Jeremy Gebben40a22942020-12-22 14:22:06 -07002467SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002468 SyncExecScope result;
2469 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002470 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2471 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002472 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2473 return result;
2474}
2475
2476SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002477 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002478 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002479 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002480 dst_access_scope = 0;
2481}
2482
2483template <typename Barrier>
2484SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002485 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002486 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002487 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002488 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2489}
2490
2491SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002492 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2493 if (barrier) {
2494 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002495 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002496 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002497
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002498 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002499 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002500 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2501
2502 } else {
2503 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002504 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002505 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2506
2507 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002508 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002509 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2510 }
2511}
2512
2513template <typename Barrier>
2514SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2515 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2516 src_exec_scope = src.exec_scope;
2517 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2518
2519 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002520 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002521 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002522}
2523
John Zulaufb02c1eb2020-10-06 16:33:36 -06002524// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2525void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2526 for (const auto &barrier : barriers) {
2527 ApplyBarrier(barrier, layout_transition);
2528 }
2529}
2530
John Zulauf89311b42020-09-29 16:28:47 -06002531// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2532// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2533// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002534void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2535 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002536 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002537 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002538 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002539 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002540 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002541 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002542}
John Zulauf9cb530d2019-09-30 14:14:10 -06002543HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2544 HazardResult hazard;
2545 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002546 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002547 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002548 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002549 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002550 }
2551 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002552 // Write operation:
2553 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2554 // If reads exists -- test only against them because either:
2555 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2556 // * 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
2557 // the current write happens after the reads, so just test the write against the reades
2558 // Otherwise test against last_write
2559 //
2560 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002561 if (last_reads.size()) {
2562 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002563 if (IsReadHazard(usage_stage, read_access)) {
2564 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2565 break;
2566 }
2567 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002568 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002569 // Write-After-Write check -- if we have a previous write to test against
2570 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002571 }
2572 }
2573 return hazard;
2574}
2575
John Zulauf8e3c3e92021-01-06 11:19:36 -07002576HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering &ordering_rule) const {
2577 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06002578 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2579 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002580 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002581 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002582 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2583 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002584 if (IsRead(usage_bit)) {
2585 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2586 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2587 if (is_raw_hazard) {
2588 // NOTE: we know last_write is non-zero
2589 // See if the ordering rules save us from the simple RAW check above
2590 // First check to see if the current usage is covered by the ordering rules
2591 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2592 const bool usage_is_ordered =
2593 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2594 if (usage_is_ordered) {
2595 // Now see of the most recent write (or a subsequent read) are ordered
2596 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2597 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002598 }
2599 }
John Zulauf4285ee92020-09-23 10:20:52 -06002600 if (is_raw_hazard) {
2601 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2602 }
John Zulauf361fb532020-07-22 10:45:39 -06002603 } else {
2604 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002605 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002606 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002607 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002608 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002609 if (usage_write_is_ordered) {
2610 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2611 ordered_stages = GetOrderedStages(ordering);
2612 }
2613 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2614 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002615 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002616 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2617 if (IsReadHazard(usage_stage, read_access)) {
2618 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2619 break;
2620 }
John Zulaufd14743a2020-07-03 09:42:39 -06002621 }
2622 }
John Zulauf4285ee92020-09-23 10:20:52 -06002623 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002624 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002625 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002626 }
John Zulauf69133422020-05-20 14:55:53 -06002627 }
2628 }
2629 return hazard;
2630}
2631
John Zulauf2f952d22020-02-10 11:34:51 -07002632// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002633HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002634 HazardResult hazard;
2635 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002636 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2637 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2638 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002639 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002640 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002641 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002642 }
2643 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002644 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002645 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002646 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002647 // 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 -07002648 for (const auto &read_access : last_reads) {
2649 if (read_access.tag.index >= start_tag.index) {
2650 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002651 break;
2652 }
2653 }
John Zulauf2f952d22020-02-10 11:34:51 -07002654 }
2655 }
2656 return hazard;
2657}
2658
Jeremy Gebben40a22942020-12-22 14:22:06 -07002659HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002660 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002661 // Only supporting image layout transitions for now
2662 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2663 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002664 // only test for WAW if there no intervening read operations.
2665 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002666 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002667 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002668 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002669 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002670 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002671 break;
2672 }
2673 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002674 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2675 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2676 }
2677
2678 return hazard;
2679}
2680
Jeremy Gebben40a22942020-12-22 14:22:06 -07002681HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07002682 const SyncStageAccessFlags &src_access_scope,
2683 const ResourceUsageTag &event_tag) const {
2684 // Only supporting image layout transitions for now
2685 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2686 HazardResult hazard;
2687 // only test for WAW if there no intervening read operations.
2688 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2689
John Zulaufab7756b2020-12-29 16:10:16 -07002690 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002691 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
2692 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07002693 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002694 if (read_access.tag.IsBefore(event_tag)) {
2695 // The read is in the events first synchronization scope, so we use a barrier hazard check
2696 // If the read stage is not in the src sync scope
2697 // *AND* not execution chained with an existing sync barrier (that's the or)
2698 // then the barrier access is unsafe (R/W after R)
2699 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
2700 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2701 break;
2702 }
2703 } else {
2704 // The read not in the event first sync scope and so is a hazard vs. the layout transition
2705 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2706 }
2707 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002708 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002709 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
2710 if (write_tag.IsBefore(event_tag)) {
2711 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
2712 // So do a normal barrier hazard check
2713 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2714 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2715 }
2716 } else {
2717 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06002718 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2719 }
John Zulaufd14743a2020-07-03 09:42:39 -06002720 }
John Zulauf361fb532020-07-22 10:45:39 -06002721
John Zulauf0cb5be22020-01-23 12:18:22 -07002722 return hazard;
2723}
2724
John Zulauf5f13a792020-03-10 07:31:21 -06002725// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2726// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2727// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2728void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2729 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002730 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2731 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002732 *this = other;
2733 } else if (!other.write_tag.IsBefore(write_tag)) {
2734 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2735 // dependency chaining logic or any stage expansion)
2736 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002737 pending_write_barriers |= other.pending_write_barriers;
2738 pending_layout_transition |= other.pending_layout_transition;
2739 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002740
John Zulaufd14743a2020-07-03 09:42:39 -06002741 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07002742 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06002743 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07002744 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002745 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002746 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002747 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002748 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2749 // but we should wait on profiling data for that.
2750 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002751 auto &my_read = last_reads[my_read_index];
2752 if (other_read.stage == my_read.stage) {
2753 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002754 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002755 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002756 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002757 my_read.pending_dep_chain = other_read.pending_dep_chain;
2758 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2759 // May require tracking more than one access per stage.
2760 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002761 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06002762 // Since I'm overwriting the fragement stage read, also update the input attachment info
2763 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002764 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002765 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002766 } else if (other_read.tag.IsBefore(my_read.tag)) {
2767 // The read tags match so merge the barriers
2768 my_read.barriers |= other_read.barriers;
2769 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002770 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002771
John Zulauf5f13a792020-03-10 07:31:21 -06002772 break;
2773 }
2774 }
2775 } else {
2776 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07002777 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06002778 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002779 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002780 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002781 }
John Zulauf5f13a792020-03-10 07:31:21 -06002782 }
2783 }
John Zulauf361fb532020-07-22 10:45:39 -06002784 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002785 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2786 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07002787
2788 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
2789 // of the copy and other into this using the update first logic.
2790 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
2791 // of the other first_accesses... )
2792 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
2793 FirstAccesses firsts(std::move(first_accesses_));
2794 first_accesses_.clear();
2795 first_read_stages_ = 0U;
2796 auto a = firsts.begin();
2797 auto a_end = firsts.end();
2798 for (auto &b : other.first_accesses_) {
2799 // TODO: Determine whether "IsBefore" or "IsGloballyBefore" is needed...
2800 while (a != a_end && a->tag.IsBefore(b.tag)) {
2801 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2802 ++a;
2803 }
2804 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
2805 }
2806 for (; a != a_end; ++a) {
2807 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2808 }
2809 }
John Zulauf5f13a792020-03-10 07:31:21 -06002810}
2811
John Zulauf8e3c3e92021-01-06 11:19:36 -07002812void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag &tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002813 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2814 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002815 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002816 // Mulitple outstanding reads may be of interest and do dependency chains independently
2817 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2818 const auto usage_stage = PipelineStageBit(usage_index);
2819 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002820 for (auto &read_access : last_reads) {
2821 if (read_access.stage == usage_stage) {
2822 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002823 break;
2824 }
2825 }
2826 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07002827 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002828 last_read_stages |= usage_stage;
2829 }
John Zulauf4285ee92020-09-23 10:20:52 -06002830
2831 // 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 -07002832 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002833 // TODO Revisit re: multiple reads for a given stage
2834 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002835 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002836 } else {
2837 // Assume write
2838 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002839 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002840 }
John Zulauffaea0ee2021-01-14 14:01:32 -07002841 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06002842}
John Zulauf5f13a792020-03-10 07:31:21 -06002843
John Zulauf89311b42020-09-29 16:28:47 -06002844// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2845// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2846// We can overwrite them as *this* write is now after them.
2847//
2848// 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 -07002849void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002850 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06002851 last_read_stages = 0;
2852 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002853 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002854
2855 write_barriers = 0;
2856 write_dependency_chain = 0;
2857 write_tag = tag;
2858 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002859}
2860
John Zulauf89311b42020-09-29 16:28:47 -06002861// Apply the memory barrier without updating the existing barriers. The execution barrier
2862// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2863// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2864// replace the current write barriers or add to them, so accumulate to pending as well.
2865void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2866 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2867 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002868 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
2869 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
2870 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
2871 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07002872 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06002873 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07002874 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002875 }
John Zulauf89311b42020-09-29 16:28:47 -06002876 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2877 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002878
John Zulauf89311b42020-09-29 16:28:47 -06002879 if (!pending_layout_transition) {
2880 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2881 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002882 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06002883 // 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 -07002884 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
2885 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002886 }
2887 }
John Zulaufa0a98292020-09-18 09:30:10 -06002888 }
John Zulaufa0a98292020-09-18 09:30:10 -06002889}
2890
John Zulauf4a6105a2020-11-17 15:11:05 -07002891// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
2892// changes the "chaining" state, but to keep barriers independent. See discussion above.
2893void ResourceAccessState::ApplyBarrier(const ResourceUsageTag &scope_tag, const SyncBarrier &barrier, bool layout_transition) {
2894 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
2895 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
2896 // in order to know if it's in the excecution scope
2897 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
2898 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
2899 // errors w.r.t. "most recent" accesses.
2900 if (layout_transition || ((write_tag.IsBefore(scope_tag)) && (barrier.src_access_scope & last_write).any())) {
2901 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07002902 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002903 }
2904 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2905 pending_layout_transition |= layout_transition;
2906
2907 if (!pending_layout_transition) {
2908 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2909 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002910 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002911 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
2912 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
2913 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
2914 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
2915 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
2916 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
2917 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulaufc523bf62021-02-16 08:20:34 -07002918 if (read_access.tag.IsBefore(scope_tag) &&
2919 (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers))) {
2920 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002921 }
2922 }
2923 }
2924}
John Zulauf89311b42020-09-29 16:28:47 -06002925void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
2926 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06002927 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2928 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07002929 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf89311b42020-09-29 16:28:47 -06002930 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002931 }
John Zulauf89311b42020-09-29 16:28:47 -06002932
2933 // Apply the accumulate execution barriers (and thus update chaining information)
2934 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07002935 for (auto &read_access : last_reads) {
2936 read_access.barriers |= read_access.pending_dep_chain;
2937 read_execution_barriers |= read_access.barriers;
2938 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06002939 }
2940
2941 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2942 write_dependency_chain |= pending_write_dep_chain;
2943 write_barriers |= pending_write_barriers;
2944 pending_write_dep_chain = 0;
2945 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002946}
2947
John Zulauf59e25072020-07-17 10:55:21 -06002948// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07002949VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
2950 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002951
John Zulaufab7756b2020-12-29 16:10:16 -07002952 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002953 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06002954 barriers = read_access.barriers;
2955 break;
John Zulauf59e25072020-07-17 10:55:21 -06002956 }
2957 }
John Zulauf4285ee92020-09-23 10:20:52 -06002958
John Zulauf59e25072020-07-17 10:55:21 -06002959 return barriers;
2960}
2961
Jeremy Gebben40a22942020-12-22 14:22:06 -07002962inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002963 assert(IsRead(usage));
2964 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
2965 // * the previous reads are not hazards, and thus last_write must be visible and available to
2966 // any reads that happen after.
2967 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2968 // 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 -07002969 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06002970}
2971
Jeremy Gebben40a22942020-12-22 14:22:06 -07002972VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002973 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07002974 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06002975 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002976 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06002977 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06002978 // 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 -07002979 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06002980 }
2981
2982 return ordered_stages;
2983}
2984
John Zulauffaea0ee2021-01-14 14:01:32 -07002985void ResourceAccessState::UpdateFirst(const ResourceUsageTag &tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
2986 // Only record until we record a write.
2987 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07002988 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07002989 if (0 == (usage_stage & first_read_stages_)) {
2990 // If this is a read we haven't seen or a write, record.
2991 first_read_stages_ |= usage_stage;
2992 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
2993 }
2994 }
2995}
2996
John Zulaufd1f85d42020-04-15 12:23:15 -06002997void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002998 auto *access_context = GetAccessContextNoInsert(command_buffer);
2999 if (access_context) {
3000 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003001 }
3002}
3003
John Zulaufd1f85d42020-04-15 12:23:15 -06003004void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3005 auto access_found = cb_access_state.find(command_buffer);
3006 if (access_found != cb_access_state.end()) {
3007 access_found->second->Reset();
3008 cb_access_state.erase(access_found);
3009 }
3010}
3011
John Zulauf9cb530d2019-09-30 14:14:10 -06003012bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3013 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3014 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003015 const auto *cb_context = GetAccessContext(commandBuffer);
3016 assert(cb_context);
3017 if (!cb_context) return skip;
3018 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003019
John Zulauf3d84f1b2020-03-09 13:33:25 -06003020 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003021 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003022 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003023
3024 for (uint32_t region = 0; region < regionCount; region++) {
3025 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003026 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003027 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003028 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003029 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003030 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003031 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003032 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003033 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003034 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003035 }
John Zulauf16adfc92020-04-08 10:28:33 -06003036 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003037 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003038 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003039 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003040 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003041 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003042 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003043 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003044 }
3045 }
3046 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003047 }
3048 return skip;
3049}
3050
3051void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3052 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003053 auto *cb_context = GetAccessContext(commandBuffer);
3054 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003055 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003056 auto *context = cb_context->GetCurrentAccessContext();
3057
John Zulauf9cb530d2019-09-30 14:14:10 -06003058 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003059 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003060
3061 for (uint32_t region = 0; region < regionCount; region++) {
3062 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003063 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003064 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003065 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003066 }
John Zulauf16adfc92020-04-08 10:28:33 -06003067 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003068 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003069 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003070 }
3071 }
3072}
3073
John Zulauf4a6105a2020-11-17 15:11:05 -07003074void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3075 // Clear out events from the command buffer contexts
3076 for (auto &cb_context : cb_access_state) {
3077 cb_context.second->RecordDestroyEvent(event);
3078 }
3079}
3080
Jeff Leger178b1e52020-10-05 12:22:23 -04003081bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3082 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3083 bool skip = false;
3084 const auto *cb_context = GetAccessContext(commandBuffer);
3085 assert(cb_context);
3086 if (!cb_context) return skip;
3087 const auto *context = cb_context->GetCurrentAccessContext();
3088
3089 // If we have no previous accesses, we have no hazards
3090 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3091 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3092
3093 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3094 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3095 if (src_buffer) {
3096 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003097 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003098 if (hazard.hazard) {
3099 // TODO -- add tag information to log msg when useful.
3100 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3101 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3102 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003103 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003104 }
3105 }
3106 if (dst_buffer && !skip) {
3107 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003108 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003109 if (hazard.hazard) {
3110 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3111 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3112 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003113 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003114 }
3115 }
3116 if (skip) break;
3117 }
3118 return skip;
3119}
3120
3121void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3122 auto *cb_context = GetAccessContext(commandBuffer);
3123 assert(cb_context);
3124 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3125 auto *context = cb_context->GetCurrentAccessContext();
3126
3127 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3128 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3129
3130 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3131 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3132 if (src_buffer) {
3133 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003134 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003135 }
3136 if (dst_buffer) {
3137 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003138 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003139 }
3140 }
3141}
3142
John Zulauf5c5e88d2019-12-26 11:22:02 -07003143bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3144 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3145 const VkImageCopy *pRegions) const {
3146 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003147 const auto *cb_access_context = GetAccessContext(commandBuffer);
3148 assert(cb_access_context);
3149 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003150
John Zulauf3d84f1b2020-03-09 13:33:25 -06003151 const auto *context = cb_access_context->GetCurrentAccessContext();
3152 assert(context);
3153 if (!context) return skip;
3154
3155 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3156 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003157 for (uint32_t region = 0; region < regionCount; region++) {
3158 const auto &copy_region = pRegions[region];
3159 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003160 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003161 copy_region.srcOffset, copy_region.extent);
3162 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003163 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003164 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003165 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003166 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003167 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003168 }
3169
3170 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003171 VkExtent3D dst_copy_extent =
3172 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003173 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003174 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003175 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003176 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003177 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003178 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003179 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003180 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003181 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003182 }
3183 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003184
John Zulauf5c5e88d2019-12-26 11:22:02 -07003185 return skip;
3186}
3187
3188void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3189 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3190 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003191 auto *cb_access_context = GetAccessContext(commandBuffer);
3192 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003193 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003194 auto *context = cb_access_context->GetCurrentAccessContext();
3195 assert(context);
3196
John Zulauf5c5e88d2019-12-26 11:22:02 -07003197 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003198 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003199
3200 for (uint32_t region = 0; region < regionCount; region++) {
3201 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003202 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003203 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003204 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003205 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003206 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003207 VkExtent3D dst_copy_extent =
3208 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003209 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003210 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003211 }
3212 }
3213}
3214
Jeff Leger178b1e52020-10-05 12:22:23 -04003215bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3216 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3217 bool skip = false;
3218 const auto *cb_access_context = GetAccessContext(commandBuffer);
3219 assert(cb_access_context);
3220 if (!cb_access_context) return skip;
3221
3222 const auto *context = cb_access_context->GetCurrentAccessContext();
3223 assert(context);
3224 if (!context) return skip;
3225
3226 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3227 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3228 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3229 const auto &copy_region = pCopyImageInfo->pRegions[region];
3230 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003231 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003232 copy_region.srcOffset, copy_region.extent);
3233 if (hazard.hazard) {
3234 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3235 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3236 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003237 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003238 }
3239 }
3240
3241 if (dst_image) {
3242 VkExtent3D dst_copy_extent =
3243 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003244 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003245 copy_region.dstOffset, dst_copy_extent);
3246 if (hazard.hazard) {
3247 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3248 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3249 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003250 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003251 }
3252 if (skip) break;
3253 }
3254 }
3255
3256 return skip;
3257}
3258
3259void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3260 auto *cb_access_context = GetAccessContext(commandBuffer);
3261 assert(cb_access_context);
3262 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3263 auto *context = cb_access_context->GetCurrentAccessContext();
3264 assert(context);
3265
3266 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3267 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3268
3269 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3270 const auto &copy_region = pCopyImageInfo->pRegions[region];
3271 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003272 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003273 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003274 }
3275 if (dst_image) {
3276 VkExtent3D dst_copy_extent =
3277 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003278 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003279 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003280 }
3281 }
3282}
3283
John Zulauf9cb530d2019-09-30 14:14:10 -06003284bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3285 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3286 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3287 uint32_t bufferMemoryBarrierCount,
3288 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3289 uint32_t imageMemoryBarrierCount,
3290 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3291 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003292 const auto *cb_access_context = GetAccessContext(commandBuffer);
3293 assert(cb_access_context);
3294 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003295
John Zulauf36ef9282021-02-02 11:47:24 -07003296 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3297 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3298 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3299 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003300 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003301 return skip;
3302}
3303
3304void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3305 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3306 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3307 uint32_t bufferMemoryBarrierCount,
3308 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3309 uint32_t imageMemoryBarrierCount,
3310 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003311 auto *cb_access_context = GetAccessContext(commandBuffer);
3312 assert(cb_access_context);
3313 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003314
John Zulauf36ef9282021-02-02 11:47:24 -07003315 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3316 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3317 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3318 pImageMemoryBarriers);
3319 pipeline_barrier.Record(cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003320}
3321
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003322bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3323 const VkDependencyInfoKHR *pDependencyInfo) const {
3324 bool skip = false;
3325 const auto *cb_access_context = GetAccessContext(commandBuffer);
3326 assert(cb_access_context);
3327 if (!cb_access_context) return skip;
3328
3329 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3330 skip = pipeline_barrier.Validate(*cb_access_context);
3331 return skip;
3332}
3333
3334void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3335 auto *cb_access_context = GetAccessContext(commandBuffer);
3336 assert(cb_access_context);
3337 if (!cb_access_context) return;
3338
3339 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3340 pipeline_barrier.Record(cb_access_context);
3341}
3342
John Zulauf9cb530d2019-09-30 14:14:10 -06003343void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3344 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3345 // The state tracker sets up the device state
3346 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3347
John Zulauf5f13a792020-03-10 07:31:21 -06003348 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3349 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003350 // TODO: Find a good way to do this hooklessly.
3351 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3352 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3353 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3354
John Zulaufd1f85d42020-04-15 12:23:15 -06003355 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3356 sync_device_state->ResetCommandBufferCallback(command_buffer);
3357 });
3358 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3359 sync_device_state->FreeCommandBufferCallback(command_buffer);
3360 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003361}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003362
John Zulauf355e49b2020-04-24 15:11:15 -06003363bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf64ffe552021-02-06 10:25:07 -07003364 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd, const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003365 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003366 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003367 if (cb_context) {
3368 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo, cmd_name);
3369 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003370 }
John Zulauf355e49b2020-04-24 15:11:15 -06003371 return skip;
3372}
3373
3374bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3375 VkSubpassContents contents) const {
3376 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003377 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003378 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003379 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003380 return skip;
3381}
3382
3383bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003384 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003385 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003386 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003387 return skip;
3388}
3389
John Zulauf64ffe552021-02-06 10:25:07 -07003390static const char *kBeginRenderPass2KhrName = "vkCmdBeginRenderPass2KHR";
John Zulauf355e49b2020-04-24 15:11:15 -06003391bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3392 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003393 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003394 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003395 skip |=
3396 ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2, kBeginRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003397 return skip;
3398}
3399
John Zulauf3d84f1b2020-03-09 13:33:25 -06003400void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3401 VkResult result) {
3402 // The state tracker sets up the command buffer state
3403 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3404
3405 // Create/initialize the structure that trackers accesses at the command buffer scope.
3406 auto cb_access_context = GetAccessContext(commandBuffer);
3407 assert(cb_access_context);
3408 cb_access_context->Reset();
3409}
3410
3411void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf64ffe552021-02-06 10:25:07 -07003412 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd, const char *cmd_name) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003413 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003414 if (cb_context) {
John Zulauf64ffe552021-02-06 10:25:07 -07003415 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo, cmd_name);
3416 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003417 }
3418}
3419
3420void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3421 VkSubpassContents contents) {
3422 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003423 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003424 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003425 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003426}
3427
3428void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3429 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3430 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003431 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003432}
3433
3434void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3435 const VkRenderPassBeginInfo *pRenderPassBegin,
3436 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3437 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003438 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2, kBeginRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003439}
3440
Mike Schuchardt2df08912020-12-15 16:28:09 -08003441bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf64ffe552021-02-06 10:25:07 -07003442 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd, const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003443 bool skip = false;
3444
3445 auto cb_context = GetAccessContext(commandBuffer);
3446 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003447 if (!cb_context) return skip;
3448 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo, cmd_name);
3449 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003450}
3451
3452bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3453 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003454 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003455 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003456 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003457 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3458 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003459 return skip;
3460}
3461
John Zulauf64ffe552021-02-06 10:25:07 -07003462static const char *kNextSubpass2KhrName = "vkCmdNextSubpass2KHR";
Mike Schuchardt2df08912020-12-15 16:28:09 -08003463bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3464 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003465 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003466 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2, kNextSubpass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003467 return skip;
3468}
3469
3470bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3471 const VkSubpassEndInfo *pSubpassEndInfo) const {
3472 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003473 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003474 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003475}
3476
3477void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf64ffe552021-02-06 10:25:07 -07003478 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd, const char *cmd_name) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003479 auto cb_context = GetAccessContext(commandBuffer);
3480 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003481 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003482
John Zulauf64ffe552021-02-06 10:25:07 -07003483 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo, cmd_name);
3484 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003485}
3486
3487void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3488 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003489 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003490 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003491 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003492}
3493
3494void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3495 const VkSubpassEndInfo *pSubpassEndInfo) {
3496 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003497 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003498}
3499
3500void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3501 const VkSubpassEndInfo *pSubpassEndInfo) {
3502 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003503 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2, kNextSubpass2KhrName);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003504}
3505
John Zulauf64ffe552021-02-06 10:25:07 -07003506bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd,
3507 const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003508 bool skip = false;
3509
3510 auto cb_context = GetAccessContext(commandBuffer);
3511 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003512 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003513
John Zulauf64ffe552021-02-06 10:25:07 -07003514 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo, cmd_name);
3515 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003516 return skip;
3517}
3518
3519bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3520 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003521 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003522 return skip;
3523}
3524
Mike Schuchardt2df08912020-12-15 16:28:09 -08003525bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003526 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003527 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003528 return skip;
3529}
3530
John Zulauf64ffe552021-02-06 10:25:07 -07003531const static char *kEndRenderPass2KhrName = "vkEndRenderPass2KHR";
John Zulauf355e49b2020-04-24 15:11:15 -06003532bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003533 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003534 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003535 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2, kEndRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003536 return skip;
3537}
3538
John Zulauf64ffe552021-02-06 10:25:07 -07003539void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd,
3540 const char *cmd_name) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003541 // Resolve the all subpass contexts to the command buffer contexts
3542 auto cb_context = GetAccessContext(commandBuffer);
3543 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003544 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003545
John Zulauf64ffe552021-02-06 10:25:07 -07003546 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo, cmd_name);
3547 sync_op.Record(cb_context);
3548 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003549}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003550
John Zulauf33fc1d52020-07-17 11:01:10 -06003551// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3552// updates to a resource which do not conflict at the byte level.
3553// TODO: Revisit this rule to see if it needs to be tighter or looser
3554// TODO: Add programatic control over suppression heuristics
3555bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3556 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3557}
3558
John Zulauf3d84f1b2020-03-09 13:33:25 -06003559void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003560 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003561 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003562}
3563
3564void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003565 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003566 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003567}
3568
3569void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf64ffe552021-02-06 10:25:07 -07003570 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2, kEndRenderPass2KhrName);
John Zulauf5a1a5382020-06-22 17:23:25 -06003571 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003572}
locke-lunarga19c71d2020-03-02 18:17:04 -07003573
Jeff Leger178b1e52020-10-05 12:22:23 -04003574template <typename BufferImageCopyRegionType>
3575bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3576 VkImageLayout dstImageLayout, uint32_t regionCount,
3577 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003578 bool skip = false;
3579 const auto *cb_access_context = GetAccessContext(commandBuffer);
3580 assert(cb_access_context);
3581 if (!cb_access_context) return skip;
3582
Jeff Leger178b1e52020-10-05 12:22:23 -04003583 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3584 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3585
locke-lunarga19c71d2020-03-02 18:17:04 -07003586 const auto *context = cb_access_context->GetCurrentAccessContext();
3587 assert(context);
3588 if (!context) return skip;
3589
3590 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003591 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3592
3593 for (uint32_t region = 0; region < regionCount; region++) {
3594 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003595 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003596 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003597 if (src_buffer) {
3598 ResourceAccessRange src_range =
3599 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003600 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07003601 if (hazard.hazard) {
3602 // PHASE1 TODO -- add tag information to log msg when useful.
3603 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3604 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3605 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003606 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003607 }
3608 }
3609
Jeremy Gebben40a22942020-12-22 14:22:06 -07003610 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07003611 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003612 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003613 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003614 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003615 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003616 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003617 }
3618 if (skip) break;
3619 }
3620 if (skip) break;
3621 }
3622 return skip;
3623}
3624
Jeff Leger178b1e52020-10-05 12:22:23 -04003625bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3626 VkImageLayout dstImageLayout, uint32_t regionCount,
3627 const VkBufferImageCopy *pRegions) const {
3628 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3629 COPY_COMMAND_VERSION_1);
3630}
3631
3632bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3633 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3634 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3635 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3636 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3637}
3638
3639template <typename BufferImageCopyRegionType>
3640void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3641 VkImageLayout dstImageLayout, uint32_t regionCount,
3642 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003643 auto *cb_access_context = GetAccessContext(commandBuffer);
3644 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003645
3646 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3647 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3648
3649 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003650 auto *context = cb_access_context->GetCurrentAccessContext();
3651 assert(context);
3652
3653 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003654 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003655
3656 for (uint32_t region = 0; region < regionCount; region++) {
3657 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07003658 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003659 if (src_buffer) {
3660 ResourceAccessRange src_range =
3661 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003662 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003663 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07003664 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003665 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003666 }
3667 }
3668}
3669
Jeff Leger178b1e52020-10-05 12:22:23 -04003670void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3671 VkImageLayout dstImageLayout, uint32_t regionCount,
3672 const VkBufferImageCopy *pRegions) {
3673 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3674 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3675}
3676
3677void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3678 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3679 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3680 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3681 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3682 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3683}
3684
3685template <typename BufferImageCopyRegionType>
3686bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3687 VkBuffer dstBuffer, uint32_t regionCount,
3688 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003689 bool skip = false;
3690 const auto *cb_access_context = GetAccessContext(commandBuffer);
3691 assert(cb_access_context);
3692 if (!cb_access_context) return skip;
3693
Jeff Leger178b1e52020-10-05 12:22:23 -04003694 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3695 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3696
locke-lunarga19c71d2020-03-02 18:17:04 -07003697 const auto *context = cb_access_context->GetCurrentAccessContext();
3698 assert(context);
3699 if (!context) return skip;
3700
3701 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3702 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3703 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3704 for (uint32_t region = 0; region < regionCount; region++) {
3705 const auto &copy_region = pRegions[region];
3706 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003707 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003708 copy_region.imageOffset, copy_region.imageExtent);
3709 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003710 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003711 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003712 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003713 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003714 }
John Zulauf477700e2021-01-06 11:41:49 -07003715 if (dst_mem) {
3716 ResourceAccessRange dst_range =
3717 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003718 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07003719 if (hazard.hazard) {
3720 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3721 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3722 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003723 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003724 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003725 }
3726 }
3727 if (skip) break;
3728 }
3729 return skip;
3730}
3731
Jeff Leger178b1e52020-10-05 12:22:23 -04003732bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3733 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3734 const VkBufferImageCopy *pRegions) const {
3735 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3736 COPY_COMMAND_VERSION_1);
3737}
3738
3739bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3740 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3741 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3742 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3743 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3744}
3745
3746template <typename BufferImageCopyRegionType>
3747void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3748 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3749 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003750 auto *cb_access_context = GetAccessContext(commandBuffer);
3751 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003752
3753 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3754 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3755
3756 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003757 auto *context = cb_access_context->GetCurrentAccessContext();
3758 assert(context);
3759
3760 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003761 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3762 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 -06003763 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003764
3765 for (uint32_t region = 0; region < regionCount; region++) {
3766 const auto &copy_region = pRegions[region];
3767 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003768 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003769 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003770 if (dst_buffer) {
3771 ResourceAccessRange dst_range =
3772 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003773 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003774 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003775 }
3776 }
3777}
3778
Jeff Leger178b1e52020-10-05 12:22:23 -04003779void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3780 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3781 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3782 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3783}
3784
3785void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3786 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3787 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3788 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3789 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3790 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3791}
3792
3793template <typename RegionType>
3794bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3795 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3796 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003797 bool skip = false;
3798 const auto *cb_access_context = GetAccessContext(commandBuffer);
3799 assert(cb_access_context);
3800 if (!cb_access_context) return skip;
3801
3802 const auto *context = cb_access_context->GetCurrentAccessContext();
3803 assert(context);
3804 if (!context) return skip;
3805
3806 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3807 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3808
3809 for (uint32_t region = 0; region < regionCount; region++) {
3810 const auto &blit_region = pRegions[region];
3811 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003812 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3813 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3814 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3815 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3816 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3817 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003818 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003819 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003820 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003821 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003822 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003823 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003824 }
3825 }
3826
3827 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003828 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3829 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3830 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3831 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3832 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3833 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003834 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003835 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003836 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003837 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003838 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003839 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003840 }
3841 if (skip) break;
3842 }
3843 }
3844
3845 return skip;
3846}
3847
Jeff Leger178b1e52020-10-05 12:22:23 -04003848bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3849 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3850 const VkImageBlit *pRegions, VkFilter filter) const {
3851 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3852 "vkCmdBlitImage");
3853}
3854
3855bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3856 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3857 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3858 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3859 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3860}
3861
3862template <typename RegionType>
3863void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3864 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3865 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003866 auto *cb_access_context = GetAccessContext(commandBuffer);
3867 assert(cb_access_context);
3868 auto *context = cb_access_context->GetCurrentAccessContext();
3869 assert(context);
3870
3871 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003872 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003873
3874 for (uint32_t region = 0; region < regionCount; region++) {
3875 const auto &blit_region = pRegions[region];
3876 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003877 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3878 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3879 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3880 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3881 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3882 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003883 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003884 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003885 }
3886 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003887 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3888 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3889 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3890 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3891 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3892 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003893 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003894 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003895 }
3896 }
3897}
locke-lunarg36ba2592020-04-03 09:42:04 -06003898
Jeff Leger178b1e52020-10-05 12:22:23 -04003899void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3900 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3901 const VkImageBlit *pRegions, VkFilter filter) {
3902 auto *cb_access_context = GetAccessContext(commandBuffer);
3903 assert(cb_access_context);
3904 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3905 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3906 pRegions, filter);
3907 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3908}
3909
3910void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3911 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3912 auto *cb_access_context = GetAccessContext(commandBuffer);
3913 assert(cb_access_context);
3914 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3915 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3916 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3917 pBlitImageInfo->filter, tag);
3918}
3919
John Zulauffaea0ee2021-01-14 14:01:32 -07003920bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
3921 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
3922 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
3923 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003924 bool skip = false;
3925 if (drawCount == 0) return skip;
3926
3927 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3928 VkDeviceSize size = struct_size;
3929 if (drawCount == 1 || stride == size) {
3930 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003931 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003932 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3933 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003934 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003935 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003936 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003937 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003938 }
3939 } else {
3940 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003941 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003942 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3943 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003944 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003945 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3946 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003947 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003948 break;
3949 }
3950 }
3951 }
3952 return skip;
3953}
3954
locke-lunarg61870c22020-06-09 14:51:50 -06003955void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3956 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3957 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003958 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3959 VkDeviceSize size = struct_size;
3960 if (drawCount == 1 || stride == size) {
3961 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003962 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003963 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003964 } else {
3965 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003966 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003967 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
3968 tag);
locke-lunargff255f92020-05-13 18:53:52 -06003969 }
3970 }
3971}
3972
John Zulauffaea0ee2021-01-14 14:01:32 -07003973bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
3974 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3975 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003976 bool skip = false;
3977
3978 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003979 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003980 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3981 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003982 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003983 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003984 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003985 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003986 }
3987 return skip;
3988}
3989
locke-lunarg61870c22020-06-09 14:51:50 -06003990void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003991 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003992 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003993 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003994}
3995
locke-lunarg36ba2592020-04-03 09:42:04 -06003996bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003997 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003998 const auto *cb_access_context = GetAccessContext(commandBuffer);
3999 assert(cb_access_context);
4000 if (!cb_access_context) return skip;
4001
locke-lunarg61870c22020-06-09 14:51:50 -06004002 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004003 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004004}
4005
4006void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004007 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004008 auto *cb_access_context = GetAccessContext(commandBuffer);
4009 assert(cb_access_context);
4010 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004011
locke-lunarg61870c22020-06-09 14:51:50 -06004012 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004013}
locke-lunarge1a67022020-04-29 00:15:36 -06004014
4015bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004016 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004017 const auto *cb_access_context = GetAccessContext(commandBuffer);
4018 assert(cb_access_context);
4019 if (!cb_access_context) return skip;
4020
4021 const auto *context = cb_access_context->GetCurrentAccessContext();
4022 assert(context);
4023 if (!context) return skip;
4024
locke-lunarg61870c22020-06-09 14:51:50 -06004025 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004026 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4027 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004028 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004029}
4030
4031void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004032 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004033 auto *cb_access_context = GetAccessContext(commandBuffer);
4034 assert(cb_access_context);
4035 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4036 auto *context = cb_access_context->GetCurrentAccessContext();
4037 assert(context);
4038
locke-lunarg61870c22020-06-09 14:51:50 -06004039 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4040 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004041}
4042
4043bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4044 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004045 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004046 const auto *cb_access_context = GetAccessContext(commandBuffer);
4047 assert(cb_access_context);
4048 if (!cb_access_context) return skip;
4049
locke-lunarg61870c22020-06-09 14:51:50 -06004050 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4051 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4052 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004053 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004054}
4055
4056void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4057 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004058 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004059 auto *cb_access_context = GetAccessContext(commandBuffer);
4060 assert(cb_access_context);
4061 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004062
locke-lunarg61870c22020-06-09 14:51:50 -06004063 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4064 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4065 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004066}
4067
4068bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4069 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004070 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004071 const auto *cb_access_context = GetAccessContext(commandBuffer);
4072 assert(cb_access_context);
4073 if (!cb_access_context) return skip;
4074
locke-lunarg61870c22020-06-09 14:51:50 -06004075 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4076 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4077 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004078 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004079}
4080
4081void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4082 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004083 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004084 auto *cb_access_context = GetAccessContext(commandBuffer);
4085 assert(cb_access_context);
4086 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004087
locke-lunarg61870c22020-06-09 14:51:50 -06004088 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4089 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4090 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004091}
4092
4093bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4094 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004095 bool skip = false;
4096 if (drawCount == 0) return skip;
4097
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
4102 const auto *context = cb_access_context->GetCurrentAccessContext();
4103 assert(context);
4104 if (!context) return skip;
4105
locke-lunarg61870c22020-06-09 14:51:50 -06004106 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4107 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004108 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4109 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004110
4111 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4112 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4113 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004114 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004115 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004116}
4117
4118void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4119 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004120 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004121 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004122 auto *cb_access_context = GetAccessContext(commandBuffer);
4123 assert(cb_access_context);
4124 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4125 auto *context = cb_access_context->GetCurrentAccessContext();
4126 assert(context);
4127
locke-lunarg61870c22020-06-09 14:51:50 -06004128 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4129 cb_access_context->RecordDrawSubpassAttachment(tag);
4130 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004131
4132 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4133 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4134 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004135 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004136}
4137
4138bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4139 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004140 bool skip = false;
4141 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004142 const auto *cb_access_context = GetAccessContext(commandBuffer);
4143 assert(cb_access_context);
4144 if (!cb_access_context) return skip;
4145
4146 const auto *context = cb_access_context->GetCurrentAccessContext();
4147 assert(context);
4148 if (!context) return skip;
4149
locke-lunarg61870c22020-06-09 14:51:50 -06004150 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4151 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004152 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4153 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004154
4155 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4156 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4157 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004158 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004159 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004160}
4161
4162void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4163 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004164 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004165 auto *cb_access_context = GetAccessContext(commandBuffer);
4166 assert(cb_access_context);
4167 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4168 auto *context = cb_access_context->GetCurrentAccessContext();
4169 assert(context);
4170
locke-lunarg61870c22020-06-09 14:51:50 -06004171 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4172 cb_access_context->RecordDrawSubpassAttachment(tag);
4173 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004174
4175 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4176 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4177 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004178 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004179}
4180
4181bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4182 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4183 uint32_t stride, const char *function) const {
4184 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004185 const auto *cb_access_context = GetAccessContext(commandBuffer);
4186 assert(cb_access_context);
4187 if (!cb_access_context) return skip;
4188
4189 const auto *context = cb_access_context->GetCurrentAccessContext();
4190 assert(context);
4191 if (!context) return skip;
4192
locke-lunarg61870c22020-06-09 14:51:50 -06004193 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4194 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004195 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4196 maxDrawCount, stride, function);
4197 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004198
4199 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4200 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4201 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004202 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004203 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004204}
4205
4206bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4207 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4208 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004209 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4210 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004211}
4212
4213void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4214 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4215 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004216 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4217 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004218 auto *cb_access_context = GetAccessContext(commandBuffer);
4219 assert(cb_access_context);
4220 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
4221 auto *context = cb_access_context->GetCurrentAccessContext();
4222 assert(context);
4223
locke-lunarg61870c22020-06-09 14:51:50 -06004224 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4225 cb_access_context->RecordDrawSubpassAttachment(tag);
4226 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4227 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004228
4229 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4230 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4231 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004232 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004233}
4234
4235bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4236 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4237 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004238 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4239 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004240}
4241
4242void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4243 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4244 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004245 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4246 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004247 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004248}
4249
4250bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4251 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4252 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004253 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4254 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004255}
4256
4257void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4258 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4259 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004260 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4261 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004262 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4263}
4264
4265bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4266 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4267 uint32_t stride, const char *function) const {
4268 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004269 const auto *cb_access_context = GetAccessContext(commandBuffer);
4270 assert(cb_access_context);
4271 if (!cb_access_context) return skip;
4272
4273 const auto *context = cb_access_context->GetCurrentAccessContext();
4274 assert(context);
4275 if (!context) return skip;
4276
locke-lunarg61870c22020-06-09 14:51:50 -06004277 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4278 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004279 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4280 offset, maxDrawCount, stride, function);
4281 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004282
4283 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4284 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4285 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004286 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004287 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004288}
4289
4290bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4291 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4292 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004293 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4294 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004295}
4296
4297void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4298 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4299 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004300 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4301 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004302 auto *cb_access_context = GetAccessContext(commandBuffer);
4303 assert(cb_access_context);
4304 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4305 auto *context = cb_access_context->GetCurrentAccessContext();
4306 assert(context);
4307
locke-lunarg61870c22020-06-09 14:51:50 -06004308 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4309 cb_access_context->RecordDrawSubpassAttachment(tag);
4310 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4311 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004312
4313 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4314 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004315 // We will update the index and vertex buffer in SubmitQueue in the future.
4316 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004317}
4318
4319bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4320 VkDeviceSize offset, VkBuffer countBuffer,
4321 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4322 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004323 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4324 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004325}
4326
4327void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4328 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4329 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004330 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4331 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004332 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4333}
4334
4335bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4336 VkDeviceSize offset, VkBuffer countBuffer,
4337 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4338 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004339 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4340 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004341}
4342
4343void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4344 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4345 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004346 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4347 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004348 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4349}
4350
4351bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4352 const VkClearColorValue *pColor, uint32_t rangeCount,
4353 const VkImageSubresourceRange *pRanges) const {
4354 bool skip = false;
4355 const auto *cb_access_context = GetAccessContext(commandBuffer);
4356 assert(cb_access_context);
4357 if (!cb_access_context) return skip;
4358
4359 const auto *context = cb_access_context->GetCurrentAccessContext();
4360 assert(context);
4361 if (!context) return skip;
4362
4363 const auto *image_state = Get<IMAGE_STATE>(image);
4364
4365 for (uint32_t index = 0; index < rangeCount; index++) {
4366 const auto &range = pRanges[index];
4367 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004368 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004369 if (hazard.hazard) {
4370 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004371 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004372 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004373 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004374 }
4375 }
4376 }
4377 return skip;
4378}
4379
4380void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4381 const VkClearColorValue *pColor, uint32_t rangeCount,
4382 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004383 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004384 auto *cb_access_context = GetAccessContext(commandBuffer);
4385 assert(cb_access_context);
4386 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4387 auto *context = cb_access_context->GetCurrentAccessContext();
4388 assert(context);
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) {
John Zulauf110413c2021-03-20 05:38:38 -06004395 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004396 }
4397 }
4398}
4399
4400bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4401 VkImageLayout imageLayout,
4402 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4403 const VkImageSubresourceRange *pRanges) const {
4404 bool skip = false;
4405 const auto *cb_access_context = GetAccessContext(commandBuffer);
4406 assert(cb_access_context);
4407 if (!cb_access_context) return skip;
4408
4409 const auto *context = cb_access_context->GetCurrentAccessContext();
4410 assert(context);
4411 if (!context) return skip;
4412
4413 const auto *image_state = Get<IMAGE_STATE>(image);
4414
4415 for (uint32_t index = 0; index < rangeCount; index++) {
4416 const auto &range = pRanges[index];
4417 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004418 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004419 if (hazard.hazard) {
4420 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004421 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004422 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004423 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004424 }
4425 }
4426 }
4427 return skip;
4428}
4429
4430void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4431 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4432 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004433 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004434 auto *cb_access_context = GetAccessContext(commandBuffer);
4435 assert(cb_access_context);
4436 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4437 auto *context = cb_access_context->GetCurrentAccessContext();
4438 assert(context);
4439
4440 const auto *image_state = Get<IMAGE_STATE>(image);
4441
4442 for (uint32_t index = 0; index < rangeCount; index++) {
4443 const auto &range = pRanges[index];
4444 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004445 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004446 }
4447 }
4448}
4449
4450bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4451 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4452 VkDeviceSize dstOffset, VkDeviceSize stride,
4453 VkQueryResultFlags flags) const {
4454 bool skip = false;
4455 const auto *cb_access_context = GetAccessContext(commandBuffer);
4456 assert(cb_access_context);
4457 if (!cb_access_context) return skip;
4458
4459 const auto *context = cb_access_context->GetCurrentAccessContext();
4460 assert(context);
4461 if (!context) return skip;
4462
4463 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4464
4465 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004466 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004467 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004468 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004469 skip |=
4470 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4471 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004472 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004473 }
4474 }
locke-lunargff255f92020-05-13 18:53:52 -06004475
4476 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004477 return skip;
4478}
4479
4480void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4481 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4482 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004483 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4484 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004485 auto *cb_access_context = GetAccessContext(commandBuffer);
4486 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004487 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004488 auto *context = cb_access_context->GetCurrentAccessContext();
4489 assert(context);
4490
4491 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4492
4493 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004494 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004495 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004496 }
locke-lunargff255f92020-05-13 18:53:52 -06004497
4498 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004499}
4500
4501bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4502 VkDeviceSize size, uint32_t data) const {
4503 bool skip = false;
4504 const auto *cb_access_context = GetAccessContext(commandBuffer);
4505 assert(cb_access_context);
4506 if (!cb_access_context) return skip;
4507
4508 const auto *context = cb_access_context->GetCurrentAccessContext();
4509 assert(context);
4510 if (!context) return skip;
4511
4512 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4513
4514 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004515 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004516 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004517 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004518 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004519 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004520 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004521 }
4522 }
4523 return skip;
4524}
4525
4526void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4527 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004528 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004529 auto *cb_access_context = GetAccessContext(commandBuffer);
4530 assert(cb_access_context);
4531 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4532 auto *context = cb_access_context->GetCurrentAccessContext();
4533 assert(context);
4534
4535 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4536
4537 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004538 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004539 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004540 }
4541}
4542
4543bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4544 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4545 const VkImageResolve *pRegions) const {
4546 bool skip = false;
4547 const auto *cb_access_context = GetAccessContext(commandBuffer);
4548 assert(cb_access_context);
4549 if (!cb_access_context) return skip;
4550
4551 const auto *context = cb_access_context->GetCurrentAccessContext();
4552 assert(context);
4553 if (!context) return skip;
4554
4555 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4556 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4557
4558 for (uint32_t region = 0; region < regionCount; region++) {
4559 const auto &resolve_region = pRegions[region];
4560 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004561 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004562 resolve_region.srcOffset, resolve_region.extent);
4563 if (hazard.hazard) {
4564 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004565 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004566 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004567 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004568 }
4569 }
4570
4571 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004572 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004573 resolve_region.dstOffset, resolve_region.extent);
4574 if (hazard.hazard) {
4575 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004576 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004577 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004578 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004579 }
4580 if (skip) break;
4581 }
4582 }
4583
4584 return skip;
4585}
4586
4587void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4588 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4589 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004590 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4591 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004592 auto *cb_access_context = GetAccessContext(commandBuffer);
4593 assert(cb_access_context);
4594 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4595 auto *context = cb_access_context->GetCurrentAccessContext();
4596 assert(context);
4597
4598 auto *src_image = Get<IMAGE_STATE>(srcImage);
4599 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4600
4601 for (uint32_t region = 0; region < regionCount; region++) {
4602 const auto &resolve_region = pRegions[region];
4603 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004604 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004605 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004606 }
4607 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004608 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004609 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004610 }
4611 }
4612}
4613
Jeff Leger178b1e52020-10-05 12:22:23 -04004614bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4615 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4616 bool skip = false;
4617 const auto *cb_access_context = GetAccessContext(commandBuffer);
4618 assert(cb_access_context);
4619 if (!cb_access_context) return skip;
4620
4621 const auto *context = cb_access_context->GetCurrentAccessContext();
4622 assert(context);
4623 if (!context) return skip;
4624
4625 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4626 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4627
4628 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4629 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4630 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004631 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004632 resolve_region.srcOffset, resolve_region.extent);
4633 if (hazard.hazard) {
4634 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4635 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4636 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004637 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004638 }
4639 }
4640
4641 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004642 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004643 resolve_region.dstOffset, resolve_region.extent);
4644 if (hazard.hazard) {
4645 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4646 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4647 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004648 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004649 }
4650 if (skip) break;
4651 }
4652 }
4653
4654 return skip;
4655}
4656
4657void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4658 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4659 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4660 auto *cb_access_context = GetAccessContext(commandBuffer);
4661 assert(cb_access_context);
4662 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4663 auto *context = cb_access_context->GetCurrentAccessContext();
4664 assert(context);
4665
4666 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4667 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4668
4669 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4670 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4671 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004672 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004673 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004674 }
4675 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004676 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004677 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004678 }
4679 }
4680}
4681
locke-lunarge1a67022020-04-29 00:15:36 -06004682bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4683 VkDeviceSize dataSize, const void *pData) const {
4684 bool skip = false;
4685 const auto *cb_access_context = GetAccessContext(commandBuffer);
4686 assert(cb_access_context);
4687 if (!cb_access_context) return skip;
4688
4689 const auto *context = cb_access_context->GetCurrentAccessContext();
4690 assert(context);
4691 if (!context) return skip;
4692
4693 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4694
4695 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004696 // VK_WHOLE_SIZE not allowed
4697 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004698 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004699 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004700 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004701 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004702 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004703 }
4704 }
4705 return skip;
4706}
4707
4708void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4709 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004710 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004711 auto *cb_access_context = GetAccessContext(commandBuffer);
4712 assert(cb_access_context);
4713 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4714 auto *context = cb_access_context->GetCurrentAccessContext();
4715 assert(context);
4716
4717 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4718
4719 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004720 // VK_WHOLE_SIZE not allowed
4721 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004722 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004723 }
4724}
locke-lunargff255f92020-05-13 18:53:52 -06004725
4726bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4727 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4728 bool skip = false;
4729 const auto *cb_access_context = GetAccessContext(commandBuffer);
4730 assert(cb_access_context);
4731 if (!cb_access_context) return skip;
4732
4733 const auto *context = cb_access_context->GetCurrentAccessContext();
4734 assert(context);
4735 if (!context) return skip;
4736
4737 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4738
4739 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004740 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004741 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06004742 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004743 skip |=
4744 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4745 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004746 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004747 }
4748 }
4749 return skip;
4750}
4751
4752void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4753 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004754 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004755 auto *cb_access_context = GetAccessContext(commandBuffer);
4756 assert(cb_access_context);
4757 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4758 auto *context = cb_access_context->GetCurrentAccessContext();
4759 assert(context);
4760
4761 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4762
4763 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004764 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004765 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004766 }
4767}
John Zulauf49beb112020-11-04 16:06:31 -07004768
4769bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
4770 bool skip = false;
4771 const auto *cb_context = GetAccessContext(commandBuffer);
4772 assert(cb_context);
4773 if (!cb_context) return skip;
4774
John Zulauf36ef9282021-02-02 11:47:24 -07004775 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004776 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004777}
4778
4779void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4780 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
4781 auto *cb_context = GetAccessContext(commandBuffer);
4782 assert(cb_context);
4783 if (!cb_context) return;
John Zulauf36ef9282021-02-02 11:47:24 -07004784 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4785 set_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004786}
4787
John Zulauf4edde622021-02-15 08:54:50 -07004788bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4789 const VkDependencyInfoKHR *pDependencyInfo) const {
4790 bool skip = false;
4791 const auto *cb_context = GetAccessContext(commandBuffer);
4792 assert(cb_context);
4793 if (!cb_context || !pDependencyInfo) return skip;
4794
4795 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4796 return set_event_op.Validate(*cb_context);
4797}
4798
4799void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4800 const VkDependencyInfoKHR *pDependencyInfo) {
4801 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
4802 auto *cb_context = GetAccessContext(commandBuffer);
4803 assert(cb_context);
4804 if (!cb_context || !pDependencyInfo) return;
4805
4806 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4807 set_event_op.Record(cb_context);
4808}
4809
John Zulauf49beb112020-11-04 16:06:31 -07004810bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
4811 VkPipelineStageFlags stageMask) const {
4812 bool skip = false;
4813 const auto *cb_context = GetAccessContext(commandBuffer);
4814 assert(cb_context);
4815 if (!cb_context) return skip;
4816
John Zulauf36ef9282021-02-02 11:47:24 -07004817 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004818 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004819}
4820
4821void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4822 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
4823 auto *cb_context = GetAccessContext(commandBuffer);
4824 assert(cb_context);
4825 if (!cb_context) return;
4826
John Zulauf36ef9282021-02-02 11:47:24 -07004827 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4828 reset_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004829}
4830
John Zulauf4edde622021-02-15 08:54:50 -07004831bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4832 VkPipelineStageFlags2KHR stageMask) const {
4833 bool skip = false;
4834 const auto *cb_context = GetAccessContext(commandBuffer);
4835 assert(cb_context);
4836 if (!cb_context) return skip;
4837
4838 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4839 return reset_event_op.Validate(*cb_context);
4840}
4841
4842void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4843 VkPipelineStageFlags2KHR stageMask) {
4844 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
4845 auto *cb_context = GetAccessContext(commandBuffer);
4846 assert(cb_context);
4847 if (!cb_context) return;
4848
4849 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4850 reset_event_op.Record(cb_context);
4851}
4852
John Zulauf49beb112020-11-04 16:06:31 -07004853bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4854 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4855 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4856 uint32_t bufferMemoryBarrierCount,
4857 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4858 uint32_t imageMemoryBarrierCount,
4859 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
4860 bool skip = false;
4861 const auto *cb_context = GetAccessContext(commandBuffer);
4862 assert(cb_context);
4863 if (!cb_context) return skip;
4864
John Zulauf36ef9282021-02-02 11:47:24 -07004865 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4866 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4867 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07004868 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004869}
4870
4871void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4872 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4873 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4874 uint32_t bufferMemoryBarrierCount,
4875 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4876 uint32_t imageMemoryBarrierCount,
4877 const VkImageMemoryBarrier *pImageMemoryBarriers) {
4878 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
4879 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
4880 imageMemoryBarrierCount, pImageMemoryBarriers);
4881
4882 auto *cb_context = GetAccessContext(commandBuffer);
4883 assert(cb_context);
4884 if (!cb_context) return;
4885
John Zulauf36ef9282021-02-02 11:47:24 -07004886 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4887 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4888 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
4889 return wait_events_op.Record(cb_context);
John Zulauf4a6105a2020-11-17 15:11:05 -07004890}
4891
John Zulauf4edde622021-02-15 08:54:50 -07004892bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4893 const VkDependencyInfoKHR *pDependencyInfos) const {
4894 bool skip = false;
4895 const auto *cb_context = GetAccessContext(commandBuffer);
4896 assert(cb_context);
4897 if (!cb_context) return skip;
4898
4899 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
4900 skip |= wait_events_op.Validate(*cb_context);
4901 return skip;
4902}
4903
4904void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4905 const VkDependencyInfoKHR *pDependencyInfos) {
4906 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
4907
4908 auto *cb_context = GetAccessContext(commandBuffer);
4909 assert(cb_context);
4910 if (!cb_context) return;
4911
4912 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
4913 wait_events_op.Record(cb_context);
4914}
4915
John Zulauf4a6105a2020-11-17 15:11:05 -07004916void SyncEventState::ResetFirstScope() {
4917 for (const auto address_type : kAddressTypes) {
4918 first_scope[static_cast<size_t>(address_type)].clear();
4919 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07004920 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07004921}
4922
4923// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07004924SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07004925 IgnoreReason reason = NotIgnored;
4926
John Zulauf4edde622021-02-15 08:54:50 -07004927 if ((CMD_WAITEVENTS2KHR == cmd) && (CMD_SETEVENT == last_command)) {
4928 reason = SetVsWait2;
4929 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
4930 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07004931 } else if (unsynchronized_set) {
4932 reason = SetRace;
4933 } else {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004934 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07004935 if (missing_bits) reason = MissingStageBits;
4936 }
4937
4938 return reason;
4939}
4940
Jeremy Gebben40a22942020-12-22 14:22:06 -07004941bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07004942 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
4943 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
4944 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07004945}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004946
John Zulauf36ef9282021-02-02 11:47:24 -07004947SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
4948 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4949 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07004950 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
4951 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
4952 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07004953 : SyncOpBase(cmd), barriers_(1) {
4954 auto &barrier_set = barriers_[0];
4955 barrier_set.dependency_flags = dependencyFlags;
4956 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
4957 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004958 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07004959 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
4960 pMemoryBarriers);
4961 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
4962 bufferMemoryBarrierCount, pBufferMemoryBarriers);
4963 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
4964 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004965}
4966
John Zulauf4edde622021-02-15 08:54:50 -07004967SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
4968 const VkDependencyInfoKHR *dep_infos)
4969 : SyncOpBase(cmd), barriers_(event_count) {
4970 for (uint32_t i = 0; i < event_count; i++) {
4971 const auto &dep_info = dep_infos[i];
4972 auto &barrier_set = barriers_[i];
4973 barrier_set.dependency_flags = dep_info.dependencyFlags;
4974 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
4975 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
4976 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
4977 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
4978 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
4979 dep_info.pMemoryBarriers);
4980 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
4981 dep_info.pBufferMemoryBarriers);
4982 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
4983 dep_info.pImageMemoryBarriers);
4984 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07004985}
4986
John Zulauf36ef9282021-02-02 11:47:24 -07004987SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07004988 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4989 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
4990 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
4991 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
4992 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07004993 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07004994 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
4995
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07004996SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
4997 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07004998 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07004999
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005000bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5001 bool skip = false;
5002 const auto *context = cb_context.GetCurrentAccessContext();
5003 assert(context);
5004 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005005 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5006
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005007 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005008 const auto &barrier_set = barriers_[0];
5009 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5010 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5011 const auto *image_state = image_barrier.image.get();
5012 if (!image_state) continue;
5013 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5014 if (hazard.hazard) {
5015 // PHASE1 TODO -- add tag information to log msg when useful.
5016 const auto &sync_state = cb_context.GetSyncState();
5017 const auto image_handle = image_state->image;
5018 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5019 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5020 string_SyncHazard(hazard.hazard), image_barrier.index,
5021 sync_state.report_data->FormatHandle(image_handle).c_str(),
5022 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005023 }
5024 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005025 return skip;
5026}
5027
John Zulaufd5115702021-01-18 12:34:33 -07005028struct SyncOpPipelineBarrierFunctorFactory {
5029 using BarrierOpFunctor = PipelineBarrierOp;
5030 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5031 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5032 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5033 using BufferRange = ResourceAccessRange;
5034 using ImageRange = subresource_adapter::ImageRangeGenerator;
5035 using GlobalRange = ResourceAccessRange;
5036
5037 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5038 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5039 }
5040 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
5041 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5042 }
5043 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5044 return GlobalBarrierOpFunctor(barrier, false);
5045 }
5046
5047 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5048 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5049 const auto base_address = ResourceBaseAddress(buffer);
5050 return (range + base_address);
5051 }
John Zulauf110413c2021-03-20 05:38:38 -06005052 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005053 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005054
5055 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005056 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005057 return range_gen;
5058 }
5059 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5060};
5061
5062template <typename Barriers, typename FunctorFactory>
5063void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
5064 AccessContext *context) {
5065 for (const auto &barrier : barriers) {
5066 const auto *state = barrier.GetState();
5067 if (state) {
5068 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5069 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5070 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5071 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5072 }
5073 }
5074}
5075
5076template <typename Barriers, typename FunctorFactory>
5077void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
5078 AccessContext *access_context) {
5079 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5080 for (const auto &barrier : barriers) {
5081 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5082 }
5083 for (const auto address_type : kAddressTypes) {
5084 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5085 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5086 }
5087}
5088
John Zulauf36ef9282021-02-02 11:47:24 -07005089void SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005090 SyncOpPipelineBarrierFunctorFactory factory;
5091 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005092 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005093
John Zulauf4edde622021-02-15 08:54:50 -07005094 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5095 assert(barriers_.size() == 1);
5096 const auto &barrier_set = barriers_[0];
5097 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5098 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5099 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
5100
5101 if (barrier_set.single_exec_scope) {
5102 cb_context->ApplyGlobalBarriersToEvents(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
5103 } else {
5104 for (const auto &barrier : barrier_set.memory_barriers) {
5105 cb_context->ApplyGlobalBarriersToEvents(barrier.src_exec_scope, barrier.dst_exec_scope);
5106 }
5107 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005108}
5109
John Zulauf4edde622021-02-15 08:54:50 -07005110void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5111 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5112 const VkMemoryBarrier *barriers) {
5113 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005114 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005115 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005116 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005117 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005118 }
5119 if (0 == memory_barrier_count) {
5120 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005121 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005122 }
John Zulauf4edde622021-02-15 08:54:50 -07005123 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005124}
5125
John Zulauf4edde622021-02-15 08:54:50 -07005126void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5127 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5128 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5129 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005130 for (uint32_t index = 0; index < barrier_count; index++) {
5131 const auto &barrier = barriers[index];
5132 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5133 if (buffer) {
5134 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5135 const auto range = MakeRange(barrier.offset, barrier_size);
5136 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005137 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005138 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005139 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005140 }
5141 }
5142}
5143
John Zulauf4edde622021-02-15 08:54:50 -07005144void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
5145 uint32_t memory_barrier_count, const VkMemoryBarrier2KHR *barriers) {
5146 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005147 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005148 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005149 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5150 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5151 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005152 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005153 }
John Zulauf4edde622021-02-15 08:54:50 -07005154 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005155}
5156
John Zulauf4edde622021-02-15 08:54:50 -07005157void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5158 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5159 const VkBufferMemoryBarrier2KHR *barriers) {
5160 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005161 for (uint32_t index = 0; index < barrier_count; index++) {
5162 const auto &barrier = barriers[index];
5163 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5164 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5165 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5166 if (buffer) {
5167 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5168 const auto range = MakeRange(barrier.offset, barrier_size);
5169 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005170 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005171 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005172 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005173 }
5174 }
5175}
5176
John Zulauf4edde622021-02-15 08:54:50 -07005177void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5178 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5179 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5180 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005181 for (uint32_t index = 0; index < barrier_count; index++) {
5182 const auto &barrier = barriers[index];
5183 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5184 if (image) {
5185 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5186 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005187 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005188 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005189 image_memory_barriers.emplace_back();
5190 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005191 }
5192 }
5193}
John Zulaufd5115702021-01-18 12:34:33 -07005194
John Zulauf4edde622021-02-15 08:54:50 -07005195void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5196 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5197 const VkImageMemoryBarrier2KHR *barriers) {
5198 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005199 for (uint32_t index = 0; index < barrier_count; index++) {
5200 const auto &barrier = barriers[index];
5201 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5202 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5203 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5204 if (image) {
5205 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5206 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005207 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005208 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005209 image_memory_barriers.emplace_back();
5210 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005211 }
5212 }
5213}
5214
John Zulauf36ef9282021-02-02 11:47:24 -07005215SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005216 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5217 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5218 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5219 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005220 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005221 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5222 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005223 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005224}
5225
John Zulauf4edde622021-02-15 08:54:50 -07005226SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5227 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5228 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5229 MakeEventsList(sync_state, eventCount, pEvents);
5230 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5231}
5232
John Zulaufd5115702021-01-18 12:34:33 -07005233bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005234 const char *const ignored = "Wait operation is ignored for this event.";
5235 bool skip = false;
5236 const auto &sync_state = cb_context.GetSyncState();
5237 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer;
5238
John Zulauf4edde622021-02-15 08:54:50 -07005239 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5240 const auto &barrier_set = barriers_[barrier_set_index];
5241 if (barrier_set.single_exec_scope) {
5242 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5243 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5244 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5245 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5246 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5247 } else {
5248 const auto &barriers = barrier_set.memory_barriers;
5249 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5250 const auto &barrier = barriers[barrier_index];
5251 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5252 const std::string vuid =
5253 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5254 skip =
5255 sync_state.LogInfo(command_buffer_handle, vuid,
5256 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5257 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5258 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5259 }
5260 }
5261 }
5262 }
John Zulaufd5115702021-01-18 12:34:33 -07005263 }
5264
Jeremy Gebben40a22942020-12-22 14:22:06 -07005265 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005266 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005267 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005268 const auto *events_context = cb_context.GetCurrentEventsContext();
5269 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005270 size_t barrier_set_index = 0;
5271 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5272 for (size_t event_index = 0; event_index < events_.size(); event_index++)
5273 for (const auto &event : events_) {
5274 const auto *sync_event = events_context->Get(event.get());
5275 const auto &barrier_set = barriers_[barrier_set_index];
5276 if (!sync_event) {
5277 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5278 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5279 // new validation error... wait without previously submitted set event...
5280 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives at *record time*
5281 barrier_set_index += barrier_set_incr;
5282 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005283 }
John Zulauf4edde622021-02-15 08:54:50 -07005284 const auto event_handle = sync_event->event->event;
5285 // TODO add "destroyed" checks
5286
5287 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5288 const auto &src_exec_scope = barrier_set.src_exec_scope;
5289 event_stage_masks |= sync_event->scope.mask_param;
5290 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5291 if (ignore_reason) {
5292 switch (ignore_reason) {
5293 case SyncEventState::ResetWaitRace:
5294 case SyncEventState::Reset2WaitRace: {
5295 // Four permuations of Reset and Wait calls...
5296 const char *vuid =
5297 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5298 if (ignore_reason == SyncEventState::Reset2WaitRace) {
5299 vuid =
Jeremy Gebben476f5e22021-03-01 15:27:20 -07005300 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2KHR-event-03831" : "VUID-vkCmdResetEvent2KHR-event-03832";
John Zulauf4edde622021-02-15 08:54:50 -07005301 }
5302 const char *const message =
5303 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5304 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5305 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
5306 CommandTypeString(sync_event->last_command), ignored);
5307 break;
5308 }
5309 case SyncEventState::SetRace: {
5310 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5311 // this event
5312 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5313 const char *const message =
5314 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5315 const char *const reason = "First synchronization scope is undefined.";
5316 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5317 sync_state.report_data->FormatHandle(event_handle).c_str(),
5318 CommandTypeString(sync_event->last_command), reason, ignored);
5319 break;
5320 }
5321 case SyncEventState::MissingStageBits: {
5322 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5323 // Issue error message that event waited for is not in wait events scope
5324 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5325 const char *const message =
5326 "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5327 ". Bits missing from srcStageMask %s. %s";
5328 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5329 sync_state.report_data->FormatHandle(event_handle).c_str(),
5330 sync_event->scope.mask_param, src_exec_scope.mask_param,
5331 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), ignored);
5332 break;
5333 }
5334 case SyncEventState::SetVsWait2: {
5335 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2KHR-pEvents-03837",
5336 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5337 sync_state.report_data->FormatHandle(event_handle).c_str(),
5338 CommandTypeString(sync_event->last_command));
5339 break;
5340 }
5341 default:
5342 assert(ignore_reason == SyncEventState::NotIgnored);
5343 }
5344 } else if (barrier_set.image_memory_barriers.size()) {
5345 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5346 const auto *context = cb_context.GetCurrentAccessContext();
5347 assert(context);
5348 for (const auto &image_memory_barrier : image_memory_barriers) {
5349 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5350 const auto *image_state = image_memory_barrier.image.get();
5351 if (!image_state) continue;
John Zulauf110413c2021-03-20 05:38:38 -06005352 const auto &subresource_range = image_memory_barrier.range;
John Zulauf4edde622021-02-15 08:54:50 -07005353 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5354 const auto hazard =
5355 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5356 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5357 if (hazard.hazard) {
5358 skip |= sync_state.LogError(image_state->image, string_SyncHazardVUID(hazard.hazard),
5359 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5360 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
5361 sync_state.report_data->FormatHandle(image_state->image).c_str(),
5362 cb_context.FormatUsage(hazard).c_str());
5363 break;
5364 }
John Zulaufd5115702021-01-18 12:34:33 -07005365 }
5366 }
John Zulauf4edde622021-02-15 08:54:50 -07005367 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5368 // 03839
5369 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005370 }
John Zulaufd5115702021-01-18 12:34:33 -07005371
5372 // 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 -07005373 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 -07005374 if (extra_stage_bits) {
5375 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005376 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5377 const char *const vuid =
5378 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2KHR-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005379 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005380 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulaufd5115702021-01-18 12:34:33 -07005381 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005382 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005383 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005384 " vkCmdSetEvent may be in previously submitted command buffer.");
5385 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005386 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005387 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005388 }
5389 }
5390 return skip;
5391}
5392
5393struct SyncOpWaitEventsFunctorFactory {
5394 using BarrierOpFunctor = WaitEventBarrierOp;
5395 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5396 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5397 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5398 using BufferRange = EventSimpleRangeGenerator;
5399 using ImageRange = EventImageRangeGenerator;
5400 using GlobalRange = EventSimpleRangeGenerator;
5401
5402 // Need to restrict to only valid exec and access scope for this event
5403 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5404 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005405 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005406 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5407 return barrier;
5408 }
5409 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5410 auto barrier = RestrictToEvent(barrier_arg);
5411 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5412 }
5413 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
5414 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5415 }
5416 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5417 auto barrier = RestrictToEvent(barrier_arg);
5418 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5419 }
5420
5421 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5422 const AccessAddressType address_type = GetAccessAddressType(buffer);
5423 const auto base_address = ResourceBaseAddress(buffer);
5424 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5425 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5426 return filtered_range_gen;
5427 }
John Zulauf110413c2021-03-20 05:38:38 -06005428 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07005429 if (!SimpleBinding(image)) return ImageRange();
5430 const auto address_type = GetAccessAddressType(image);
5431 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005432 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005433 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5434
5435 return filtered_range_gen;
5436 }
5437 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5438 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5439 }
5440 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5441 SyncEventState *sync_event;
5442};
5443
John Zulauf36ef9282021-02-02 11:47:24 -07005444void SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
5445 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07005446 auto *access_context = cb_context->GetCurrentAccessContext();
5447 assert(access_context);
5448 if (!access_context) return;
John Zulauf669dfd52021-01-27 17:15:28 -07005449 auto *events_context = cb_context->GetCurrentEventsContext();
5450 assert(events_context);
5451 if (!events_context) return;
John Zulaufd5115702021-01-18 12:34:33 -07005452
5453 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
5454 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
5455 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
5456 access_context->ResolvePreviousAccesses();
5457
John Zulaufd5115702021-01-18 12:34:33 -07005458 // TODO... this needs change the SyncEventContext it's using depending on whether this is replay... the recorded
5459 // 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 -07005460 size_t barrier_set_index = 0;
5461 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5462 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07005463 for (auto &event_shared : events_) {
5464 if (!event_shared.get()) continue;
5465 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07005466
John Zulauf4edde622021-02-15 08:54:50 -07005467 sync_event->last_command = cmd_;
John Zulaufd5115702021-01-18 12:34:33 -07005468
John Zulauf4edde622021-02-15 08:54:50 -07005469 const auto &barrier_set = barriers_[barrier_set_index];
5470 const auto &dst = barrier_set.dst_exec_scope;
5471 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07005472 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
5473 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
5474 // of the barriers is maintained.
5475 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07005476 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5477 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5478 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07005479
5480 // Apply the global barrier to the event itself (for race condition tracking)
5481 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
5482 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
5483 sync_event->barriers |= dst.exec_scope;
5484 } else {
5485 // We ignored this wait, so we don't have any effective synchronization barriers for it.
5486 sync_event->barriers = 0U;
5487 }
John Zulauf4edde622021-02-15 08:54:50 -07005488 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005489 }
5490
5491 // Apply the pending barriers
5492 ResolvePendingBarrierFunctor apply_pending_action(tag);
5493 access_context->ApplyToContext(apply_pending_action);
5494}
5495
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005496bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5497 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5498 bool skip = false;
5499 const auto *cb_access_context = GetAccessContext(commandBuffer);
5500 assert(cb_access_context);
5501 if (!cb_access_context) return skip;
5502
5503 const auto *context = cb_access_context->GetCurrentAccessContext();
5504 assert(context);
5505 if (!context) return skip;
5506
5507 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5508
5509 if (dst_buffer) {
5510 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5511 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
5512 if (hazard.hazard) {
5513 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5514 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
5515 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
5516 string_UsageTag(hazard.tag).c_str());
5517 }
5518 }
5519 return skip;
5520}
5521
John Zulauf669dfd52021-01-27 17:15:28 -07005522void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07005523 events_.reserve(event_count);
5524 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
John Zulauf669dfd52021-01-27 17:15:28 -07005525 events_.emplace_back(sync_state.GetShared<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07005526 }
5527}
John Zulauf6ce24372021-01-30 05:56:25 -07005528
John Zulauf36ef9282021-02-02 11:47:24 -07005529SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005530 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005531 : SyncOpBase(cmd),
5532 event_(sync_state.GetShared<EVENT_STATE>(event)),
5533 exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005534
5535bool SyncOpResetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005536 auto *events_context = cb_context.GetCurrentEventsContext();
5537 assert(events_context);
5538 bool skip = false;
5539 if (!events_context) return skip;
5540
5541 const auto &sync_state = cb_context.GetSyncState();
5542 const auto *sync_event = events_context->Get(event_);
5543 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5544
5545 const char *const set_wait =
5546 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5547 "hazards.";
5548 const char *message = set_wait; // Only one message this call.
5549 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
5550 const char *vuid = nullptr;
5551 switch (sync_event->last_command) {
5552 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005553 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005554 // Needs a barrier between set and reset
5555 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
5556 break;
John Zulauf4edde622021-02-15 08:54:50 -07005557 case CMD_WAITEVENTS:
5558 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07005559 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
5560 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
5561 break;
5562 }
5563 default:
5564 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07005565 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
5566 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07005567 break;
5568 }
5569 if (vuid) {
John Zulauf36ef9282021-02-02 11:47:24 -07005570 skip |= sync_state.LogError(event_->event, vuid, message, CmdName(),
5571 sync_state.report_data->FormatHandle(event_->event).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005572 CommandTypeString(sync_event->last_command));
5573 }
5574 }
5575 return skip;
5576}
5577
John Zulauf36ef9282021-02-02 11:47:24 -07005578void SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005579 auto *events_context = cb_context->GetCurrentEventsContext();
5580 assert(events_context);
5581 if (!events_context) return;
5582
5583 auto *sync_event = events_context->GetFromShared(event_);
5584 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5585
5586 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07005587 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005588 sync_event->unsynchronized_set = CMD_NONE;
5589 sync_event->ResetFirstScope();
5590 sync_event->barriers = 0U;
5591}
5592
John Zulauf36ef9282021-02-02 11:47:24 -07005593SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005594 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005595 : SyncOpBase(cmd),
5596 event_(sync_state.GetShared<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07005597 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
5598 dep_info_() {}
5599
5600SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
5601 const VkDependencyInfoKHR &dep_info)
5602 : SyncOpBase(cmd),
5603 event_(sync_state.GetShared<EVENT_STATE>(event)),
5604 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
5605 dep_info_(new safe_VkDependencyInfoKHR(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005606
5607bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
5608 // 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 -07005609 bool skip = false;
5610
5611 const auto &sync_state = cb_context.GetSyncState();
5612 auto *events_context = cb_context.GetCurrentEventsContext();
5613 assert(events_context);
5614 if (!events_context) return skip;
5615
5616 const auto *sync_event = events_context->Get(event_);
5617 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5618
5619 const char *const reset_set =
5620 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5621 "hazards.";
5622 const char *const wait =
5623 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
5624
5625 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07005626 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07005627 const char *message = nullptr;
5628 switch (sync_event->last_command) {
5629 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005630 case CMD_RESETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005631 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07005632 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07005633 message = reset_set;
5634 break;
5635 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005636 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005637 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07005638 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07005639 message = reset_set;
5640 break;
5641 case CMD_WAITEVENTS:
John Zulauf4edde622021-02-15 08:54:50 -07005642 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005643 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07005644 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07005645 message = wait;
5646 break;
5647 default:
5648 // The only other valid last command that wasn't one.
5649 assert(sync_event->last_command == CMD_NONE);
5650 break;
5651 }
John Zulauf4edde622021-02-15 08:54:50 -07005652 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07005653 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07005654 std::string vuid("SYNC-");
5655 vuid.append(CmdName()).append(vuid_stem);
5656 skip |= sync_state.LogError(event_->event, vuid.c_str(), message, CmdName(),
John Zulauf36ef9282021-02-02 11:47:24 -07005657 sync_state.report_data->FormatHandle(event_->event).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005658 CommandTypeString(sync_event->last_command));
5659 }
5660 }
5661
5662 return skip;
5663}
5664
John Zulauf36ef9282021-02-02 11:47:24 -07005665void SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
5666 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07005667 auto *events_context = cb_context->GetCurrentEventsContext();
5668 auto *access_context = cb_context->GetCurrentAccessContext();
5669 assert(events_context);
5670 if (!events_context) return;
5671
5672 auto *sync_event = events_context->GetFromShared(event_);
5673 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5674
5675 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
5676 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
5677 // any issues caused by naive scope setting here.
5678
5679 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
5680 // Given:
5681 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
5682 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
5683
5684 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
5685 sync_event->unsynchronized_set = sync_event->last_command;
5686 sync_event->ResetFirstScope();
5687 } else if (sync_event->scope.exec_scope == 0) {
5688 // We only set the scope if there isn't one
5689 sync_event->scope = src_exec_scope_;
5690
5691 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
5692 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
5693 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
5694 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
5695 }
5696 };
5697 access_context->ForAll(set_scope);
5698 sync_event->unsynchronized_set = CMD_NONE;
5699 sync_event->first_scope_tag = tag;
5700 }
John Zulauf4edde622021-02-15 08:54:50 -07005701 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
5702 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005703 sync_event->barriers = 0U;
5704}
John Zulauf64ffe552021-02-06 10:25:07 -07005705
5706SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
5707 const VkRenderPassBeginInfo *pRenderPassBegin,
5708 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *cmd_name)
5709 : SyncOpBase(cmd, cmd_name) {
5710 if (pRenderPassBegin) {
5711 rp_state_ = sync_state.GetShared<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
5712 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
5713 const auto *fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
5714 if (fb_state) {
5715 shared_attachments_ = sync_state.GetSharedAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
5716 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
5717 // Note that this a safe to presist as long as shared_attachments is not cleared
5718 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08005719 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07005720 attachments_.emplace_back(attachment.get());
5721 }
5722 }
5723 if (pSubpassBeginInfo) {
5724 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
5725 }
5726 }
5727}
5728
5729bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5730 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
5731 bool skip = false;
5732
5733 assert(rp_state_.get());
5734 if (nullptr == rp_state_.get()) return skip;
5735 auto &rp_state = *rp_state_.get();
5736
5737 const uint32_t subpass = 0;
5738
5739 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
5740 // hasn't happened yet)
5741 const std::vector<AccessContext> empty_context_vector;
5742 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
5743 cb_context.GetCurrentAccessContext());
5744
5745 // Validate attachment operations
5746 if (attachments_.size() == 0) return skip;
5747 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07005748
5749 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
5750 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
5751 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
5752 // operations (though it's currently a messy approach)
5753 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
5754 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07005755
5756 // Validate load operations if there were no layout transition hazards
5757 if (!skip) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07005758 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kCurrentCommandTag);
5759 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07005760 }
5761
5762 return skip;
5763}
5764
5765void SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5766 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5767 assert(rp_state_.get());
5768 if (nullptr == rp_state_.get()) return;
5769 const auto tag = cb_context->NextCommandTag(cmd_);
5770 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
5771}
5772
5773SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
5774 const VkSubpassEndInfo *pSubpassEndInfo, const char *name_override)
5775 : SyncOpBase(cmd, name_override) {
5776 if (pSubpassBeginInfo) {
5777 subpass_begin_info_.initialize(pSubpassBeginInfo);
5778 }
5779 if (pSubpassEndInfo) {
5780 subpass_end_info_.initialize(pSubpassEndInfo);
5781 }
5782}
5783
5784bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
5785 bool skip = false;
5786 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5787 if (!renderpass_context) return skip;
5788
5789 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
5790 return skip;
5791}
5792
5793void SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
5794 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5795 cb_context->RecordNextSubpass(cmd_);
5796}
5797
5798SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo,
5799 const char *name_override)
5800 : SyncOpBase(cmd, name_override) {
5801 if (pSubpassEndInfo) {
5802 subpass_end_info_.initialize(pSubpassEndInfo);
5803 }
5804}
5805
5806bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5807 bool skip = false;
5808 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5809
5810 if (!renderpass_context) return skip;
5811 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
5812 return skip;
5813}
5814
5815void SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5816 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5817 cb_context->RecordEndRenderPass(cmd_);
5818}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005819
5820void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5821 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
5822 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
5823 auto *cb_access_context = GetAccessContext(commandBuffer);
5824 assert(cb_access_context);
5825 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5826 auto *context = cb_access_context->GetCurrentAccessContext();
5827 assert(context);
5828
5829 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5830
5831 if (dst_buffer) {
5832 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5833 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
5834 }
5835}
John Zulaufd05c5842021-03-26 11:32:16 -06005836
John Zulaufd0ec59f2021-03-13 14:25:08 -07005837AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
5838 : view_(view), view_mask_(), gen_store_() {
5839 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
5840 const IMAGE_STATE &image_state = *view_->image_state.get();
5841 const auto base_address = ResourceBaseAddress(image_state);
5842 const auto *encoder = image_state.fragment_encoder.get();
5843 if (!encoder) return;
5844 const VkOffset3D zero_offset = {0, 0, 0};
5845 const VkExtent3D &image_extent = image_state.createInfo.extent;
5846 // Intentional copy
5847 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
5848 view_mask_ = subres_range.aspectMask;
5849 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
5850 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5851
5852 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
5853 if (depth && (depth != view_mask_)) {
5854 subres_range.aspectMask = depth;
5855 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5856 }
5857 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
5858 if (stencil && (stencil != view_mask_)) {
5859 subres_range.aspectMask = stencil;
5860 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5861 }
5862}
5863
5864const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
5865 const ImageRangeGen *got = nullptr;
5866 switch (gen_type) {
5867 case kViewSubresource:
5868 got = &gen_store_[kViewSubresource];
5869 break;
5870 case kRenderArea:
5871 got = &gen_store_[kRenderArea];
5872 break;
5873 case kDepthOnlyRenderArea:
5874 got =
5875 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
5876 break;
5877 case kStencilOnlyRenderArea:
5878 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
5879 : &gen_store_[Gen::kStencilOnlyRenderArea];
5880 break;
5881 default:
5882 assert(got);
5883 }
5884 return got;
5885}
5886
5887AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
5888 assert(IsValid());
5889 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
5890 if (depth_op) {
5891 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
5892 if (stencil_op) {
5893 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
5894 return kRenderArea;
5895 }
5896 return kDepthOnlyRenderArea;
5897 }
5898 if (stencil_op) {
5899 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
5900 return kStencilOnlyRenderArea;
5901 }
5902
5903 assert(depth_op || stencil_op);
5904 return kRenderArea;
5905}
5906
5907AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }