blob: b6d5806e6a2d326723ed7a34cd500a49e9f075dc [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 Zulauf43cc7462020-12-03 12:33:12 -070031const static std::array<AccessAddressType, static_cast<size_t>(AccessAddressType::kTypeCount)> kAddressTypes = {
32 AccessAddressType::kLinear, AccessAddressType::kIdealized};
33
John Zulaufd5115702021-01-18 12:34:33 -070034static constexpr AccessAddressType GetAccessAddressType(const BUFFER_STATE &) { return AccessAddressType::kLinear; };
John Zulauf264cce02021-02-05 14:40:47 -070035static AccessAddressType GetAccessAddressType(const IMAGE_STATE &image) {
36 return SimpleBinding(image) ? AccessContext::ImageAddressType(image) : AccessAddressType::kIdealized;
37}
John Zulaufd5115702021-01-18 12:34:33 -070038
John Zulauf9cb530d2019-09-30 14:14:10 -060039static const char *string_SyncHazardVUID(SyncHazard hazard) {
40 switch (hazard) {
41 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070042 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060043 break;
44 case SyncHazard::READ_AFTER_WRITE:
45 return "SYNC-HAZARD-READ_AFTER_WRITE";
46 break;
47 case SyncHazard::WRITE_AFTER_READ:
48 return "SYNC-HAZARD-WRITE_AFTER_READ";
49 break;
50 case SyncHazard::WRITE_AFTER_WRITE:
51 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
52 break;
John Zulauf2f952d22020-02-10 11:34:51 -070053 case SyncHazard::READ_RACING_WRITE:
54 return "SYNC-HAZARD-READ-RACING-WRITE";
55 break;
56 case SyncHazard::WRITE_RACING_WRITE:
57 return "SYNC-HAZARD-WRITE-RACING-WRITE";
58 break;
59 case SyncHazard::WRITE_RACING_READ:
60 return "SYNC-HAZARD-WRITE-RACING-READ";
61 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060062 default:
63 assert(0);
64 }
65 return "SYNC-HAZARD-INVALID";
66}
67
John Zulauf59e25072020-07-17 10:55:21 -060068static bool IsHazardVsRead(SyncHazard hazard) {
69 switch (hazard) {
70 case SyncHazard::NONE:
71 return false;
72 break;
73 case SyncHazard::READ_AFTER_WRITE:
74 return false;
75 break;
76 case SyncHazard::WRITE_AFTER_READ:
77 return true;
78 break;
79 case SyncHazard::WRITE_AFTER_WRITE:
80 return false;
81 break;
82 case SyncHazard::READ_RACING_WRITE:
83 return false;
84 break;
85 case SyncHazard::WRITE_RACING_WRITE:
86 return false;
87 break;
88 case SyncHazard::WRITE_RACING_READ:
89 return true;
90 break;
91 default:
92 assert(0);
93 }
94 return false;
95}
96
John Zulauf9cb530d2019-09-30 14:14:10 -060097static const char *string_SyncHazard(SyncHazard hazard) {
98 switch (hazard) {
99 case SyncHazard::NONE:
100 return "NONR";
101 break;
102 case SyncHazard::READ_AFTER_WRITE:
103 return "READ_AFTER_WRITE";
104 break;
105 case SyncHazard::WRITE_AFTER_READ:
106 return "WRITE_AFTER_READ";
107 break;
108 case SyncHazard::WRITE_AFTER_WRITE:
109 return "WRITE_AFTER_WRITE";
110 break;
John Zulauf2f952d22020-02-10 11:34:51 -0700111 case SyncHazard::READ_RACING_WRITE:
112 return "READ_RACING_WRITE";
113 break;
114 case SyncHazard::WRITE_RACING_WRITE:
115 return "WRITE_RACING_WRITE";
116 break;
117 case SyncHazard::WRITE_RACING_READ:
118 return "WRITE_RACING_READ";
119 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600120 default:
121 assert(0);
122 }
123 return "INVALID HAZARD";
124}
125
John Zulauf37ceaed2020-07-03 16:18:15 -0600126static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
127 // Return the info for the first bit found
128 const SyncStageAccessInfoType *info = nullptr;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700129 for (size_t i = 0; i < flags.size(); i++) {
130 if (flags.test(i)) {
131 info = &syncStageAccessInfoByStageAccessIndex[i];
132 break;
John Zulauf37ceaed2020-07-03 16:18:15 -0600133 }
134 }
135 return info;
136}
137
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700138static std::string string_SyncStageAccessFlags(const SyncStageAccessFlags &flags, const char *sep = "|") {
John Zulauf59e25072020-07-17 10:55:21 -0600139 std::string out_str;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700140 if (flags.none()) {
John Zulauf389c34b2020-07-28 11:19:35 -0600141 out_str = "0";
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700142 } else {
143 for (size_t i = 0; i < syncStageAccessInfoByStageAccessIndex.size(); i++) {
144 const auto &info = syncStageAccessInfoByStageAccessIndex[i];
145 if ((flags & info.stage_access_bit).any()) {
146 if (!out_str.empty()) {
147 out_str.append(sep);
148 }
149 out_str.append(info.name);
John Zulauf59e25072020-07-17 10:55:21 -0600150 }
John Zulauf59e25072020-07-17 10:55:21 -0600151 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700152 if (out_str.length() == 0) {
153 out_str.append("Unhandled SyncStageAccess");
154 }
John Zulauf59e25072020-07-17 10:55:21 -0600155 }
156 return out_str;
157}
158
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700159static std::string string_UsageTag(const ResourceUsageTag &tag) {
160 std::stringstream out;
161
John Zulauffaea0ee2021-01-14 14:01:32 -0700162 out << "command: " << CommandTypeString(tag.command);
163 out << ", seq_no: " << tag.seq_num;
164 if (tag.sub_command != 0) {
165 out << ", subcmd: " << tag.sub_command;
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700166 }
167 return out.str();
168}
169
John Zulauffaea0ee2021-01-14 14:01:32 -0700170std::string CommandBufferAccessContext::FormatUsage(const HazardResult &hazard) const {
John Zulauf37ceaed2020-07-03 16:18:15 -0600171 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600172 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
173 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600174 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600175 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
176 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf59e25072020-07-17 10:55:21 -0600177 out << "(usage: " << usage_info.name << ", prior_usage: " << stage_access_name;
178 if (IsHazardVsRead(hazard.hazard)) {
179 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
180 out << ", read_barriers: " << string_VkPipelineStageFlags(barriers);
181 } else {
182 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
183 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
184 }
185
John Zulauffaea0ee2021-01-14 14:01:32 -0700186 // PHASE2 TODO -- add comand buffer and reset from secondary if applicable
187 out << ", " << string_UsageTag(tag) << ", reset_no: " << reset_count_;
John Zulauf1dae9192020-06-16 15:46:44 -0600188 return out.str();
189}
190
John Zulaufd14743a2020-07-03 09:42:39 -0600191// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
192// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
193// also reflects this special case for read hazard detection (using access instead of exec scope)
John Zulaufb027cdb2020-05-21 14:25:22 -0600194static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700195static const SyncStageAccessFlags kColorAttachmentAccessScope =
196 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
197 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
198 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
199 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600200static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
201 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700202static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
203 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
204 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
205 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulauf8e3c3e92021-01-06 11:19:36 -0700206static constexpr VkPipelineStageFlags kRasterAttachmentExecScope = kDepthStencilAttachmentExecScope | kColorAttachmentExecScope;
207static const SyncStageAccessFlags kRasterAttachmentAccessScope = kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope;
John Zulaufb027cdb2020-05-21 14:25:22 -0600208
John Zulauf8e3c3e92021-01-06 11:19:36 -0700209ResourceAccessState::OrderingBarriers ResourceAccessState::kOrderingRules = {
210 {{0U, SyncStageAccessFlags()},
211 {kColorAttachmentExecScope, kColorAttachmentAccessScope},
212 {kDepthStencilAttachmentExecScope, kDepthStencilAttachmentAccessScope},
213 {kRasterAttachmentExecScope, kRasterAttachmentAccessScope}}};
214
John Zulauf7635de32020-05-29 17:14:15 -0600215// 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 -0700216static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, ResourceUsageTag::kMaxCount,
217 ResourceUsageTag::kMaxCount, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600218
John Zulaufb02c1eb2020-10-06 16:33:36 -0600219static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) {
220 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
221}
222
locke-lunarg3c038002020-04-30 23:08:08 -0600223inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
224 if (size == VK_WHOLE_SIZE) {
225 return (whole_size - offset);
226 }
227 return size;
228}
229
John Zulauf3e86bf02020-09-12 10:47:57 -0600230static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
231 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
232}
233
John Zulauf16adfc92020-04-08 10:28:33 -0600234template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600235static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600236 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
237}
238
John Zulauf355e49b2020-04-24 15:11:15 -0600239static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600240
John Zulauf3e86bf02020-09-12 10:47:57 -0600241static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
242 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
243}
244
245static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
246 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
247}
248
John Zulauf4a6105a2020-11-17 15:11:05 -0700249// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
250//
John Zulauf10f1f522020-12-18 12:00:35 -0700251// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
252//
John Zulauf4a6105a2020-11-17 15:11:05 -0700253// Usage:
254// Constructor() -- initializes the generator to point to the begin of the space declared.
255// * -- the current range of the generator empty signfies end
256// ++ -- advance to the next non-empty range (or end)
257
258// A wrapper for a single range with the same semantics as the actual generators below
259template <typename KeyType>
260class SingleRangeGenerator {
261 public:
262 SingleRangeGenerator(const KeyType &range) : current_(range) {}
John Zulaufd5115702021-01-18 12:34:33 -0700263 const KeyType &operator*() const { return current_; }
264 const KeyType *operator->() const { return &current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700265 SingleRangeGenerator &operator++() {
266 current_ = KeyType(); // just one real range
267 return *this;
268 }
269
270 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
271
272 private:
273 SingleRangeGenerator() = default;
274 const KeyType range_;
275 KeyType current_;
276};
277
278// Generate the ranges that are the intersection of range and the entries in the FilterMap
279template <typename FilterMap, typename KeyType = typename FilterMap::key_type>
280class FilteredRangeGenerator {
281 public:
John Zulaufd5115702021-01-18 12:34:33 -0700282 // Default constructed is safe to dereference for "empty" test, but for no other operation.
283 FilteredRangeGenerator() : range_(), filter_(nullptr), filter_pos_(), current_() {
284 // Default construction for KeyType *must* be empty range
285 assert(current_.empty());
286 }
John Zulauf4a6105a2020-11-17 15:11:05 -0700287 FilteredRangeGenerator(const FilterMap &filter, const KeyType &range)
288 : range_(range), filter_(&filter), filter_pos_(), current_() {
289 SeekBegin();
290 }
John Zulaufd5115702021-01-18 12:34:33 -0700291 FilteredRangeGenerator(const FilteredRangeGenerator &from) = default;
292
John Zulauf4a6105a2020-11-17 15:11:05 -0700293 const KeyType &operator*() const { return current_; }
294 const KeyType *operator->() const { return &current_; }
295 FilteredRangeGenerator &operator++() {
296 ++filter_pos_;
297 UpdateCurrent();
298 return *this;
299 }
300
301 bool operator==(const FilteredRangeGenerator &other) const { return current_ == other.current_; }
302
303 private:
John Zulauf4a6105a2020-11-17 15:11:05 -0700304 void UpdateCurrent() {
305 if (filter_pos_ != filter_->cend()) {
306 current_ = range_ & filter_pos_->first;
307 } else {
308 current_ = KeyType();
309 }
310 }
311 void SeekBegin() {
312 filter_pos_ = filter_->lower_bound(range_);
313 UpdateCurrent();
314 }
315 const KeyType range_;
316 const FilterMap *filter_;
317 typename FilterMap::const_iterator filter_pos_;
318 KeyType current_;
319};
John Zulaufd5115702021-01-18 12:34:33 -0700320using SingleAccessRangeGenerator = SingleRangeGenerator<ResourceAccessRange>;
John Zulauf4a6105a2020-11-17 15:11:05 -0700321using EventSimpleRangeGenerator = FilteredRangeGenerator<SyncEventState::ScopeMap>;
322
323// Templated to allow for different Range generators or map sources...
324
325// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulauf4a6105a2020-11-17 15:11:05 -0700326template <typename FilterMap, typename RangeGen, typename KeyType = typename FilterMap::key_type>
327class FilteredGeneratorGenerator {
328 public:
John Zulaufd5115702021-01-18 12:34:33 -0700329 // Default constructed is safe to dereference for "empty" test, but for no other operation.
330 FilteredGeneratorGenerator() : filter_(nullptr), gen_(), filter_pos_(), current_() {
331 // Default construction for KeyType *must* be empty range
332 assert(current_.empty());
333 }
334 FilteredGeneratorGenerator(const FilterMap &filter, RangeGen &gen) : filter_(&filter), gen_(gen), filter_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700335 SeekBegin();
336 }
John Zulaufd5115702021-01-18 12:34:33 -0700337 FilteredGeneratorGenerator(const FilteredGeneratorGenerator &from) = default;
John Zulauf4a6105a2020-11-17 15:11:05 -0700338 const KeyType &operator*() const { return current_; }
339 const KeyType *operator->() const { return &current_; }
340 FilteredGeneratorGenerator &operator++() {
341 KeyType gen_range = GenRange();
342 KeyType filter_range = FilterRange();
343 current_ = KeyType();
344 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
345 if (gen_range.end > filter_range.end) {
346 // if the generated range is beyond the filter_range, advance the filter range
347 filter_range = AdvanceFilter();
348 } else {
349 gen_range = AdvanceGen();
350 }
351 current_ = gen_range & filter_range;
352 }
353 return *this;
354 }
355
356 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
357
358 private:
359 KeyType AdvanceFilter() {
360 ++filter_pos_;
361 auto filter_range = FilterRange();
362 if (filter_range.valid()) {
363 FastForwardGen(filter_range);
364 }
365 return filter_range;
366 }
367 KeyType AdvanceGen() {
John Zulaufd5115702021-01-18 12:34:33 -0700368 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700369 auto gen_range = GenRange();
370 if (gen_range.valid()) {
371 FastForwardFilter(gen_range);
372 }
373 return gen_range;
374 }
375
376 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
John Zulaufd5115702021-01-18 12:34:33 -0700377 KeyType GenRange() const { return *gen_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700378
379 KeyType FastForwardFilter(const KeyType &range) {
380 auto filter_range = FilterRange();
381 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700382 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700383 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
384 if (retry_count < kRetryLimit) {
385 ++filter_pos_;
386 filter_range = FilterRange();
387 retry_count++;
388 } else {
389 // Okay we've tried walking, do a seek.
390 filter_pos_ = filter_->lower_bound(range);
391 break;
392 }
393 }
394 return FilterRange();
395 }
396
397 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
398 // faster.
399 KeyType FastForwardGen(const KeyType &range) {
400 auto gen_range = GenRange();
401 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
John Zulaufd5115702021-01-18 12:34:33 -0700402 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700403 gen_range = GenRange();
404 }
405 return gen_range;
406 }
407
408 void SeekBegin() {
409 auto gen_range = GenRange();
410 if (gen_range.empty()) {
411 current_ = KeyType();
412 filter_pos_ = filter_->cend();
413 } else {
414 filter_pos_ = filter_->lower_bound(gen_range);
415 current_ = gen_range & FilterRange();
416 }
417 }
418
John Zulauf4a6105a2020-11-17 15:11:05 -0700419 const FilterMap *filter_;
John Zulaufd5115702021-01-18 12:34:33 -0700420 RangeGen gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700421 typename FilterMap::const_iterator filter_pos_;
422 KeyType current_;
423};
424
425using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
426
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700427static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700428
John Zulauf3e86bf02020-09-12 10:47:57 -0600429ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
430 VkDeviceSize stride) {
431 VkDeviceSize range_start = offset + first_index * stride;
432 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600433 if (count == UINT32_MAX) {
434 range_size = buf_whole_size - range_start;
435 } else {
436 range_size = count * stride;
437 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600438 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600439}
440
locke-lunarg654e3692020-06-04 17:19:15 -0600441SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
442 VkShaderStageFlagBits stage_flag) {
443 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
444 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
445 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
446 }
447 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
448 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
449 assert(0);
450 }
451 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
452 return stage_access->second.uniform_read;
453 }
454
455 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
456 // Because if write hazard happens, read hazard might or might not happen.
457 // But if write hazard doesn't happen, read hazard is impossible to happen.
458 if (descriptor_data.is_writable) {
459 return stage_access->second.shader_write;
460 }
461 return stage_access->second.shader_read;
462}
463
locke-lunarg37047832020-06-12 13:44:45 -0600464bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
465 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
466 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
467 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
468 ? true
469 : false;
470}
471
472bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
473 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
474 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
475 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
476 ? true
477 : false;
478}
479
John Zulauf355e49b2020-04-24 15:11:15 -0600480// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600481template <typename Action>
482static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
483 Action &action) {
484 // At this point the "apply over range" logic only supports a single memory binding
485 if (!SimpleBinding(image_state)) return;
486 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600487 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700488 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
489 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600490 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700491 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600492 }
493}
494
John Zulauf7635de32020-05-29 17:14:15 -0600495// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
496// Used by both validation and record operations
497//
498// The signature for Action() reflect the needs of both uses.
499template <typename Action>
500void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
501 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
502 VkExtent3D extent = CastTo3D(render_area.extent);
503 VkOffset3D offset = CastTo3D(render_area.offset);
504 const auto &rp_ci = rp_state.createInfo;
505 const auto *attachment_ci = rp_ci.pAttachments;
506 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
507
508 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
509 const auto *color_attachments = subpass_ci.pColorAttachments;
510 const auto *color_resolve = subpass_ci.pResolveAttachments;
511 if (color_resolve && color_attachments) {
512 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
513 const auto &color_attach = color_attachments[i].attachment;
514 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
515 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
516 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulauf8e3c3e92021-01-06 11:19:36 -0700517 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kColorAttachment, offset, extent, 0);
John Zulauf7635de32020-05-29 17:14:15 -0600518 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulauf8e3c3e92021-01-06 11:19:36 -0700519 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment, offset, extent, 0);
John Zulauf7635de32020-05-29 17:14:15 -0600520 }
521 }
522 }
523
524 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700525 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600526 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
527 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
528 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
529 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
530 const auto src_ci = attachment_ci[src_at];
531 // The formats are required to match so we can pick either
532 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
533 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
534 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
535 VkImageAspectFlags aspect_mask = 0u;
536
537 // Figure out which aspects are actually touched during resolve operations
538 const char *aspect_string = nullptr;
539 if (resolve_depth && resolve_stencil) {
540 // Validate all aspects together
541 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
542 aspect_string = "depth/stencil";
543 } else if (resolve_depth) {
544 // Validate depth only
545 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
546 aspect_string = "depth";
547 } else if (resolve_stencil) {
548 // Validate all stencil only
549 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
550 aspect_string = "stencil";
551 }
552
553 if (aspect_mask) {
554 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
John Zulauf8e3c3e92021-01-06 11:19:36 -0700555 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster, offset, extent, aspect_mask);
John Zulauf7635de32020-05-29 17:14:15 -0600556 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
John Zulauf8e3c3e92021-01-06 11:19:36 -0700557 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, offset, extent, aspect_mask);
John Zulauf7635de32020-05-29 17:14:15 -0600558 }
559 }
560}
561
562// Action for validating resolve operations
563class ValidateResolveAction {
564 public:
John Zulauffaea0ee2021-01-14 14:01:32 -0700565 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context,
566 const CommandBufferAccessContext &cb_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600567 : render_pass_(render_pass),
568 subpass_(subpass),
569 context_(context),
John Zulauffaea0ee2021-01-14 14:01:32 -0700570 cb_context_(cb_context),
John Zulauf7635de32020-05-29 17:14:15 -0600571 func_name_(func_name),
572 skip_(false) {}
573 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
John Zulauf8e3c3e92021-01-06 11:19:36 -0700574 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf7635de32020-05-29 17:14:15 -0600575 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
576 HazardResult hazard;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700577 hazard = context_.DetectHazard(view, current_usage, ordering_rule, offset, extent, aspect_mask);
John Zulauf7635de32020-05-29 17:14:15 -0600578 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -0700579 skip_ |=
580 cb_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
581 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
582 " to resolve attachment %" PRIu32 ". Access info %s.",
583 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name,
584 attachment_name, src_at, dst_at, cb_context_.FormatUsage(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600585 }
586 }
587 // Providing a mechanism for the constructing caller to get the result of the validation
588 bool GetSkip() const { return skip_; }
589
590 private:
591 VkRenderPass render_pass_;
592 const uint32_t subpass_;
593 const AccessContext &context_;
John Zulauffaea0ee2021-01-14 14:01:32 -0700594 const CommandBufferAccessContext &cb_context_;
John Zulauf7635de32020-05-29 17:14:15 -0600595 const char *func_name_;
596 bool skip_;
597};
598
599// Update action for resolve operations
600class UpdateStateResolveAction {
601 public:
602 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
603 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
John Zulauf8e3c3e92021-01-06 11:19:36 -0700604 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf7635de32020-05-29 17:14:15 -0600605 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
606 // Ignores validation only arguments...
John Zulauf8e3c3e92021-01-06 11:19:36 -0700607 context_.UpdateAccessState(view, current_usage, ordering_rule, offset, extent, aspect_mask, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600608 }
609
610 private:
611 AccessContext &context_;
612 const ResourceUsageTag &tag_;
613};
614
John Zulauf59e25072020-07-17 10:55:21 -0600615void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700616 const SyncStageAccessFlags &prior_, const ResourceUsageTag &tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600617 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
618 usage_index = usage_index_;
619 hazard = hazard_;
620 prior_access = prior_;
621 tag = tag_;
622}
623
John Zulauf540266b2020-04-06 18:54:53 -0600624AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
625 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600626 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600627 Reset();
628 const auto &subpass_dep = dependencies[subpass];
629 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600630 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600631 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600632 const auto prev_pass = prev_dep.first->pass;
633 const auto &prev_barriers = prev_dep.second;
634 assert(prev_dep.second.size());
635 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
636 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700637 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600638
639 async_.reserve(subpass_dep.async.size());
640 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700641 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600642 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600643 if (subpass_dep.barrier_from_external.size()) {
644 src_external_ = TrackBack(external_context, queue_flags, subpass_dep.barrier_from_external);
John Zulaufe5da6e52020-03-18 15:32:18 -0600645 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600646 if (subpass_dep.barrier_to_external.size()) {
647 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600648 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700649}
650
John Zulauf5f13a792020-03-10 07:31:21 -0600651template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700652HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600653 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600654 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600655 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600656
657 HazardResult hazard;
658 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
659 hazard = detector.Detect(prev);
660 }
661 return hazard;
662}
663
John Zulauf4a6105a2020-11-17 15:11:05 -0700664template <typename Action>
665void AccessContext::ForAll(Action &&action) {
666 for (const auto address_type : kAddressTypes) {
667 auto &accesses = GetAccessStateMap(address_type);
668 for (const auto &access : accesses) {
669 action(address_type, access);
670 }
671 }
672}
673
John Zulauf3d84f1b2020-03-09 13:33:25 -0600674// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
675// the DAG of the contexts (for example subpasses)
676template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700677HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600678 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600679 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600680
John Zulauf1a224292020-06-30 14:52:13 -0600681 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600682 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
683 // so we'll check these first
684 for (const auto &async_context : async_) {
685 hazard = async_context->DetectAsyncHazard(type, detector, range);
686 if (hazard.hazard) return hazard;
687 }
John Zulauf5f13a792020-03-10 07:31:21 -0600688 }
689
John Zulauf1a224292020-06-30 14:52:13 -0600690 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600691
John Zulauf69133422020-05-20 14:55:53 -0600692 const auto &accesses = GetAccessStateMap(type);
693 const auto from = accesses.lower_bound(range);
694 const auto to = accesses.upper_bound(range);
695 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600696
John Zulauf69133422020-05-20 14:55:53 -0600697 for (auto pos = from; pos != to; ++pos) {
698 // Cover any leading gap, or gap between entries
699 if (detect_prev) {
700 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
701 // Cover any leading gap, or gap between entries
702 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600703 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600704 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600705 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600706 if (hazard.hazard) return hazard;
707 }
John Zulauf69133422020-05-20 14:55:53 -0600708 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
709 gap.begin = pos->first.end;
710 }
711
712 hazard = detector.Detect(pos);
713 if (hazard.hazard) return hazard;
714 }
715
716 if (detect_prev) {
717 // Detect in the trailing empty as needed
718 gap.end = range.end;
719 if (gap.non_empty()) {
720 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600721 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600722 }
723
724 return hazard;
725}
726
727// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
728template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700729HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
730 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600731 auto &accesses = GetAccessStateMap(type);
732 const auto from = accesses.lower_bound(range);
733 const auto to = accesses.upper_bound(range);
734
John Zulauf3d84f1b2020-03-09 13:33:25 -0600735 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600736 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700737 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600738 }
John Zulauf16adfc92020-04-08 10:28:33 -0600739
John Zulauf3d84f1b2020-03-09 13:33:25 -0600740 return hazard;
741}
742
John Zulaufb02c1eb2020-10-06 16:33:36 -0600743struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700744 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600745 void operator()(ResourceAccessState *access) const {
746 assert(access);
747 access->ApplyBarriers(barriers, true);
748 }
749 const std::vector<SyncBarrier> &barriers;
750};
751
752struct ApplyTrackbackBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700753 explicit ApplyTrackbackBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600754 void operator()(ResourceAccessState *access) const {
755 assert(access);
756 assert(!access->HasPendingState());
757 access->ApplyBarriers(barriers, false);
758 access->ApplyPendingBarriers(kCurrentCommandTag);
759 }
760 const std::vector<SyncBarrier> &barriers;
761};
762
763// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
764// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
765// *different* map from dest.
766// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
767// range [first, last)
768template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600769static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
770 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600771 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600772 auto at = entry;
773 for (auto pos = first; pos != last; ++pos) {
774 // Every member of the input iterator range must fit within the remaining portion of entry
775 assert(at->first.includes(pos->first));
776 assert(at != dest->end());
777 // Trim up at to the same size as the entry to resolve
778 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600779 auto access = pos->second; // intentional copy
780 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600781 at->second.Resolve(access);
782 ++at; // Go to the remaining unused section of entry
783 }
784}
785
John Zulaufa0a98292020-09-18 09:30:10 -0600786static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
787 SyncBarrier merged = {};
788 for (const auto &barrier : barriers) {
789 merged.Merge(barrier);
790 }
791 return merged;
792}
793
John Zulaufb02c1eb2020-10-06 16:33:36 -0600794template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700795void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600796 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
797 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600798 if (!range.non_empty()) return;
799
John Zulauf355e49b2020-04-24 15:11:15 -0600800 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
801 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600802 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600803 if (current->pos_B->valid) {
804 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600805 auto access = src_pos->second; // intentional copy
806 barrier_action(&access);
807
John Zulauf16adfc92020-04-08 10:28:33 -0600808 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600809 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
810 trimmed->second.Resolve(access);
811 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600812 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600813 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600814 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600815 }
John Zulauf16adfc92020-04-08 10:28:33 -0600816 } else {
817 // we have to descend to fill this gap
818 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600819 if (current->pos_A->valid) {
820 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
821 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600822 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600823 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -0600824 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600825 // There isn't anything in dest in current)range, so we can accumulate directly into it.
826 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600827 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
828 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
829 barrier_action(&pos->second);
John Zulauf355e49b2020-04-24 15:11:15 -0600830 }
831 }
832 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
833 // iterator of the outer while.
834
835 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
836 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
837 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600838 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
839 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600840 current.seek(seek_to);
841 } else if (!current->pos_A->valid && infill_state) {
842 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
843 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
844 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600845 }
John Zulauf5f13a792020-03-10 07:31:21 -0600846 }
John Zulauf16adfc92020-04-08 10:28:33 -0600847 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600848 }
John Zulauf1a224292020-06-30 14:52:13 -0600849
850 // Infill if range goes passed both the current and resolve map prior contents
851 if (recur_to_infill && (current->range.end < range.end)) {
852 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
853 ResourceAccessRangeMap gap_map;
854 const auto the_end = resolve_map->end();
855 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
856 for (auto &access : gap_map) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600857 barrier_action(&access.second);
John Zulauf1a224292020-06-30 14:52:13 -0600858 resolve_map->insert(the_end, access);
859 }
860 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600861}
862
John Zulauf43cc7462020-12-03 12:33:12 -0700863void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
864 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600865 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600866 if (range.non_empty() && infill_state) {
867 descent_map->insert(std::make_pair(range, *infill_state));
868 }
869 } else {
870 // Look for something to fill the gap further along.
871 for (const auto &prev_dep : prev_) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600872 const ApplyTrackbackBarriersAction barrier_action(prev_dep.barriers);
873 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600874 }
875
John Zulaufe5da6e52020-03-18 15:32:18 -0600876 if (src_external_.context) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600877 const ApplyTrackbackBarriersAction barrier_action(src_external_.barriers);
878 src_external_.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600879 }
880 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600881}
882
John Zulauf4a6105a2020-11-17 15:11:05 -0700883// Non-lazy import of all accesses, WaitEvents needs this.
884void AccessContext::ResolvePreviousAccesses() {
885 ResourceAccessState default_state;
886 for (const auto address_type : kAddressTypes) {
887 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
888 }
889}
890
John Zulauf43cc7462020-12-03 12:33:12 -0700891AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
892 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -0600893}
894
John Zulauf1507ee42020-05-18 11:33:09 -0600895static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
896 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
897 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
898 return stage_access;
899}
900static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
901 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
902 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
903 return stage_access;
904}
905
John Zulauf7635de32020-05-29 17:14:15 -0600906// Caller must manage returned pointer
907static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
908 uint32_t subpass, const VkRect2D &render_area,
909 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
910 auto *proxy = new AccessContext(context);
911 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600912 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600913 return proxy;
914}
915
John Zulaufb02c1eb2020-10-06 16:33:36 -0600916template <typename BarrierAction>
John Zulauf52446eb2020-10-22 16:40:08 -0600917class ResolveAccessRangeFunctor {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600918 public:
John Zulauf43cc7462020-12-03 12:33:12 -0700919 ResolveAccessRangeFunctor(const AccessContext &context, AccessAddressType address_type, ResourceAccessRangeMap *descent_map,
920 const ResourceAccessState *infill_state, BarrierAction &barrier_action)
John Zulauf52446eb2020-10-22 16:40:08 -0600921 : context_(context),
922 address_type_(address_type),
923 descent_map_(descent_map),
924 infill_state_(infill_state),
925 barrier_action_(barrier_action) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600926 ResolveAccessRangeFunctor() = delete;
927 void operator()(const ResourceAccessRange &range) const {
928 context_.ResolveAccessRange(address_type_, range, barrier_action_, descent_map_, infill_state_);
929 }
930
931 private:
John Zulauf52446eb2020-10-22 16:40:08 -0600932 const AccessContext &context_;
John Zulauf43cc7462020-12-03 12:33:12 -0700933 const AccessAddressType address_type_;
John Zulauf52446eb2020-10-22 16:40:08 -0600934 ResourceAccessRangeMap *const descent_map_;
935 const ResourceAccessState *infill_state_;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600936 BarrierAction &barrier_action_;
937};
938
John Zulaufb02c1eb2020-10-06 16:33:36 -0600939template <typename BarrierAction>
940void AccessContext::ResolveAccessRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -0700941 BarrierAction &barrier_action, AccessAddressType address_type,
942 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600943 const ResolveAccessRangeFunctor<BarrierAction> action(*this, address_type, descent_map, infill_state, barrier_action);
944 ApplyOverImageRange(image_state, subresource_range, action);
John Zulauf62f10592020-04-03 12:20:02 -0600945}
946
John Zulauf7635de32020-05-29 17:14:15 -0600947// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauffaea0ee2021-01-14 14:01:32 -0700948bool AccessContext::ValidateLayoutTransitions(const CommandBufferAccessContext &cb_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600949 const VkRect2D &render_area, uint32_t subpass,
950 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
951 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600952 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600953 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
954 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
955 // those affects have not been recorded yet.
956 //
957 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
958 // to apply and only copy then, if this proves a hot spot.
959 std::unique_ptr<AccessContext> proxy_for_prev;
960 TrackBack proxy_track_back;
961
John Zulauf355e49b2020-04-24 15:11:15 -0600962 const auto &transitions = rp_state.subpass_transitions[subpass];
963 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600964 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
965
966 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
967 if (prev_needs_proxy) {
968 if (!proxy_for_prev) {
969 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
970 render_area, attachment_views));
971 proxy_track_back = *track_back;
972 proxy_track_back.context = proxy_for_prev.get();
973 }
974 track_back = &proxy_track_back;
975 }
976 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600977 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -0700978 skip |= cb_context.GetSyncState().LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
979 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
980 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
981 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
982 string_VkImageLayout(transition.old_layout),
983 string_VkImageLayout(transition.new_layout),
984 cb_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600985 }
986 }
987 return skip;
988}
989
John Zulauffaea0ee2021-01-14 14:01:32 -0700990bool AccessContext::ValidateLoadOperation(const CommandBufferAccessContext &cb_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600991 const VkRect2D &render_area, uint32_t subpass,
992 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
993 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600994 bool skip = false;
995 const auto *attachment_ci = rp_state.createInfo.pAttachments;
996 VkExtent3D extent = CastTo3D(render_area.extent);
997 VkOffset3D offset = CastTo3D(render_area.offset);
John Zulaufa0a98292020-09-18 09:30:10 -0600998
John Zulauf1507ee42020-05-18 11:33:09 -0600999 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1000 if (subpass == rp_state.attachment_first_subpass[i]) {
1001 if (attachment_views[i] == nullptr) continue;
1002 const IMAGE_VIEW_STATE &view = *attachment_views[i];
1003 const IMAGE_STATE *image = view.image_state.get();
1004 if (image == nullptr) continue;
1005 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001006
1007 // Need check in the following way
1008 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1009 // vs. transition
1010 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1011 // for each aspect loaded.
1012
1013 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001014 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001015 const bool is_color = !(has_depth || has_stencil);
1016
1017 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001018 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001019
John Zulaufaff20662020-06-01 14:07:58 -06001020 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001021 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001022
John Zulaufb02c1eb2020-10-06 16:33:36 -06001023 auto hazard_range = view.normalized_subresource_range;
1024 bool checked_stencil = false;
1025 if (is_color) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001026 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, SyncOrdering::kColorAttachment, offset,
John Zulauf859089b2020-10-29 17:37:03 -06001027 extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001028 aspect = "color";
1029 } else {
1030 if (has_depth) {
1031 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001032 hazard = DetectHazard(*image, load_index, hazard_range, SyncOrdering::kDepthStencilAttachment, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001033 aspect = "depth";
1034 }
1035 if (!hazard.hazard && has_stencil) {
1036 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001037 hazard = DetectHazard(*image, stencil_load_index, hazard_range, SyncOrdering::kDepthStencilAttachment, offset,
1038 extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001039 aspect = "stencil";
1040 checked_stencil = true;
1041 }
1042 }
1043
1044 if (hazard.hazard) {
1045 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
John Zulauffaea0ee2021-01-14 14:01:32 -07001046 const auto &sync_state = cb_context.GetSyncState();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001047 if (hazard.tag == kCurrentCommandTag) {
1048 // Hazard vs. ILT
1049 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1050 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1051 " aspect %s during load with loadOp %s.",
1052 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1053 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06001054 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1055 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001056 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001057 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauffaea0ee2021-01-14 14:01:32 -07001058 cb_context.FormatUsage(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001059 }
1060 }
1061 }
1062 }
1063 return skip;
1064}
1065
John Zulaufaff20662020-06-01 14:07:58 -06001066// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1067// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1068// store is part of the same Next/End operation.
1069// The latter is handled in layout transistion validation directly
John Zulauffaea0ee2021-01-14 14:01:32 -07001070bool AccessContext::ValidateStoreOperation(const CommandBufferAccessContext &cb_context, const RENDER_PASS_STATE &rp_state,
John Zulaufaff20662020-06-01 14:07:58 -06001071 const VkRect2D &render_area, uint32_t subpass,
1072 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1073 const char *func_name) const {
1074 bool skip = false;
1075 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1076 VkExtent3D extent = CastTo3D(render_area.extent);
1077 VkOffset3D offset = CastTo3D(render_area.offset);
1078
1079 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1080 if (subpass == rp_state.attachment_last_subpass[i]) {
1081 if (attachment_views[i] == nullptr) continue;
1082 const IMAGE_VIEW_STATE &view = *attachment_views[i];
1083 const IMAGE_STATE *image = view.image_state.get();
1084 if (image == nullptr) continue;
1085 const auto &ci = attachment_ci[i];
1086
1087 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1088 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1089 // sake, we treat DONT_CARE as writing.
1090 const bool has_depth = FormatHasDepth(ci.format);
1091 const bool has_stencil = FormatHasStencil(ci.format);
1092 const bool is_color = !(has_depth || has_stencil);
1093 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1094 if (!has_stencil && !store_op_stores) continue;
1095
1096 HazardResult hazard;
1097 const char *aspect = nullptr;
1098 bool checked_stencil = false;
1099 if (is_color) {
1100 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001101 view.normalized_subresource_range, SyncOrdering::kRaster, offset, extent);
John Zulaufaff20662020-06-01 14:07:58 -06001102 aspect = "color";
1103 } else {
1104 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1105 auto hazard_range = view.normalized_subresource_range;
1106 if (has_depth && store_op_stores) {
1107 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1108 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001109 SyncOrdering::kRaster, offset, extent);
John Zulaufaff20662020-06-01 14:07:58 -06001110 aspect = "depth";
1111 }
1112 if (!hazard.hazard && has_stencil && stencil_op_stores) {
1113 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1114 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001115 SyncOrdering::kRaster, offset, extent);
John Zulaufaff20662020-06-01 14:07:58 -06001116 aspect = "stencil";
1117 checked_stencil = true;
1118 }
1119 }
1120
1121 if (hazard.hazard) {
1122 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1123 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauffaea0ee2021-01-14 14:01:32 -07001124 skip |= cb_context.GetSyncState().LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1125 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1126 " %s aspect during store with %s %s. Access info %s",
1127 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect,
1128 op_type_string, store_op_string, cb_context.FormatUsage(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001129 }
1130 }
1131 }
1132 return skip;
1133}
1134
John Zulauffaea0ee2021-01-14 14:01:32 -07001135bool AccessContext::ValidateResolveOperations(const CommandBufferAccessContext &cb_context, const RENDER_PASS_STATE &rp_state,
John Zulaufb027cdb2020-05-21 14:25:22 -06001136 const VkRect2D &render_area,
1137 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
1138 uint32_t subpass) const {
John Zulauffaea0ee2021-01-14 14:01:32 -07001139 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, cb_context, func_name);
John Zulauf7635de32020-05-29 17:14:15 -06001140 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
1141 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>
1178HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1179 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1180 const VkExtent3D &extent, DetectOptions options) const {
1181 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001182 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001183 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1184 base_address);
1185 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001186 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001187 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001188 if (hazard.hazard) return hazard;
1189 }
1190 return HazardResult();
1191}
1192
John Zulauf540266b2020-04-06 18:54:53 -06001193HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1194 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1195 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001196 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1197 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -06001198 return DetectHazard(image, current_usage, subresource_range, offset, extent);
1199}
1200
1201HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1202 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1203 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001204 HazardDetector detector(current_usage);
1205 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1206}
1207
1208HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001209 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001210 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001211 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001212 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001213}
1214
John Zulaufb027cdb2020-05-21 14:25:22 -06001215// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1216// should have reported the issue regarding an invalid attachment entry
1217HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001218 SyncOrdering ordering_rule, const VkOffset3D &offset, const VkExtent3D &extent,
John Zulaufb027cdb2020-05-21 14:25:22 -06001219 VkImageAspectFlags aspect_mask) const {
1220 if (view != nullptr) {
1221 const IMAGE_STATE *image = view->image_state.get();
1222 if (image != nullptr) {
1223 auto *detect_range = &view->normalized_subresource_range;
1224 VkImageSubresourceRange masked_range;
1225 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1226 masked_range = view->normalized_subresource_range;
1227 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1228 detect_range = &masked_range;
1229 }
1230
1231 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1232 if (detect_range->aspectMask) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001233 return DetectHazard(*image, current_usage, *detect_range, ordering_rule, offset, extent);
John Zulaufb027cdb2020-05-21 14:25:22 -06001234 }
1235 }
1236 }
1237 return HazardResult();
1238}
John Zulauf43cc7462020-12-03 12:33:12 -07001239
John Zulauf3d84f1b2020-03-09 13:33:25 -06001240class BarrierHazardDetector {
1241 public:
1242 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1243 SyncStageAccessFlags src_access_scope)
1244 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1245
John Zulauf5f13a792020-03-10 07:31:21 -06001246 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1247 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001248 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001249 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001250 // 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 -07001251 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001252 }
1253
1254 private:
1255 SyncStageAccessIndex usage_index_;
1256 VkPipelineStageFlags src_exec_scope_;
1257 SyncStageAccessFlags src_access_scope_;
1258};
1259
John Zulauf4a6105a2020-11-17 15:11:05 -07001260class EventBarrierHazardDetector {
1261 public:
1262 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1263 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
1264 const ResourceUsageTag &scope_tag)
1265 : usage_index_(usage_index),
1266 src_exec_scope_(src_exec_scope),
1267 src_access_scope_(src_access_scope),
1268 event_scope_(event_scope),
1269 scope_pos_(event_scope.cbegin()),
1270 scope_end_(event_scope.cend()),
1271 scope_tag_(scope_tag) {}
1272
1273 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1274 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1275 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1276 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1277 if (scope_pos_ == scope_end_) return HazardResult();
1278 if (!scope_pos_->first.intersects(pos->first)) {
1279 event_scope_.lower_bound(pos->first);
1280 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1281 }
1282
1283 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1284 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1285 }
1286 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1287 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1288 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1289 }
1290
1291 private:
1292 SyncStageAccessIndex usage_index_;
1293 VkPipelineStageFlags src_exec_scope_;
1294 SyncStageAccessFlags src_access_scope_;
1295 const SyncEventState::ScopeMap &event_scope_;
1296 SyncEventState::ScopeMap::const_iterator scope_pos_;
1297 SyncEventState::ScopeMap::const_iterator scope_end_;
1298 const ResourceUsageTag &scope_tag_;
1299};
1300
1301HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1302 const SyncStageAccessFlags &src_access_scope,
1303 const VkImageSubresourceRange &subresource_range,
1304 const SyncEventState &sync_event, DetectOptions options) const {
1305 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1306 // first access scope map to use, and there's no easy way to plumb it in below.
1307 const auto address_type = ImageAddressType(image);
1308 const auto &event_scope = sync_event.FirstScope(address_type);
1309
1310 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1311 event_scope, sync_event.first_scope_tag);
1312 VkOffset3D zero_offset = {0, 0, 0};
1313 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
1314}
1315
John Zulauf16adfc92020-04-08 10:28:33 -06001316HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001317 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001318 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001319 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001320 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1321 VkOffset3D zero_offset = {0, 0, 0};
1322 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001323}
1324
John Zulauf355e49b2020-04-24 15:11:15 -06001325HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001326 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001327 const VkImageMemoryBarrier &barrier) const {
1328 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1329 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1330 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1331}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001332HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
1333 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope,
John Zulaufd5115702021-01-18 12:34:33 -07001334 image_barrier.barrier.src_access_scope, image_barrier.range.subresource_range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001335}
John Zulauf355e49b2020-04-24 15:11:15 -06001336
John Zulauf9cb530d2019-09-30 14:14:10 -06001337template <typename Flags, typename Map>
1338SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1339 SyncStageAccessFlags scope = 0;
1340 for (const auto &bit_scope : map) {
1341 if (flag_mask < bit_scope.first) break;
1342
1343 if (flag_mask & bit_scope.first) {
1344 scope |= bit_scope.second;
1345 }
1346 }
1347 return scope;
1348}
1349
1350SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1351 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1352}
1353
1354SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1355 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1356}
1357
1358// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1359SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001360 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1361 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1362 // 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 -06001363 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1364}
1365
1366template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001367void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001368 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1369 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001370 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001371 auto pos = accesses->lower_bound(range);
1372 if (pos == accesses->end() || !pos->first.intersects(range)) {
1373 // The range is empty, fill it with a default value.
1374 pos = action.Infill(accesses, pos, range);
1375 } else if (range.begin < pos->first.begin) {
1376 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001377 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001378 } else if (pos->first.begin < range.begin) {
1379 // Trim the beginning if needed
1380 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1381 ++pos;
1382 }
1383
1384 const auto the_end = accesses->end();
1385 while ((pos != the_end) && pos->first.intersects(range)) {
1386 if (pos->first.end > range.end) {
1387 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1388 }
1389
1390 pos = action(accesses, pos);
1391 if (pos == the_end) break;
1392
1393 auto next = pos;
1394 ++next;
1395 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1396 // Need to infill if next is disjoint
1397 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001398 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001399 next = action.Infill(accesses, next, new_range);
1400 }
1401 pos = next;
1402 }
1403}
John Zulaufd5115702021-01-18 12:34:33 -07001404
1405// Give a comparable interface for range generators and ranges
1406template <typename Action>
1407inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1408 assert(range);
1409 UpdateMemoryAccessState(accesses, *range, action);
1410}
1411
John Zulauf4a6105a2020-11-17 15:11:05 -07001412template <typename Action, typename RangeGen>
1413void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1414 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001415 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 -07001416 for (; range_gen->non_empty(); ++range_gen) {
1417 UpdateMemoryAccessState(accesses, *range_gen, action);
1418 }
1419}
John Zulauf9cb530d2019-09-30 14:14:10 -06001420
1421struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001422 using Iterator = ResourceAccessRangeMap::iterator;
1423 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001424 // this is only called on gaps, and never returns a gap.
1425 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001426 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001427 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001428 }
John Zulauf5f13a792020-03-10 07:31:21 -06001429
John Zulauf5c5e88d2019-12-26 11:22:02 -07001430 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001431 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001432 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001433 return pos;
1434 }
1435
John Zulauf43cc7462020-12-03 12:33:12 -07001436 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001437 SyncOrdering ordering_rule_, const ResourceUsageTag &tag_)
1438 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001439 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001440 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001441 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001442 const SyncOrdering ordering_rule;
John Zulauf9cb530d2019-09-30 14:14:10 -06001443 const ResourceUsageTag &tag;
1444};
1445
John Zulauf4a6105a2020-11-17 15:11:05 -07001446// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001447struct PipelineBarrierOp {
1448 SyncBarrier barrier;
1449 bool layout_transition;
1450 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1451 : barrier(barrier_), layout_transition(layout_transition_) {}
1452 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001453 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001454 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1455};
John Zulauf4a6105a2020-11-17 15:11:05 -07001456// The barrier operation for wait events
1457struct WaitEventBarrierOp {
1458 const ResourceUsageTag *scope_tag;
1459 SyncBarrier barrier;
1460 bool layout_transition;
1461 WaitEventBarrierOp(const ResourceUsageTag &scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1462 : scope_tag(&scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
1463 WaitEventBarrierOp() = default;
1464 void operator()(ResourceAccessState *access_state) const {
1465 assert(scope_tag); // Not valid to have a non-scope op executed, default construct included for std::vector support
1466 access_state->ApplyBarrier(*scope_tag, barrier, layout_transition);
1467 }
1468};
John Zulauf1e331ec2020-12-04 18:29:38 -07001469
John Zulauf4a6105a2020-11-17 15:11:05 -07001470// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1471// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1472// of a collection is known/present.
John Zulauf1e331ec2020-12-04 18:29:38 -07001473template <typename BarrierOp>
John Zulauf89311b42020-09-29 16:28:47 -06001474class ApplyBarrierOpsFunctor {
1475 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001476 using Iterator = ResourceAccessRangeMap::iterator;
1477 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001478
John Zulauf5c5e88d2019-12-26 11:22:02 -07001479 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001480 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001481 for (const auto &op : barrier_ops_) {
1482 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001483 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001484
John Zulauf89311b42020-09-29 16:28:47 -06001485 if (resolve_) {
1486 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1487 // another walk
1488 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001489 }
1490 return pos;
1491 }
1492
John Zulauf89311b42020-09-29 16:28:47 -06001493 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulaufd5115702021-01-18 12:34:33 -07001494 ApplyBarrierOpsFunctor(bool resolve, size_t size_hint, const ResourceUsageTag &tag)
1495 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1496 barrier_ops_.reserve(size_hint);
1497 }
1498 void EmplaceBack(const BarrierOp &op) { barrier_ops_.emplace_back(op); }
John Zulauf89311b42020-09-29 16:28:47 -06001499
1500 private:
1501 bool resolve_;
John Zulaufd5115702021-01-18 12:34:33 -07001502 std::vector<BarrierOp> barrier_ops_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001503 const ResourceUsageTag &tag_;
1504};
1505
John Zulauf4a6105a2020-11-17 15:11:05 -07001506// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1507// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1508template <typename BarrierOp>
1509class ApplyBarrierFunctor {
1510 public:
1511 using Iterator = ResourceAccessRangeMap::iterator;
1512 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1513
1514 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1515 auto &access_state = pos->second;
1516 barrier_op_(&access_state);
1517 return pos;
1518 }
1519
1520 ApplyBarrierFunctor(const BarrierOp &barrier_op) : barrier_op_(barrier_op) {}
1521
1522 private:
John Zulaufd5115702021-01-18 12:34:33 -07001523 BarrierOp barrier_op_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001524};
1525
John Zulauf1e331ec2020-12-04 18:29:38 -07001526// This functor resolves the pendinging state.
1527class ResolvePendingBarrierFunctor {
1528 public:
1529 using Iterator = ResourceAccessRangeMap::iterator;
1530 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1531
1532 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1533 auto &access_state = pos->second;
1534 access_state.ApplyPendingBarriers(tag_);
1535 return pos;
1536 }
1537
1538 ResolvePendingBarrierFunctor(const ResourceUsageTag &tag) : tag_(tag) {}
1539
1540 private:
John Zulauf89311b42020-09-29 16:28:47 -06001541 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001542};
1543
John Zulauf8e3c3e92021-01-06 11:19:36 -07001544void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
1545 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
1546 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001547 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001548}
1549
John Zulauf8e3c3e92021-01-06 11:19:36 -07001550void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001551 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001552 if (!SimpleBinding(buffer)) return;
1553 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001554 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001555}
John Zulauf355e49b2020-04-24 15:11:15 -06001556
John Zulauf8e3c3e92021-01-06 11:19:36 -07001557void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001558 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001559 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001560 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001561 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001562 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1563 base_address);
1564 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001565 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001566 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001567 UpdateMemoryAccessState(&GetAccessStateMap(address_type), *range_gen, action);
John Zulauf5f13a792020-03-10 07:31:21 -06001568 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001569}
John Zulauf8e3c3e92021-01-06 11:19:36 -07001570void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
1571 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask,
1572 const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001573 if (view != nullptr) {
1574 const IMAGE_STATE *image = view->image_state.get();
1575 if (image != nullptr) {
1576 auto *update_range = &view->normalized_subresource_range;
1577 VkImageSubresourceRange masked_range;
1578 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1579 masked_range = view->normalized_subresource_range;
1580 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1581 update_range = &masked_range;
1582 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001583 UpdateAccessState(*image, current_usage, ordering_rule, *update_range, offset, extent, tag);
John Zulauf7635de32020-05-29 17:14:15 -06001584 }
1585 }
1586}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001587
John Zulauf8e3c3e92021-01-06 11:19:36 -07001588void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001589 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1590 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001591 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1592 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001593 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001594}
1595
John Zulauf540266b2020-04-06 18:54:53 -06001596template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001597void AccessContext::UpdateResourceAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001598 if (!SimpleBinding(buffer)) return;
1599 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf43cc7462020-12-03 12:33:12 -07001600 UpdateMemoryAccessState(&GetAccessStateMap(AccessAddressType::kLinear), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001601}
1602
1603template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001604void AccessContext::UpdateResourceAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1605 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001606 if (!SimpleBinding(image)) return;
1607 const auto address_type = ImageAddressType(image);
1608 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001609
John Zulauf16adfc92020-04-08 10:28:33 -06001610 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001611 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
1612 image.createInfo.extent, base_address);
1613
John Zulauf540266b2020-04-06 18:54:53 -06001614 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001615 UpdateMemoryAccessState(accesses, *range_gen, action);
John Zulauf540266b2020-04-06 18:54:53 -06001616 }
1617}
1618
John Zulauf7635de32020-05-29 17:14:15 -06001619void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1620 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1621 const ResourceUsageTag &tag) {
1622 UpdateStateResolveAction update(*this, tag);
1623 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1624}
1625
John Zulaufaff20662020-06-01 14:07:58 -06001626void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1627 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1628 const ResourceUsageTag &tag) {
1629 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1630 VkExtent3D extent = CastTo3D(render_area.extent);
1631 VkOffset3D offset = CastTo3D(render_area.offset);
1632
1633 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1634 if (rp_state.attachment_last_subpass[i] == subpass) {
1635 if (attachment_views[i] == nullptr) continue; // UNUSED
1636 const auto &view = *attachment_views[i];
1637 const IMAGE_STATE *image = view.image_state.get();
1638 if (image == nullptr) continue;
1639
1640 const auto &ci = attachment_ci[i];
1641 const bool has_depth = FormatHasDepth(ci.format);
1642 const bool has_stencil = FormatHasStencil(ci.format);
1643 const bool is_color = !(has_depth || has_stencil);
1644 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1645
1646 if (is_color && store_op_stores) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001647 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster,
1648 view.normalized_subresource_range, offset, extent, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001649 } else {
1650 auto update_range = view.normalized_subresource_range;
1651 if (has_depth && store_op_stores) {
1652 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001653 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster,
1654 update_range, offset, extent, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001655 }
1656 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1657 if (has_stencil && stencil_op_stores) {
1658 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001659 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster,
1660 update_range, offset, extent, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001661 }
1662 }
1663 }
1664 }
1665}
1666
John Zulauf540266b2020-04-06 18:54:53 -06001667template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001668void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001669 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001670 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001671 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001672 }
1673}
1674
1675void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001676 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1677 auto &context = contexts[subpass_index];
John Zulaufb02c1eb2020-10-06 16:33:36 -06001678 ApplyTrackbackBarriersAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001679 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001680 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001681 }
1682 }
1683}
1684
John Zulauf355e49b2020-04-24 15:11:15 -06001685// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001686HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001687 if (!attach_view) return HazardResult();
1688 const auto image_state = attach_view->image_state.get();
1689 if (!image_state) return HazardResult();
1690
John Zulauf355e49b2020-04-24 15:11:15 -06001691 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001692 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001693
1694 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001695 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1696 const auto merged_barrier = MergeBarriers(track_back.barriers);
1697 HazardResult hazard =
1698 track_back.context->DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
1699 attach_view->normalized_subresource_range, 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 Zulaufa0a98292020-09-18 09:30:10 -06001702 hazard = DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001703 attach_view->normalized_subresource_range, kDetectAsync);
1704 }
John Zulaufa0a98292020-09-18 09:30:10 -06001705
John Zulauf355e49b2020-04-24 15:11:15 -06001706 return hazard;
1707}
1708
John Zulaufb02c1eb2020-10-06 16:33:36 -06001709void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
1710 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1711 const ResourceUsageTag &tag) {
1712 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001713 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001714 for (const auto &transition : transitions) {
1715 const auto prev_pass = transition.prev_pass;
1716 const auto attachment_view = attachment_views[transition.attachment];
1717 if (!attachment_view) continue;
1718 const auto *image = attachment_view->image_state.get();
1719 if (!image) continue;
1720 if (!SimpleBinding(*image)) continue;
1721
1722 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1723 assert(trackback);
1724
1725 // Import the attachments into the current context
1726 const auto *prev_context = trackback->context;
1727 assert(prev_context);
1728 const auto address_type = ImageAddressType(*image);
1729 auto &target_map = GetAccessStateMap(address_type);
1730 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
1731 prev_context->ResolveAccessRange(*image, attachment_view->normalized_subresource_range, barrier_action, address_type,
John Zulauf646cc292020-10-23 09:16:45 -06001732 &target_map, &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001733 }
1734
John Zulauf86356ca2020-10-19 11:46:41 -06001735 // If there were no transitions skip this global map walk
1736 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001737 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001738 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001739 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001740}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001741
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001742void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
1743 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
John Zulauf669dfd52021-01-27 17:15:28 -07001744
1745 auto *events_context = GetCurrentEventsContext();
1746 assert(events_context);
1747 for (auto &event_pair : *events_context) {
John Zulauf4a6105a2020-11-17 15:11:05 -07001748 assert(event_pair.second); // Shouldn't be storing empty
1749 auto &sync_event = *event_pair.second;
1750 // 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 -07001751 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
1752 sync_event.barriers |= dst.exec_scope;
1753 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
John Zulauf4a6105a2020-11-17 15:11:05 -07001754 }
1755 }
1756}
1757
John Zulauf355e49b2020-04-24 15:11:15 -06001758// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1759bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1760
1761 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001762 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001763 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1764 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06001765
John Zulauf86356ca2020-10-19 11:46:41 -06001766 assert(pRenderPassBegin);
1767 if (nullptr == pRenderPassBegin) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06001768
John Zulauf86356ca2020-10-19 11:46:41 -06001769 const uint32_t subpass = 0;
John Zulauf355e49b2020-04-24 15:11:15 -06001770
John Zulauf86356ca2020-10-19 11:46:41 -06001771 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
1772 // hasn't happened yet)
1773 const std::vector<AccessContext> empty_context_vector;
1774 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1775 const_cast<AccessContext *>(&cb_access_context_));
John Zulauf355e49b2020-04-24 15:11:15 -06001776
John Zulauf86356ca2020-10-19 11:46:41 -06001777 // Create a view list
1778 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1779 assert(fb_state);
1780 if (nullptr == fb_state) return skip;
1781 // NOTE: Must not use COMMAND_BUFFER_STATE variant of this as RecordCmdBeginRenderPass hasn't run and thus
1782 // the activeRenderPass.* fields haven't been set.
1783 const auto views = sync_state_->GetAttachmentViews(*pRenderPassBegin, *fb_state);
1784
1785 // Validate transitions
John Zulauffaea0ee2021-01-14 14:01:32 -07001786 skip |= temp_context.ValidateLayoutTransitions(*this, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
John Zulauf86356ca2020-10-19 11:46:41 -06001787
1788 // Validate load operations if there were no layout transition hazards
1789 if (!skip) {
1790 temp_context.RecordLayoutTransitions(rp_state, subpass, views, kCurrentCommandTag);
John Zulauffaea0ee2021-01-14 14:01:32 -07001791 skip |= temp_context.ValidateLoadOperation(*this, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001792 }
John Zulauf86356ca2020-10-19 11:46:41 -06001793
John Zulauf355e49b2020-04-24 15:11:15 -06001794 return skip;
1795}
1796
locke-lunarg61870c22020-06-09 14:51:50 -06001797bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1798 const char *func_name) const {
1799 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001800 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001801 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001802 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1803 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001804 return skip;
1805 }
1806
1807 using DescriptorClass = cvdescriptorset::DescriptorClass;
1808 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1809 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1810 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1811 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1812
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001813 for (const auto &stage_state : pipe->stage_state) {
1814 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1815 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001816 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001817 }
locke-lunarg61870c22020-06-09 14:51:50 -06001818 for (const auto &set_binding : stage_state.descriptor_uses) {
1819 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1820 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1821 set_binding.first.second);
1822 const auto descriptor_type = binding_it.GetType();
1823 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1824 auto array_idx = 0;
1825
1826 if (binding_it.IsVariableDescriptorCount()) {
1827 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1828 }
1829 SyncStageAccessIndex sync_index =
1830 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1831
1832 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1833 uint32_t index = i - index_range.start;
1834 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1835 switch (descriptor->GetClass()) {
1836 case DescriptorClass::ImageSampler:
1837 case DescriptorClass::Image: {
1838 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001839 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001840 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001841 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1842 img_view_state = image_sampler_descriptor->GetImageViewState();
1843 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001844 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001845 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1846 img_view_state = image_descriptor->GetImageViewState();
1847 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001848 }
1849 if (!img_view_state) continue;
1850 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1851 VkExtent3D extent = {};
1852 VkOffset3D offset = {};
1853 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1854 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1855 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1856 } else {
1857 extent = img_state->createInfo.extent;
1858 }
John Zulauf361fb532020-07-22 10:45:39 -06001859 HazardResult hazard;
1860 const auto &subresource_range = img_view_state->normalized_subresource_range;
1861 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1862 // Input attachments are subject to raster ordering rules
1863 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001864 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001865 } else {
1866 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1867 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001868 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001869 skip |= sync_state_->LogError(
1870 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001871 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1872 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001873 func_name, string_SyncHazard(hazard.hazard),
1874 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1875 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001876 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001877 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1878 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
John Zulauffaea0ee2021-01-14 14:01:32 -07001879 set_binding.first.second, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001880 }
1881 break;
1882 }
1883 case DescriptorClass::TexelBuffer: {
1884 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1885 if (!buf_view_state) continue;
1886 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001887 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001888 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001889 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001890 skip |= sync_state_->LogError(
1891 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001892 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1893 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001894 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1895 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001896 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001897 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1898 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001899 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001900 }
1901 break;
1902 }
1903 case DescriptorClass::GeneralBuffer: {
1904 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1905 auto buf_state = buffer_descriptor->GetBufferState();
1906 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001907 const ResourceAccessRange range =
1908 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001909 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001910 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001911 skip |= sync_state_->LogError(
1912 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001913 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1914 func_name, string_SyncHazard(hazard.hazard),
1915 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001916 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001917 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001918 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1919 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001920 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001921 }
1922 break;
1923 }
1924 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1925 default:
1926 break;
1927 }
1928 }
1929 }
1930 }
1931 return skip;
1932}
1933
1934void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1935 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001936 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001937 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001938 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1939 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001940 return;
1941 }
1942
1943 using DescriptorClass = cvdescriptorset::DescriptorClass;
1944 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1945 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1946 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1947 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1948
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001949 for (const auto &stage_state : pipe->stage_state) {
1950 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1951 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001952 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001953 }
locke-lunarg61870c22020-06-09 14:51:50 -06001954 for (const auto &set_binding : stage_state.descriptor_uses) {
1955 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1956 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1957 set_binding.first.second);
1958 const auto descriptor_type = binding_it.GetType();
1959 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1960 auto array_idx = 0;
1961
1962 if (binding_it.IsVariableDescriptorCount()) {
1963 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1964 }
1965 SyncStageAccessIndex sync_index =
1966 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1967
1968 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1969 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1970 switch (descriptor->GetClass()) {
1971 case DescriptorClass::ImageSampler:
1972 case DescriptorClass::Image: {
1973 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1974 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1975 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1976 } else {
1977 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1978 }
1979 if (!img_view_state) continue;
1980 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1981 VkExtent3D extent = {};
1982 VkOffset3D offset = {};
1983 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1984 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1985 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1986 } else {
1987 extent = img_state->createInfo.extent;
1988 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001989 SyncOrdering ordering_rule = (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)
1990 ? SyncOrdering::kRaster
1991 : SyncOrdering::kNonAttachment;
1992 current_context_->UpdateAccessState(*img_state, sync_index, ordering_rule,
1993 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001994 break;
1995 }
1996 case DescriptorClass::TexelBuffer: {
1997 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1998 if (!buf_view_state) continue;
1999 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002000 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07002001 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002002 break;
2003 }
2004 case DescriptorClass::GeneralBuffer: {
2005 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
2006 auto buf_state = buffer_descriptor->GetBufferState();
2007 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002008 const ResourceAccessRange range =
2009 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07002010 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002011 break;
2012 }
2013 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2014 default:
2015 break;
2016 }
2017 }
2018 }
2019 }
2020}
2021
2022bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2023 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002024 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2025 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002026 return skip;
2027 }
2028
2029 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2030 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002031 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002032
2033 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002034 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002035 if (binding_description.binding < binding_buffers_size) {
2036 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002037 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002038
locke-lunarg1ae57d62020-11-18 10:49:19 -07002039 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002040 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2041 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06002042 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
2043 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002044 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002045 buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002046 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07002047 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002048 }
2049 }
2050 }
2051 return skip;
2052}
2053
2054void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002055 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2056 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002057 return;
2058 }
2059 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2060 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002061 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002062
2063 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002064 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002065 if (binding_description.binding < binding_buffers_size) {
2066 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002067 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002068
locke-lunarg1ae57d62020-11-18 10:49:19 -07002069 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002070 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2071 vertexCount, binding_description.stride);
John Zulauf8e3c3e92021-01-06 11:19:36 -07002072 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, SyncOrdering::kNonAttachment,
2073 range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002074 }
2075 }
2076}
2077
2078bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2079 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002080 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002081 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002082 }
locke-lunarg61870c22020-06-09 14:51:50 -06002083
locke-lunarg1ae57d62020-11-18 10:49:19 -07002084 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002085 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002086 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2087 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06002088 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
2089 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002090 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002091 index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002092 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07002093 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002094 }
2095
2096 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2097 // We will detect more accurate range in the future.
2098 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2099 return skip;
2100}
2101
2102void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002103 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) return;
locke-lunarg61870c22020-06-09 14:51:50 -06002104
locke-lunarg1ae57d62020-11-18 10:49:19 -07002105 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002106 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002107 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2108 firstIndex, indexCount, index_size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07002109 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002110
2111 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2112 // We will detect more accurate range in the future.
2113 RecordDrawVertex(UINT32_MAX, 0, tag);
2114}
2115
2116bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002117 bool skip = false;
2118 if (!current_renderpass_context_) return skip;
John Zulauffaea0ee2021-01-14 14:01:32 -07002119 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*this, *cb_state_.get(),
locke-lunarg7077d502020-06-18 21:37:26 -06002120 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
2121 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002122}
2123
2124void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002125 if (current_renderpass_context_) {
locke-lunarg7077d502020-06-18 21:37:26 -06002126 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
2127 tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002128 }
locke-lunarg61870c22020-06-09 14:51:50 -06002129}
2130
John Zulauf355e49b2020-04-24 15:11:15 -06002131bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002132 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06002133 if (!current_renderpass_context_) return skip;
John Zulauffaea0ee2021-01-14 14:01:32 -07002134 skip |= current_renderpass_context_->ValidateNextSubpass(*this, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002135
2136 return skip;
2137}
2138
2139bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
2140 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06002141 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002142 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06002143 if (!current_renderpass_context_) return skip;
John Zulauffaea0ee2021-01-14 14:01:32 -07002144 skip |= current_renderpass_context_->ValidateEndRenderPass(*this, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002145
2146 return skip;
2147}
2148
2149void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
2150 assert(sync_state_);
2151 if (!cb_state_) return;
2152
2153 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06002154 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06002155 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06002156 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002157 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002158}
2159
John Zulauffaea0ee2021-01-14 14:01:32 -07002160void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002161 assert(current_renderpass_context_);
John Zulauffaea0ee2021-01-14 14:01:32 -07002162 auto prev_tag = NextCommandTag(command);
2163 auto next_tag = NextSubcommandTag(command);
2164 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, prev_tag, next_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002165 current_context_ = &current_renderpass_context_->CurrentContext();
2166}
2167
John Zulauffaea0ee2021-01-14 14:01:32 -07002168void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002169 assert(current_renderpass_context_);
2170 if (!current_renderpass_context_) return;
2171
John Zulauffaea0ee2021-01-14 14:01:32 -07002172 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea,
2173 NextCommandTag(command));
John Zulauf355e49b2020-04-24 15:11:15 -06002174 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002175 current_renderpass_context_ = nullptr;
2176}
2177
John Zulauf4a6105a2020-11-17 15:11:05 -07002178void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2179 // Erase is okay with the key not being
John Zulauf669dfd52021-01-27 17:15:28 -07002180 const auto *event_state = sync_state_->Get<EVENT_STATE>(event);
2181 if (event_state) {
2182 GetCurrentEventsContext()->Destroy(event_state);
John Zulaufd5115702021-01-18 12:34:33 -07002183 }
2184}
2185
John Zulauffaea0ee2021-01-14 14:01:32 -07002186bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandBufferAccessContext &cb_context,
2187 const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
2188 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002189 bool skip = false;
John Zulauffaea0ee2021-01-14 14:01:32 -07002190 const auto &sync_state = cb_context.GetSyncState();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002191 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2192 if (!pipe ||
2193 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002194 return skip;
2195 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002196 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002197 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2198 VkExtent3D extent = CastTo3D(render_area.extent);
2199 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06002200
John Zulauf1a224292020-06-30 14:52:13 -06002201 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002202 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002203 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2204 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002205 if (location >= subpass.colorAttachmentCount ||
2206 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002207 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002208 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002209 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002210 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07002211 SyncOrdering::kColorAttachment, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06002212 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002213 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002214 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002215 func_name, string_SyncHazard(hazard.hazard),
2216 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2217 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauffaea0ee2021-01-14 14:01:32 -07002218 location, cb_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002219 }
2220 }
2221 }
locke-lunarg37047832020-06-12 13:44:45 -06002222
2223 // PHASE1 TODO: Add layout based read/vs. write selection.
2224 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002225 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002226 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002227 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002228 bool depth_write = false, stencil_write = false;
2229
2230 // PHASE1 TODO: These validation should be in core_checks.
2231 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002232 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2233 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002234 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2235 depth_write = true;
2236 }
2237 // PHASE1 TODO: It needs to check if stencil is writable.
2238 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2239 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2240 // PHASE1 TODO: These validation should be in core_checks.
2241 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002242 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002243 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2244 stencil_write = true;
2245 }
2246
2247 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2248 if (depth_write) {
2249 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002250 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07002251 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002252 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002253 skip |= sync_state.LogError(
2254 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002255 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002256 func_name, string_SyncHazard(hazard.hazard),
2257 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2258 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauffaea0ee2021-01-14 14:01:32 -07002259 cb_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002260 }
2261 }
2262 if (stencil_write) {
2263 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002264 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07002265 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002266 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002267 skip |= sync_state.LogError(
2268 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002269 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002270 func_name, string_SyncHazard(hazard.hazard),
2271 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2272 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauffaea0ee2021-01-14 14:01:32 -07002273 cb_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002274 }
locke-lunarg61870c22020-06-09 14:51:50 -06002275 }
2276 }
2277 return skip;
2278}
2279
locke-lunarg96dc9632020-06-10 17:22:18 -06002280void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
2281 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002282 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2283 if (!pipe ||
2284 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002285 return;
2286 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002287 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002288 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2289 VkExtent3D extent = CastTo3D(render_area.extent);
2290 VkOffset3D offset = CastTo3D(render_area.offset);
2291
John Zulauf1a224292020-06-30 14:52:13 -06002292 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002293 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002294 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2295 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002296 if (location >= subpass.colorAttachmentCount ||
2297 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002298 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002299 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002300 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf8e3c3e92021-01-06 11:19:36 -07002301 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
2302 SyncOrdering::kColorAttachment, offset, extent, 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002303 }
2304 }
locke-lunarg37047832020-06-12 13:44:45 -06002305
2306 // PHASE1 TODO: Add layout based read/vs. write selection.
2307 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002308 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002309 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002310 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002311 bool depth_write = false, stencil_write = false;
2312
2313 // PHASE1 TODO: These validation should be in core_checks.
2314 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002315 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2316 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002317 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2318 depth_write = true;
2319 }
2320 // PHASE1 TODO: It needs to check if stencil is writable.
2321 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2322 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2323 // PHASE1 TODO: These validation should be in core_checks.
2324 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002325 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002326 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2327 stencil_write = true;
2328 }
2329
2330 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2331 if (depth_write) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002332 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2333 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT,
2334 tag);
locke-lunarg37047832020-06-12 13:44:45 -06002335 }
2336 if (stencil_write) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002337 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2338 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT,
2339 tag);
locke-lunarg37047832020-06-12 13:44:45 -06002340 }
locke-lunarg61870c22020-06-09 14:51:50 -06002341 }
2342}
2343
John Zulauffaea0ee2021-01-14 14:01:32 -07002344bool RenderPassAccessContext::ValidateNextSubpass(const CommandBufferAccessContext &cb_context, const VkRect2D &render_area,
John Zulauf1507ee42020-05-18 11:33:09 -06002345 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002346 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002347 bool skip = false;
John Zulauffaea0ee2021-01-14 14:01:32 -07002348 skip |= CurrentContext().ValidateResolveOperations(cb_context, *rp_state_, render_area, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002349 current_subpass_);
John Zulauffaea0ee2021-01-14 14:01:32 -07002350 skip |= CurrentContext().ValidateStoreOperation(cb_context, *rp_state_, render_area, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002351 func_name);
2352
John Zulauf355e49b2020-04-24 15:11:15 -06002353 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002354 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauffaea0ee2021-01-14 14:01:32 -07002355 skip |= next_context.ValidateLayoutTransitions(cb_context, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002356 if (!skip) {
2357 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2358 // on a copy of the (empty) next context.
2359 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2360 AccessContext temp_context(next_context);
2361 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauffaea0ee2021-01-14 14:01:32 -07002362 skip |= temp_context.ValidateLoadOperation(cb_context, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002363 }
John Zulauf7635de32020-05-29 17:14:15 -06002364 return skip;
2365}
John Zulauffaea0ee2021-01-14 14:01:32 -07002366bool RenderPassAccessContext::ValidateEndRenderPass(const CommandBufferAccessContext &cb_context, const VkRect2D &render_area,
John Zulauf7635de32020-05-29 17:14:15 -06002367 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002368 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002369 bool skip = false;
John Zulauffaea0ee2021-01-14 14:01:32 -07002370 skip |= CurrentContext().ValidateResolveOperations(cb_context, *rp_state_, render_area, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002371 current_subpass_);
John Zulauffaea0ee2021-01-14 14:01:32 -07002372 skip |= CurrentContext().ValidateStoreOperation(cb_context, *rp_state_, render_area, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002373 func_name);
John Zulauffaea0ee2021-01-14 14:01:32 -07002374 skip |= ValidateFinalSubpassLayoutTransitions(cb_context, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002375 return skip;
2376}
2377
John Zulauf7635de32020-05-29 17:14:15 -06002378AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2379 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2380}
2381
John Zulauffaea0ee2021-01-14 14:01:32 -07002382bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandBufferAccessContext &cb_context,
2383 const VkRect2D &render_area, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002384 bool skip = false;
2385
John Zulauf7635de32020-05-29 17:14:15 -06002386 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2387 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2388 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2389 // to apply and only copy then, if this proves a hot spot.
2390 std::unique_ptr<AccessContext> proxy_for_current;
2391
John Zulauf355e49b2020-04-24 15:11:15 -06002392 // Validate the "finalLayout" transitions to external
2393 // Get them from where there we're hidding in the extra entry.
2394 const auto &final_transitions = rp_state_->subpass_transitions.back();
2395 for (const auto &transition : final_transitions) {
2396 const auto &attach_view = attachment_views_[transition.attachment];
2397 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2398 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002399 auto *context = trackback.context;
2400
2401 if (transition.prev_pass == current_subpass_) {
2402 if (!proxy_for_current) {
2403 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2404 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2405 }
2406 context = proxy_for_current.get();
2407 }
2408
John Zulaufa0a98292020-09-18 09:30:10 -06002409 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2410 const auto merged_barrier = MergeBarriers(trackback.barriers);
2411 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2412 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2413 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002414 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -07002415 skip |= cb_context.GetSyncState().LogError(
2416 rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2417 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2418 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2419 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2420 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
2421 cb_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002422 }
2423 }
2424 return skip;
2425}
2426
2427void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2428 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002429 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002430}
2431
John Zulauf1507ee42020-05-18 11:33:09 -06002432void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2433 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2434 auto &subpass_context = subpass_contexts_[current_subpass_];
2435 VkExtent3D extent = CastTo3D(render_area.extent);
2436 VkOffset3D offset = CastTo3D(render_area.offset);
2437
2438 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2439 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2440 if (attachment_views_[i] == nullptr) continue; // UNUSED
2441 const auto &view = *attachment_views_[i];
2442 const IMAGE_STATE *image = view.image_state.get();
2443 if (image == nullptr) continue;
2444
2445 const auto &ci = attachment_ci[i];
2446 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002447 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002448 const bool is_color = !(has_depth || has_stencil);
2449
2450 if (is_color) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002451 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), SyncOrdering::kColorAttachment,
2452 view.normalized_subresource_range, offset, extent, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002453 } else {
2454 auto update_range = view.normalized_subresource_range;
2455 if (has_depth) {
2456 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07002457 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp),
2458 SyncOrdering::kDepthStencilAttachment, update_range, offset, extent, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002459 }
2460 if (has_stencil) {
2461 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07002462 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp),
2463 SyncOrdering::kDepthStencilAttachment, update_range, offset, extent, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002464 }
2465 }
2466 }
2467 }
2468}
2469
John Zulauf355e49b2020-04-24 15:11:15 -06002470void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002471 const AccessContext *external_context, VkQueueFlags queue_flags,
2472 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002473 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002474 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002475 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2476 // Add this for all subpasses here so that they exsist during next subpass validation
2477 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002478 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002479 }
2480 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2481
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002482 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002483 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002484 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002485}
John Zulauf1507ee42020-05-18 11:33:09 -06002486
John Zulauffaea0ee2021-01-14 14:01:32 -07002487void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &prev_subpass_tag,
2488 const ResourceUsageTag &next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002489 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulauffaea0ee2021-01-14 14:01:32 -07002490 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, prev_subpass_tag);
2491 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002492
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002493 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2494 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002495 current_subpass_++;
2496 assert(current_subpass_ < subpass_contexts_.size());
John Zulauffaea0ee2021-01-14 14:01:32 -07002497 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2498 RecordLayoutTransitions(next_subpass_tag);
2499 RecordLoadOperations(render_area, next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002500}
2501
John Zulauf1a224292020-06-30 14:52:13 -06002502void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2503 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002504 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002505 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002506 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002507
John Zulauf355e49b2020-04-24 15:11:15 -06002508 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002509 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002510
2511 // Add the "finalLayout" transitions to external
2512 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002513 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2514 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2515 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002516 const auto &final_transitions = rp_state_->subpass_transitions.back();
2517 for (const auto &transition : final_transitions) {
2518 const auto &attachment = attachment_views_[transition.attachment];
2519 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002520 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002521 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002522 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002523 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002524 }
John Zulauf1e331ec2020-12-04 18:29:38 -07002525 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002526 }
2527}
2528
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002529SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags mask_param) {
2530 SyncExecScope result;
2531 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002532 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2533 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002534 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2535 return result;
2536}
2537
2538SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags mask_param) {
2539 SyncExecScope result;
2540 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002541 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2542 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002543 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2544 return result;
2545}
2546
2547SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
2548 src_exec_scope = src.exec_scope;
2549 src_access_scope = 0;
2550 dst_exec_scope = dst.exec_scope;
2551 dst_access_scope = 0;
2552}
2553
2554template <typename Barrier>
2555SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
2556 src_exec_scope = src.exec_scope;
2557 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2558 dst_exec_scope = dst.exec_scope;
2559 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2560}
2561
2562SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
2563 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
2564 src_exec_scope = src.exec_scope;
2565 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2566
2567 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
2568 dst_exec_scope = dst.exec_scope;
2569 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002570}
2571
John Zulaufb02c1eb2020-10-06 16:33:36 -06002572// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2573void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2574 for (const auto &barrier : barriers) {
2575 ApplyBarrier(barrier, layout_transition);
2576 }
2577}
2578
John Zulauf89311b42020-09-29 16:28:47 -06002579// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2580// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2581// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002582void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2583 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002584 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002585 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002586 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002587 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002588 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002589 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002590}
John Zulauf9cb530d2019-09-30 14:14:10 -06002591HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2592 HazardResult hazard;
2593 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002594 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002595 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002596 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002597 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002598 }
2599 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002600 // Write operation:
2601 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2602 // If reads exists -- test only against them because either:
2603 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2604 // * 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
2605 // the current write happens after the reads, so just test the write against the reades
2606 // Otherwise test against last_write
2607 //
2608 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002609 if (last_reads.size()) {
2610 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002611 if (IsReadHazard(usage_stage, read_access)) {
2612 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2613 break;
2614 }
2615 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002616 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002617 // Write-After-Write check -- if we have a previous write to test against
2618 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002619 }
2620 }
2621 return hazard;
2622}
2623
John Zulauf8e3c3e92021-01-06 11:19:36 -07002624HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering &ordering_rule) const {
2625 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06002626 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2627 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002628 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002629 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002630 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2631 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002632 if (IsRead(usage_bit)) {
2633 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2634 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2635 if (is_raw_hazard) {
2636 // NOTE: we know last_write is non-zero
2637 // See if the ordering rules save us from the simple RAW check above
2638 // First check to see if the current usage is covered by the ordering rules
2639 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2640 const bool usage_is_ordered =
2641 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2642 if (usage_is_ordered) {
2643 // Now see of the most recent write (or a subsequent read) are ordered
2644 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2645 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002646 }
2647 }
John Zulauf4285ee92020-09-23 10:20:52 -06002648 if (is_raw_hazard) {
2649 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2650 }
John Zulauf361fb532020-07-22 10:45:39 -06002651 } else {
2652 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002653 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002654 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002655 // Look for any WAR hazards outside the ordered set of stages
John Zulauf4285ee92020-09-23 10:20:52 -06002656 VkPipelineStageFlags ordered_stages = 0;
2657 if (usage_write_is_ordered) {
2658 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2659 ordered_stages = GetOrderedStages(ordering);
2660 }
2661 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2662 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002663 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002664 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2665 if (IsReadHazard(usage_stage, read_access)) {
2666 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2667 break;
2668 }
John Zulaufd14743a2020-07-03 09:42:39 -06002669 }
2670 }
John Zulauf4285ee92020-09-23 10:20:52 -06002671 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002672 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002673 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002674 }
John Zulauf69133422020-05-20 14:55:53 -06002675 }
2676 }
2677 return hazard;
2678}
2679
John Zulauf2f952d22020-02-10 11:34:51 -07002680// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002681HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002682 HazardResult hazard;
2683 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002684 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2685 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2686 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002687 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002688 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002689 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002690 }
2691 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002692 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002693 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002694 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002695 // 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 -07002696 for (const auto &read_access : last_reads) {
2697 if (read_access.tag.index >= start_tag.index) {
2698 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002699 break;
2700 }
2701 }
John Zulauf2f952d22020-02-10 11:34:51 -07002702 }
2703 }
2704 return hazard;
2705}
2706
John Zulauf36bcf6a2020-02-03 15:12:52 -07002707HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002708 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002709 // Only supporting image layout transitions for now
2710 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2711 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002712 // only test for WAW if there no intervening read operations.
2713 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002714 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002715 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002716 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002717 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002718 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002719 break;
2720 }
2721 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002722 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2723 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2724 }
2725
2726 return hazard;
2727}
2728
2729HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2730 const SyncStageAccessFlags &src_access_scope,
2731 const ResourceUsageTag &event_tag) const {
2732 // Only supporting image layout transitions for now
2733 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2734 HazardResult hazard;
2735 // only test for WAW if there no intervening read operations.
2736 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2737
John Zulaufab7756b2020-12-29 16:10:16 -07002738 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002739 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
2740 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07002741 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002742 if (read_access.tag.IsBefore(event_tag)) {
2743 // The read is in the events first synchronization scope, so we use a barrier hazard check
2744 // If the read stage is not in the src sync scope
2745 // *AND* not execution chained with an existing sync barrier (that's the or)
2746 // then the barrier access is unsafe (R/W after R)
2747 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
2748 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2749 break;
2750 }
2751 } else {
2752 // The read not in the event first sync scope and so is a hazard vs. the layout transition
2753 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2754 }
2755 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002756 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002757 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
2758 if (write_tag.IsBefore(event_tag)) {
2759 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
2760 // So do a normal barrier hazard check
2761 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2762 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2763 }
2764 } else {
2765 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06002766 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2767 }
John Zulaufd14743a2020-07-03 09:42:39 -06002768 }
John Zulauf361fb532020-07-22 10:45:39 -06002769
John Zulauf0cb5be22020-01-23 12:18:22 -07002770 return hazard;
2771}
2772
John Zulauf5f13a792020-03-10 07:31:21 -06002773// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2774// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2775// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2776void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2777 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002778 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2779 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002780 *this = other;
2781 } else if (!other.write_tag.IsBefore(write_tag)) {
2782 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2783 // dependency chaining logic or any stage expansion)
2784 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002785 pending_write_barriers |= other.pending_write_barriers;
2786 pending_layout_transition |= other.pending_layout_transition;
2787 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002788
John Zulaufd14743a2020-07-03 09:42:39 -06002789 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07002790 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06002791 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07002792 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002793 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002794 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002795 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002796 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2797 // but we should wait on profiling data for that.
2798 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002799 auto &my_read = last_reads[my_read_index];
2800 if (other_read.stage == my_read.stage) {
2801 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002802 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002803 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002804 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002805 my_read.pending_dep_chain = other_read.pending_dep_chain;
2806 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2807 // May require tracking more than one access per stage.
2808 my_read.barriers = other_read.barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002809 if (my_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
2810 // Since I'm overwriting the fragement stage read, also update the input attachment info
2811 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002812 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002813 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002814 } else if (other_read.tag.IsBefore(my_read.tag)) {
2815 // The read tags match so merge the barriers
2816 my_read.barriers |= other_read.barriers;
2817 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002818 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002819
John Zulauf5f13a792020-03-10 07:31:21 -06002820 break;
2821 }
2822 }
2823 } else {
2824 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07002825 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06002826 last_read_stages |= other_read.stage;
John Zulauf4285ee92020-09-23 10:20:52 -06002827 if (other_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002828 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002829 }
John Zulauf5f13a792020-03-10 07:31:21 -06002830 }
2831 }
John Zulauf361fb532020-07-22 10:45:39 -06002832 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002833 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2834 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07002835
2836 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
2837 // of the copy and other into this using the update first logic.
2838 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
2839 // of the other first_accesses... )
2840 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
2841 FirstAccesses firsts(std::move(first_accesses_));
2842 first_accesses_.clear();
2843 first_read_stages_ = 0U;
2844 auto a = firsts.begin();
2845 auto a_end = firsts.end();
2846 for (auto &b : other.first_accesses_) {
2847 // TODO: Determine whether "IsBefore" or "IsGloballyBefore" is needed...
2848 while (a != a_end && a->tag.IsBefore(b.tag)) {
2849 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2850 ++a;
2851 }
2852 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
2853 }
2854 for (; a != a_end; ++a) {
2855 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2856 }
2857 }
John Zulauf5f13a792020-03-10 07:31:21 -06002858}
2859
John Zulauf8e3c3e92021-01-06 11:19:36 -07002860void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag &tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002861 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2862 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002863 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002864 // Mulitple outstanding reads may be of interest and do dependency chains independently
2865 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2866 const auto usage_stage = PipelineStageBit(usage_index);
2867 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002868 for (auto &read_access : last_reads) {
2869 if (read_access.stage == usage_stage) {
2870 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002871 break;
2872 }
2873 }
2874 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07002875 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002876 last_read_stages |= usage_stage;
2877 }
John Zulauf4285ee92020-09-23 10:20:52 -06002878
2879 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
2880 if (usage_stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002881 // TODO Revisit re: multiple reads for a given stage
2882 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002883 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002884 } else {
2885 // Assume write
2886 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002887 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002888 }
John Zulauffaea0ee2021-01-14 14:01:32 -07002889 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06002890}
John Zulauf5f13a792020-03-10 07:31:21 -06002891
John Zulauf89311b42020-09-29 16:28:47 -06002892// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2893// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2894// We can overwrite them as *this* write is now after them.
2895//
2896// 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 -07002897void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002898 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06002899 last_read_stages = 0;
2900 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002901 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002902
2903 write_barriers = 0;
2904 write_dependency_chain = 0;
2905 write_tag = tag;
2906 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002907}
2908
John Zulauf89311b42020-09-29 16:28:47 -06002909// Apply the memory barrier without updating the existing barriers. The execution barrier
2910// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2911// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2912// replace the current write barriers or add to them, so accumulate to pending as well.
2913void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2914 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2915 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002916 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
2917 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
2918 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
2919 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulauf4a6105a2020-11-17 15:11:05 -07002920 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06002921 pending_write_barriers |= barrier.dst_access_scope;
2922 pending_write_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002923 }
John Zulauf89311b42020-09-29 16:28:47 -06002924 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2925 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002926
John Zulauf89311b42020-09-29 16:28:47 -06002927 if (!pending_layout_transition) {
2928 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2929 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002930 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06002931 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
John Zulaufab7756b2020-12-29 16:10:16 -07002932 if (barrier.src_exec_scope & (read_access.stage | read_access.barriers)) {
2933 read_access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002934 }
2935 }
John Zulaufa0a98292020-09-18 09:30:10 -06002936 }
John Zulaufa0a98292020-09-18 09:30:10 -06002937}
2938
John Zulauf4a6105a2020-11-17 15:11:05 -07002939// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
2940// changes the "chaining" state, but to keep barriers independent. See discussion above.
2941void ResourceAccessState::ApplyBarrier(const ResourceUsageTag &scope_tag, const SyncBarrier &barrier, bool layout_transition) {
2942 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
2943 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
2944 // in order to know if it's in the excecution scope
2945 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
2946 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
2947 // errors w.r.t. "most recent" accesses.
2948 if (layout_transition || ((write_tag.IsBefore(scope_tag)) && (barrier.src_access_scope & last_write).any())) {
2949 pending_write_barriers |= barrier.dst_access_scope;
2950 pending_write_dep_chain |= barrier.dst_exec_scope;
2951 }
2952 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2953 pending_layout_transition |= layout_transition;
2954
2955 if (!pending_layout_transition) {
2956 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2957 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002958 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002959 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
2960 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
2961 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
2962 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
2963 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
2964 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
2965 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulaufab7756b2020-12-29 16:10:16 -07002966 if (read_access.tag.IsBefore(scope_tag) && (barrier.src_exec_scope & (read_access.stage | read_access.barriers))) {
2967 read_access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002968 }
2969 }
2970 }
2971}
John Zulauf89311b42020-09-29 16:28:47 -06002972void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
2973 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06002974 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2975 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07002976 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf89311b42020-09-29 16:28:47 -06002977 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002978 }
John Zulauf89311b42020-09-29 16:28:47 -06002979
2980 // Apply the accumulate execution barriers (and thus update chaining information)
2981 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07002982 for (auto &read_access : last_reads) {
2983 read_access.barriers |= read_access.pending_dep_chain;
2984 read_execution_barriers |= read_access.barriers;
2985 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06002986 }
2987
2988 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2989 write_dependency_chain |= pending_write_dep_chain;
2990 write_barriers |= pending_write_barriers;
2991 pending_write_dep_chain = 0;
2992 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002993}
2994
John Zulauf59e25072020-07-17 10:55:21 -06002995// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002996VkPipelineStageFlags ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
John Zulauf59e25072020-07-17 10:55:21 -06002997 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002998
John Zulaufab7756b2020-12-29 16:10:16 -07002999 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003000 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003001 barriers = read_access.barriers;
3002 break;
John Zulauf59e25072020-07-17 10:55:21 -06003003 }
3004 }
John Zulauf4285ee92020-09-23 10:20:52 -06003005
John Zulauf59e25072020-07-17 10:55:21 -06003006 return barriers;
3007}
3008
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003009inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003010 assert(IsRead(usage));
3011 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3012 // * the previous reads are not hazards, and thus last_write must be visible and available to
3013 // any reads that happen after.
3014 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3015 // 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 -07003016 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003017}
3018
John Zulauf8e3c3e92021-01-06 11:19:36 -07003019VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003020 // Whether the stage are in the ordering scope only matters if the current write is ordered
3021 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
3022 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003023 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003024 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003025 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
3026 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
3027 }
3028
3029 return ordered_stages;
3030}
3031
John Zulauffaea0ee2021-01-14 14:01:32 -07003032void ResourceAccessState::UpdateFirst(const ResourceUsageTag &tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
3033 // Only record until we record a write.
3034 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003035 const VkPipelineStageFlags usage_stage =
3036 IsRead(usage_index) ? static_cast<VkPipelineStageFlags>(PipelineStageBit(usage_index)) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003037 if (0 == (usage_stage & first_read_stages_)) {
3038 // If this is a read we haven't seen or a write, record.
3039 first_read_stages_ |= usage_stage;
3040 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3041 }
3042 }
3043}
3044
John Zulaufd1f85d42020-04-15 12:23:15 -06003045void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003046 auto *access_context = GetAccessContextNoInsert(command_buffer);
3047 if (access_context) {
3048 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003049 }
3050}
3051
John Zulaufd1f85d42020-04-15 12:23:15 -06003052void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3053 auto access_found = cb_access_state.find(command_buffer);
3054 if (access_found != cb_access_state.end()) {
3055 access_found->second->Reset();
3056 cb_access_state.erase(access_found);
3057 }
3058}
3059
John Zulauf9cb530d2019-09-30 14:14:10 -06003060bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3061 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3062 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003063 const auto *cb_context = GetAccessContext(commandBuffer);
3064 assert(cb_context);
3065 if (!cb_context) return skip;
3066 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003067
John Zulauf3d84f1b2020-03-09 13:33:25 -06003068 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003069 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003070 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003071
3072 for (uint32_t region = 0; region < regionCount; region++) {
3073 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003074 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003075 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003076 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003077 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003078 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003079 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003080 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003081 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003082 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003083 }
John Zulauf16adfc92020-04-08 10:28:33 -06003084 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003085 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06003086 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003087 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003088 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003089 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003090 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003091 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003092 }
3093 }
3094 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003095 }
3096 return skip;
3097}
3098
3099void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3100 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003101 auto *cb_context = GetAccessContext(commandBuffer);
3102 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003103 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003104 auto *context = cb_context->GetCurrentAccessContext();
3105
John Zulauf9cb530d2019-09-30 14:14:10 -06003106 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003107 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003108
3109 for (uint32_t region = 0; region < regionCount; region++) {
3110 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003111 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003112 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003113 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003114 }
John Zulauf16adfc92020-04-08 10:28:33 -06003115 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003116 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003117 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003118 }
3119 }
3120}
3121
John Zulauf4a6105a2020-11-17 15:11:05 -07003122void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3123 // Clear out events from the command buffer contexts
3124 for (auto &cb_context : cb_access_state) {
3125 cb_context.second->RecordDestroyEvent(event);
3126 }
3127}
3128
Jeff Leger178b1e52020-10-05 12:22:23 -04003129bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3130 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3131 bool skip = false;
3132 const auto *cb_context = GetAccessContext(commandBuffer);
3133 assert(cb_context);
3134 if (!cb_context) return skip;
3135 const auto *context = cb_context->GetCurrentAccessContext();
3136
3137 // If we have no previous accesses, we have no hazards
3138 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3139 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3140
3141 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3142 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3143 if (src_buffer) {
3144 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
3145 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
3146 if (hazard.hazard) {
3147 // TODO -- add tag information to log msg when useful.
3148 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3149 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3150 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003151 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003152 }
3153 }
3154 if (dst_buffer && !skip) {
3155 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
3156 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
3157 if (hazard.hazard) {
3158 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3159 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3160 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003161 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003162 }
3163 }
3164 if (skip) break;
3165 }
3166 return skip;
3167}
3168
3169void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3170 auto *cb_context = GetAccessContext(commandBuffer);
3171 assert(cb_context);
3172 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3173 auto *context = cb_context->GetCurrentAccessContext();
3174
3175 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3176 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3177
3178 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3179 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3180 if (src_buffer) {
3181 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003182 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003183 }
3184 if (dst_buffer) {
3185 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003186 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003187 }
3188 }
3189}
3190
John Zulauf5c5e88d2019-12-26 11:22:02 -07003191bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3192 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3193 const VkImageCopy *pRegions) const {
3194 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003195 const auto *cb_access_context = GetAccessContext(commandBuffer);
3196 assert(cb_access_context);
3197 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003198
John Zulauf3d84f1b2020-03-09 13:33:25 -06003199 const auto *context = cb_access_context->GetCurrentAccessContext();
3200 assert(context);
3201 if (!context) return skip;
3202
3203 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3204 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003205 for (uint32_t region = 0; region < regionCount; region++) {
3206 const auto &copy_region = pRegions[region];
3207 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003208 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003209 copy_region.srcOffset, copy_region.extent);
3210 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003211 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003212 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003213 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003214 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003215 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003216 }
3217
3218 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003219 VkExtent3D dst_copy_extent =
3220 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06003221 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003222 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003223 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003224 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003225 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003226 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003227 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003228 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003229 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003230 }
3231 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003232
John Zulauf5c5e88d2019-12-26 11:22:02 -07003233 return skip;
3234}
3235
3236void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3237 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3238 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003239 auto *cb_access_context = GetAccessContext(commandBuffer);
3240 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003241 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003242 auto *context = cb_access_context->GetCurrentAccessContext();
3243 assert(context);
3244
John Zulauf5c5e88d2019-12-26 11:22:02 -07003245 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003246 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003247
3248 for (uint32_t region = 0; region < regionCount; region++) {
3249 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003250 if (src_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07003251 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
3252 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003253 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003254 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003255 VkExtent3D dst_copy_extent =
3256 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003257 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
3258 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003259 }
3260 }
3261}
3262
Jeff Leger178b1e52020-10-05 12:22:23 -04003263bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3264 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3265 bool skip = false;
3266 const auto *cb_access_context = GetAccessContext(commandBuffer);
3267 assert(cb_access_context);
3268 if (!cb_access_context) return skip;
3269
3270 const auto *context = cb_access_context->GetCurrentAccessContext();
3271 assert(context);
3272 if (!context) return skip;
3273
3274 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3275 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3276 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3277 const auto &copy_region = pCopyImageInfo->pRegions[region];
3278 if (src_image) {
3279 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
3280 copy_region.srcOffset, copy_region.extent);
3281 if (hazard.hazard) {
3282 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3283 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3284 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003285 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003286 }
3287 }
3288
3289 if (dst_image) {
3290 VkExtent3D dst_copy_extent =
3291 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
3292 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
3293 copy_region.dstOffset, dst_copy_extent);
3294 if (hazard.hazard) {
3295 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3296 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3297 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003298 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003299 }
3300 if (skip) break;
3301 }
3302 }
3303
3304 return skip;
3305}
3306
3307void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3308 auto *cb_access_context = GetAccessContext(commandBuffer);
3309 assert(cb_access_context);
3310 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3311 auto *context = cb_access_context->GetCurrentAccessContext();
3312 assert(context);
3313
3314 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3315 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3316
3317 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3318 const auto &copy_region = pCopyImageInfo->pRegions[region];
3319 if (src_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07003320 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
3321 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003322 }
3323 if (dst_image) {
3324 VkExtent3D dst_copy_extent =
3325 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003326 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
3327 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003328 }
3329 }
3330}
3331
John Zulauf9cb530d2019-09-30 14:14:10 -06003332bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3333 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3334 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3335 uint32_t bufferMemoryBarrierCount,
3336 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3337 uint32_t imageMemoryBarrierCount,
3338 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3339 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003340 const auto *cb_access_context = GetAccessContext(commandBuffer);
3341 assert(cb_access_context);
3342 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003343
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003344 SyncOpPipelineBarrier pipeline_barrier(*this, cb_access_context->GetQueueFlags(), srcStageMask, dstStageMask, dependencyFlags,
3345 memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
3346 imageMemoryBarrierCount, pImageMemoryBarriers);
3347 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003348 return skip;
3349}
3350
3351void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3352 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3353 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3354 uint32_t bufferMemoryBarrierCount,
3355 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3356 uint32_t imageMemoryBarrierCount,
3357 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003358 auto *cb_access_context = GetAccessContext(commandBuffer);
3359 assert(cb_access_context);
3360 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003361
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003362 SyncOpPipelineBarrier pipeline_barrier(*this, cb_access_context->GetQueueFlags(), srcStageMask, dstStageMask, dependencyFlags,
3363 memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
3364 imageMemoryBarrierCount, pImageMemoryBarriers);
3365 pipeline_barrier.Record(cb_access_context, cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER));
John Zulauf9cb530d2019-09-30 14:14:10 -06003366}
3367
3368void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3369 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3370 // The state tracker sets up the device state
3371 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3372
John Zulauf5f13a792020-03-10 07:31:21 -06003373 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3374 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003375 // TODO: Find a good way to do this hooklessly.
3376 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3377 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3378 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3379
John Zulaufd1f85d42020-04-15 12:23:15 -06003380 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3381 sync_device_state->ResetCommandBufferCallback(command_buffer);
3382 });
3383 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3384 sync_device_state->FreeCommandBufferCallback(command_buffer);
3385 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003386}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003387
John Zulauf355e49b2020-04-24 15:11:15 -06003388bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003389 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003390 bool skip = false;
3391 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
3392 auto cb_context = GetAccessContext(commandBuffer);
3393
3394 if (rp_state && cb_context) {
3395 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
3396 }
3397
3398 return skip;
3399}
3400
3401bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3402 VkSubpassContents contents) const {
3403 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003404 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003405 subpass_begin_info.contents = contents;
3406 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
3407 return skip;
3408}
3409
3410bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003411 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003412 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3413 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
3414 return skip;
3415}
3416
3417bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3418 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003419 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003420 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3421 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3422 return skip;
3423}
3424
John Zulauf3d84f1b2020-03-09 13:33:25 -06003425void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3426 VkResult result) {
3427 // The state tracker sets up the command buffer state
3428 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3429
3430 // Create/initialize the structure that trackers accesses at the command buffer scope.
3431 auto cb_access_context = GetAccessContext(commandBuffer);
3432 assert(cb_access_context);
3433 cb_access_context->Reset();
3434}
3435
3436void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003437 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003438 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003439 if (cb_context) {
3440 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003441 }
3442}
3443
3444void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3445 VkSubpassContents contents) {
3446 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003447 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003448 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003449 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003450}
3451
3452void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3453 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3454 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003455 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003456}
3457
3458void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3459 const VkRenderPassBeginInfo *pRenderPassBegin,
3460 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3461 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003462 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3463}
3464
Mike Schuchardt2df08912020-12-15 16:28:09 -08003465bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3466 const VkSubpassEndInfo *pSubpassEndInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003467 bool skip = false;
3468
3469 auto cb_context = GetAccessContext(commandBuffer);
3470 assert(cb_context);
3471 auto cb_state = cb_context->GetCommandBufferState();
3472 if (!cb_state) return skip;
3473
3474 auto rp_state = cb_state->activeRenderPass;
3475 if (!rp_state) return skip;
3476
3477 skip |= cb_context->ValidateNextSubpass(func_name);
3478
3479 return skip;
3480}
3481
3482bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3483 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003484 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003485 subpass_begin_info.contents = contents;
3486 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3487 return skip;
3488}
3489
Mike Schuchardt2df08912020-12-15 16:28:09 -08003490bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3491 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003492 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3493 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3494 return skip;
3495}
3496
3497bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3498 const VkSubpassEndInfo *pSubpassEndInfo) const {
3499 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3500 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3501 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003502}
3503
3504void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003505 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003506 auto cb_context = GetAccessContext(commandBuffer);
3507 assert(cb_context);
3508 auto cb_state = cb_context->GetCommandBufferState();
3509 if (!cb_state) return;
3510
3511 auto rp_state = cb_state->activeRenderPass;
3512 if (!rp_state) return;
3513
John Zulauffaea0ee2021-01-14 14:01:32 -07003514 cb_context->RecordNextSubpass(*rp_state, command);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003515}
3516
3517void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3518 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003519 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003520 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003521 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003522}
3523
3524void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3525 const VkSubpassEndInfo *pSubpassEndInfo) {
3526 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003527 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003528}
3529
3530void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3531 const VkSubpassEndInfo *pSubpassEndInfo) {
3532 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003533 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003534}
3535
Mike Schuchardt2df08912020-12-15 16:28:09 -08003536bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003537 const char *func_name) const {
3538 bool skip = false;
3539
3540 auto cb_context = GetAccessContext(commandBuffer);
3541 assert(cb_context);
3542 auto cb_state = cb_context->GetCommandBufferState();
3543 if (!cb_state) return skip;
3544
3545 auto rp_state = cb_state->activeRenderPass;
3546 if (!rp_state) return skip;
3547
3548 skip |= cb_context->ValidateEndRenderpass(func_name);
3549 return skip;
3550}
3551
3552bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3553 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
3554 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
3555 return skip;
3556}
3557
Mike Schuchardt2df08912020-12-15 16:28:09 -08003558bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003559 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
3560 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
3561 return skip;
3562}
3563
3564bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003565 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003566 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
3567 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
3568 return skip;
3569}
3570
3571void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3572 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003573 // Resolve the all subpass contexts to the command buffer contexts
3574 auto cb_context = GetAccessContext(commandBuffer);
3575 assert(cb_context);
3576 auto cb_state = cb_context->GetCommandBufferState();
3577 if (!cb_state) return;
3578
locke-lunargaecf2152020-05-12 17:15:41 -06003579 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06003580 if (!rp_state) return;
3581
John Zulauffaea0ee2021-01-14 14:01:32 -07003582 cb_context->RecordEndRenderPass(*rp_state, command);
John Zulaufe5da6e52020-03-18 15:32:18 -06003583}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003584
John Zulauf33fc1d52020-07-17 11:01:10 -06003585// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3586// updates to a resource which do not conflict at the byte level.
3587// TODO: Revisit this rule to see if it needs to be tighter or looser
3588// TODO: Add programatic control over suppression heuristics
3589bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3590 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3591}
3592
John Zulauf3d84f1b2020-03-09 13:33:25 -06003593void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003594 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003595 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003596}
3597
3598void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003599 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003600 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003601}
3602
3603void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003604 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003605 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003606}
locke-lunarga19c71d2020-03-02 18:17:04 -07003607
Jeff Leger178b1e52020-10-05 12:22:23 -04003608template <typename BufferImageCopyRegionType>
3609bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3610 VkImageLayout dstImageLayout, uint32_t regionCount,
3611 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003612 bool skip = false;
3613 const auto *cb_access_context = GetAccessContext(commandBuffer);
3614 assert(cb_access_context);
3615 if (!cb_access_context) return skip;
3616
Jeff Leger178b1e52020-10-05 12:22:23 -04003617 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3618 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3619
locke-lunarga19c71d2020-03-02 18:17:04 -07003620 const auto *context = cb_access_context->GetCurrentAccessContext();
3621 assert(context);
3622 if (!context) return skip;
3623
3624 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003625 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3626
3627 for (uint32_t region = 0; region < regionCount; region++) {
3628 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003629 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003630 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003631 if (src_buffer) {
3632 ResourceAccessRange src_range =
3633 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
3634 hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
3635 if (hazard.hazard) {
3636 // PHASE1 TODO -- add tag information to log msg when useful.
3637 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3638 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3639 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003640 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003641 }
3642 }
3643
3644 hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
3645 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003646 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003647 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003648 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003649 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003650 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003651 }
3652 if (skip) break;
3653 }
3654 if (skip) break;
3655 }
3656 return skip;
3657}
3658
Jeff Leger178b1e52020-10-05 12:22:23 -04003659bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3660 VkImageLayout dstImageLayout, uint32_t regionCount,
3661 const VkBufferImageCopy *pRegions) const {
3662 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3663 COPY_COMMAND_VERSION_1);
3664}
3665
3666bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3667 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3668 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3669 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3670 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3671}
3672
3673template <typename BufferImageCopyRegionType>
3674void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3675 VkImageLayout dstImageLayout, uint32_t regionCount,
3676 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003677 auto *cb_access_context = GetAccessContext(commandBuffer);
3678 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003679
3680 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3681 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3682
3683 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003684 auto *context = cb_access_context->GetCurrentAccessContext();
3685 assert(context);
3686
3687 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003688 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003689
3690 for (uint32_t region = 0; region < regionCount; region++) {
3691 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07003692 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003693 if (src_buffer) {
3694 ResourceAccessRange src_range =
3695 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
3696 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
3697 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07003698 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
3699 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003700 }
3701 }
3702}
3703
Jeff Leger178b1e52020-10-05 12:22:23 -04003704void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3705 VkImageLayout dstImageLayout, uint32_t regionCount,
3706 const VkBufferImageCopy *pRegions) {
3707 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3708 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3709}
3710
3711void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3712 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3713 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3714 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3715 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3716 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3717}
3718
3719template <typename BufferImageCopyRegionType>
3720bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3721 VkBuffer dstBuffer, uint32_t regionCount,
3722 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003723 bool skip = false;
3724 const auto *cb_access_context = GetAccessContext(commandBuffer);
3725 assert(cb_access_context);
3726 if (!cb_access_context) return skip;
3727
Jeff Leger178b1e52020-10-05 12:22:23 -04003728 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3729 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3730
locke-lunarga19c71d2020-03-02 18:17:04 -07003731 const auto *context = cb_access_context->GetCurrentAccessContext();
3732 assert(context);
3733 if (!context) return skip;
3734
3735 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3736 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3737 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3738 for (uint32_t region = 0; region < regionCount; region++) {
3739 const auto &copy_region = pRegions[region];
3740 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003741 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003742 copy_region.imageOffset, copy_region.imageExtent);
3743 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003744 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003745 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003746 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003747 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003748 }
John Zulauf477700e2021-01-06 11:41:49 -07003749 if (dst_mem) {
3750 ResourceAccessRange dst_range =
3751 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
3752 hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
3753 if (hazard.hazard) {
3754 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3755 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3756 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003757 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003758 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003759 }
3760 }
3761 if (skip) break;
3762 }
3763 return skip;
3764}
3765
Jeff Leger178b1e52020-10-05 12:22:23 -04003766bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3767 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3768 const VkBufferImageCopy *pRegions) const {
3769 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3770 COPY_COMMAND_VERSION_1);
3771}
3772
3773bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3774 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3775 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3776 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3777 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3778}
3779
3780template <typename BufferImageCopyRegionType>
3781void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3782 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3783 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003784 auto *cb_access_context = GetAccessContext(commandBuffer);
3785 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003786
3787 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3788 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3789
3790 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003791 auto *context = cb_access_context->GetCurrentAccessContext();
3792 assert(context);
3793
3794 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003795 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3796 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 -06003797 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003798
3799 for (uint32_t region = 0; region < regionCount; region++) {
3800 const auto &copy_region = pRegions[region];
3801 if (src_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07003802 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
3803 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003804 if (dst_buffer) {
3805 ResourceAccessRange dst_range =
3806 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
3807 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
3808 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003809 }
3810 }
3811}
3812
Jeff Leger178b1e52020-10-05 12:22:23 -04003813void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3814 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3815 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3816 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3817}
3818
3819void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3820 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3821 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3822 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3823 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3824 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3825}
3826
3827template <typename RegionType>
3828bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3829 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3830 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003831 bool skip = false;
3832 const auto *cb_access_context = GetAccessContext(commandBuffer);
3833 assert(cb_access_context);
3834 if (!cb_access_context) return skip;
3835
3836 const auto *context = cb_access_context->GetCurrentAccessContext();
3837 assert(context);
3838 if (!context) return skip;
3839
3840 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3841 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3842
3843 for (uint32_t region = 0; region < regionCount; region++) {
3844 const auto &blit_region = pRegions[region];
3845 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003846 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3847 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3848 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3849 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3850 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3851 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3852 auto hazard =
3853 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003854 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003855 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003856 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003857 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003858 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003859 }
3860 }
3861
3862 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003863 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3864 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3865 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3866 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3867 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3868 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3869 auto hazard =
3870 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003871 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003872 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003873 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003874 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003875 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003876 }
3877 if (skip) break;
3878 }
3879 }
3880
3881 return skip;
3882}
3883
Jeff Leger178b1e52020-10-05 12:22:23 -04003884bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3885 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3886 const VkImageBlit *pRegions, VkFilter filter) const {
3887 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3888 "vkCmdBlitImage");
3889}
3890
3891bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3892 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3893 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3894 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3895 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3896}
3897
3898template <typename RegionType>
3899void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3900 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3901 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003902 auto *cb_access_context = GetAccessContext(commandBuffer);
3903 assert(cb_access_context);
3904 auto *context = cb_access_context->GetCurrentAccessContext();
3905 assert(context);
3906
3907 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003908 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003909
3910 for (uint32_t region = 0; region < regionCount; region++) {
3911 const auto &blit_region = pRegions[region];
3912 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003913 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3914 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3915 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3916 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3917 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3918 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
John Zulauf8e3c3e92021-01-06 11:19:36 -07003919 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
3920 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003921 }
3922 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003923 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3924 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3925 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3926 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3927 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3928 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
John Zulauf8e3c3e92021-01-06 11:19:36 -07003929 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
3930 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003931 }
3932 }
3933}
locke-lunarg36ba2592020-04-03 09:42:04 -06003934
Jeff Leger178b1e52020-10-05 12:22:23 -04003935void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3936 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3937 const VkImageBlit *pRegions, VkFilter filter) {
3938 auto *cb_access_context = GetAccessContext(commandBuffer);
3939 assert(cb_access_context);
3940 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3941 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3942 pRegions, filter);
3943 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3944}
3945
3946void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3947 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3948 auto *cb_access_context = GetAccessContext(commandBuffer);
3949 assert(cb_access_context);
3950 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3951 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3952 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3953 pBlitImageInfo->filter, tag);
3954}
3955
John Zulauffaea0ee2021-01-14 14:01:32 -07003956bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
3957 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
3958 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
3959 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003960 bool skip = false;
3961 if (drawCount == 0) return skip;
3962
3963 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3964 VkDeviceSize size = struct_size;
3965 if (drawCount == 1 || stride == size) {
3966 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003967 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003968 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3969 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003970 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003971 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003972 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003973 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003974 }
3975 } else {
3976 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003977 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003978 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3979 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003980 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003981 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3982 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003983 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003984 break;
3985 }
3986 }
3987 }
3988 return skip;
3989}
3990
locke-lunarg61870c22020-06-09 14:51:50 -06003991void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3992 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3993 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003994 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3995 VkDeviceSize size = struct_size;
3996 if (drawCount == 1 || stride == size) {
3997 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003998 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003999 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004000 } else {
4001 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004002 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004003 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4004 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004005 }
4006 }
4007}
4008
John Zulauffaea0ee2021-01-14 14:01:32 -07004009bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4010 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4011 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004012 bool skip = false;
4013
4014 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004015 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004016 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4017 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004018 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004019 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004020 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004021 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004022 }
4023 return skip;
4024}
4025
locke-lunarg61870c22020-06-09 14:51:50 -06004026void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06004027 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004028 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004029 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004030}
4031
locke-lunarg36ba2592020-04-03 09:42:04 -06004032bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004033 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004034 const auto *cb_access_context = GetAccessContext(commandBuffer);
4035 assert(cb_access_context);
4036 if (!cb_access_context) return skip;
4037
locke-lunarg61870c22020-06-09 14:51:50 -06004038 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004039 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004040}
4041
4042void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004043 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004044 auto *cb_access_context = GetAccessContext(commandBuffer);
4045 assert(cb_access_context);
4046 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004047
locke-lunarg61870c22020-06-09 14:51:50 -06004048 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004049}
locke-lunarge1a67022020-04-29 00:15:36 -06004050
4051bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004052 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004053 const auto *cb_access_context = GetAccessContext(commandBuffer);
4054 assert(cb_access_context);
4055 if (!cb_access_context) return skip;
4056
4057 const auto *context = cb_access_context->GetCurrentAccessContext();
4058 assert(context);
4059 if (!context) return skip;
4060
locke-lunarg61870c22020-06-09 14:51:50 -06004061 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004062 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4063 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004064 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004065}
4066
4067void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004068 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004069 auto *cb_access_context = GetAccessContext(commandBuffer);
4070 assert(cb_access_context);
4071 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4072 auto *context = cb_access_context->GetCurrentAccessContext();
4073 assert(context);
4074
locke-lunarg61870c22020-06-09 14:51:50 -06004075 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4076 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004077}
4078
4079bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4080 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004081 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004082 const auto *cb_access_context = GetAccessContext(commandBuffer);
4083 assert(cb_access_context);
4084 if (!cb_access_context) return skip;
4085
locke-lunarg61870c22020-06-09 14:51:50 -06004086 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4087 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4088 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004089 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004090}
4091
4092void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4093 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004094 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004095 auto *cb_access_context = GetAccessContext(commandBuffer);
4096 assert(cb_access_context);
4097 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004098
locke-lunarg61870c22020-06-09 14:51:50 -06004099 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4100 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4101 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004102}
4103
4104bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4105 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004106 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004107 const auto *cb_access_context = GetAccessContext(commandBuffer);
4108 assert(cb_access_context);
4109 if (!cb_access_context) return skip;
4110
locke-lunarg61870c22020-06-09 14:51:50 -06004111 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4112 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4113 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004114 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004115}
4116
4117void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4118 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004119 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004120 auto *cb_access_context = GetAccessContext(commandBuffer);
4121 assert(cb_access_context);
4122 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004123
locke-lunarg61870c22020-06-09 14:51:50 -06004124 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4125 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4126 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004127}
4128
4129bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4130 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004131 bool skip = false;
4132 if (drawCount == 0) return skip;
4133
locke-lunargff255f92020-05-13 18:53:52 -06004134 const auto *cb_access_context = GetAccessContext(commandBuffer);
4135 assert(cb_access_context);
4136 if (!cb_access_context) return skip;
4137
4138 const auto *context = cb_access_context->GetCurrentAccessContext();
4139 assert(context);
4140 if (!context) return skip;
4141
locke-lunarg61870c22020-06-09 14:51:50 -06004142 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4143 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004144 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4145 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004146
4147 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4148 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4149 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004150 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004151 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004152}
4153
4154void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4155 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004156 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004157 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004158 auto *cb_access_context = GetAccessContext(commandBuffer);
4159 assert(cb_access_context);
4160 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4161 auto *context = cb_access_context->GetCurrentAccessContext();
4162 assert(context);
4163
locke-lunarg61870c22020-06-09 14:51:50 -06004164 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4165 cb_access_context->RecordDrawSubpassAttachment(tag);
4166 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004167
4168 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4169 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4170 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004171 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004172}
4173
4174bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4175 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004176 bool skip = false;
4177 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004178 const auto *cb_access_context = GetAccessContext(commandBuffer);
4179 assert(cb_access_context);
4180 if (!cb_access_context) return skip;
4181
4182 const auto *context = cb_access_context->GetCurrentAccessContext();
4183 assert(context);
4184 if (!context) return skip;
4185
locke-lunarg61870c22020-06-09 14:51:50 -06004186 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4187 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004188 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4189 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004190
4191 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4192 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4193 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004194 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004195 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004196}
4197
4198void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4199 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004200 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004201 auto *cb_access_context = GetAccessContext(commandBuffer);
4202 assert(cb_access_context);
4203 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4204 auto *context = cb_access_context->GetCurrentAccessContext();
4205 assert(context);
4206
locke-lunarg61870c22020-06-09 14:51:50 -06004207 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4208 cb_access_context->RecordDrawSubpassAttachment(tag);
4209 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004210
4211 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4212 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4213 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004214 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004215}
4216
4217bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4218 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4219 uint32_t stride, const char *function) const {
4220 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004221 const auto *cb_access_context = GetAccessContext(commandBuffer);
4222 assert(cb_access_context);
4223 if (!cb_access_context) return skip;
4224
4225 const auto *context = cb_access_context->GetCurrentAccessContext();
4226 assert(context);
4227 if (!context) return skip;
4228
locke-lunarg61870c22020-06-09 14:51:50 -06004229 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4230 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004231 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4232 maxDrawCount, stride, function);
4233 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004234
4235 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4236 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4237 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004238 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004239 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004240}
4241
4242bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4243 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4244 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004245 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4246 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004247}
4248
4249void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4250 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4251 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004252 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4253 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004254 auto *cb_access_context = GetAccessContext(commandBuffer);
4255 assert(cb_access_context);
4256 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
4257 auto *context = cb_access_context->GetCurrentAccessContext();
4258 assert(context);
4259
locke-lunarg61870c22020-06-09 14:51:50 -06004260 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4261 cb_access_context->RecordDrawSubpassAttachment(tag);
4262 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4263 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004264
4265 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4266 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4267 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004268 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004269}
4270
4271bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4272 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4273 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004274 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4275 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004276}
4277
4278void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4279 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4280 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004281 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4282 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004283 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004284}
4285
4286bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4287 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4288 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004289 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4290 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004291}
4292
4293void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4294 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4295 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004296 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4297 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004298 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4299}
4300
4301bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4302 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4303 uint32_t stride, const char *function) const {
4304 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004305 const auto *cb_access_context = GetAccessContext(commandBuffer);
4306 assert(cb_access_context);
4307 if (!cb_access_context) return skip;
4308
4309 const auto *context = cb_access_context->GetCurrentAccessContext();
4310 assert(context);
4311 if (!context) return skip;
4312
locke-lunarg61870c22020-06-09 14:51:50 -06004313 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4314 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004315 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4316 offset, maxDrawCount, stride, function);
4317 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004318
4319 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4320 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4321 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004322 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004323 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004324}
4325
4326bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4327 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4328 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004329 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4330 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004331}
4332
4333void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4334 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4335 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004336 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4337 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004338 auto *cb_access_context = GetAccessContext(commandBuffer);
4339 assert(cb_access_context);
4340 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4341 auto *context = cb_access_context->GetCurrentAccessContext();
4342 assert(context);
4343
locke-lunarg61870c22020-06-09 14:51:50 -06004344 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4345 cb_access_context->RecordDrawSubpassAttachment(tag);
4346 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4347 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004348
4349 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4350 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004351 // We will update the index and vertex buffer in SubmitQueue in the future.
4352 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004353}
4354
4355bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4356 VkDeviceSize offset, VkBuffer countBuffer,
4357 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4358 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004359 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4360 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004361}
4362
4363void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4364 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4365 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004366 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4367 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004368 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4369}
4370
4371bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4372 VkDeviceSize offset, VkBuffer countBuffer,
4373 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4374 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004375 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4376 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004377}
4378
4379void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4380 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4381 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004382 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4383 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004384 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4385}
4386
4387bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4388 const VkClearColorValue *pColor, uint32_t rangeCount,
4389 const VkImageSubresourceRange *pRanges) const {
4390 bool skip = false;
4391 const auto *cb_access_context = GetAccessContext(commandBuffer);
4392 assert(cb_access_context);
4393 if (!cb_access_context) return skip;
4394
4395 const auto *context = cb_access_context->GetCurrentAccessContext();
4396 assert(context);
4397 if (!context) return skip;
4398
4399 const auto *image_state = Get<IMAGE_STATE>(image);
4400
4401 for (uint32_t index = 0; index < rangeCount; index++) {
4402 const auto &range = pRanges[index];
4403 if (image_state) {
4404 auto hazard =
4405 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4406 if (hazard.hazard) {
4407 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004408 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004409 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004410 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004411 }
4412 }
4413 }
4414 return skip;
4415}
4416
4417void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4418 const VkClearColorValue *pColor, uint32_t rangeCount,
4419 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004420 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004421 auto *cb_access_context = GetAccessContext(commandBuffer);
4422 assert(cb_access_context);
4423 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4424 auto *context = cb_access_context->GetCurrentAccessContext();
4425 assert(context);
4426
4427 const auto *image_state = Get<IMAGE_STATE>(image);
4428
4429 for (uint32_t index = 0; index < rangeCount; index++) {
4430 const auto &range = pRanges[index];
4431 if (image_state) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004432 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, {0, 0, 0},
4433 image_state->createInfo.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004434 }
4435 }
4436}
4437
4438bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4439 VkImageLayout imageLayout,
4440 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4441 const VkImageSubresourceRange *pRanges) const {
4442 bool skip = false;
4443 const auto *cb_access_context = GetAccessContext(commandBuffer);
4444 assert(cb_access_context);
4445 if (!cb_access_context) return skip;
4446
4447 const auto *context = cb_access_context->GetCurrentAccessContext();
4448 assert(context);
4449 if (!context) return skip;
4450
4451 const auto *image_state = Get<IMAGE_STATE>(image);
4452
4453 for (uint32_t index = 0; index < rangeCount; index++) {
4454 const auto &range = pRanges[index];
4455 if (image_state) {
4456 auto hazard =
4457 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4458 if (hazard.hazard) {
4459 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004460 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004461 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004462 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004463 }
4464 }
4465 }
4466 return skip;
4467}
4468
4469void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4470 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4471 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004472 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004473 auto *cb_access_context = GetAccessContext(commandBuffer);
4474 assert(cb_access_context);
4475 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4476 auto *context = cb_access_context->GetCurrentAccessContext();
4477 assert(context);
4478
4479 const auto *image_state = Get<IMAGE_STATE>(image);
4480
4481 for (uint32_t index = 0; index < rangeCount; index++) {
4482 const auto &range = pRanges[index];
4483 if (image_state) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004484 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, {0, 0, 0},
4485 image_state->createInfo.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004486 }
4487 }
4488}
4489
4490bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4491 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4492 VkDeviceSize dstOffset, VkDeviceSize stride,
4493 VkQueryResultFlags flags) const {
4494 bool skip = false;
4495 const auto *cb_access_context = GetAccessContext(commandBuffer);
4496 assert(cb_access_context);
4497 if (!cb_access_context) return skip;
4498
4499 const auto *context = cb_access_context->GetCurrentAccessContext();
4500 assert(context);
4501 if (!context) return skip;
4502
4503 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4504
4505 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004506 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004507 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4508 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004509 skip |=
4510 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4511 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004512 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004513 }
4514 }
locke-lunargff255f92020-05-13 18:53:52 -06004515
4516 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004517 return skip;
4518}
4519
4520void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4521 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4522 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004523 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4524 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004525 auto *cb_access_context = GetAccessContext(commandBuffer);
4526 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004527 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004528 auto *context = cb_access_context->GetCurrentAccessContext();
4529 assert(context);
4530
4531 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4532
4533 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004534 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004535 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004536 }
locke-lunargff255f92020-05-13 18:53:52 -06004537
4538 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004539}
4540
4541bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4542 VkDeviceSize size, uint32_t data) const {
4543 bool skip = false;
4544 const auto *cb_access_context = GetAccessContext(commandBuffer);
4545 assert(cb_access_context);
4546 if (!cb_access_context) return skip;
4547
4548 const auto *context = cb_access_context->GetCurrentAccessContext();
4549 assert(context);
4550 if (!context) return skip;
4551
4552 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4553
4554 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004555 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004556 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4557 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004558 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004559 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004560 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004561 }
4562 }
4563 return skip;
4564}
4565
4566void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4567 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004568 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004569 auto *cb_access_context = GetAccessContext(commandBuffer);
4570 assert(cb_access_context);
4571 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4572 auto *context = cb_access_context->GetCurrentAccessContext();
4573 assert(context);
4574
4575 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4576
4577 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004578 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004579 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004580 }
4581}
4582
4583bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4584 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4585 const VkImageResolve *pRegions) const {
4586 bool skip = false;
4587 const auto *cb_access_context = GetAccessContext(commandBuffer);
4588 assert(cb_access_context);
4589 if (!cb_access_context) return skip;
4590
4591 const auto *context = cb_access_context->GetCurrentAccessContext();
4592 assert(context);
4593 if (!context) return skip;
4594
4595 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4596 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4597
4598 for (uint32_t region = 0; region < regionCount; region++) {
4599 const auto &resolve_region = pRegions[region];
4600 if (src_image) {
4601 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4602 resolve_region.srcOffset, resolve_region.extent);
4603 if (hazard.hazard) {
4604 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004605 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004606 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004607 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004608 }
4609 }
4610
4611 if (dst_image) {
4612 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4613 resolve_region.dstOffset, resolve_region.extent);
4614 if (hazard.hazard) {
4615 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004616 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004617 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004618 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004619 }
4620 if (skip) break;
4621 }
4622 }
4623
4624 return skip;
4625}
4626
4627void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4628 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4629 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004630 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4631 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004632 auto *cb_access_context = GetAccessContext(commandBuffer);
4633 assert(cb_access_context);
4634 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4635 auto *context = cb_access_context->GetCurrentAccessContext();
4636 assert(context);
4637
4638 auto *src_image = Get<IMAGE_STATE>(srcImage);
4639 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4640
4641 for (uint32_t region = 0; region < regionCount; region++) {
4642 const auto &resolve_region = pRegions[region];
4643 if (src_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004644 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
4645 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004646 }
4647 if (dst_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004648 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
4649 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004650 }
4651 }
4652}
4653
Jeff Leger178b1e52020-10-05 12:22:23 -04004654bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4655 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4656 bool skip = false;
4657 const auto *cb_access_context = GetAccessContext(commandBuffer);
4658 assert(cb_access_context);
4659 if (!cb_access_context) return skip;
4660
4661 const auto *context = cb_access_context->GetCurrentAccessContext();
4662 assert(context);
4663 if (!context) return skip;
4664
4665 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4666 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4667
4668 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4669 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4670 if (src_image) {
4671 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4672 resolve_region.srcOffset, resolve_region.extent);
4673 if (hazard.hazard) {
4674 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4675 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4676 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004677 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004678 }
4679 }
4680
4681 if (dst_image) {
4682 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4683 resolve_region.dstOffset, resolve_region.extent);
4684 if (hazard.hazard) {
4685 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4686 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4687 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004688 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004689 }
4690 if (skip) break;
4691 }
4692 }
4693
4694 return skip;
4695}
4696
4697void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4698 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4699 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4700 auto *cb_access_context = GetAccessContext(commandBuffer);
4701 assert(cb_access_context);
4702 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4703 auto *context = cb_access_context->GetCurrentAccessContext();
4704 assert(context);
4705
4706 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4707 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4708
4709 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4710 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4711 if (src_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004712 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
4713 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004714 }
4715 if (dst_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004716 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
4717 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004718 }
4719 }
4720}
4721
locke-lunarge1a67022020-04-29 00:15:36 -06004722bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4723 VkDeviceSize dataSize, const void *pData) const {
4724 bool skip = false;
4725 const auto *cb_access_context = GetAccessContext(commandBuffer);
4726 assert(cb_access_context);
4727 if (!cb_access_context) return skip;
4728
4729 const auto *context = cb_access_context->GetCurrentAccessContext();
4730 assert(context);
4731 if (!context) return skip;
4732
4733 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4734
4735 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004736 // VK_WHOLE_SIZE not allowed
4737 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004738 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4739 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004740 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004741 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004742 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004743 }
4744 }
4745 return skip;
4746}
4747
4748void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4749 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004750 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004751 auto *cb_access_context = GetAccessContext(commandBuffer);
4752 assert(cb_access_context);
4753 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4754 auto *context = cb_access_context->GetCurrentAccessContext();
4755 assert(context);
4756
4757 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4758
4759 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004760 // VK_WHOLE_SIZE not allowed
4761 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004762 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004763 }
4764}
locke-lunargff255f92020-05-13 18:53:52 -06004765
4766bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4767 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4768 bool skip = false;
4769 const auto *cb_access_context = GetAccessContext(commandBuffer);
4770 assert(cb_access_context);
4771 if (!cb_access_context) return skip;
4772
4773 const auto *context = cb_access_context->GetCurrentAccessContext();
4774 assert(context);
4775 if (!context) return skip;
4776
4777 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4778
4779 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004780 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004781 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4782 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004783 skip |=
4784 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4785 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004786 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004787 }
4788 }
4789 return skip;
4790}
4791
4792void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4793 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004794 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004795 auto *cb_access_context = GetAccessContext(commandBuffer);
4796 assert(cb_access_context);
4797 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4798 auto *context = cb_access_context->GetCurrentAccessContext();
4799 assert(context);
4800
4801 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4802
4803 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004804 const ResourceAccessRange range = MakeRange(dstOffset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004805 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004806 }
4807}
John Zulauf49beb112020-11-04 16:06:31 -07004808
4809bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
4810 bool skip = false;
4811 const auto *cb_context = GetAccessContext(commandBuffer);
4812 assert(cb_context);
4813 if (!cb_context) return skip;
4814
John Zulauf6ce24372021-01-30 05:56:25 -07004815 SyncOpSetEvent set_event_op(*this, cb_context->GetQueueFlags(), event, stageMask);
4816 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004817}
4818
4819void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4820 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
4821 auto *cb_context = GetAccessContext(commandBuffer);
4822 assert(cb_context);
4823 if (!cb_context) return;
John Zulauf6ce24372021-01-30 05:56:25 -07004824 SyncOpSetEvent set_event_op(*this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf4a6105a2020-11-17 15:11:05 -07004825 const auto tag = cb_context->NextCommandTag(CMD_SETEVENT);
John Zulauf6ce24372021-01-30 05:56:25 -07004826 set_event_op.Record(cb_context, tag);
John Zulauf49beb112020-11-04 16:06:31 -07004827}
4828
4829bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
4830 VkPipelineStageFlags stageMask) const {
4831 bool skip = false;
4832 const auto *cb_context = GetAccessContext(commandBuffer);
4833 assert(cb_context);
4834 if (!cb_context) return skip;
4835
John Zulauf6ce24372021-01-30 05:56:25 -07004836 SyncOpResetEvent reset_event_op(*this, cb_context->GetQueueFlags(), event, stageMask);
4837 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004838}
4839
4840void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4841 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
4842 auto *cb_context = GetAccessContext(commandBuffer);
4843 assert(cb_context);
4844 if (!cb_context) return;
4845
John Zulauf6ce24372021-01-30 05:56:25 -07004846 const auto tag = cb_context->NextCommandTag(CMD_RESETEVENT);
4847 SyncOpResetEvent reset_event_op(*this, cb_context->GetQueueFlags(), event, stageMask);
4848 reset_event_op.Record(cb_context, tag);
John Zulauf49beb112020-11-04 16:06:31 -07004849}
4850
4851bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4852 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4853 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4854 uint32_t bufferMemoryBarrierCount,
4855 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4856 uint32_t imageMemoryBarrierCount,
4857 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
4858 bool skip = false;
4859 const auto *cb_context = GetAccessContext(commandBuffer);
4860 assert(cb_context);
4861 if (!cb_context) return skip;
4862
John Zulauf669dfd52021-01-27 17:15:28 -07004863 SyncOpWaitEvents wait_events_op(*this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask, dstStageMask,
John Zulaufd5115702021-01-18 12:34:33 -07004864 memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
4865 imageMemoryBarrierCount, pImageMemoryBarriers);
4866 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004867}
4868
4869void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4870 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4871 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4872 uint32_t bufferMemoryBarrierCount,
4873 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4874 uint32_t imageMemoryBarrierCount,
4875 const VkImageMemoryBarrier *pImageMemoryBarriers) {
4876 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
4877 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
4878 imageMemoryBarrierCount, pImageMemoryBarriers);
4879
4880 auto *cb_context = GetAccessContext(commandBuffer);
4881 assert(cb_context);
4882 if (!cb_context) return;
4883
John Zulauf4a6105a2020-11-17 15:11:05 -07004884 const auto tag = cb_context->NextCommandTag(CMD_WAITEVENTS);
John Zulauf669dfd52021-01-27 17:15:28 -07004885 SyncOpWaitEvents wait_events_op(*this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask, dstStageMask,
John Zulaufd5115702021-01-18 12:34:33 -07004886 memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
4887 imageMemoryBarrierCount, pImageMemoryBarriers);
4888 return wait_events_op.Record(cb_context, tag);
John Zulauf4a6105a2020-11-17 15:11:05 -07004889}
4890
4891void SyncEventState::ResetFirstScope() {
4892 for (const auto address_type : kAddressTypes) {
4893 first_scope[static_cast<size_t>(address_type)].clear();
4894 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07004895 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07004896}
4897
4898// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
4899SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(VkPipelineStageFlags srcStageMask) const {
4900 IgnoreReason reason = NotIgnored;
4901
4902 if (last_command == CMD_RESETEVENT && !HasBarrier(0U, 0U)) {
4903 reason = ResetWaitRace;
4904 } else if (unsynchronized_set) {
4905 reason = SetRace;
4906 } else {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07004907 const VkPipelineStageFlags missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07004908 if (missing_bits) reason = MissingStageBits;
4909 }
4910
4911 return reason;
4912}
4913
4914bool SyncEventState::HasBarrier(VkPipelineStageFlags stageMask, VkPipelineStageFlags exec_scope_arg) const {
4915 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
4916 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
4917 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07004918}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004919
John Zulaufd5115702021-01-18 12:34:33 -07004920SyncOpBarriers::SyncOpBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags, VkPipelineStageFlags srcStageMask,
4921 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
4922 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
4923 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
4924 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004925 : dependency_flags_(dependencyFlags),
4926 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, srcStageMask)),
4927 dst_exec_scope_(SyncExecScope::MakeDst(queue_flags, dstStageMask)) {
4928 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
4929 MakeMemoryBarriers(src_exec_scope_, dst_exec_scope_, dependencyFlags, memoryBarrierCount, pMemoryBarriers);
4930 MakeBufferMemoryBarriers(sync_state, src_exec_scope_, dst_exec_scope_, dependencyFlags, bufferMemoryBarrierCount,
4931 pBufferMemoryBarriers);
4932 MakeImageMemoryBarriers(sync_state, src_exec_scope_, dst_exec_scope_, dependencyFlags, imageMemoryBarrierCount,
4933 pImageMemoryBarriers);
4934}
4935
John Zulaufd5115702021-01-18 12:34:33 -07004936SyncOpPipelineBarrier::SyncOpPipelineBarrier(const SyncValidator &sync_state, VkQueueFlags queue_flags,
4937 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4938 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
4939 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
4940 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
4941 const VkImageMemoryBarrier *pImageMemoryBarriers)
4942 : SyncOpBarriers(sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
4943 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
4944
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004945bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
4946 bool skip = false;
4947 const auto *context = cb_context.GetCurrentAccessContext();
4948 assert(context);
4949 if (!context) return skip;
4950 // Validate Image Layout transitions
Nathaniel Cesarioe3025c62021-02-03 16:36:22 -07004951 for (const auto &image_barrier : image_memory_barriers_) {
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004952 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
4953 const auto *image_state = image_barrier.image.get();
4954 if (!image_state) continue;
4955 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
4956 if (hazard.hazard) {
4957 // PHASE1 TODO -- add tag information to log msg when useful.
4958 const auto &sync_state = cb_context.GetSyncState();
4959 const auto image_handle = image_state->image;
4960 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
4961 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
4962 string_SyncHazard(hazard.hazard), image_barrier.index,
4963 sync_state.report_data->FormatHandle(image_handle).c_str(),
4964 cb_context.FormatUsage(hazard).c_str());
4965 }
4966 }
4967
4968 return skip;
4969}
4970
John Zulaufd5115702021-01-18 12:34:33 -07004971struct SyncOpPipelineBarrierFunctorFactory {
4972 using BarrierOpFunctor = PipelineBarrierOp;
4973 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
4974 using GlobalBarrierOpFunctor = PipelineBarrierOp;
4975 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
4976 using BufferRange = ResourceAccessRange;
4977 using ImageRange = subresource_adapter::ImageRangeGenerator;
4978 using GlobalRange = ResourceAccessRange;
4979
4980 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
4981 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
4982 }
4983 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
4984 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
4985 }
4986 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
4987 return GlobalBarrierOpFunctor(barrier, false);
4988 }
4989
4990 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
4991 if (!SimpleBinding(buffer)) return ResourceAccessRange();
4992 const auto base_address = ResourceBaseAddress(buffer);
4993 return (range + base_address);
4994 }
4995 ImageRange MakeRangeGen(const IMAGE_STATE &image, const SyncImageMemoryBarrier::SubImageRange &range) const {
John Zulauf264cce02021-02-05 14:40:47 -07004996 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07004997
4998 const auto base_address = ResourceBaseAddress(image);
4999 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), range.subresource_range, range.offset,
5000 range.extent, base_address);
5001 return range_gen;
5002 }
5003 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5004};
5005
5006template <typename Barriers, typename FunctorFactory>
5007void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
5008 AccessContext *context) {
5009 for (const auto &barrier : barriers) {
5010 const auto *state = barrier.GetState();
5011 if (state) {
5012 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5013 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5014 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5015 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5016 }
5017 }
5018}
5019
5020template <typename Barriers, typename FunctorFactory>
5021void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
5022 AccessContext *access_context) {
5023 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5024 for (const auto &barrier : barriers) {
5025 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5026 }
5027 for (const auto address_type : kAddressTypes) {
5028 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5029 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5030 }
5031}
5032
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005033void SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context, const ResourceUsageTag &tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005034 SyncOpPipelineBarrierFunctorFactory factory;
5035 auto *access_context = cb_context->GetCurrentAccessContext();
5036 ApplyBarriers(buffer_memory_barriers_, factory, tag, access_context);
5037 ApplyBarriers(image_memory_barriers_, factory, tag, access_context);
5038 ApplyGlobalBarriers(memory_barriers_, factory, tag, access_context);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005039
5040 cb_context->ApplyGlobalBarriersToEvents(src_exec_scope_, dst_exec_scope_);
5041}
5042
John Zulaufd5115702021-01-18 12:34:33 -07005043void SyncOpBarriers::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst, VkDependencyFlags dependency_flags,
5044 uint32_t memory_barrier_count, const VkMemoryBarrier *memory_barriers) {
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005045 memory_barriers_.reserve(std::min<uint32_t>(1, memory_barrier_count));
5046 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
5047 const auto &barrier = memory_barriers[barrier_index];
5048 SyncBarrier sync_barrier(barrier, src, dst);
5049 memory_barriers_.emplace_back(sync_barrier);
5050 }
5051 if (0 == memory_barrier_count) {
5052 // If there are no global memory barriers, force an exec barrier
5053 memory_barriers_.emplace_back(SyncBarrier(src, dst));
5054 }
5055}
5056
John Zulaufd5115702021-01-18 12:34:33 -07005057void SyncOpBarriers::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src, const SyncExecScope &dst,
5058 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5059 const VkBufferMemoryBarrier *barriers) {
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005060 buffer_memory_barriers_.reserve(barrier_count);
5061 for (uint32_t index = 0; index < barrier_count; index++) {
5062 const auto &barrier = barriers[index];
5063 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5064 if (buffer) {
5065 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5066 const auto range = MakeRange(barrier.offset, barrier_size);
5067 const SyncBarrier sync_barrier(barrier, src, dst);
5068 buffer_memory_barriers_.emplace_back(buffer, sync_barrier, range);
5069 } else {
5070 buffer_memory_barriers_.emplace_back();
5071 }
5072 }
5073}
5074
John Zulaufd5115702021-01-18 12:34:33 -07005075void SyncOpBarriers::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src, const SyncExecScope &dst,
5076 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5077 const VkImageMemoryBarrier *barriers) {
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005078 image_memory_barriers_.reserve(barrier_count);
5079 for (uint32_t index = 0; index < barrier_count; index++) {
5080 const auto &barrier = barriers[index];
5081 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5082 if (image) {
5083 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5084 const SyncBarrier sync_barrier(barrier, src, dst);
5085 image_memory_barriers_.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout,
5086 subresource_range);
5087 } else {
5088 image_memory_barriers_.emplace_back();
5089 image_memory_barriers_.back().index = index; // Just in case we're interested in the ones we skipped.
5090 }
5091 }
5092}
John Zulaufd5115702021-01-18 12:34:33 -07005093
John Zulauf669dfd52021-01-27 17:15:28 -07005094SyncOpWaitEvents::SyncOpWaitEvents(const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005095 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5096 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5097 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5098 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf669dfd52021-01-27 17:15:28 -07005099 : SyncOpBarriers(sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005100 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5101 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005102 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005103}
5104
5105bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
5106 const auto cmd = CMD_WAITEVENTS;
5107 const char *const ignored = "Wait operation is ignored for this event.";
5108 bool skip = false;
5109 const auto &sync_state = cb_context.GetSyncState();
5110 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer;
5111
5112 if (src_exec_scope_.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5113 const char *const cmd_name = CommandTypeString(cmd);
5114 const char *const vuid = "SYNC-vkCmdWaitEvents-hostevent-unsupported";
5115 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5116 "%s, srcStageMask includes %s, unsupported by synchronization validaton.", cmd_name,
5117 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT), ignored);
5118 }
5119
5120 VkPipelineStageFlags event_stage_masks = 0U;
5121 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005122 const auto *events_context = cb_context.GetCurrentEventsContext();
5123 assert(events_context);
5124 for (const auto &sync_event_pair : *events_context) {
5125 const auto *sync_event = sync_event_pair.second.get();
John Zulaufd5115702021-01-18 12:34:33 -07005126 if (!sync_event) {
5127 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
John Zulauf669dfd52021-01-27 17:15:28 -07005128 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5129 // new validation error... wait without previously submitted set event...
5130 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives at *record time*
John Zulaufd5115702021-01-18 12:34:33 -07005131
5132 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
5133 }
5134 const auto event = sync_event->event->event;
5135 // TODO add "destroyed" checks
5136
5137 event_stage_masks |= sync_event->scope.mask_param;
5138 const auto ignore_reason = sync_event->IsIgnoredByWait(src_exec_scope_.mask_param);
5139 if (ignore_reason) {
5140 switch (ignore_reason) {
5141 case SyncEventState::ResetWaitRace: {
5142 const char *const cmd_name = CommandTypeString(cmd);
5143 const char *const vuid = "SYNC-vkCmdWaitEvents-missingbarrier-reset";
5144 const char *const message =
5145 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5146 skip |= sync_state.LogError(event, vuid, message, cmd_name, sync_state.report_data->FormatHandle(event).c_str(),
5147 cmd_name, CommandTypeString(sync_event->last_command), ignored);
5148 break;
5149 }
5150 case SyncEventState::SetRace: {
5151 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for this
5152 // event
5153 const char *const cmd_name = CommandTypeString(cmd);
5154 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5155 const char *const message =
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07005156 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
John Zulaufd5115702021-01-18 12:34:33 -07005157 const char *const reason = "First synchronization scope is undefined.";
5158 skip |= sync_state.LogError(event, vuid, message, cmd_name, sync_state.report_data->FormatHandle(event).c_str(),
5159 CommandTypeString(sync_event->last_command), reason, ignored);
5160 break;
5161 }
5162 case SyncEventState::MissingStageBits: {
5163 const VkPipelineStageFlags missing_bits = sync_event->scope.mask_param & ~src_exec_scope_.mask_param;
5164 // Issue error message that event waited for is not in wait events scope
5165 const char *const cmd_name = CommandTypeString(cmd);
5166 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5167 const char *const message =
5168 "%s: %s stageMask 0x%" PRIx32 " includes bits not present in srcStageMask 0x%" PRIx32
5169 ". Bits missing from srcStageMask %s. %s";
5170 skip |= sync_state.LogError(event, vuid, message, cmd_name, sync_state.report_data->FormatHandle(event).c_str(),
5171 sync_event->scope.mask_param, src_exec_scope_.mask_param,
5172 string_VkPipelineStageFlags(missing_bits).c_str(), ignored);
5173 break;
5174 }
5175 default:
5176 assert(ignore_reason == SyncEventState::NotIgnored);
5177 }
5178 } else if (image_memory_barriers_.size()) {
5179 const auto *context = cb_context.GetCurrentAccessContext();
5180 assert(context);
5181 for (const auto &image_memory_barrier : image_memory_barriers_) {
5182 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5183 const auto *image_state = image_memory_barrier.image.get();
5184 if (!image_state) continue;
5185 const auto &subresource_range = image_memory_barrier.range.subresource_range;
5186 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5187 const auto hazard =
5188 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5189 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5190 if (hazard.hazard) {
5191 const char *const cmd_name = CommandTypeString(cmd);
5192 skip |= sync_state.LogError(image_state->image, string_SyncHazardVUID(hazard.hazard),
5193 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", cmd_name,
5194 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
5195 sync_state.report_data->FormatHandle(image_state->image).c_str(),
5196 cb_context.FormatUsage(hazard).c_str());
5197 break;
5198 }
5199 }
5200 }
5201 }
5202
5203 // Note that we can't check for HOST in pEvents as we don't track that set event type
5204 const auto extra_stage_bits = (src_exec_scope_.mask_param & ~VK_PIPELINE_STAGE_HOST_BIT) & ~event_stage_masks;
5205 if (extra_stage_bits) {
5206 // Issue error message that event waited for is not in wait events scope
5207 const char *const cmd_name = CommandTypeString(cmd);
5208 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5209 const char *const message =
5210 "%s: srcStageMask 0x%" PRIx32 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
5211 if (events_not_found) {
5212 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, cmd_name, src_exec_scope_.mask_param,
5213 string_VkPipelineStageFlags(extra_stage_bits).c_str(),
5214 " vkCmdSetEvent may be in previously submitted command buffer.");
5215 } else {
5216 skip |= sync_state.LogError(command_buffer_handle, vuid, message, cmd_name, src_exec_scope_.mask_param,
5217 string_VkPipelineStageFlags(extra_stage_bits).c_str(), "");
5218 }
5219 }
5220 return skip;
5221}
5222
5223struct SyncOpWaitEventsFunctorFactory {
5224 using BarrierOpFunctor = WaitEventBarrierOp;
5225 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5226 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5227 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5228 using BufferRange = EventSimpleRangeGenerator;
5229 using ImageRange = EventImageRangeGenerator;
5230 using GlobalRange = EventSimpleRangeGenerator;
5231
5232 // Need to restrict to only valid exec and access scope for this event
5233 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5234 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
5235 barrier.src_exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope;
5236 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5237 return barrier;
5238 }
5239 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5240 auto barrier = RestrictToEvent(barrier_arg);
5241 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5242 }
5243 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
5244 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5245 }
5246 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5247 auto barrier = RestrictToEvent(barrier_arg);
5248 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5249 }
5250
5251 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5252 const AccessAddressType address_type = GetAccessAddressType(buffer);
5253 const auto base_address = ResourceBaseAddress(buffer);
5254 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5255 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5256 return filtered_range_gen;
5257 }
5258 ImageRange MakeRangeGen(const IMAGE_STATE &image, const SyncImageMemoryBarrier::SubImageRange &range) const {
5259 if (!SimpleBinding(image)) return ImageRange();
5260 const auto address_type = GetAccessAddressType(image);
5261 const auto base_address = ResourceBaseAddress(image);
5262 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), range.subresource_range,
5263 range.offset, range.extent, base_address);
5264 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5265
5266 return filtered_range_gen;
5267 }
5268 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5269 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5270 }
5271 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5272 SyncEventState *sync_event;
5273};
5274
5275void SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context, const ResourceUsageTag &tag) const {
5276 auto *access_context = cb_context->GetCurrentAccessContext();
5277 assert(access_context);
5278 if (!access_context) return;
John Zulauf669dfd52021-01-27 17:15:28 -07005279 auto *events_context = cb_context->GetCurrentEventsContext();
5280 assert(events_context);
5281 if (!events_context) return;
John Zulaufd5115702021-01-18 12:34:33 -07005282
5283 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
5284 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
5285 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
5286 access_context->ResolvePreviousAccesses();
5287
5288 const auto &dst = dst_exec_scope_;
5289 // TODO... this needs change the SyncEventContext it's using depending on whether this is replay... the recorded
5290 // sync_event will be in the recorded context, but we need to update the sync_events in the current context....
John Zulauf669dfd52021-01-27 17:15:28 -07005291 for (auto &event_shared : events_) {
5292 if (!event_shared.get()) continue;
5293 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07005294
5295 sync_event->last_command = CMD_WAITEVENTS;
5296
5297 if (!sync_event->IsIgnoredByWait(src_exec_scope_.mask_param)) {
5298 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
5299 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
5300 // of the barriers is maintained.
5301 SyncOpWaitEventsFunctorFactory factory(sync_event);
5302 ApplyBarriers(buffer_memory_barriers_, factory, tag, access_context);
5303 ApplyBarriers(image_memory_barriers_, factory, tag, access_context);
5304 ApplyGlobalBarriers(memory_barriers_, factory, tag, access_context);
5305
5306 // Apply the global barrier to the event itself (for race condition tracking)
5307 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
5308 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
5309 sync_event->barriers |= dst.exec_scope;
5310 } else {
5311 // We ignored this wait, so we don't have any effective synchronization barriers for it.
5312 sync_event->barriers = 0U;
5313 }
5314 }
5315
5316 // Apply the pending barriers
5317 ResolvePendingBarrierFunctor apply_pending_action(tag);
5318 access_context->ApplyToContext(apply_pending_action);
5319}
5320
John Zulauf669dfd52021-01-27 17:15:28 -07005321void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07005322 events_.reserve(event_count);
5323 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
John Zulauf669dfd52021-01-27 17:15:28 -07005324 events_.emplace_back(sync_state.GetShared<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07005325 }
5326}
John Zulauf6ce24372021-01-30 05:56:25 -07005327
5328SyncOpResetEvent::SyncOpResetEvent(const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
5329 VkPipelineStageFlags stageMask)
5330 : event_(sync_state.GetShared<EVENT_STATE>(event)), exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
5331
5332bool SyncOpResetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
5333 const auto cmd = CMD_RESETEVENT;
5334 auto *events_context = cb_context.GetCurrentEventsContext();
5335 assert(events_context);
5336 bool skip = false;
5337 if (!events_context) return skip;
5338
5339 const auto &sync_state = cb_context.GetSyncState();
5340 const auto *sync_event = events_context->Get(event_);
5341 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5342
5343 const char *const set_wait =
5344 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5345 "hazards.";
5346 const char *message = set_wait; // Only one message this call.
5347 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
5348 const char *vuid = nullptr;
5349 switch (sync_event->last_command) {
5350 case CMD_SETEVENT:
5351 // Needs a barrier between set and reset
5352 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
5353 break;
5354 case CMD_WAITEVENTS: {
5355 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
5356 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
5357 break;
5358 }
5359 default:
5360 // The only other valid last command that wasn't one.
5361 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT));
5362 break;
5363 }
5364 if (vuid) {
5365 const char *const cmd_name = CommandTypeString(cmd);
5366 skip |= sync_state.LogError(event_->event, vuid, message, cmd_name,
5367 sync_state.report_data->FormatHandle(event_->event).c_str(), cmd_name,
5368 CommandTypeString(sync_event->last_command));
5369 }
5370 }
5371 return skip;
5372}
5373
5374void SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context, const ResourceUsageTag &tag) const {
5375 const auto cmd = CMD_RESETEVENT;
5376
5377 auto *events_context = cb_context->GetCurrentEventsContext();
5378 assert(events_context);
5379 if (!events_context) return;
5380
5381 auto *sync_event = events_context->GetFromShared(event_);
5382 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5383
5384 // Update the event state
5385 sync_event->last_command = cmd;
5386 sync_event->unsynchronized_set = CMD_NONE;
5387 sync_event->ResetFirstScope();
5388 sync_event->barriers = 0U;
5389}
5390
5391SyncOpSetEvent::SyncOpSetEvent(const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
5392 VkPipelineStageFlags stageMask)
5393 : event_(sync_state.GetShared<EVENT_STATE>(event)), src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
5394
5395bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
5396 // I'll put this here just in case we need to pass this in for future extension support
5397 const auto cmd = CMD_SETEVENT;
5398 bool skip = false;
5399
5400 const auto &sync_state = cb_context.GetSyncState();
5401 auto *events_context = cb_context.GetCurrentEventsContext();
5402 assert(events_context);
5403 if (!events_context) return skip;
5404
5405 const auto *sync_event = events_context->Get(event_);
5406 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5407
5408 const char *const reset_set =
5409 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5410 "hazards.";
5411 const char *const wait =
5412 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
5413
5414 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
5415 const char *vuid = nullptr;
5416 const char *message = nullptr;
5417 switch (sync_event->last_command) {
5418 case CMD_RESETEVENT:
5419 // Needs a barrier between reset and set
5420 vuid = "SYNC-vkCmdSetEvent-missingbarrier-reset";
5421 message = reset_set;
5422 break;
5423 case CMD_SETEVENT:
5424 // Needs a barrier between set and set
5425 vuid = "SYNC-vkCmdSetEvent-missingbarrier-set";
5426 message = reset_set;
5427 break;
5428 case CMD_WAITEVENTS:
5429 // Needs a barrier or is in second execution scope
5430 vuid = "SYNC-vkCmdSetEvent-missingbarrier-wait";
5431 message = wait;
5432 break;
5433 default:
5434 // The only other valid last command that wasn't one.
5435 assert(sync_event->last_command == CMD_NONE);
5436 break;
5437 }
5438 if (vuid) {
5439 assert(nullptr != message);
5440 const char *const cmd_name = CommandTypeString(cmd);
5441 skip |= sync_state.LogError(event_->event, vuid, message, cmd_name,
5442 sync_state.report_data->FormatHandle(event_->event).c_str(), cmd_name,
5443 CommandTypeString(sync_event->last_command));
5444 }
5445 }
5446
5447 return skip;
5448}
5449
5450void SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context, const ResourceUsageTag &tag) const {
5451 auto *events_context = cb_context->GetCurrentEventsContext();
5452 auto *access_context = cb_context->GetCurrentAccessContext();
5453 assert(events_context);
5454 if (!events_context) return;
5455
5456 auto *sync_event = events_context->GetFromShared(event_);
5457 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5458
5459 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
5460 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
5461 // any issues caused by naive scope setting here.
5462
5463 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
5464 // Given:
5465 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
5466 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
5467
5468 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
5469 sync_event->unsynchronized_set = sync_event->last_command;
5470 sync_event->ResetFirstScope();
5471 } else if (sync_event->scope.exec_scope == 0) {
5472 // We only set the scope if there isn't one
5473 sync_event->scope = src_exec_scope_;
5474
5475 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
5476 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
5477 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
5478 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
5479 }
5480 };
5481 access_context->ForAll(set_scope);
5482 sync_event->unsynchronized_set = CMD_NONE;
5483 sync_event->first_scope_tag = tag;
5484 }
5485 sync_event->last_command = CMD_SETEVENT;
5486 sync_event->barriers = 0U;
5487}