blob: 2b3c17d491a1f84489d5e467c17671283718d9cb [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,
John Zulauf64ffe552021-02-06 10:25:07 -0700566 const CommandExecutionContext &ex_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600567 : render_pass_(render_pass),
568 subpass_(subpass),
569 context_(context),
John Zulauf64ffe552021-02-06 10:25:07 -0700570 ex_context_(ex_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_ |=
John Zulauf64ffe552021-02-06 10:25:07 -0700580 ex_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700581 "%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,
John Zulauf64ffe552021-02-06 10:25:07 -0700584 attachment_name, src_at, dst_at, ex_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 Zulauf64ffe552021-02-06 10:25:07 -0700594 const CommandExecutionContext &ex_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 Zulauf64ffe552021-02-06 10:25:07 -0700948bool AccessContext::ValidateLayoutTransitions(const CommandExecutionContext &ex_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 Zulauf64ffe552021-02-06 10:25:07 -0700978 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700979 "%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),
John Zulauf64ffe552021-02-06 10:25:07 -0700984 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600985 }
986 }
987 return skip;
988}
989
John Zulauf64ffe552021-02-06 10:25:07 -0700990bool AccessContext::ValidateLoadOperation(const CommandExecutionContext &ex_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 Zulauf64ffe552021-02-06 10:25:07 -07001046 const auto &sync_state = ex_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 Zulauf64ffe552021-02-06 10:25:07 -07001058 ex_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 Zulauf64ffe552021-02-06 10:25:07 -07001070bool AccessContext::ValidateStoreOperation(const CommandExecutionContext &ex_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 Zulauf64ffe552021-02-06 10:25:07 -07001124 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001125 "%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,
John Zulauf64ffe552021-02-06 10:25:07 -07001128 op_type_string, store_op_string, ex_context.FormatUsage(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001129 }
1130 }
1131 }
1132 return skip;
1133}
1134
John Zulauf64ffe552021-02-06 10:25:07 -07001135bool AccessContext::ValidateResolveOperations(const CommandExecutionContext &ex_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 Zulauf64ffe552021-02-06 10:25:07 -07001139 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, ex_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
locke-lunarg61870c22020-06-09 14:51:50 -06001759bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1760 const char *func_name) const {
1761 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001762 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001763 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001764 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1765 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001766 return skip;
1767 }
1768
1769 using DescriptorClass = cvdescriptorset::DescriptorClass;
1770 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1771 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1772 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1773 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1774
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001775 for (const auto &stage_state : pipe->stage_state) {
1776 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1777 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001778 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001779 }
locke-lunarg61870c22020-06-09 14:51:50 -06001780 for (const auto &set_binding : stage_state.descriptor_uses) {
1781 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1782 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1783 set_binding.first.second);
1784 const auto descriptor_type = binding_it.GetType();
1785 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1786 auto array_idx = 0;
1787
1788 if (binding_it.IsVariableDescriptorCount()) {
1789 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1790 }
1791 SyncStageAccessIndex sync_index =
1792 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1793
1794 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1795 uint32_t index = i - index_range.start;
1796 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1797 switch (descriptor->GetClass()) {
1798 case DescriptorClass::ImageSampler:
1799 case DescriptorClass::Image: {
1800 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001801 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001802 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001803 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1804 img_view_state = image_sampler_descriptor->GetImageViewState();
1805 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001806 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001807 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1808 img_view_state = image_descriptor->GetImageViewState();
1809 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001810 }
1811 if (!img_view_state) continue;
1812 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1813 VkExtent3D extent = {};
1814 VkOffset3D offset = {};
1815 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1816 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1817 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1818 } else {
1819 extent = img_state->createInfo.extent;
1820 }
John Zulauf361fb532020-07-22 10:45:39 -06001821 HazardResult hazard;
1822 const auto &subresource_range = img_view_state->normalized_subresource_range;
1823 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1824 // Input attachments are subject to raster ordering rules
1825 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001826 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001827 } else {
1828 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1829 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001830 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001831 skip |= sync_state_->LogError(
1832 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001833 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1834 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001835 func_name, string_SyncHazard(hazard.hazard),
1836 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1837 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001838 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001839 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1840 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
John Zulauffaea0ee2021-01-14 14:01:32 -07001841 set_binding.first.second, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001842 }
1843 break;
1844 }
1845 case DescriptorClass::TexelBuffer: {
1846 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1847 if (!buf_view_state) continue;
1848 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001849 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001850 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001851 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001852 skip |= sync_state_->LogError(
1853 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001854 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1855 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001856 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1857 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001858 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001859 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1860 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001861 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001862 }
1863 break;
1864 }
1865 case DescriptorClass::GeneralBuffer: {
1866 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1867 auto buf_state = buffer_descriptor->GetBufferState();
1868 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001869 const ResourceAccessRange range =
1870 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001871 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001872 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001873 skip |= sync_state_->LogError(
1874 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001875 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1876 func_name, string_SyncHazard(hazard.hazard),
1877 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001878 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001879 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001880 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1881 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001882 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001883 }
1884 break;
1885 }
1886 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1887 default:
1888 break;
1889 }
1890 }
1891 }
1892 }
1893 return skip;
1894}
1895
1896void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1897 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001898 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001899 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001900 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1901 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001902 return;
1903 }
1904
1905 using DescriptorClass = cvdescriptorset::DescriptorClass;
1906 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1907 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1908 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1909 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1910
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001911 for (const auto &stage_state : pipe->stage_state) {
1912 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1913 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001914 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001915 }
locke-lunarg61870c22020-06-09 14:51:50 -06001916 for (const auto &set_binding : stage_state.descriptor_uses) {
1917 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1918 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1919 set_binding.first.second);
1920 const auto descriptor_type = binding_it.GetType();
1921 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1922 auto array_idx = 0;
1923
1924 if (binding_it.IsVariableDescriptorCount()) {
1925 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1926 }
1927 SyncStageAccessIndex sync_index =
1928 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1929
1930 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1931 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1932 switch (descriptor->GetClass()) {
1933 case DescriptorClass::ImageSampler:
1934 case DescriptorClass::Image: {
1935 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1936 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1937 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1938 } else {
1939 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1940 }
1941 if (!img_view_state) continue;
1942 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1943 VkExtent3D extent = {};
1944 VkOffset3D offset = {};
1945 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1946 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1947 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1948 } else {
1949 extent = img_state->createInfo.extent;
1950 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001951 SyncOrdering ordering_rule = (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)
1952 ? SyncOrdering::kRaster
1953 : SyncOrdering::kNonAttachment;
1954 current_context_->UpdateAccessState(*img_state, sync_index, ordering_rule,
1955 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001956 break;
1957 }
1958 case DescriptorClass::TexelBuffer: {
1959 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1960 if (!buf_view_state) continue;
1961 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001962 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001963 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001964 break;
1965 }
1966 case DescriptorClass::GeneralBuffer: {
1967 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1968 auto buf_state = buffer_descriptor->GetBufferState();
1969 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001970 const ResourceAccessRange range =
1971 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07001972 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001973 break;
1974 }
1975 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1976 default:
1977 break;
1978 }
1979 }
1980 }
1981 }
1982}
1983
1984bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1985 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001986 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1987 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06001988 return skip;
1989 }
1990
1991 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1992 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001993 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06001994
1995 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001996 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06001997 if (binding_description.binding < binding_buffers_size) {
1998 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07001999 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002000
locke-lunarg1ae57d62020-11-18 10:49:19 -07002001 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002002 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2003 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06002004 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
2005 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002006 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002007 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 -06002008 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07002009 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002010 }
2011 }
2012 }
2013 return skip;
2014}
2015
2016void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002017 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2018 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002019 return;
2020 }
2021 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2022 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002023 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002024
2025 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002026 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002027 if (binding_description.binding < binding_buffers_size) {
2028 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002029 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002030
locke-lunarg1ae57d62020-11-18 10:49:19 -07002031 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002032 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2033 vertexCount, binding_description.stride);
John Zulauf8e3c3e92021-01-06 11:19:36 -07002034 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, SyncOrdering::kNonAttachment,
2035 range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002036 }
2037 }
2038}
2039
2040bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2041 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002042 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002043 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002044 }
locke-lunarg61870c22020-06-09 14:51:50 -06002045
locke-lunarg1ae57d62020-11-18 10:49:19 -07002046 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002047 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002048 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2049 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06002050 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
2051 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002052 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002053 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 -06002054 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07002055 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002056 }
2057
2058 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2059 // We will detect more accurate range in the future.
2060 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2061 return skip;
2062}
2063
2064void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002065 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 -06002066
locke-lunarg1ae57d62020-11-18 10:49:19 -07002067 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002068 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002069 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2070 firstIndex, indexCount, index_size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07002071 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002072
2073 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2074 // We will detect more accurate range in the future.
2075 RecordDrawVertex(UINT32_MAX, 0, tag);
2076}
2077
2078bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002079 bool skip = false;
2080 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002081 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002082 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002083}
2084
2085void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002086 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002087 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002088 }
locke-lunarg61870c22020-06-09 14:51:50 -06002089}
2090
John Zulauf64ffe552021-02-06 10:25:07 -07002091void CommandBufferAccessContext::RecordBeginRenderPass(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2092 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2093 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002094 // Create an access context the current renderpass.
John Zulauf64ffe552021-02-06 10:25:07 -07002095 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002096 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf64ffe552021-02-06 10:25:07 -07002097 current_renderpass_context_->RecordBeginRenderPass(tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002098 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002099}
2100
John Zulauf64ffe552021-02-06 10:25:07 -07002101void CommandBufferAccessContext::RecordNextSubpass(CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002102 assert(current_renderpass_context_);
John Zulauffaea0ee2021-01-14 14:01:32 -07002103 auto prev_tag = NextCommandTag(command);
2104 auto next_tag = NextSubcommandTag(command);
John Zulauf64ffe552021-02-06 10:25:07 -07002105 current_renderpass_context_->RecordNextSubpass(prev_tag, next_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002106 current_context_ = &current_renderpass_context_->CurrentContext();
2107}
2108
John Zulauf64ffe552021-02-06 10:25:07 -07002109void CommandBufferAccessContext::RecordEndRenderPass(CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002110 assert(current_renderpass_context_);
2111 if (!current_renderpass_context_) return;
2112
John Zulauf64ffe552021-02-06 10:25:07 -07002113 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, NextCommandTag(command));
John Zulauf355e49b2020-04-24 15:11:15 -06002114 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002115 current_renderpass_context_ = nullptr;
2116}
2117
John Zulauf4a6105a2020-11-17 15:11:05 -07002118void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2119 // Erase is okay with the key not being
John Zulauf669dfd52021-01-27 17:15:28 -07002120 const auto *event_state = sync_state_->Get<EVENT_STATE>(event);
2121 if (event_state) {
2122 GetCurrentEventsContext()->Destroy(event_state);
John Zulaufd5115702021-01-18 12:34:33 -07002123 }
2124}
2125
John Zulauf64ffe552021-02-06 10:25:07 -07002126bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
John Zulauffaea0ee2021-01-14 14:01:32 -07002127 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002128 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002129 const auto &sync_state = ex_context.GetSyncState();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002130 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2131 if (!pipe ||
2132 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002133 return skip;
2134 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002135 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002136 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
John Zulauf64ffe552021-02-06 10:25:07 -07002137 VkExtent3D extent = CastTo3D(render_area_.extent);
2138 VkOffset3D offset = CastTo3D(render_area_.offset);
locke-lunarg37047832020-06-12 13:44:45 -06002139
John Zulauf1a224292020-06-30 14:52:13 -06002140 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002141 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002142 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2143 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002144 if (location >= subpass.colorAttachmentCount ||
2145 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002146 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002147 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002148 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002149 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07002150 SyncOrdering::kColorAttachment, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06002151 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002152 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002153 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002154 func_name, string_SyncHazard(hazard.hazard),
2155 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2156 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002157 location, ex_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002158 }
2159 }
2160 }
locke-lunarg37047832020-06-12 13:44:45 -06002161
2162 // PHASE1 TODO: Add layout based read/vs. write selection.
2163 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002164 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002165 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002166 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002167 bool depth_write = false, stencil_write = false;
2168
2169 // PHASE1 TODO: These validation should be in core_checks.
2170 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002171 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2172 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002173 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2174 depth_write = true;
2175 }
2176 // PHASE1 TODO: It needs to check if stencil is writable.
2177 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2178 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2179 // PHASE1 TODO: These validation should be in core_checks.
2180 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002181 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002182 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2183 stencil_write = true;
2184 }
2185
2186 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2187 if (depth_write) {
2188 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002189 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07002190 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002191 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002192 skip |= sync_state.LogError(
2193 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002194 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002195 func_name, string_SyncHazard(hazard.hazard),
2196 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2197 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002198 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002199 }
2200 }
2201 if (stencil_write) {
2202 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002203 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07002204 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002205 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002206 skip |= sync_state.LogError(
2207 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002208 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002209 func_name, string_SyncHazard(hazard.hazard),
2210 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2211 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002212 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002213 }
locke-lunarg61870c22020-06-09 14:51:50 -06002214 }
2215 }
2216 return skip;
2217}
2218
John Zulauf64ffe552021-02-06 10:25:07 -07002219void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002220 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2221 if (!pipe ||
2222 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002223 return;
2224 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002225 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002226 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
John Zulauf64ffe552021-02-06 10:25:07 -07002227 VkExtent3D extent = CastTo3D(render_area_.extent);
2228 VkOffset3D offset = CastTo3D(render_area_.offset);
locke-lunarg61870c22020-06-09 14:51:50 -06002229
John Zulauf1a224292020-06-30 14:52:13 -06002230 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002231 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002232 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2233 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002234 if (location >= subpass.colorAttachmentCount ||
2235 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002236 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002237 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002238 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf8e3c3e92021-01-06 11:19:36 -07002239 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
2240 SyncOrdering::kColorAttachment, offset, extent, 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002241 }
2242 }
locke-lunarg37047832020-06-12 13:44:45 -06002243
2244 // PHASE1 TODO: Add layout based read/vs. write selection.
2245 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002246 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002247 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002248 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002249 bool depth_write = false, stencil_write = false;
2250
2251 // PHASE1 TODO: These validation should be in core_checks.
2252 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002253 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2254 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002255 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2256 depth_write = true;
2257 }
2258 // PHASE1 TODO: It needs to check if stencil is writable.
2259 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2260 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2261 // PHASE1 TODO: These validation should be in core_checks.
2262 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002263 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002264 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2265 stencil_write = true;
2266 }
2267
2268 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2269 if (depth_write) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002270 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2271 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT,
2272 tag);
locke-lunarg37047832020-06-12 13:44:45 -06002273 }
2274 if (stencil_write) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002275 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2276 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT,
2277 tag);
locke-lunarg37047832020-06-12 13:44:45 -06002278 }
locke-lunarg61870c22020-06-09 14:51:50 -06002279 }
2280}
2281
John Zulauf64ffe552021-02-06 10:25:07 -07002282bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002283 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002284 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002285 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002286 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002287 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002288 func_name);
2289
John Zulauf355e49b2020-04-24 15:11:15 -06002290 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002291 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002292 skip |=
2293 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002294 if (!skip) {
2295 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2296 // on a copy of the (empty) next context.
2297 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2298 AccessContext temp_context(next_context);
2299 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002300 skip |=
2301 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002302 }
John Zulauf7635de32020-05-29 17:14:15 -06002303 return skip;
2304}
John Zulauf64ffe552021-02-06 10:25:07 -07002305bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002306 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002307 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002308 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002309 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002310 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002311 func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002312 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002313 return skip;
2314}
2315
John Zulauf64ffe552021-02-06 10:25:07 -07002316AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
2317 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002318}
2319
John Zulauf64ffe552021-02-06 10:25:07 -07002320bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2321 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002322 bool skip = false;
2323
John Zulauf7635de32020-05-29 17:14:15 -06002324 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2325 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2326 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2327 // to apply and only copy then, if this proves a hot spot.
2328 std::unique_ptr<AccessContext> proxy_for_current;
2329
John Zulauf355e49b2020-04-24 15:11:15 -06002330 // Validate the "finalLayout" transitions to external
2331 // Get them from where there we're hidding in the extra entry.
2332 const auto &final_transitions = rp_state_->subpass_transitions.back();
2333 for (const auto &transition : final_transitions) {
2334 const auto &attach_view = attachment_views_[transition.attachment];
2335 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2336 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002337 auto *context = trackback.context;
2338
2339 if (transition.prev_pass == current_subpass_) {
2340 if (!proxy_for_current) {
2341 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
John Zulauf64ffe552021-02-06 10:25:07 -07002342 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002343 }
2344 context = proxy_for_current.get();
2345 }
2346
John Zulaufa0a98292020-09-18 09:30:10 -06002347 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2348 const auto merged_barrier = MergeBarriers(trackback.barriers);
2349 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2350 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2351 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002352 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002353 skip |= ex_context.GetSyncState().LogError(
John Zulauffaea0ee2021-01-14 14:01:32 -07002354 rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2355 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2356 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2357 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2358 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002359 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002360 }
2361 }
2362 return skip;
2363}
2364
2365void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2366 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002367 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002368}
2369
John Zulauf64ffe552021-02-06 10:25:07 -07002370void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag &tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002371 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2372 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf64ffe552021-02-06 10:25:07 -07002373 VkExtent3D extent = CastTo3D(render_area_.extent);
2374 VkOffset3D offset = CastTo3D(render_area_.offset);
John Zulauf1507ee42020-05-18 11:33:09 -06002375
2376 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2377 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2378 if (attachment_views_[i] == nullptr) continue; // UNUSED
2379 const auto &view = *attachment_views_[i];
2380 const IMAGE_STATE *image = view.image_state.get();
2381 if (image == nullptr) continue;
2382
2383 const auto &ci = attachment_ci[i];
2384 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002385 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002386 const bool is_color = !(has_depth || has_stencil);
2387
2388 if (is_color) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002389 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), SyncOrdering::kColorAttachment,
2390 view.normalized_subresource_range, offset, extent, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002391 } else {
2392 auto update_range = view.normalized_subresource_range;
2393 if (has_depth) {
2394 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07002395 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp),
2396 SyncOrdering::kDepthStencilAttachment, update_range, offset, extent, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002397 }
2398 if (has_stencil) {
2399 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07002400 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp),
2401 SyncOrdering::kDepthStencilAttachment, update_range, offset, extent, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002402 }
2403 }
2404 }
2405 }
2406}
John Zulauf64ffe552021-02-06 10:25:07 -07002407RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2408 VkQueueFlags queue_flags,
2409 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2410 const AccessContext *external_context)
2411 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_(attachment_views) {
John Zulauf355e49b2020-04-24 15:11:15 -06002412 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002413 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002414 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002415 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002416 }
John Zulauf64ffe552021-02-06 10:25:07 -07002417}
2418void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
2419 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002420 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002421 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002422 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002423}
John Zulauf1507ee42020-05-18 11:33:09 -06002424
John Zulauf64ffe552021-02-06 10:25:07 -07002425void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag &prev_subpass_tag,
John Zulauffaea0ee2021-01-14 14:01:32 -07002426 const ResourceUsageTag &next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002427 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulauf64ffe552021-02-06 10:25:07 -07002428 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area_, attachment_views_, current_subpass_, prev_subpass_tag);
2429 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002430
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002431 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2432 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002433 current_subpass_++;
2434 assert(current_subpass_ < subpass_contexts_.size());
John Zulauffaea0ee2021-01-14 14:01:32 -07002435 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2436 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002437 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002438}
2439
John Zulauf64ffe552021-02-06 10:25:07 -07002440void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002441 // Add the resolve and store accesses
John Zulauf64ffe552021-02-06 10:25:07 -07002442 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area_, attachment_views_, current_subpass_, tag);
2443 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002444
John Zulauf355e49b2020-04-24 15:11:15 -06002445 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002446 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002447
2448 // Add the "finalLayout" transitions to external
2449 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002450 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2451 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2452 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002453 const auto &final_transitions = rp_state_->subpass_transitions.back();
2454 for (const auto &transition : final_transitions) {
2455 const auto &attachment = attachment_views_[transition.attachment];
2456 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002457 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002458 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002459 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002460 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002461 }
John Zulauf1e331ec2020-12-04 18:29:38 -07002462 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002463 }
2464}
2465
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002466SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags mask_param) {
2467 SyncExecScope result;
2468 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002469 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2470 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002471 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2472 return result;
2473}
2474
2475SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags mask_param) {
2476 SyncExecScope result;
2477 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002478 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2479 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002480 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2481 return result;
2482}
2483
2484SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
2485 src_exec_scope = src.exec_scope;
2486 src_access_scope = 0;
2487 dst_exec_scope = dst.exec_scope;
2488 dst_access_scope = 0;
2489}
2490
2491template <typename Barrier>
2492SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
2493 src_exec_scope = src.exec_scope;
2494 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2495 dst_exec_scope = dst.exec_scope;
2496 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2497}
2498
2499SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
2500 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
2501 src_exec_scope = src.exec_scope;
2502 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2503
2504 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
2505 dst_exec_scope = dst.exec_scope;
2506 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002507}
2508
John Zulaufb02c1eb2020-10-06 16:33:36 -06002509// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2510void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2511 for (const auto &barrier : barriers) {
2512 ApplyBarrier(barrier, layout_transition);
2513 }
2514}
2515
John Zulauf89311b42020-09-29 16:28:47 -06002516// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2517// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2518// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002519void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2520 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002521 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002522 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002523 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002524 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002525 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002526 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002527}
John Zulauf9cb530d2019-09-30 14:14:10 -06002528HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2529 HazardResult hazard;
2530 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002531 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002532 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002533 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002534 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002535 }
2536 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002537 // Write operation:
2538 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2539 // If reads exists -- test only against them because either:
2540 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2541 // * 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
2542 // the current write happens after the reads, so just test the write against the reades
2543 // Otherwise test against last_write
2544 //
2545 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002546 if (last_reads.size()) {
2547 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002548 if (IsReadHazard(usage_stage, read_access)) {
2549 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2550 break;
2551 }
2552 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002553 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002554 // Write-After-Write check -- if we have a previous write to test against
2555 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002556 }
2557 }
2558 return hazard;
2559}
2560
John Zulauf8e3c3e92021-01-06 11:19:36 -07002561HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering &ordering_rule) const {
2562 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06002563 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2564 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002565 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002566 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002567 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2568 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002569 if (IsRead(usage_bit)) {
2570 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2571 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2572 if (is_raw_hazard) {
2573 // NOTE: we know last_write is non-zero
2574 // See if the ordering rules save us from the simple RAW check above
2575 // First check to see if the current usage is covered by the ordering rules
2576 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2577 const bool usage_is_ordered =
2578 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2579 if (usage_is_ordered) {
2580 // Now see of the most recent write (or a subsequent read) are ordered
2581 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2582 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002583 }
2584 }
John Zulauf4285ee92020-09-23 10:20:52 -06002585 if (is_raw_hazard) {
2586 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2587 }
John Zulauf361fb532020-07-22 10:45:39 -06002588 } else {
2589 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002590 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002591 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002592 // Look for any WAR hazards outside the ordered set of stages
John Zulauf4285ee92020-09-23 10:20:52 -06002593 VkPipelineStageFlags ordered_stages = 0;
2594 if (usage_write_is_ordered) {
2595 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2596 ordered_stages = GetOrderedStages(ordering);
2597 }
2598 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2599 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002600 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002601 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2602 if (IsReadHazard(usage_stage, read_access)) {
2603 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2604 break;
2605 }
John Zulaufd14743a2020-07-03 09:42:39 -06002606 }
2607 }
John Zulauf4285ee92020-09-23 10:20:52 -06002608 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002609 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002610 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002611 }
John Zulauf69133422020-05-20 14:55:53 -06002612 }
2613 }
2614 return hazard;
2615}
2616
John Zulauf2f952d22020-02-10 11:34:51 -07002617// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002618HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002619 HazardResult hazard;
2620 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002621 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2622 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2623 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002624 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002625 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002626 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002627 }
2628 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002629 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002630 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002631 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002632 // 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 -07002633 for (const auto &read_access : last_reads) {
2634 if (read_access.tag.index >= start_tag.index) {
2635 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002636 break;
2637 }
2638 }
John Zulauf2f952d22020-02-10 11:34:51 -07002639 }
2640 }
2641 return hazard;
2642}
2643
John Zulauf36bcf6a2020-02-03 15:12:52 -07002644HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002645 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002646 // Only supporting image layout transitions for now
2647 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2648 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002649 // only test for WAW if there no intervening read operations.
2650 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002651 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002652 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002653 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002654 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002655 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002656 break;
2657 }
2658 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002659 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2660 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2661 }
2662
2663 return hazard;
2664}
2665
2666HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2667 const SyncStageAccessFlags &src_access_scope,
2668 const ResourceUsageTag &event_tag) const {
2669 // Only supporting image layout transitions for now
2670 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2671 HazardResult hazard;
2672 // only test for WAW if there no intervening read operations.
2673 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2674
John Zulaufab7756b2020-12-29 16:10:16 -07002675 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002676 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
2677 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07002678 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002679 if (read_access.tag.IsBefore(event_tag)) {
2680 // The read is in the events first synchronization scope, so we use a barrier hazard check
2681 // If the read stage is not in the src sync scope
2682 // *AND* not execution chained with an existing sync barrier (that's the or)
2683 // then the barrier access is unsafe (R/W after R)
2684 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
2685 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2686 break;
2687 }
2688 } else {
2689 // The read not in the event first sync scope and so is a hazard vs. the layout transition
2690 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2691 }
2692 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002693 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002694 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
2695 if (write_tag.IsBefore(event_tag)) {
2696 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
2697 // So do a normal barrier hazard check
2698 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2699 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2700 }
2701 } else {
2702 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06002703 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2704 }
John Zulaufd14743a2020-07-03 09:42:39 -06002705 }
John Zulauf361fb532020-07-22 10:45:39 -06002706
John Zulauf0cb5be22020-01-23 12:18:22 -07002707 return hazard;
2708}
2709
John Zulauf5f13a792020-03-10 07:31:21 -06002710// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2711// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2712// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2713void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2714 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002715 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2716 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002717 *this = other;
2718 } else if (!other.write_tag.IsBefore(write_tag)) {
2719 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2720 // dependency chaining logic or any stage expansion)
2721 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002722 pending_write_barriers |= other.pending_write_barriers;
2723 pending_layout_transition |= other.pending_layout_transition;
2724 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002725
John Zulaufd14743a2020-07-03 09:42:39 -06002726 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07002727 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06002728 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07002729 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002730 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002731 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002732 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002733 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2734 // but we should wait on profiling data for that.
2735 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002736 auto &my_read = last_reads[my_read_index];
2737 if (other_read.stage == my_read.stage) {
2738 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002739 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002740 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002741 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002742 my_read.pending_dep_chain = other_read.pending_dep_chain;
2743 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2744 // May require tracking more than one access per stage.
2745 my_read.barriers = other_read.barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002746 if (my_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
2747 // Since I'm overwriting the fragement stage read, also update the input attachment info
2748 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002749 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002750 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002751 } else if (other_read.tag.IsBefore(my_read.tag)) {
2752 // The read tags match so merge the barriers
2753 my_read.barriers |= other_read.barriers;
2754 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002755 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002756
John Zulauf5f13a792020-03-10 07:31:21 -06002757 break;
2758 }
2759 }
2760 } else {
2761 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07002762 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06002763 last_read_stages |= other_read.stage;
John Zulauf4285ee92020-09-23 10:20:52 -06002764 if (other_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002765 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002766 }
John Zulauf5f13a792020-03-10 07:31:21 -06002767 }
2768 }
John Zulauf361fb532020-07-22 10:45:39 -06002769 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002770 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2771 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07002772
2773 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
2774 // of the copy and other into this using the update first logic.
2775 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
2776 // of the other first_accesses... )
2777 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
2778 FirstAccesses firsts(std::move(first_accesses_));
2779 first_accesses_.clear();
2780 first_read_stages_ = 0U;
2781 auto a = firsts.begin();
2782 auto a_end = firsts.end();
2783 for (auto &b : other.first_accesses_) {
2784 // TODO: Determine whether "IsBefore" or "IsGloballyBefore" is needed...
2785 while (a != a_end && a->tag.IsBefore(b.tag)) {
2786 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2787 ++a;
2788 }
2789 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
2790 }
2791 for (; a != a_end; ++a) {
2792 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2793 }
2794 }
John Zulauf5f13a792020-03-10 07:31:21 -06002795}
2796
John Zulauf8e3c3e92021-01-06 11:19:36 -07002797void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag &tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002798 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2799 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002800 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002801 // Mulitple outstanding reads may be of interest and do dependency chains independently
2802 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2803 const auto usage_stage = PipelineStageBit(usage_index);
2804 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002805 for (auto &read_access : last_reads) {
2806 if (read_access.stage == usage_stage) {
2807 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002808 break;
2809 }
2810 }
2811 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07002812 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002813 last_read_stages |= usage_stage;
2814 }
John Zulauf4285ee92020-09-23 10:20:52 -06002815
2816 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
2817 if (usage_stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06002818 // TODO Revisit re: multiple reads for a given stage
2819 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002820 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002821 } else {
2822 // Assume write
2823 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002824 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002825 }
John Zulauffaea0ee2021-01-14 14:01:32 -07002826 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06002827}
John Zulauf5f13a792020-03-10 07:31:21 -06002828
John Zulauf89311b42020-09-29 16:28:47 -06002829// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2830// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2831// We can overwrite them as *this* write is now after them.
2832//
2833// 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 -07002834void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002835 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06002836 last_read_stages = 0;
2837 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002838 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002839
2840 write_barriers = 0;
2841 write_dependency_chain = 0;
2842 write_tag = tag;
2843 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002844}
2845
John Zulauf89311b42020-09-29 16:28:47 -06002846// Apply the memory barrier without updating the existing barriers. The execution barrier
2847// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2848// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2849// replace the current write barriers or add to them, so accumulate to pending as well.
2850void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2851 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2852 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002853 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
2854 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
2855 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
2856 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulauf4a6105a2020-11-17 15:11:05 -07002857 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06002858 pending_write_barriers |= barrier.dst_access_scope;
2859 pending_write_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002860 }
John Zulauf89311b42020-09-29 16:28:47 -06002861 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2862 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002863
John Zulauf89311b42020-09-29 16:28:47 -06002864 if (!pending_layout_transition) {
2865 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2866 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002867 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06002868 // 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 -07002869 if (barrier.src_exec_scope & (read_access.stage | read_access.barriers)) {
2870 read_access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002871 }
2872 }
John Zulaufa0a98292020-09-18 09:30:10 -06002873 }
John Zulaufa0a98292020-09-18 09:30:10 -06002874}
2875
John Zulauf4a6105a2020-11-17 15:11:05 -07002876// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
2877// changes the "chaining" state, but to keep barriers independent. See discussion above.
2878void ResourceAccessState::ApplyBarrier(const ResourceUsageTag &scope_tag, const SyncBarrier &barrier, bool layout_transition) {
2879 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
2880 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
2881 // in order to know if it's in the excecution scope
2882 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
2883 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
2884 // errors w.r.t. "most recent" accesses.
2885 if (layout_transition || ((write_tag.IsBefore(scope_tag)) && (barrier.src_access_scope & last_write).any())) {
2886 pending_write_barriers |= barrier.dst_access_scope;
2887 pending_write_dep_chain |= barrier.dst_exec_scope;
2888 }
2889 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2890 pending_layout_transition |= layout_transition;
2891
2892 if (!pending_layout_transition) {
2893 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2894 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002895 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002896 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
2897 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
2898 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
2899 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
2900 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
2901 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
2902 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulaufab7756b2020-12-29 16:10:16 -07002903 if (read_access.tag.IsBefore(scope_tag) && (barrier.src_exec_scope & (read_access.stage | read_access.barriers))) {
2904 read_access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002905 }
2906 }
2907 }
2908}
John Zulauf89311b42020-09-29 16:28:47 -06002909void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
2910 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06002911 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2912 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07002913 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf89311b42020-09-29 16:28:47 -06002914 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002915 }
John Zulauf89311b42020-09-29 16:28:47 -06002916
2917 // Apply the accumulate execution barriers (and thus update chaining information)
2918 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07002919 for (auto &read_access : last_reads) {
2920 read_access.barriers |= read_access.pending_dep_chain;
2921 read_execution_barriers |= read_access.barriers;
2922 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06002923 }
2924
2925 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2926 write_dependency_chain |= pending_write_dep_chain;
2927 write_barriers |= pending_write_barriers;
2928 pending_write_dep_chain = 0;
2929 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002930}
2931
John Zulauf59e25072020-07-17 10:55:21 -06002932// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002933VkPipelineStageFlags ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
John Zulauf59e25072020-07-17 10:55:21 -06002934 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002935
John Zulaufab7756b2020-12-29 16:10:16 -07002936 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002937 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06002938 barriers = read_access.barriers;
2939 break;
John Zulauf59e25072020-07-17 10:55:21 -06002940 }
2941 }
John Zulauf4285ee92020-09-23 10:20:52 -06002942
John Zulauf59e25072020-07-17 10:55:21 -06002943 return barriers;
2944}
2945
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002946inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002947 assert(IsRead(usage));
2948 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
2949 // * the previous reads are not hazards, and thus last_write must be visible and available to
2950 // any reads that happen after.
2951 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2952 // 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 -07002953 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06002954}
2955
John Zulauf8e3c3e92021-01-06 11:19:36 -07002956VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002957 // Whether the stage are in the ordering scope only matters if the current write is ordered
2958 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
2959 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002960 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06002961 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06002962 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
2963 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
2964 }
2965
2966 return ordered_stages;
2967}
2968
John Zulauffaea0ee2021-01-14 14:01:32 -07002969void ResourceAccessState::UpdateFirst(const ResourceUsageTag &tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
2970 // Only record until we record a write.
2971 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
John Zulaufe7f6a5e2021-01-16 14:31:18 -07002972 const VkPipelineStageFlags usage_stage =
2973 IsRead(usage_index) ? static_cast<VkPipelineStageFlags>(PipelineStageBit(usage_index)) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07002974 if (0 == (usage_stage & first_read_stages_)) {
2975 // If this is a read we haven't seen or a write, record.
2976 first_read_stages_ |= usage_stage;
2977 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
2978 }
2979 }
2980}
2981
John Zulaufd1f85d42020-04-15 12:23:15 -06002982void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002983 auto *access_context = GetAccessContextNoInsert(command_buffer);
2984 if (access_context) {
2985 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002986 }
2987}
2988
John Zulaufd1f85d42020-04-15 12:23:15 -06002989void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2990 auto access_found = cb_access_state.find(command_buffer);
2991 if (access_found != cb_access_state.end()) {
2992 access_found->second->Reset();
2993 cb_access_state.erase(access_found);
2994 }
2995}
2996
John Zulauf9cb530d2019-09-30 14:14:10 -06002997bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2998 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2999 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003000 const auto *cb_context = GetAccessContext(commandBuffer);
3001 assert(cb_context);
3002 if (!cb_context) return skip;
3003 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003004
John Zulauf3d84f1b2020-03-09 13:33:25 -06003005 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003006 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003007 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003008
3009 for (uint32_t region = 0; region < regionCount; region++) {
3010 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003011 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003012 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003013 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003014 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003015 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003016 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003017 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003018 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003019 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003020 }
John Zulauf16adfc92020-04-08 10:28:33 -06003021 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003022 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06003023 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003024 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003025 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003026 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003027 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003028 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003029 }
3030 }
3031 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003032 }
3033 return skip;
3034}
3035
3036void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3037 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003038 auto *cb_context = GetAccessContext(commandBuffer);
3039 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003040 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003041 auto *context = cb_context->GetCurrentAccessContext();
3042
John Zulauf9cb530d2019-09-30 14:14:10 -06003043 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003044 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003045
3046 for (uint32_t region = 0; region < regionCount; region++) {
3047 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003048 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003049 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003050 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003051 }
John Zulauf16adfc92020-04-08 10:28:33 -06003052 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003053 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003054 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003055 }
3056 }
3057}
3058
John Zulauf4a6105a2020-11-17 15:11:05 -07003059void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3060 // Clear out events from the command buffer contexts
3061 for (auto &cb_context : cb_access_state) {
3062 cb_context.second->RecordDestroyEvent(event);
3063 }
3064}
3065
Jeff Leger178b1e52020-10-05 12:22:23 -04003066bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3067 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3068 bool skip = false;
3069 const auto *cb_context = GetAccessContext(commandBuffer);
3070 assert(cb_context);
3071 if (!cb_context) return skip;
3072 const auto *context = cb_context->GetCurrentAccessContext();
3073
3074 // If we have no previous accesses, we have no hazards
3075 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3076 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3077
3078 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3079 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3080 if (src_buffer) {
3081 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
3082 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
3083 if (hazard.hazard) {
3084 // TODO -- add tag information to log msg when useful.
3085 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3086 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3087 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003088 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003089 }
3090 }
3091 if (dst_buffer && !skip) {
3092 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
3093 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
3094 if (hazard.hazard) {
3095 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3096 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3097 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003098 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003099 }
3100 }
3101 if (skip) break;
3102 }
3103 return skip;
3104}
3105
3106void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3107 auto *cb_context = GetAccessContext(commandBuffer);
3108 assert(cb_context);
3109 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3110 auto *context = cb_context->GetCurrentAccessContext();
3111
3112 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3113 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3114
3115 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3116 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3117 if (src_buffer) {
3118 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003119 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003120 }
3121 if (dst_buffer) {
3122 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003123 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003124 }
3125 }
3126}
3127
John Zulauf5c5e88d2019-12-26 11:22:02 -07003128bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3129 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3130 const VkImageCopy *pRegions) const {
3131 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003132 const auto *cb_access_context = GetAccessContext(commandBuffer);
3133 assert(cb_access_context);
3134 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003135
John Zulauf3d84f1b2020-03-09 13:33:25 -06003136 const auto *context = cb_access_context->GetCurrentAccessContext();
3137 assert(context);
3138 if (!context) return skip;
3139
3140 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3141 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003142 for (uint32_t region = 0; region < regionCount; region++) {
3143 const auto &copy_region = pRegions[region];
3144 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003145 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003146 copy_region.srcOffset, copy_region.extent);
3147 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003148 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003149 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003150 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003151 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003152 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003153 }
3154
3155 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003156 VkExtent3D dst_copy_extent =
3157 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06003158 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003159 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003160 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003161 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003162 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003163 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003164 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003165 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003166 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003167 }
3168 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003169
John Zulauf5c5e88d2019-12-26 11:22:02 -07003170 return skip;
3171}
3172
3173void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3174 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3175 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003176 auto *cb_access_context = GetAccessContext(commandBuffer);
3177 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003178 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003179 auto *context = cb_access_context->GetCurrentAccessContext();
3180 assert(context);
3181
John Zulauf5c5e88d2019-12-26 11:22:02 -07003182 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003183 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003184
3185 for (uint32_t region = 0; region < regionCount; region++) {
3186 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003187 if (src_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07003188 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
3189 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003190 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003191 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003192 VkExtent3D dst_copy_extent =
3193 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003194 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
3195 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003196 }
3197 }
3198}
3199
Jeff Leger178b1e52020-10-05 12:22:23 -04003200bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3201 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3202 bool skip = false;
3203 const auto *cb_access_context = GetAccessContext(commandBuffer);
3204 assert(cb_access_context);
3205 if (!cb_access_context) return skip;
3206
3207 const auto *context = cb_access_context->GetCurrentAccessContext();
3208 assert(context);
3209 if (!context) return skip;
3210
3211 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3212 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3213 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3214 const auto &copy_region = pCopyImageInfo->pRegions[region];
3215 if (src_image) {
3216 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
3217 copy_region.srcOffset, copy_region.extent);
3218 if (hazard.hazard) {
3219 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3220 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3221 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003222 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003223 }
3224 }
3225
3226 if (dst_image) {
3227 VkExtent3D dst_copy_extent =
3228 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
3229 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
3230 copy_region.dstOffset, dst_copy_extent);
3231 if (hazard.hazard) {
3232 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3233 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3234 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003235 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003236 }
3237 if (skip) break;
3238 }
3239 }
3240
3241 return skip;
3242}
3243
3244void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3245 auto *cb_access_context = GetAccessContext(commandBuffer);
3246 assert(cb_access_context);
3247 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3248 auto *context = cb_access_context->GetCurrentAccessContext();
3249 assert(context);
3250
3251 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3252 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3253
3254 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3255 const auto &copy_region = pCopyImageInfo->pRegions[region];
3256 if (src_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07003257 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
3258 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003259 }
3260 if (dst_image) {
3261 VkExtent3D dst_copy_extent =
3262 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003263 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
3264 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003265 }
3266 }
3267}
3268
John Zulauf9cb530d2019-09-30 14:14:10 -06003269bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3270 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3271 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3272 uint32_t bufferMemoryBarrierCount,
3273 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3274 uint32_t imageMemoryBarrierCount,
3275 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3276 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003277 const auto *cb_access_context = GetAccessContext(commandBuffer);
3278 assert(cb_access_context);
3279 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003280
John Zulauf36ef9282021-02-02 11:47:24 -07003281 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3282 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3283 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3284 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003285 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003286 return skip;
3287}
3288
3289void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3290 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3291 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3292 uint32_t bufferMemoryBarrierCount,
3293 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3294 uint32_t imageMemoryBarrierCount,
3295 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003296 auto *cb_access_context = GetAccessContext(commandBuffer);
3297 assert(cb_access_context);
3298 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003299
John Zulauf36ef9282021-02-02 11:47:24 -07003300 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3301 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3302 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3303 pImageMemoryBarriers);
3304 pipeline_barrier.Record(cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003305}
3306
3307void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3308 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3309 // The state tracker sets up the device state
3310 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3311
John Zulauf5f13a792020-03-10 07:31:21 -06003312 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3313 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003314 // TODO: Find a good way to do this hooklessly.
3315 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3316 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3317 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3318
John Zulaufd1f85d42020-04-15 12:23:15 -06003319 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3320 sync_device_state->ResetCommandBufferCallback(command_buffer);
3321 });
3322 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3323 sync_device_state->FreeCommandBufferCallback(command_buffer);
3324 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003325}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003326
John Zulauf355e49b2020-04-24 15:11:15 -06003327bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf64ffe552021-02-06 10:25:07 -07003328 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd, const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003329 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003330 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003331 if (cb_context) {
3332 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo, cmd_name);
3333 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003334 }
John Zulauf355e49b2020-04-24 15:11:15 -06003335 return skip;
3336}
3337
3338bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3339 VkSubpassContents contents) const {
3340 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003341 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003342 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003343 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003344 return skip;
3345}
3346
3347bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003348 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003349 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003350 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003351 return skip;
3352}
3353
John Zulauf64ffe552021-02-06 10:25:07 -07003354static const char *kBeginRenderPass2KhrName = "vkCmdBeginRenderPass2KHR";
John Zulauf355e49b2020-04-24 15:11:15 -06003355bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3356 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003357 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003358 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003359 skip |=
3360 ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2, kBeginRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003361 return skip;
3362}
3363
John Zulauf3d84f1b2020-03-09 13:33:25 -06003364void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3365 VkResult result) {
3366 // The state tracker sets up the command buffer state
3367 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3368
3369 // Create/initialize the structure that trackers accesses at the command buffer scope.
3370 auto cb_access_context = GetAccessContext(commandBuffer);
3371 assert(cb_access_context);
3372 cb_access_context->Reset();
3373}
3374
3375void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf64ffe552021-02-06 10:25:07 -07003376 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd, const char *cmd_name) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003377 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003378 if (cb_context) {
John Zulauf64ffe552021-02-06 10:25:07 -07003379 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo, cmd_name);
3380 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003381 }
3382}
3383
3384void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3385 VkSubpassContents contents) {
3386 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003387 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003388 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003389 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003390}
3391
3392void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3393 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3394 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003395 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003396}
3397
3398void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3399 const VkRenderPassBeginInfo *pRenderPassBegin,
3400 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3401 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003402 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2, kBeginRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003403}
3404
Mike Schuchardt2df08912020-12-15 16:28:09 -08003405bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf64ffe552021-02-06 10:25:07 -07003406 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd, const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003407 bool skip = false;
3408
3409 auto cb_context = GetAccessContext(commandBuffer);
3410 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003411 if (!cb_context) return skip;
3412 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo, cmd_name);
3413 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003414}
3415
3416bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3417 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003418 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003419 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003420 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003421 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3422 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003423 return skip;
3424}
3425
John Zulauf64ffe552021-02-06 10:25:07 -07003426static const char *kNextSubpass2KhrName = "vkCmdNextSubpass2KHR";
Mike Schuchardt2df08912020-12-15 16:28:09 -08003427bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3428 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003429 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003430 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2, kNextSubpass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003431 return skip;
3432}
3433
3434bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3435 const VkSubpassEndInfo *pSubpassEndInfo) const {
3436 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003437 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003438 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003439}
3440
3441void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf64ffe552021-02-06 10:25:07 -07003442 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd, const char *cmd_name) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003443 auto cb_context = GetAccessContext(commandBuffer);
3444 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003445 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003446
John Zulauf64ffe552021-02-06 10:25:07 -07003447 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo, cmd_name);
3448 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003449}
3450
3451void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3452 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003453 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003454 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003455 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003456}
3457
3458void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3459 const VkSubpassEndInfo *pSubpassEndInfo) {
3460 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003461 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003462}
3463
3464void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3465 const VkSubpassEndInfo *pSubpassEndInfo) {
3466 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003467 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2, kNextSubpass2KhrName);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003468}
3469
John Zulauf64ffe552021-02-06 10:25:07 -07003470bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd,
3471 const char *cmd_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003472 bool skip = false;
3473
3474 auto cb_context = GetAccessContext(commandBuffer);
3475 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003476 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003477
John Zulauf64ffe552021-02-06 10:25:07 -07003478 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo, cmd_name);
3479 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003480 return skip;
3481}
3482
3483bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3484 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003485 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003486 return skip;
3487}
3488
Mike Schuchardt2df08912020-12-15 16:28:09 -08003489bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003490 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003491 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003492 return skip;
3493}
3494
John Zulauf64ffe552021-02-06 10:25:07 -07003495const static char *kEndRenderPass2KhrName = "vkEndRenderPass2KHR";
John Zulauf355e49b2020-04-24 15:11:15 -06003496bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003497 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003498 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003499 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2, kEndRenderPass2KhrName);
John Zulauf355e49b2020-04-24 15:11:15 -06003500 return skip;
3501}
3502
John Zulauf64ffe552021-02-06 10:25:07 -07003503void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd,
3504 const char *cmd_name) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003505 // Resolve the all subpass contexts to the command buffer contexts
3506 auto cb_context = GetAccessContext(commandBuffer);
3507 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003508 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003509
John Zulauf64ffe552021-02-06 10:25:07 -07003510 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo, cmd_name);
3511 sync_op.Record(cb_context);
3512 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003513}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003514
John Zulauf33fc1d52020-07-17 11:01:10 -06003515// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3516// updates to a resource which do not conflict at the byte level.
3517// TODO: Revisit this rule to see if it needs to be tighter or looser
3518// TODO: Add programatic control over suppression heuristics
3519bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3520 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3521}
3522
John Zulauf3d84f1b2020-03-09 13:33:25 -06003523void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003524 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003525 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003526}
3527
3528void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003529 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003530 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003531}
3532
3533void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf64ffe552021-02-06 10:25:07 -07003534 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2, kEndRenderPass2KhrName);
John Zulauf5a1a5382020-06-22 17:23:25 -06003535 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003536}
locke-lunarga19c71d2020-03-02 18:17:04 -07003537
Jeff Leger178b1e52020-10-05 12:22:23 -04003538template <typename BufferImageCopyRegionType>
3539bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3540 VkImageLayout dstImageLayout, uint32_t regionCount,
3541 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003542 bool skip = false;
3543 const auto *cb_access_context = GetAccessContext(commandBuffer);
3544 assert(cb_access_context);
3545 if (!cb_access_context) return skip;
3546
Jeff Leger178b1e52020-10-05 12:22:23 -04003547 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3548 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3549
locke-lunarga19c71d2020-03-02 18:17:04 -07003550 const auto *context = cb_access_context->GetCurrentAccessContext();
3551 assert(context);
3552 if (!context) return skip;
3553
3554 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003555 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3556
3557 for (uint32_t region = 0; region < regionCount; region++) {
3558 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003559 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003560 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003561 if (src_buffer) {
3562 ResourceAccessRange src_range =
3563 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
3564 hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
3565 if (hazard.hazard) {
3566 // PHASE1 TODO -- add tag information to log msg when useful.
3567 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3568 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3569 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003570 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003571 }
3572 }
3573
3574 hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
3575 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003576 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003577 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003578 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003579 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003580 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003581 }
3582 if (skip) break;
3583 }
3584 if (skip) break;
3585 }
3586 return skip;
3587}
3588
Jeff Leger178b1e52020-10-05 12:22:23 -04003589bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3590 VkImageLayout dstImageLayout, uint32_t regionCount,
3591 const VkBufferImageCopy *pRegions) const {
3592 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3593 COPY_COMMAND_VERSION_1);
3594}
3595
3596bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3597 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3598 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3599 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3600 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3601}
3602
3603template <typename BufferImageCopyRegionType>
3604void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3605 VkImageLayout dstImageLayout, uint32_t regionCount,
3606 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003607 auto *cb_access_context = GetAccessContext(commandBuffer);
3608 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003609
3610 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3611 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3612
3613 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003614 auto *context = cb_access_context->GetCurrentAccessContext();
3615 assert(context);
3616
3617 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003618 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003619
3620 for (uint32_t region = 0; region < regionCount; region++) {
3621 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07003622 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003623 if (src_buffer) {
3624 ResourceAccessRange src_range =
3625 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
3626 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
3627 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07003628 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
3629 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003630 }
3631 }
3632}
3633
Jeff Leger178b1e52020-10-05 12:22:23 -04003634void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3635 VkImageLayout dstImageLayout, uint32_t regionCount,
3636 const VkBufferImageCopy *pRegions) {
3637 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3638 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3639}
3640
3641void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3642 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3643 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3644 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3645 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3646 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3647}
3648
3649template <typename BufferImageCopyRegionType>
3650bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3651 VkBuffer dstBuffer, uint32_t regionCount,
3652 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003653 bool skip = false;
3654 const auto *cb_access_context = GetAccessContext(commandBuffer);
3655 assert(cb_access_context);
3656 if (!cb_access_context) return skip;
3657
Jeff Leger178b1e52020-10-05 12:22:23 -04003658 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3659 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3660
locke-lunarga19c71d2020-03-02 18:17:04 -07003661 const auto *context = cb_access_context->GetCurrentAccessContext();
3662 assert(context);
3663 if (!context) return skip;
3664
3665 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3666 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3667 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
3668 for (uint32_t region = 0; region < regionCount; region++) {
3669 const auto &copy_region = pRegions[region];
3670 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003671 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003672 copy_region.imageOffset, copy_region.imageExtent);
3673 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003674 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003675 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003676 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003677 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003678 }
John Zulauf477700e2021-01-06 11:41:49 -07003679 if (dst_mem) {
3680 ResourceAccessRange dst_range =
3681 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
3682 hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
3683 if (hazard.hazard) {
3684 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3685 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3686 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003687 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003688 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003689 }
3690 }
3691 if (skip) break;
3692 }
3693 return skip;
3694}
3695
Jeff Leger178b1e52020-10-05 12:22:23 -04003696bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3697 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3698 const VkBufferImageCopy *pRegions) const {
3699 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3700 COPY_COMMAND_VERSION_1);
3701}
3702
3703bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3704 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3705 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3706 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3707 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3708}
3709
3710template <typename BufferImageCopyRegionType>
3711void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3712 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3713 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003714 auto *cb_access_context = GetAccessContext(commandBuffer);
3715 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003716
3717 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3718 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3719
3720 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003721 auto *context = cb_access_context->GetCurrentAccessContext();
3722 assert(context);
3723
3724 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003725 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3726 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 -06003727 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003728
3729 for (uint32_t region = 0; region < regionCount; region++) {
3730 const auto &copy_region = pRegions[region];
3731 if (src_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07003732 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
3733 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003734 if (dst_buffer) {
3735 ResourceAccessRange dst_range =
3736 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
3737 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
3738 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003739 }
3740 }
3741}
3742
Jeff Leger178b1e52020-10-05 12:22:23 -04003743void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3744 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3745 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3746 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3747}
3748
3749void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3750 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3751 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3752 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3753 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3754 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3755}
3756
3757template <typename RegionType>
3758bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3759 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3760 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003761 bool skip = false;
3762 const auto *cb_access_context = GetAccessContext(commandBuffer);
3763 assert(cb_access_context);
3764 if (!cb_access_context) return skip;
3765
3766 const auto *context = cb_access_context->GetCurrentAccessContext();
3767 assert(context);
3768 if (!context) return skip;
3769
3770 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3771 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3772
3773 for (uint32_t region = 0; region < regionCount; region++) {
3774 const auto &blit_region = pRegions[region];
3775 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003776 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3777 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3778 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3779 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3780 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3781 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3782 auto hazard =
3783 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003784 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003785 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003786 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003787 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003788 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003789 }
3790 }
3791
3792 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003793 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3794 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3795 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3796 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3797 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3798 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3799 auto hazard =
3800 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003801 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003802 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003803 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003804 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003805 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003806 }
3807 if (skip) break;
3808 }
3809 }
3810
3811 return skip;
3812}
3813
Jeff Leger178b1e52020-10-05 12:22:23 -04003814bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3815 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3816 const VkImageBlit *pRegions, VkFilter filter) const {
3817 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3818 "vkCmdBlitImage");
3819}
3820
3821bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3822 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3823 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3824 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3825 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3826}
3827
3828template <typename RegionType>
3829void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3830 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3831 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003832 auto *cb_access_context = GetAccessContext(commandBuffer);
3833 assert(cb_access_context);
3834 auto *context = cb_access_context->GetCurrentAccessContext();
3835 assert(context);
3836
3837 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003838 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003839
3840 for (uint32_t region = 0; region < regionCount; region++) {
3841 const auto &blit_region = pRegions[region];
3842 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003843 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3844 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3845 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3846 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3847 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3848 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
John Zulauf8e3c3e92021-01-06 11:19:36 -07003849 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
3850 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003851 }
3852 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003853 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3854 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3855 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3856 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3857 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3858 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
John Zulauf8e3c3e92021-01-06 11:19:36 -07003859 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
3860 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003861 }
3862 }
3863}
locke-lunarg36ba2592020-04-03 09:42:04 -06003864
Jeff Leger178b1e52020-10-05 12:22:23 -04003865void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3866 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3867 const VkImageBlit *pRegions, VkFilter filter) {
3868 auto *cb_access_context = GetAccessContext(commandBuffer);
3869 assert(cb_access_context);
3870 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3871 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3872 pRegions, filter);
3873 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3874}
3875
3876void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3877 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3878 auto *cb_access_context = GetAccessContext(commandBuffer);
3879 assert(cb_access_context);
3880 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3881 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3882 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3883 pBlitImageInfo->filter, tag);
3884}
3885
John Zulauffaea0ee2021-01-14 14:01:32 -07003886bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
3887 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
3888 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
3889 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003890 bool skip = false;
3891 if (drawCount == 0) return skip;
3892
3893 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3894 VkDeviceSize size = struct_size;
3895 if (drawCount == 1 || stride == size) {
3896 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003897 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003898 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3899 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003900 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003901 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003902 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003903 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003904 }
3905 } else {
3906 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003907 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003908 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3909 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003910 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003911 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3912 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003913 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003914 break;
3915 }
3916 }
3917 }
3918 return skip;
3919}
3920
locke-lunarg61870c22020-06-09 14:51:50 -06003921void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3922 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3923 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003924 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3925 VkDeviceSize size = struct_size;
3926 if (drawCount == 1 || stride == size) {
3927 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003928 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003929 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003930 } else {
3931 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003932 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003933 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
3934 tag);
locke-lunargff255f92020-05-13 18:53:52 -06003935 }
3936 }
3937}
3938
John Zulauffaea0ee2021-01-14 14:01:32 -07003939bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
3940 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3941 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003942 bool skip = false;
3943
3944 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003945 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003946 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3947 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003948 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003949 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003950 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003951 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003952 }
3953 return skip;
3954}
3955
locke-lunarg61870c22020-06-09 14:51:50 -06003956void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003957 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003958 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003959 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003960}
3961
locke-lunarg36ba2592020-04-03 09:42:04 -06003962bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003963 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003964 const auto *cb_access_context = GetAccessContext(commandBuffer);
3965 assert(cb_access_context);
3966 if (!cb_access_context) return skip;
3967
locke-lunarg61870c22020-06-09 14:51:50 -06003968 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003969 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003970}
3971
3972void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003973 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003974 auto *cb_access_context = GetAccessContext(commandBuffer);
3975 assert(cb_access_context);
3976 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003977
locke-lunarg61870c22020-06-09 14:51:50 -06003978 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003979}
locke-lunarge1a67022020-04-29 00:15:36 -06003980
3981bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003982 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003983 const auto *cb_access_context = GetAccessContext(commandBuffer);
3984 assert(cb_access_context);
3985 if (!cb_access_context) return skip;
3986
3987 const auto *context = cb_access_context->GetCurrentAccessContext();
3988 assert(context);
3989 if (!context) return skip;
3990
locke-lunarg61870c22020-06-09 14:51:50 -06003991 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07003992 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
3993 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003994 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003995}
3996
3997void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003998 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003999 auto *cb_access_context = GetAccessContext(commandBuffer);
4000 assert(cb_access_context);
4001 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4002 auto *context = cb_access_context->GetCurrentAccessContext();
4003 assert(context);
4004
locke-lunarg61870c22020-06-09 14:51:50 -06004005 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4006 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004007}
4008
4009bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4010 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004011 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004012 const auto *cb_access_context = GetAccessContext(commandBuffer);
4013 assert(cb_access_context);
4014 if (!cb_access_context) return skip;
4015
locke-lunarg61870c22020-06-09 14:51:50 -06004016 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4017 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4018 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004019 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004020}
4021
4022void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4023 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004024 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004025 auto *cb_access_context = GetAccessContext(commandBuffer);
4026 assert(cb_access_context);
4027 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004028
locke-lunarg61870c22020-06-09 14:51:50 -06004029 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4030 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4031 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004032}
4033
4034bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4035 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004036 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004037 const auto *cb_access_context = GetAccessContext(commandBuffer);
4038 assert(cb_access_context);
4039 if (!cb_access_context) return skip;
4040
locke-lunarg61870c22020-06-09 14:51:50 -06004041 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4042 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4043 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004044 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004045}
4046
4047void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4048 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004049 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004050 auto *cb_access_context = GetAccessContext(commandBuffer);
4051 assert(cb_access_context);
4052 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004053
locke-lunarg61870c22020-06-09 14:51:50 -06004054 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4055 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4056 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004057}
4058
4059bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4060 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004061 bool skip = false;
4062 if (drawCount == 0) return skip;
4063
locke-lunargff255f92020-05-13 18:53:52 -06004064 const auto *cb_access_context = GetAccessContext(commandBuffer);
4065 assert(cb_access_context);
4066 if (!cb_access_context) return skip;
4067
4068 const auto *context = cb_access_context->GetCurrentAccessContext();
4069 assert(context);
4070 if (!context) return skip;
4071
locke-lunarg61870c22020-06-09 14:51:50 -06004072 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4073 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004074 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4075 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004076
4077 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4078 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4079 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004080 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004081 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004082}
4083
4084void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4085 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004086 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004087 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004088 auto *cb_access_context = GetAccessContext(commandBuffer);
4089 assert(cb_access_context);
4090 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4091 auto *context = cb_access_context->GetCurrentAccessContext();
4092 assert(context);
4093
locke-lunarg61870c22020-06-09 14:51:50 -06004094 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4095 cb_access_context->RecordDrawSubpassAttachment(tag);
4096 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004097
4098 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4099 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4100 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004101 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004102}
4103
4104bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4105 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004106 bool skip = false;
4107 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004108 const auto *cb_access_context = GetAccessContext(commandBuffer);
4109 assert(cb_access_context);
4110 if (!cb_access_context) return skip;
4111
4112 const auto *context = cb_access_context->GetCurrentAccessContext();
4113 assert(context);
4114 if (!context) return skip;
4115
locke-lunarg61870c22020-06-09 14:51:50 -06004116 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4117 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004118 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4119 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004120
4121 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4122 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4123 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004124 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004125 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004126}
4127
4128void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4129 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004130 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004131 auto *cb_access_context = GetAccessContext(commandBuffer);
4132 assert(cb_access_context);
4133 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4134 auto *context = cb_access_context->GetCurrentAccessContext();
4135 assert(context);
4136
locke-lunarg61870c22020-06-09 14:51:50 -06004137 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4138 cb_access_context->RecordDrawSubpassAttachment(tag);
4139 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004140
4141 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4142 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4143 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004144 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004145}
4146
4147bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4148 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4149 uint32_t stride, const char *function) const {
4150 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004151 const auto *cb_access_context = GetAccessContext(commandBuffer);
4152 assert(cb_access_context);
4153 if (!cb_access_context) return skip;
4154
4155 const auto *context = cb_access_context->GetCurrentAccessContext();
4156 assert(context);
4157 if (!context) return skip;
4158
locke-lunarg61870c22020-06-09 14:51:50 -06004159 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4160 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004161 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4162 maxDrawCount, stride, function);
4163 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004164
4165 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4166 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4167 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004168 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004169 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004170}
4171
4172bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4173 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4174 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004175 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4176 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004177}
4178
4179void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4180 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4181 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004182 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4183 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004184 auto *cb_access_context = GetAccessContext(commandBuffer);
4185 assert(cb_access_context);
4186 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
4187 auto *context = cb_access_context->GetCurrentAccessContext();
4188 assert(context);
4189
locke-lunarg61870c22020-06-09 14:51:50 -06004190 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4191 cb_access_context->RecordDrawSubpassAttachment(tag);
4192 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4193 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004194
4195 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4196 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4197 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004198 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004199}
4200
4201bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4202 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4203 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004204 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4205 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004206}
4207
4208void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4209 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4210 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004211 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4212 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004213 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004214}
4215
4216bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4217 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4218 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004219 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4220 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004221}
4222
4223void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4224 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4225 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004226 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4227 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004228 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4229}
4230
4231bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4232 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4233 uint32_t stride, const char *function) const {
4234 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004235 const auto *cb_access_context = GetAccessContext(commandBuffer);
4236 assert(cb_access_context);
4237 if (!cb_access_context) return skip;
4238
4239 const auto *context = cb_access_context->GetCurrentAccessContext();
4240 assert(context);
4241 if (!context) return skip;
4242
locke-lunarg61870c22020-06-09 14:51:50 -06004243 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4244 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004245 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4246 offset, maxDrawCount, stride, function);
4247 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004248
4249 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4250 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4251 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004252 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004253 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004254}
4255
4256bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4257 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4258 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004259 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4260 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004261}
4262
4263void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4264 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4265 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004266 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4267 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004268 auto *cb_access_context = GetAccessContext(commandBuffer);
4269 assert(cb_access_context);
4270 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4271 auto *context = cb_access_context->GetCurrentAccessContext();
4272 assert(context);
4273
locke-lunarg61870c22020-06-09 14:51:50 -06004274 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4275 cb_access_context->RecordDrawSubpassAttachment(tag);
4276 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4277 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004278
4279 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4280 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004281 // We will update the index and vertex buffer in SubmitQueue in the future.
4282 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004283}
4284
4285bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4286 VkDeviceSize offset, VkBuffer countBuffer,
4287 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4288 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004289 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4290 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004291}
4292
4293void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(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::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4297 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004298 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4299}
4300
4301bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4302 VkDeviceSize offset, VkBuffer countBuffer,
4303 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4304 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004305 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4306 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004307}
4308
4309void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4310 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4311 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004312 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4313 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004314 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4315}
4316
4317bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4318 const VkClearColorValue *pColor, uint32_t rangeCount,
4319 const VkImageSubresourceRange *pRanges) const {
4320 bool skip = false;
4321 const auto *cb_access_context = GetAccessContext(commandBuffer);
4322 assert(cb_access_context);
4323 if (!cb_access_context) return skip;
4324
4325 const auto *context = cb_access_context->GetCurrentAccessContext();
4326 assert(context);
4327 if (!context) return skip;
4328
4329 const auto *image_state = Get<IMAGE_STATE>(image);
4330
4331 for (uint32_t index = 0; index < rangeCount; index++) {
4332 const auto &range = pRanges[index];
4333 if (image_state) {
4334 auto hazard =
4335 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4336 if (hazard.hazard) {
4337 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004338 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004339 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004340 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004341 }
4342 }
4343 }
4344 return skip;
4345}
4346
4347void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4348 const VkClearColorValue *pColor, uint32_t rangeCount,
4349 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004350 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004351 auto *cb_access_context = GetAccessContext(commandBuffer);
4352 assert(cb_access_context);
4353 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4354 auto *context = cb_access_context->GetCurrentAccessContext();
4355 assert(context);
4356
4357 const auto *image_state = Get<IMAGE_STATE>(image);
4358
4359 for (uint32_t index = 0; index < rangeCount; index++) {
4360 const auto &range = pRanges[index];
4361 if (image_state) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004362 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, {0, 0, 0},
4363 image_state->createInfo.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004364 }
4365 }
4366}
4367
4368bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4369 VkImageLayout imageLayout,
4370 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4371 const VkImageSubresourceRange *pRanges) const {
4372 bool skip = false;
4373 const auto *cb_access_context = GetAccessContext(commandBuffer);
4374 assert(cb_access_context);
4375 if (!cb_access_context) return skip;
4376
4377 const auto *context = cb_access_context->GetCurrentAccessContext();
4378 assert(context);
4379 if (!context) return skip;
4380
4381 const auto *image_state = Get<IMAGE_STATE>(image);
4382
4383 for (uint32_t index = 0; index < rangeCount; index++) {
4384 const auto &range = pRanges[index];
4385 if (image_state) {
4386 auto hazard =
4387 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4388 if (hazard.hazard) {
4389 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004390 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004391 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004392 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004393 }
4394 }
4395 }
4396 return skip;
4397}
4398
4399void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4400 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4401 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004402 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004403 auto *cb_access_context = GetAccessContext(commandBuffer);
4404 assert(cb_access_context);
4405 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4406 auto *context = cb_access_context->GetCurrentAccessContext();
4407 assert(context);
4408
4409 const auto *image_state = Get<IMAGE_STATE>(image);
4410
4411 for (uint32_t index = 0; index < rangeCount; index++) {
4412 const auto &range = pRanges[index];
4413 if (image_state) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004414 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, {0, 0, 0},
4415 image_state->createInfo.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004416 }
4417 }
4418}
4419
4420bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4421 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4422 VkDeviceSize dstOffset, VkDeviceSize stride,
4423 VkQueryResultFlags flags) const {
4424 bool skip = false;
4425 const auto *cb_access_context = GetAccessContext(commandBuffer);
4426 assert(cb_access_context);
4427 if (!cb_access_context) return skip;
4428
4429 const auto *context = cb_access_context->GetCurrentAccessContext();
4430 assert(context);
4431 if (!context) return skip;
4432
4433 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4434
4435 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004436 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004437 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4438 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004439 skip |=
4440 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4441 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004442 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004443 }
4444 }
locke-lunargff255f92020-05-13 18:53:52 -06004445
4446 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004447 return skip;
4448}
4449
4450void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4451 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4452 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004453 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4454 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004455 auto *cb_access_context = GetAccessContext(commandBuffer);
4456 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004457 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004458 auto *context = cb_access_context->GetCurrentAccessContext();
4459 assert(context);
4460
4461 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4462
4463 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004464 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004465 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004466 }
locke-lunargff255f92020-05-13 18:53:52 -06004467
4468 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004469}
4470
4471bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4472 VkDeviceSize size, uint32_t data) const {
4473 bool skip = false;
4474 const auto *cb_access_context = GetAccessContext(commandBuffer);
4475 assert(cb_access_context);
4476 if (!cb_access_context) return skip;
4477
4478 const auto *context = cb_access_context->GetCurrentAccessContext();
4479 assert(context);
4480 if (!context) return skip;
4481
4482 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4483
4484 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004485 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004486 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4487 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004488 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004489 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004490 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004491 }
4492 }
4493 return skip;
4494}
4495
4496void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4497 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004498 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004499 auto *cb_access_context = GetAccessContext(commandBuffer);
4500 assert(cb_access_context);
4501 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4502 auto *context = cb_access_context->GetCurrentAccessContext();
4503 assert(context);
4504
4505 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4506
4507 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004508 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004509 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004510 }
4511}
4512
4513bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4514 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4515 const VkImageResolve *pRegions) const {
4516 bool skip = false;
4517 const auto *cb_access_context = GetAccessContext(commandBuffer);
4518 assert(cb_access_context);
4519 if (!cb_access_context) return skip;
4520
4521 const auto *context = cb_access_context->GetCurrentAccessContext();
4522 assert(context);
4523 if (!context) return skip;
4524
4525 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4526 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4527
4528 for (uint32_t region = 0; region < regionCount; region++) {
4529 const auto &resolve_region = pRegions[region];
4530 if (src_image) {
4531 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
4532 resolve_region.srcOffset, resolve_region.extent);
4533 if (hazard.hazard) {
4534 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004535 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004536 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004537 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004538 }
4539 }
4540
4541 if (dst_image) {
4542 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
4543 resolve_region.dstOffset, resolve_region.extent);
4544 if (hazard.hazard) {
4545 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004546 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004547 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004548 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004549 }
4550 if (skip) break;
4551 }
4552 }
4553
4554 return skip;
4555}
4556
4557void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4558 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4559 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004560 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4561 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004562 auto *cb_access_context = GetAccessContext(commandBuffer);
4563 assert(cb_access_context);
4564 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4565 auto *context = cb_access_context->GetCurrentAccessContext();
4566 assert(context);
4567
4568 auto *src_image = Get<IMAGE_STATE>(srcImage);
4569 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4570
4571 for (uint32_t region = 0; region < regionCount; region++) {
4572 const auto &resolve_region = pRegions[region];
4573 if (src_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004574 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
4575 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004576 }
4577 if (dst_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004578 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
4579 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004580 }
4581 }
4582}
4583
Jeff Leger178b1e52020-10-05 12:22:23 -04004584bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4585 const VkResolveImageInfo2KHR *pResolveImageInfo) 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>(pResolveImageInfo->srcImage);
4596 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4597
4598 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4599 const auto &resolve_region = pResolveImageInfo->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(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4605 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4606 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004607 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004608 }
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(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4616 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4617 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004618 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004619 }
4620 if (skip) break;
4621 }
4622 }
4623
4624 return skip;
4625}
4626
4627void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4628 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4629 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4630 auto *cb_access_context = GetAccessContext(commandBuffer);
4631 assert(cb_access_context);
4632 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4633 auto *context = cb_access_context->GetCurrentAccessContext();
4634 assert(context);
4635
4636 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4637 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4638
4639 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4640 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4641 if (src_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004642 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
4643 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004644 }
4645 if (dst_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004646 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
4647 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004648 }
4649 }
4650}
4651
locke-lunarge1a67022020-04-29 00:15:36 -06004652bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4653 VkDeviceSize dataSize, const void *pData) const {
4654 bool skip = false;
4655 const auto *cb_access_context = GetAccessContext(commandBuffer);
4656 assert(cb_access_context);
4657 if (!cb_access_context) return skip;
4658
4659 const auto *context = cb_access_context->GetCurrentAccessContext();
4660 assert(context);
4661 if (!context) return skip;
4662
4663 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4664
4665 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004666 // VK_WHOLE_SIZE not allowed
4667 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06004668 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4669 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004670 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004671 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004672 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004673 }
4674 }
4675 return skip;
4676}
4677
4678void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4679 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004680 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004681 auto *cb_access_context = GetAccessContext(commandBuffer);
4682 assert(cb_access_context);
4683 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4684 auto *context = cb_access_context->GetCurrentAccessContext();
4685 assert(context);
4686
4687 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4688
4689 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004690 // VK_WHOLE_SIZE not allowed
4691 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004692 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004693 }
4694}
locke-lunargff255f92020-05-13 18:53:52 -06004695
4696bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4697 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4698 bool skip = false;
4699 const auto *cb_access_context = GetAccessContext(commandBuffer);
4700 assert(cb_access_context);
4701 if (!cb_access_context) return skip;
4702
4703 const auto *context = cb_access_context->GetCurrentAccessContext();
4704 assert(context);
4705 if (!context) return skip;
4706
4707 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4708
4709 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004710 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004711 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4712 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004713 skip |=
4714 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4715 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004716 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004717 }
4718 }
4719 return skip;
4720}
4721
4722void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4723 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004724 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004725 auto *cb_access_context = GetAccessContext(commandBuffer);
4726 assert(cb_access_context);
4727 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4728 auto *context = cb_access_context->GetCurrentAccessContext();
4729 assert(context);
4730
4731 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4732
4733 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004734 const ResourceAccessRange range = MakeRange(dstOffset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004735 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004736 }
4737}
John Zulauf49beb112020-11-04 16:06:31 -07004738
4739bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
4740 bool skip = false;
4741 const auto *cb_context = GetAccessContext(commandBuffer);
4742 assert(cb_context);
4743 if (!cb_context) return skip;
4744
John Zulauf36ef9282021-02-02 11:47:24 -07004745 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004746 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004747}
4748
4749void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4750 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
4751 auto *cb_context = GetAccessContext(commandBuffer);
4752 assert(cb_context);
4753 if (!cb_context) return;
John Zulauf36ef9282021-02-02 11:47:24 -07004754 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4755 set_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004756}
4757
4758bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
4759 VkPipelineStageFlags stageMask) const {
4760 bool skip = false;
4761 const auto *cb_context = GetAccessContext(commandBuffer);
4762 assert(cb_context);
4763 if (!cb_context) return skip;
4764
John Zulauf36ef9282021-02-02 11:47:24 -07004765 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004766 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004767}
4768
4769void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4770 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
4771 auto *cb_context = GetAccessContext(commandBuffer);
4772 assert(cb_context);
4773 if (!cb_context) return;
4774
John Zulauf36ef9282021-02-02 11:47:24 -07004775 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4776 reset_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004777}
4778
4779bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4780 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4781 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4782 uint32_t bufferMemoryBarrierCount,
4783 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4784 uint32_t imageMemoryBarrierCount,
4785 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
4786 bool skip = false;
4787 const auto *cb_context = GetAccessContext(commandBuffer);
4788 assert(cb_context);
4789 if (!cb_context) return skip;
4790
John Zulauf36ef9282021-02-02 11:47:24 -07004791 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4792 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4793 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07004794 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004795}
4796
4797void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4798 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4799 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4800 uint32_t bufferMemoryBarrierCount,
4801 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4802 uint32_t imageMemoryBarrierCount,
4803 const VkImageMemoryBarrier *pImageMemoryBarriers) {
4804 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
4805 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
4806 imageMemoryBarrierCount, pImageMemoryBarriers);
4807
4808 auto *cb_context = GetAccessContext(commandBuffer);
4809 assert(cb_context);
4810 if (!cb_context) return;
4811
John Zulauf36ef9282021-02-02 11:47:24 -07004812 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4813 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4814 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
4815 return wait_events_op.Record(cb_context);
John Zulauf4a6105a2020-11-17 15:11:05 -07004816}
4817
4818void SyncEventState::ResetFirstScope() {
4819 for (const auto address_type : kAddressTypes) {
4820 first_scope[static_cast<size_t>(address_type)].clear();
4821 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07004822 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07004823}
4824
4825// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
4826SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(VkPipelineStageFlags srcStageMask) const {
4827 IgnoreReason reason = NotIgnored;
4828
4829 if (last_command == CMD_RESETEVENT && !HasBarrier(0U, 0U)) {
4830 reason = ResetWaitRace;
4831 } else if (unsynchronized_set) {
4832 reason = SetRace;
4833 } else {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07004834 const VkPipelineStageFlags missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07004835 if (missing_bits) reason = MissingStageBits;
4836 }
4837
4838 return reason;
4839}
4840
4841bool SyncEventState::HasBarrier(VkPipelineStageFlags stageMask, VkPipelineStageFlags exec_scope_arg) const {
4842 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
4843 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
4844 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07004845}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004846
John Zulauf36ef9282021-02-02 11:47:24 -07004847SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
4848 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4849 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07004850 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
4851 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
4852 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07004853 : SyncOpBase(cmd),
4854 dependency_flags_(dependencyFlags),
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004855 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, srcStageMask)),
4856 dst_exec_scope_(SyncExecScope::MakeDst(queue_flags, dstStageMask)) {
4857 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
4858 MakeMemoryBarriers(src_exec_scope_, dst_exec_scope_, dependencyFlags, memoryBarrierCount, pMemoryBarriers);
4859 MakeBufferMemoryBarriers(sync_state, src_exec_scope_, dst_exec_scope_, dependencyFlags, bufferMemoryBarrierCount,
4860 pBufferMemoryBarriers);
4861 MakeImageMemoryBarriers(sync_state, src_exec_scope_, dst_exec_scope_, dependencyFlags, imageMemoryBarrierCount,
4862 pImageMemoryBarriers);
4863}
4864
John Zulauf36ef9282021-02-02 11:47:24 -07004865SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07004866 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4867 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
4868 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
4869 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
4870 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07004871 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07004872 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
4873
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004874bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
4875 bool skip = false;
4876 const auto *context = cb_context.GetCurrentAccessContext();
4877 assert(context);
4878 if (!context) return skip;
4879 // Validate Image Layout transitions
Nathaniel Cesarioe3025c62021-02-03 16:36:22 -07004880 for (const auto &image_barrier : image_memory_barriers_) {
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004881 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
4882 const auto *image_state = image_barrier.image.get();
4883 if (!image_state) continue;
4884 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
4885 if (hazard.hazard) {
4886 // PHASE1 TODO -- add tag information to log msg when useful.
4887 const auto &sync_state = cb_context.GetSyncState();
4888 const auto image_handle = image_state->image;
4889 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
4890 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
4891 string_SyncHazard(hazard.hazard), image_barrier.index,
4892 sync_state.report_data->FormatHandle(image_handle).c_str(),
4893 cb_context.FormatUsage(hazard).c_str());
4894 }
4895 }
4896
4897 return skip;
4898}
4899
John Zulaufd5115702021-01-18 12:34:33 -07004900struct SyncOpPipelineBarrierFunctorFactory {
4901 using BarrierOpFunctor = PipelineBarrierOp;
4902 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
4903 using GlobalBarrierOpFunctor = PipelineBarrierOp;
4904 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
4905 using BufferRange = ResourceAccessRange;
4906 using ImageRange = subresource_adapter::ImageRangeGenerator;
4907 using GlobalRange = ResourceAccessRange;
4908
4909 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
4910 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
4911 }
4912 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
4913 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
4914 }
4915 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
4916 return GlobalBarrierOpFunctor(barrier, false);
4917 }
4918
4919 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
4920 if (!SimpleBinding(buffer)) return ResourceAccessRange();
4921 const auto base_address = ResourceBaseAddress(buffer);
4922 return (range + base_address);
4923 }
4924 ImageRange MakeRangeGen(const IMAGE_STATE &image, const SyncImageMemoryBarrier::SubImageRange &range) const {
John Zulauf264cce02021-02-05 14:40:47 -07004925 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07004926
4927 const auto base_address = ResourceBaseAddress(image);
4928 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), range.subresource_range, range.offset,
4929 range.extent, base_address);
4930 return range_gen;
4931 }
4932 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
4933};
4934
4935template <typename Barriers, typename FunctorFactory>
4936void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
4937 AccessContext *context) {
4938 for (const auto &barrier : barriers) {
4939 const auto *state = barrier.GetState();
4940 if (state) {
4941 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
4942 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
4943 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
4944 UpdateMemoryAccessState(accesses, update_action, &range_gen);
4945 }
4946 }
4947}
4948
4949template <typename Barriers, typename FunctorFactory>
4950void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
4951 AccessContext *access_context) {
4952 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
4953 for (const auto &barrier : barriers) {
4954 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
4955 }
4956 for (const auto address_type : kAddressTypes) {
4957 auto range_gen = factory.MakeGlobalRangeGen(address_type);
4958 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
4959 }
4960}
4961
John Zulauf36ef9282021-02-02 11:47:24 -07004962void SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07004963 SyncOpPipelineBarrierFunctorFactory factory;
4964 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf36ef9282021-02-02 11:47:24 -07004965 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07004966 ApplyBarriers(buffer_memory_barriers_, factory, tag, access_context);
4967 ApplyBarriers(image_memory_barriers_, factory, tag, access_context);
4968 ApplyGlobalBarriers(memory_barriers_, factory, tag, access_context);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004969
4970 cb_context->ApplyGlobalBarriersToEvents(src_exec_scope_, dst_exec_scope_);
4971}
4972
John Zulaufd5115702021-01-18 12:34:33 -07004973void SyncOpBarriers::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst, VkDependencyFlags dependency_flags,
4974 uint32_t memory_barrier_count, const VkMemoryBarrier *memory_barriers) {
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004975 memory_barriers_.reserve(std::min<uint32_t>(1, memory_barrier_count));
4976 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
4977 const auto &barrier = memory_barriers[barrier_index];
4978 SyncBarrier sync_barrier(barrier, src, dst);
4979 memory_barriers_.emplace_back(sync_barrier);
4980 }
4981 if (0 == memory_barrier_count) {
4982 // If there are no global memory barriers, force an exec barrier
4983 memory_barriers_.emplace_back(SyncBarrier(src, dst));
4984 }
4985}
4986
John Zulaufd5115702021-01-18 12:34:33 -07004987void SyncOpBarriers::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src, const SyncExecScope &dst,
4988 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
4989 const VkBufferMemoryBarrier *barriers) {
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004990 buffer_memory_barriers_.reserve(barrier_count);
4991 for (uint32_t index = 0; index < barrier_count; index++) {
4992 const auto &barrier = barriers[index];
4993 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
4994 if (buffer) {
4995 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
4996 const auto range = MakeRange(barrier.offset, barrier_size);
4997 const SyncBarrier sync_barrier(barrier, src, dst);
4998 buffer_memory_barriers_.emplace_back(buffer, sync_barrier, range);
4999 } else {
5000 buffer_memory_barriers_.emplace_back();
5001 }
5002 }
5003}
5004
John Zulaufd5115702021-01-18 12:34:33 -07005005void SyncOpBarriers::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src, const SyncExecScope &dst,
5006 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5007 const VkImageMemoryBarrier *barriers) {
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005008 image_memory_barriers_.reserve(barrier_count);
5009 for (uint32_t index = 0; index < barrier_count; index++) {
5010 const auto &barrier = barriers[index];
5011 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5012 if (image) {
5013 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5014 const SyncBarrier sync_barrier(barrier, src, dst);
5015 image_memory_barriers_.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout,
5016 subresource_range);
5017 } else {
5018 image_memory_barriers_.emplace_back();
5019 image_memory_barriers_.back().index = index; // Just in case we're interested in the ones we skipped.
5020 }
5021 }
5022}
John Zulaufd5115702021-01-18 12:34:33 -07005023
John Zulauf36ef9282021-02-02 11:47:24 -07005024SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005025 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5026 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5027 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5028 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005029 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005030 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5031 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005032 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005033}
5034
5035bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005036 const char *const ignored = "Wait operation is ignored for this event.";
5037 bool skip = false;
5038 const auto &sync_state = cb_context.GetSyncState();
5039 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer;
5040
5041 if (src_exec_scope_.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
John Zulaufd5115702021-01-18 12:34:33 -07005042 const char *const vuid = "SYNC-vkCmdWaitEvents-hostevent-unsupported";
5043 skip = sync_state.LogInfo(command_buffer_handle, vuid,
John Zulauf36ef9282021-02-02 11:47:24 -07005044 "%s, srcStageMask includes %s, unsupported by synchronization validaton.", CmdName(),
John Zulaufd5115702021-01-18 12:34:33 -07005045 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT), ignored);
5046 }
5047
5048 VkPipelineStageFlags event_stage_masks = 0U;
5049 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005050 const auto *events_context = cb_context.GetCurrentEventsContext();
5051 assert(events_context);
5052 for (const auto &sync_event_pair : *events_context) {
5053 const auto *sync_event = sync_event_pair.second.get();
John Zulaufd5115702021-01-18 12:34:33 -07005054 if (!sync_event) {
5055 // 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 -07005056 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5057 // new validation error... wait without previously submitted set event...
5058 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 -07005059
5060 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
5061 }
5062 const auto event = sync_event->event->event;
5063 // TODO add "destroyed" checks
5064
5065 event_stage_masks |= sync_event->scope.mask_param;
5066 const auto ignore_reason = sync_event->IsIgnoredByWait(src_exec_scope_.mask_param);
5067 if (ignore_reason) {
5068 switch (ignore_reason) {
5069 case SyncEventState::ResetWaitRace: {
John Zulaufd5115702021-01-18 12:34:33 -07005070 const char *const vuid = "SYNC-vkCmdWaitEvents-missingbarrier-reset";
5071 const char *const message =
5072 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
John Zulauf36ef9282021-02-02 11:47:24 -07005073 skip |=
5074 sync_state.LogError(event, vuid, message, CmdName(), sync_state.report_data->FormatHandle(event).c_str(),
5075 CmdName(), CommandTypeString(sync_event->last_command), ignored);
John Zulaufd5115702021-01-18 12:34:33 -07005076 break;
5077 }
5078 case SyncEventState::SetRace: {
5079 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for this
5080 // event
John Zulaufd5115702021-01-18 12:34:33 -07005081 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5082 const char *const message =
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07005083 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
John Zulaufd5115702021-01-18 12:34:33 -07005084 const char *const reason = "First synchronization scope is undefined.";
John Zulauf36ef9282021-02-02 11:47:24 -07005085 skip |=
5086 sync_state.LogError(event, vuid, message, CmdName(), sync_state.report_data->FormatHandle(event).c_str(),
5087 CommandTypeString(sync_event->last_command), reason, ignored);
John Zulaufd5115702021-01-18 12:34:33 -07005088 break;
5089 }
5090 case SyncEventState::MissingStageBits: {
5091 const VkPipelineStageFlags missing_bits = sync_event->scope.mask_param & ~src_exec_scope_.mask_param;
5092 // Issue error message that event waited for is not in wait events scope
John Zulaufd5115702021-01-18 12:34:33 -07005093 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5094 const char *const message =
5095 "%s: %s stageMask 0x%" PRIx32 " includes bits not present in srcStageMask 0x%" PRIx32
5096 ". Bits missing from srcStageMask %s. %s";
John Zulauf36ef9282021-02-02 11:47:24 -07005097 skip |=
5098 sync_state.LogError(event, vuid, message, CmdName(), sync_state.report_data->FormatHandle(event).c_str(),
5099 sync_event->scope.mask_param, src_exec_scope_.mask_param,
5100 string_VkPipelineStageFlags(missing_bits).c_str(), ignored);
John Zulaufd5115702021-01-18 12:34:33 -07005101 break;
5102 }
5103 default:
5104 assert(ignore_reason == SyncEventState::NotIgnored);
5105 }
5106 } else if (image_memory_barriers_.size()) {
5107 const auto *context = cb_context.GetCurrentAccessContext();
5108 assert(context);
5109 for (const auto &image_memory_barrier : image_memory_barriers_) {
5110 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5111 const auto *image_state = image_memory_barrier.image.get();
5112 if (!image_state) continue;
5113 const auto &subresource_range = image_memory_barrier.range.subresource_range;
5114 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5115 const auto hazard =
5116 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5117 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5118 if (hazard.hazard) {
John Zulaufd5115702021-01-18 12:34:33 -07005119 skip |= sync_state.LogError(image_state->image, string_SyncHazardVUID(hazard.hazard),
John Zulauf36ef9282021-02-02 11:47:24 -07005120 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
John Zulaufd5115702021-01-18 12:34:33 -07005121 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
5122 sync_state.report_data->FormatHandle(image_state->image).c_str(),
5123 cb_context.FormatUsage(hazard).c_str());
5124 break;
5125 }
5126 }
5127 }
5128 }
5129
5130 // Note that we can't check for HOST in pEvents as we don't track that set event type
5131 const auto extra_stage_bits = (src_exec_scope_.mask_param & ~VK_PIPELINE_STAGE_HOST_BIT) & ~event_stage_masks;
5132 if (extra_stage_bits) {
5133 // Issue error message that event waited for is not in wait events scope
John Zulaufd5115702021-01-18 12:34:33 -07005134 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5135 const char *const message =
5136 "%s: srcStageMask 0x%" PRIx32 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
5137 if (events_not_found) {
John Zulauf36ef9282021-02-02 11:47:24 -07005138 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), src_exec_scope_.mask_param,
John Zulaufd5115702021-01-18 12:34:33 -07005139 string_VkPipelineStageFlags(extra_stage_bits).c_str(),
5140 " vkCmdSetEvent may be in previously submitted command buffer.");
5141 } else {
John Zulauf36ef9282021-02-02 11:47:24 -07005142 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), src_exec_scope_.mask_param,
John Zulaufd5115702021-01-18 12:34:33 -07005143 string_VkPipelineStageFlags(extra_stage_bits).c_str(), "");
5144 }
5145 }
5146 return skip;
5147}
5148
5149struct SyncOpWaitEventsFunctorFactory {
5150 using BarrierOpFunctor = WaitEventBarrierOp;
5151 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5152 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5153 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5154 using BufferRange = EventSimpleRangeGenerator;
5155 using ImageRange = EventImageRangeGenerator;
5156 using GlobalRange = EventSimpleRangeGenerator;
5157
5158 // Need to restrict to only valid exec and access scope for this event
5159 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5160 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
5161 barrier.src_exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope;
5162 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5163 return barrier;
5164 }
5165 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5166 auto barrier = RestrictToEvent(barrier_arg);
5167 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5168 }
5169 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
5170 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5171 }
5172 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5173 auto barrier = RestrictToEvent(barrier_arg);
5174 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5175 }
5176
5177 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5178 const AccessAddressType address_type = GetAccessAddressType(buffer);
5179 const auto base_address = ResourceBaseAddress(buffer);
5180 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5181 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5182 return filtered_range_gen;
5183 }
5184 ImageRange MakeRangeGen(const IMAGE_STATE &image, const SyncImageMemoryBarrier::SubImageRange &range) const {
5185 if (!SimpleBinding(image)) return ImageRange();
5186 const auto address_type = GetAccessAddressType(image);
5187 const auto base_address = ResourceBaseAddress(image);
5188 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), range.subresource_range,
5189 range.offset, range.extent, base_address);
5190 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5191
5192 return filtered_range_gen;
5193 }
5194 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5195 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5196 }
5197 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5198 SyncEventState *sync_event;
5199};
5200
John Zulauf36ef9282021-02-02 11:47:24 -07005201void SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
5202 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07005203 auto *access_context = cb_context->GetCurrentAccessContext();
5204 assert(access_context);
5205 if (!access_context) return;
John Zulauf669dfd52021-01-27 17:15:28 -07005206 auto *events_context = cb_context->GetCurrentEventsContext();
5207 assert(events_context);
5208 if (!events_context) return;
John Zulaufd5115702021-01-18 12:34:33 -07005209
5210 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
5211 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
5212 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
5213 access_context->ResolvePreviousAccesses();
5214
5215 const auto &dst = dst_exec_scope_;
5216 // TODO... this needs change the SyncEventContext it's using depending on whether this is replay... the recorded
5217 // 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 -07005218 for (auto &event_shared : events_) {
5219 if (!event_shared.get()) continue;
5220 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07005221
5222 sync_event->last_command = CMD_WAITEVENTS;
5223
5224 if (!sync_event->IsIgnoredByWait(src_exec_scope_.mask_param)) {
5225 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
5226 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
5227 // of the barriers is maintained.
5228 SyncOpWaitEventsFunctorFactory factory(sync_event);
5229 ApplyBarriers(buffer_memory_barriers_, factory, tag, access_context);
5230 ApplyBarriers(image_memory_barriers_, factory, tag, access_context);
5231 ApplyGlobalBarriers(memory_barriers_, factory, tag, access_context);
5232
5233 // Apply the global barrier to the event itself (for race condition tracking)
5234 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
5235 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
5236 sync_event->barriers |= dst.exec_scope;
5237 } else {
5238 // We ignored this wait, so we don't have any effective synchronization barriers for it.
5239 sync_event->barriers = 0U;
5240 }
5241 }
5242
5243 // Apply the pending barriers
5244 ResolvePendingBarrierFunctor apply_pending_action(tag);
5245 access_context->ApplyToContext(apply_pending_action);
5246}
5247
John Zulauf669dfd52021-01-27 17:15:28 -07005248void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07005249 events_.reserve(event_count);
5250 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
John Zulauf669dfd52021-01-27 17:15:28 -07005251 events_.emplace_back(sync_state.GetShared<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07005252 }
5253}
John Zulauf6ce24372021-01-30 05:56:25 -07005254
John Zulauf36ef9282021-02-02 11:47:24 -07005255SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf6ce24372021-01-30 05:56:25 -07005256 VkPipelineStageFlags stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005257 : SyncOpBase(cmd),
5258 event_(sync_state.GetShared<EVENT_STATE>(event)),
5259 exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005260
5261bool SyncOpResetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005262 auto *events_context = cb_context.GetCurrentEventsContext();
5263 assert(events_context);
5264 bool skip = false;
5265 if (!events_context) return skip;
5266
5267 const auto &sync_state = cb_context.GetSyncState();
5268 const auto *sync_event = events_context->Get(event_);
5269 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5270
5271 const char *const set_wait =
5272 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5273 "hazards.";
5274 const char *message = set_wait; // Only one message this call.
5275 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
5276 const char *vuid = nullptr;
5277 switch (sync_event->last_command) {
5278 case CMD_SETEVENT:
5279 // Needs a barrier between set and reset
5280 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
5281 break;
5282 case CMD_WAITEVENTS: {
5283 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
5284 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
5285 break;
5286 }
5287 default:
5288 // The only other valid last command that wasn't one.
5289 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT));
5290 break;
5291 }
5292 if (vuid) {
John Zulauf36ef9282021-02-02 11:47:24 -07005293 skip |= sync_state.LogError(event_->event, vuid, message, CmdName(),
5294 sync_state.report_data->FormatHandle(event_->event).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005295 CommandTypeString(sync_event->last_command));
5296 }
5297 }
5298 return skip;
5299}
5300
John Zulauf36ef9282021-02-02 11:47:24 -07005301void SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005302 auto *events_context = cb_context->GetCurrentEventsContext();
5303 assert(events_context);
5304 if (!events_context) return;
5305
5306 auto *sync_event = events_context->GetFromShared(event_);
5307 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5308
5309 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07005310 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005311 sync_event->unsynchronized_set = CMD_NONE;
5312 sync_event->ResetFirstScope();
5313 sync_event->barriers = 0U;
5314}
5315
John Zulauf36ef9282021-02-02 11:47:24 -07005316SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf6ce24372021-01-30 05:56:25 -07005317 VkPipelineStageFlags stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005318 : SyncOpBase(cmd),
5319 event_(sync_state.GetShared<EVENT_STATE>(event)),
5320 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005321
5322bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
5323 // I'll put this here just in case we need to pass this in for future extension support
John Zulauf6ce24372021-01-30 05:56:25 -07005324 bool skip = false;
5325
5326 const auto &sync_state = cb_context.GetSyncState();
5327 auto *events_context = cb_context.GetCurrentEventsContext();
5328 assert(events_context);
5329 if (!events_context) return skip;
5330
5331 const auto *sync_event = events_context->Get(event_);
5332 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5333
5334 const char *const reset_set =
5335 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5336 "hazards.";
5337 const char *const wait =
5338 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
5339
5340 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
5341 const char *vuid = nullptr;
5342 const char *message = nullptr;
5343 switch (sync_event->last_command) {
5344 case CMD_RESETEVENT:
5345 // Needs a barrier between reset and set
5346 vuid = "SYNC-vkCmdSetEvent-missingbarrier-reset";
5347 message = reset_set;
5348 break;
5349 case CMD_SETEVENT:
5350 // Needs a barrier between set and set
5351 vuid = "SYNC-vkCmdSetEvent-missingbarrier-set";
5352 message = reset_set;
5353 break;
5354 case CMD_WAITEVENTS:
5355 // Needs a barrier or is in second execution scope
5356 vuid = "SYNC-vkCmdSetEvent-missingbarrier-wait";
5357 message = wait;
5358 break;
5359 default:
5360 // The only other valid last command that wasn't one.
5361 assert(sync_event->last_command == CMD_NONE);
5362 break;
5363 }
5364 if (vuid) {
5365 assert(nullptr != message);
John Zulauf36ef9282021-02-02 11:47:24 -07005366 skip |= sync_state.LogError(event_->event, vuid, message, CmdName(),
5367 sync_state.report_data->FormatHandle(event_->event).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005368 CommandTypeString(sync_event->last_command));
5369 }
5370 }
5371
5372 return skip;
5373}
5374
John Zulauf36ef9282021-02-02 11:47:24 -07005375void SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
5376 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07005377 auto *events_context = cb_context->GetCurrentEventsContext();
5378 auto *access_context = cb_context->GetCurrentAccessContext();
5379 assert(events_context);
5380 if (!events_context) return;
5381
5382 auto *sync_event = events_context->GetFromShared(event_);
5383 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5384
5385 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
5386 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
5387 // any issues caused by naive scope setting here.
5388
5389 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
5390 // Given:
5391 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
5392 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
5393
5394 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
5395 sync_event->unsynchronized_set = sync_event->last_command;
5396 sync_event->ResetFirstScope();
5397 } else if (sync_event->scope.exec_scope == 0) {
5398 // We only set the scope if there isn't one
5399 sync_event->scope = src_exec_scope_;
5400
5401 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
5402 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
5403 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
5404 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
5405 }
5406 };
5407 access_context->ForAll(set_scope);
5408 sync_event->unsynchronized_set = CMD_NONE;
5409 sync_event->first_scope_tag = tag;
5410 }
5411 sync_event->last_command = CMD_SETEVENT;
5412 sync_event->barriers = 0U;
5413}
John Zulauf64ffe552021-02-06 10:25:07 -07005414
5415SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
5416 const VkRenderPassBeginInfo *pRenderPassBegin,
5417 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *cmd_name)
5418 : SyncOpBase(cmd, cmd_name) {
5419 if (pRenderPassBegin) {
5420 rp_state_ = sync_state.GetShared<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
5421 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
5422 const auto *fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
5423 if (fb_state) {
5424 shared_attachments_ = sync_state.GetSharedAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
5425 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
5426 // Note that this a safe to presist as long as shared_attachments is not cleared
5427 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08005428 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07005429 attachments_.emplace_back(attachment.get());
5430 }
5431 }
5432 if (pSubpassBeginInfo) {
5433 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
5434 }
5435 }
5436}
5437
5438bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5439 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
5440 bool skip = false;
5441
5442 assert(rp_state_.get());
5443 if (nullptr == rp_state_.get()) return skip;
5444 auto &rp_state = *rp_state_.get();
5445
5446 const uint32_t subpass = 0;
5447
5448 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
5449 // hasn't happened yet)
5450 const std::vector<AccessContext> empty_context_vector;
5451 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
5452 cb_context.GetCurrentAccessContext());
5453
5454 // Validate attachment operations
5455 if (attachments_.size() == 0) return skip;
5456 const auto &render_area = renderpass_begin_info_.renderArea;
5457 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, attachments_, CmdName());
5458
5459 // Validate load operations if there were no layout transition hazards
5460 if (!skip) {
5461 temp_context.RecordLayoutTransitions(rp_state, subpass, attachments_, kCurrentCommandTag);
5462 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, attachments_, CmdName());
5463 }
5464
5465 return skip;
5466}
5467
5468void SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5469 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5470 assert(rp_state_.get());
5471 if (nullptr == rp_state_.get()) return;
5472 const auto tag = cb_context->NextCommandTag(cmd_);
5473 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
5474}
5475
5476SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
5477 const VkSubpassEndInfo *pSubpassEndInfo, const char *name_override)
5478 : SyncOpBase(cmd, name_override) {
5479 if (pSubpassBeginInfo) {
5480 subpass_begin_info_.initialize(pSubpassBeginInfo);
5481 }
5482 if (pSubpassEndInfo) {
5483 subpass_end_info_.initialize(pSubpassEndInfo);
5484 }
5485}
5486
5487bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
5488 bool skip = false;
5489 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5490 if (!renderpass_context) return skip;
5491
5492 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
5493 return skip;
5494}
5495
5496void SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
5497 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5498 cb_context->RecordNextSubpass(cmd_);
5499}
5500
5501SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo,
5502 const char *name_override)
5503 : SyncOpBase(cmd, name_override) {
5504 if (pSubpassEndInfo) {
5505 subpass_end_info_.initialize(pSubpassEndInfo);
5506 }
5507}
5508
5509bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5510 bool skip = false;
5511 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5512
5513 if (!renderpass_context) return skip;
5514 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
5515 return skip;
5516}
5517
5518void SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5519 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5520 cb_context->RecordEndRenderPass(cmd_);
5521}