blob: 5429e5207c51ae91bdabc958c27ea4de9aa5138b [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"
27
John Zulauf43cc7462020-12-03 12:33:12 -070028const static std::array<AccessAddressType, static_cast<size_t>(AccessAddressType::kTypeCount)> kAddressTypes = {
29 AccessAddressType::kLinear, AccessAddressType::kIdealized};
30
John Zulauf9cb530d2019-09-30 14:14:10 -060031static const char *string_SyncHazardVUID(SyncHazard hazard) {
32 switch (hazard) {
33 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070034 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060035 break;
36 case SyncHazard::READ_AFTER_WRITE:
37 return "SYNC-HAZARD-READ_AFTER_WRITE";
38 break;
39 case SyncHazard::WRITE_AFTER_READ:
40 return "SYNC-HAZARD-WRITE_AFTER_READ";
41 break;
42 case SyncHazard::WRITE_AFTER_WRITE:
43 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
44 break;
John Zulauf2f952d22020-02-10 11:34:51 -070045 case SyncHazard::READ_RACING_WRITE:
46 return "SYNC-HAZARD-READ-RACING-WRITE";
47 break;
48 case SyncHazard::WRITE_RACING_WRITE:
49 return "SYNC-HAZARD-WRITE-RACING-WRITE";
50 break;
51 case SyncHazard::WRITE_RACING_READ:
52 return "SYNC-HAZARD-WRITE-RACING-READ";
53 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060054 default:
55 assert(0);
56 }
57 return "SYNC-HAZARD-INVALID";
58}
59
John Zulauf59e25072020-07-17 10:55:21 -060060static bool IsHazardVsRead(SyncHazard hazard) {
61 switch (hazard) {
62 case SyncHazard::NONE:
63 return false;
64 break;
65 case SyncHazard::READ_AFTER_WRITE:
66 return false;
67 break;
68 case SyncHazard::WRITE_AFTER_READ:
69 return true;
70 break;
71 case SyncHazard::WRITE_AFTER_WRITE:
72 return false;
73 break;
74 case SyncHazard::READ_RACING_WRITE:
75 return false;
76 break;
77 case SyncHazard::WRITE_RACING_WRITE:
78 return false;
79 break;
80 case SyncHazard::WRITE_RACING_READ:
81 return true;
82 break;
83 default:
84 assert(0);
85 }
86 return false;
87}
88
John Zulauf9cb530d2019-09-30 14:14:10 -060089static const char *string_SyncHazard(SyncHazard hazard) {
90 switch (hazard) {
91 case SyncHazard::NONE:
92 return "NONR";
93 break;
94 case SyncHazard::READ_AFTER_WRITE:
95 return "READ_AFTER_WRITE";
96 break;
97 case SyncHazard::WRITE_AFTER_READ:
98 return "WRITE_AFTER_READ";
99 break;
100 case SyncHazard::WRITE_AFTER_WRITE:
101 return "WRITE_AFTER_WRITE";
102 break;
John Zulauf2f952d22020-02-10 11:34:51 -0700103 case SyncHazard::READ_RACING_WRITE:
104 return "READ_RACING_WRITE";
105 break;
106 case SyncHazard::WRITE_RACING_WRITE:
107 return "WRITE_RACING_WRITE";
108 break;
109 case SyncHazard::WRITE_RACING_READ:
110 return "WRITE_RACING_READ";
111 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600112 default:
113 assert(0);
114 }
115 return "INVALID HAZARD";
116}
117
John Zulauf37ceaed2020-07-03 16:18:15 -0600118static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
119 // Return the info for the first bit found
120 const SyncStageAccessInfoType *info = nullptr;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700121 for (size_t i = 0; i < flags.size(); i++) {
122 if (flags.test(i)) {
123 info = &syncStageAccessInfoByStageAccessIndex[i];
124 break;
John Zulauf37ceaed2020-07-03 16:18:15 -0600125 }
126 }
127 return info;
128}
129
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700130static std::string string_SyncStageAccessFlags(const SyncStageAccessFlags &flags, const char *sep = "|") {
John Zulauf59e25072020-07-17 10:55:21 -0600131 std::string out_str;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700132 if (flags.none()) {
John Zulauf389c34b2020-07-28 11:19:35 -0600133 out_str = "0";
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700134 } else {
135 for (size_t i = 0; i < syncStageAccessInfoByStageAccessIndex.size(); i++) {
136 const auto &info = syncStageAccessInfoByStageAccessIndex[i];
137 if ((flags & info.stage_access_bit).any()) {
138 if (!out_str.empty()) {
139 out_str.append(sep);
140 }
141 out_str.append(info.name);
John Zulauf59e25072020-07-17 10:55:21 -0600142 }
John Zulauf59e25072020-07-17 10:55:21 -0600143 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700144 if (out_str.length() == 0) {
145 out_str.append("Unhandled SyncStageAccess");
146 }
John Zulauf59e25072020-07-17 10:55:21 -0600147 }
148 return out_str;
149}
150
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700151static std::string string_UsageTag(const ResourceUsageTag &tag) {
152 std::stringstream out;
153
Jeremy Gebben4bb73502020-12-14 11:17:50 -0700154 out << "command: " << CommandTypeString(tag.GetCommand());
155 out << ", seq_no: " << tag.GetSeqNum() << ", reset_no: " << tag.GetResetNum();
156 if (tag.GetSubCommand() != 0) {
157 out << ", subcmd: " << tag.GetSubCommand();
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700158 }
159 return out.str();
160}
161
John Zulauf37ceaed2020-07-03 16:18:15 -0600162static std::string string_UsageTag(const HazardResult &hazard) {
163 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600164 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
165 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600166 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600167 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
168 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf59e25072020-07-17 10:55:21 -0600169 out << "(usage: " << usage_info.name << ", prior_usage: " << stage_access_name;
170 if (IsHazardVsRead(hazard.hazard)) {
171 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
172 out << ", read_barriers: " << string_VkPipelineStageFlags(barriers);
173 } else {
174 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
175 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
176 }
177
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700178 out << ", " << string_UsageTag(tag) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600179 return out.str();
180}
181
John Zulaufd14743a2020-07-03 09:42:39 -0600182// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
183// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
184// also reflects this special case for read hazard detection (using access instead of exec scope)
John Zulaufb027cdb2020-05-21 14:25:22 -0600185static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700186static const SyncStageAccessFlags kColorAttachmentAccessScope =
187 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
188 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
189 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
190 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600191static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
192 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700193static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
194 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
195 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
196 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600197
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700198static const SyncOrderingBarrier kColorAttachmentRasterOrder = {kColorAttachmentExecScope, kColorAttachmentAccessScope};
199static const SyncOrderingBarrier kDepthStencilAttachmentRasterOrder = {kDepthStencilAttachmentExecScope,
200 kDepthStencilAttachmentAccessScope};
201static const SyncOrderingBarrier kAttachmentRasterOrder = {kDepthStencilAttachmentExecScope | kColorAttachmentExecScope,
202 kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope};
John Zulauf7635de32020-05-29 17:14:15 -0600203// Sometimes we have an internal access conflict, and we using the kCurrentCommandTag to set and detect in temporary/proxy contexts
John Zulaufcc6fecb2020-06-17 15:24:54 -0600204static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600205
John Zulaufb02c1eb2020-10-06 16:33:36 -0600206static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) {
207 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
208}
209
210static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
211
locke-lunarg3c038002020-04-30 23:08:08 -0600212inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
213 if (size == VK_WHOLE_SIZE) {
214 return (whole_size - offset);
215 }
216 return size;
217}
218
John Zulauf3e86bf02020-09-12 10:47:57 -0600219static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
220 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
221}
222
John Zulauf16adfc92020-04-08 10:28:33 -0600223template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600224static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600225 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
226}
227
John Zulauf355e49b2020-04-24 15:11:15 -0600228static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600229
John Zulauf3e86bf02020-09-12 10:47:57 -0600230static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
231 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
232}
233
234static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
235 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
236}
237
John Zulauf4a6105a2020-11-17 15:11:05 -0700238// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
239//
John Zulauf10f1f522020-12-18 12:00:35 -0700240// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
241//
John Zulauf4a6105a2020-11-17 15:11:05 -0700242// Usage:
243// Constructor() -- initializes the generator to point to the begin of the space declared.
244// * -- the current range of the generator empty signfies end
245// ++ -- advance to the next non-empty range (or end)
246
247// A wrapper for a single range with the same semantics as the actual generators below
248template <typename KeyType>
249class SingleRangeGenerator {
250 public:
251 SingleRangeGenerator(const KeyType &range) : current_(range) {}
252 KeyType &operator*() const { return *current_; }
253 KeyType *operator->() const { return &*current_; }
254 SingleRangeGenerator &operator++() {
255 current_ = KeyType(); // just one real range
256 return *this;
257 }
258
259 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
260
261 private:
262 SingleRangeGenerator() = default;
263 const KeyType range_;
264 KeyType current_;
265};
266
267// Generate the ranges that are the intersection of range and the entries in the FilterMap
268template <typename FilterMap, typename KeyType = typename FilterMap::key_type>
269class FilteredRangeGenerator {
270 public:
271 FilteredRangeGenerator(const FilterMap &filter, const KeyType &range)
272 : range_(range), filter_(&filter), filter_pos_(), current_() {
273 SeekBegin();
274 }
275 const KeyType &operator*() const { return current_; }
276 const KeyType *operator->() const { return &current_; }
277 FilteredRangeGenerator &operator++() {
278 ++filter_pos_;
279 UpdateCurrent();
280 return *this;
281 }
282
283 bool operator==(const FilteredRangeGenerator &other) const { return current_ == other.current_; }
284
285 private:
286 FilteredRangeGenerator() = default;
287 void UpdateCurrent() {
288 if (filter_pos_ != filter_->cend()) {
289 current_ = range_ & filter_pos_->first;
290 } else {
291 current_ = KeyType();
292 }
293 }
294 void SeekBegin() {
295 filter_pos_ = filter_->lower_bound(range_);
296 UpdateCurrent();
297 }
298 const KeyType range_;
299 const FilterMap *filter_;
300 typename FilterMap::const_iterator filter_pos_;
301 KeyType current_;
302};
303using EventSimpleRangeGenerator = FilteredRangeGenerator<SyncEventState::ScopeMap>;
304
305// Templated to allow for different Range generators or map sources...
306
307// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulauf4a6105a2020-11-17 15:11:05 -0700308template <typename FilterMap, typename RangeGen, typename KeyType = typename FilterMap::key_type>
309class FilteredGeneratorGenerator {
310 public:
311 FilteredGeneratorGenerator(const FilterMap &filter, RangeGen &gen) : filter_(&filter), gen_(&gen), filter_pos_(), current_() {
312 SeekBegin();
313 }
314 const KeyType &operator*() const { return current_; }
315 const KeyType *operator->() const { return &current_; }
316 FilteredGeneratorGenerator &operator++() {
317 KeyType gen_range = GenRange();
318 KeyType filter_range = FilterRange();
319 current_ = KeyType();
320 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
321 if (gen_range.end > filter_range.end) {
322 // if the generated range is beyond the filter_range, advance the filter range
323 filter_range = AdvanceFilter();
324 } else {
325 gen_range = AdvanceGen();
326 }
327 current_ = gen_range & filter_range;
328 }
329 return *this;
330 }
331
332 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
333
334 private:
335 KeyType AdvanceFilter() {
336 ++filter_pos_;
337 auto filter_range = FilterRange();
338 if (filter_range.valid()) {
339 FastForwardGen(filter_range);
340 }
341 return filter_range;
342 }
343 KeyType AdvanceGen() {
344 ++(*gen_);
345 auto gen_range = GenRange();
346 if (gen_range.valid()) {
347 FastForwardFilter(gen_range);
348 }
349 return gen_range;
350 }
351
352 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
353 KeyType GenRange() const { return *(*gen_); }
354
355 KeyType FastForwardFilter(const KeyType &range) {
356 auto filter_range = FilterRange();
357 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700358 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700359 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
360 if (retry_count < kRetryLimit) {
361 ++filter_pos_;
362 filter_range = FilterRange();
363 retry_count++;
364 } else {
365 // Okay we've tried walking, do a seek.
366 filter_pos_ = filter_->lower_bound(range);
367 break;
368 }
369 }
370 return FilterRange();
371 }
372
373 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
374 // faster.
375 KeyType FastForwardGen(const KeyType &range) {
376 auto gen_range = GenRange();
377 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
378 ++(*gen_);
379 gen_range = GenRange();
380 }
381 return gen_range;
382 }
383
384 void SeekBegin() {
385 auto gen_range = GenRange();
386 if (gen_range.empty()) {
387 current_ = KeyType();
388 filter_pos_ = filter_->cend();
389 } else {
390 filter_pos_ = filter_->lower_bound(gen_range);
391 current_ = gen_range & FilterRange();
392 }
393 }
394
395 FilteredGeneratorGenerator() = default;
396 const FilterMap *filter_;
397 RangeGen *const gen_;
398 typename FilterMap::const_iterator filter_pos_;
399 KeyType current_;
400};
401
402using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
403
John Zulauf0cb5be22020-01-23 12:18:22 -0700404// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
405VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
406 VkPipelineStageFlags expanded = stage_mask;
407 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
408 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
409 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
410 if (all_commands.first & queue_flags) {
411 expanded |= all_commands.second;
412 }
413 }
414 }
415 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
416 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
417 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
418 }
419 return expanded;
420}
421
John Zulauf36bcf6a2020-02-03 15:12:52 -0700422VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
Jeremy Gebben91c36902020-11-09 08:17:08 -0700423 const std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
John Zulauf36bcf6a2020-02-03 15:12:52 -0700424 VkPipelineStageFlags unscanned = stage_mask;
425 VkPipelineStageFlags related = 0;
Jonah Ryan-Davis185189c2020-07-14 10:28:52 -0400426 for (const auto &entry : map) {
427 const auto &stage = entry.first;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700428 if (stage & unscanned) {
429 related = related | entry.second;
430 unscanned = unscanned & ~stage;
431 if (!unscanned) break;
432 }
433 }
434 return related;
435}
436
437VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
438 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
439}
440
441VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
442 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
443}
444
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700445static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700446
John Zulauf3e86bf02020-09-12 10:47:57 -0600447ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
448 VkDeviceSize stride) {
449 VkDeviceSize range_start = offset + first_index * stride;
450 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600451 if (count == UINT32_MAX) {
452 range_size = buf_whole_size - range_start;
453 } else {
454 range_size = count * stride;
455 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600456 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600457}
458
locke-lunarg654e3692020-06-04 17:19:15 -0600459SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
460 VkShaderStageFlagBits stage_flag) {
461 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
462 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
463 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
464 }
465 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
466 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
467 assert(0);
468 }
469 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
470 return stage_access->second.uniform_read;
471 }
472
473 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
474 // Because if write hazard happens, read hazard might or might not happen.
475 // But if write hazard doesn't happen, read hazard is impossible to happen.
476 if (descriptor_data.is_writable) {
477 return stage_access->second.shader_write;
478 }
479 return stage_access->second.shader_read;
480}
481
locke-lunarg37047832020-06-12 13:44:45 -0600482bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
483 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
484 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
485 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
486 ? true
487 : false;
488}
489
490bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
491 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
492 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
493 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
494 ? true
495 : false;
496}
497
John Zulauf355e49b2020-04-24 15:11:15 -0600498// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600499template <typename Action>
500static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
501 Action &action) {
502 // At this point the "apply over range" logic only supports a single memory binding
503 if (!SimpleBinding(image_state)) return;
504 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600505 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700506 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
507 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600508 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700509 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600510 }
511}
512
John Zulauf7635de32020-05-29 17:14:15 -0600513// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
514// Used by both validation and record operations
515//
516// The signature for Action() reflect the needs of both uses.
517template <typename Action>
518void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
519 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
520 VkExtent3D extent = CastTo3D(render_area.extent);
521 VkOffset3D offset = CastTo3D(render_area.offset);
522 const auto &rp_ci = rp_state.createInfo;
523 const auto *attachment_ci = rp_ci.pAttachments;
524 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
525
526 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
527 const auto *color_attachments = subpass_ci.pColorAttachments;
528 const auto *color_resolve = subpass_ci.pResolveAttachments;
529 if (color_resolve && color_attachments) {
530 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
531 const auto &color_attach = color_attachments[i].attachment;
532 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
533 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
534 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
535 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
536 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
537 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
538 }
539 }
540 }
541
542 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700543 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600544 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
545 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
546 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
547 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
548 const auto src_ci = attachment_ci[src_at];
549 // The formats are required to match so we can pick either
550 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
551 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
552 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
553 VkImageAspectFlags aspect_mask = 0u;
554
555 // Figure out which aspects are actually touched during resolve operations
556 const char *aspect_string = nullptr;
557 if (resolve_depth && resolve_stencil) {
558 // Validate all aspects together
559 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
560 aspect_string = "depth/stencil";
561 } else if (resolve_depth) {
562 // Validate depth only
563 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
564 aspect_string = "depth";
565 } else if (resolve_stencil) {
566 // Validate all stencil only
567 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
568 aspect_string = "stencil";
569 }
570
571 if (aspect_mask) {
572 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700573 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kAttachmentRasterOrder, offset, extent, aspect_mask);
John Zulauf7635de32020-05-29 17:14:15 -0600574 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
575 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
576 }
577 }
578}
579
580// Action for validating resolve operations
581class ValidateResolveAction {
582 public:
583 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
584 const char *func_name)
585 : render_pass_(render_pass),
586 subpass_(subpass),
587 context_(context),
588 sync_state_(sync_state),
589 func_name_(func_name),
590 skip_(false) {}
591 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
592 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
593 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
594 HazardResult hazard;
595 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
596 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600597 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
598 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600599 " to resolve attachment %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -0600600 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
John Zulauf37ceaed2020-07-03 16:18:15 -0600601 src_at, dst_at, string_UsageTag(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600602 }
603 }
604 // Providing a mechanism for the constructing caller to get the result of the validation
605 bool GetSkip() const { return skip_; }
606
607 private:
608 VkRenderPass render_pass_;
609 const uint32_t subpass_;
610 const AccessContext &context_;
611 const SyncValidator &sync_state_;
612 const char *func_name_;
613 bool skip_;
614};
615
616// Update action for resolve operations
617class UpdateStateResolveAction {
618 public:
619 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
620 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
621 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
622 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
623 // Ignores validation only arguments...
624 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
625 }
626
627 private:
628 AccessContext &context_;
629 const ResourceUsageTag &tag_;
630};
631
John Zulauf59e25072020-07-17 10:55:21 -0600632void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700633 const SyncStageAccessFlags &prior_, const ResourceUsageTag &tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600634 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
635 usage_index = usage_index_;
636 hazard = hazard_;
637 prior_access = prior_;
638 tag = tag_;
639}
640
John Zulauf540266b2020-04-06 18:54:53 -0600641AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
642 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600643 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600644 Reset();
645 const auto &subpass_dep = dependencies[subpass];
646 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600647 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600648 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600649 const auto prev_pass = prev_dep.first->pass;
650 const auto &prev_barriers = prev_dep.second;
651 assert(prev_dep.second.size());
652 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
653 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700654 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600655
656 async_.reserve(subpass_dep.async.size());
657 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700658 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600659 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600660 if (subpass_dep.barrier_from_external.size()) {
661 src_external_ = TrackBack(external_context, queue_flags, subpass_dep.barrier_from_external);
John Zulaufe5da6e52020-03-18 15:32:18 -0600662 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600663 if (subpass_dep.barrier_to_external.size()) {
664 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600665 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700666}
667
John Zulauf5f13a792020-03-10 07:31:21 -0600668template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700669HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600670 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600671 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600672 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600673
674 HazardResult hazard;
675 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
676 hazard = detector.Detect(prev);
677 }
678 return hazard;
679}
680
John Zulauf4a6105a2020-11-17 15:11:05 -0700681template <typename Action>
682void AccessContext::ForAll(Action &&action) {
683 for (const auto address_type : kAddressTypes) {
684 auto &accesses = GetAccessStateMap(address_type);
685 for (const auto &access : accesses) {
686 action(address_type, access);
687 }
688 }
689}
690
John Zulauf3d84f1b2020-03-09 13:33:25 -0600691// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
692// the DAG of the contexts (for example subpasses)
693template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700694HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600695 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600696 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600697
John Zulauf1a224292020-06-30 14:52:13 -0600698 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600699 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
700 // so we'll check these first
701 for (const auto &async_context : async_) {
702 hazard = async_context->DetectAsyncHazard(type, detector, range);
703 if (hazard.hazard) return hazard;
704 }
John Zulauf5f13a792020-03-10 07:31:21 -0600705 }
706
John Zulauf1a224292020-06-30 14:52:13 -0600707 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600708
John Zulauf69133422020-05-20 14:55:53 -0600709 const auto &accesses = GetAccessStateMap(type);
710 const auto from = accesses.lower_bound(range);
711 const auto to = accesses.upper_bound(range);
712 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600713
John Zulauf69133422020-05-20 14:55:53 -0600714 for (auto pos = from; pos != to; ++pos) {
715 // Cover any leading gap, or gap between entries
716 if (detect_prev) {
717 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
718 // Cover any leading gap, or gap between entries
719 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600720 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600721 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600722 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600723 if (hazard.hazard) return hazard;
724 }
John Zulauf69133422020-05-20 14:55:53 -0600725 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
726 gap.begin = pos->first.end;
727 }
728
729 hazard = detector.Detect(pos);
730 if (hazard.hazard) return hazard;
731 }
732
733 if (detect_prev) {
734 // Detect in the trailing empty as needed
735 gap.end = range.end;
736 if (gap.non_empty()) {
737 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600738 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600739 }
740
741 return hazard;
742}
743
744// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
745template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700746HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
747 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600748 auto &accesses = GetAccessStateMap(type);
749 const auto from = accesses.lower_bound(range);
750 const auto to = accesses.upper_bound(range);
751
John Zulauf3d84f1b2020-03-09 13:33:25 -0600752 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600753 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700754 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600755 }
John Zulauf16adfc92020-04-08 10:28:33 -0600756
John Zulauf3d84f1b2020-03-09 13:33:25 -0600757 return hazard;
758}
759
John Zulaufb02c1eb2020-10-06 16:33:36 -0600760struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700761 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600762 void operator()(ResourceAccessState *access) const {
763 assert(access);
764 access->ApplyBarriers(barriers, true);
765 }
766 const std::vector<SyncBarrier> &barriers;
767};
768
769struct ApplyTrackbackBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700770 explicit ApplyTrackbackBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600771 void operator()(ResourceAccessState *access) const {
772 assert(access);
773 assert(!access->HasPendingState());
774 access->ApplyBarriers(barriers, false);
775 access->ApplyPendingBarriers(kCurrentCommandTag);
776 }
777 const std::vector<SyncBarrier> &barriers;
778};
779
780// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
781// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
782// *different* map from dest.
783// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
784// range [first, last)
785template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600786static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
787 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600788 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600789 auto at = entry;
790 for (auto pos = first; pos != last; ++pos) {
791 // Every member of the input iterator range must fit within the remaining portion of entry
792 assert(at->first.includes(pos->first));
793 assert(at != dest->end());
794 // Trim up at to the same size as the entry to resolve
795 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600796 auto access = pos->second; // intentional copy
797 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600798 at->second.Resolve(access);
799 ++at; // Go to the remaining unused section of entry
800 }
801}
802
John Zulaufa0a98292020-09-18 09:30:10 -0600803static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
804 SyncBarrier merged = {};
805 for (const auto &barrier : barriers) {
806 merged.Merge(barrier);
807 }
808 return merged;
809}
810
John Zulaufb02c1eb2020-10-06 16:33:36 -0600811template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700812void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600813 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
814 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600815 if (!range.non_empty()) return;
816
John Zulauf355e49b2020-04-24 15:11:15 -0600817 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
818 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600819 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600820 if (current->pos_B->valid) {
821 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600822 auto access = src_pos->second; // intentional copy
823 barrier_action(&access);
824
John Zulauf16adfc92020-04-08 10:28:33 -0600825 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600826 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
827 trimmed->second.Resolve(access);
828 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600829 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600830 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600831 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600832 }
John Zulauf16adfc92020-04-08 10:28:33 -0600833 } else {
834 // we have to descend to fill this gap
835 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600836 if (current->pos_A->valid) {
837 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
838 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600839 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600840 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -0600841 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600842 // There isn't anything in dest in current)range, so we can accumulate directly into it.
843 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600844 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
845 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
846 barrier_action(&pos->second);
John Zulauf355e49b2020-04-24 15:11:15 -0600847 }
848 }
849 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
850 // iterator of the outer while.
851
852 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
853 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
854 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600855 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
856 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600857 current.seek(seek_to);
858 } else if (!current->pos_A->valid && infill_state) {
859 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
860 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
861 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600862 }
John Zulauf5f13a792020-03-10 07:31:21 -0600863 }
John Zulauf16adfc92020-04-08 10:28:33 -0600864 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600865 }
John Zulauf1a224292020-06-30 14:52:13 -0600866
867 // Infill if range goes passed both the current and resolve map prior contents
868 if (recur_to_infill && (current->range.end < range.end)) {
869 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
870 ResourceAccessRangeMap gap_map;
871 const auto the_end = resolve_map->end();
872 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
873 for (auto &access : gap_map) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600874 barrier_action(&access.second);
John Zulauf1a224292020-06-30 14:52:13 -0600875 resolve_map->insert(the_end, access);
876 }
877 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600878}
879
John Zulauf43cc7462020-12-03 12:33:12 -0700880void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
881 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600882 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600883 if (range.non_empty() && infill_state) {
884 descent_map->insert(std::make_pair(range, *infill_state));
885 }
886 } else {
887 // Look for something to fill the gap further along.
888 for (const auto &prev_dep : prev_) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600889 const ApplyTrackbackBarriersAction barrier_action(prev_dep.barriers);
890 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600891 }
892
John Zulaufe5da6e52020-03-18 15:32:18 -0600893 if (src_external_.context) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600894 const ApplyTrackbackBarriersAction barrier_action(src_external_.barriers);
895 src_external_.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600896 }
897 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600898}
899
John Zulauf4a6105a2020-11-17 15:11:05 -0700900// Non-lazy import of all accesses, WaitEvents needs this.
901void AccessContext::ResolvePreviousAccesses() {
902 ResourceAccessState default_state;
903 for (const auto address_type : kAddressTypes) {
904 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
905 }
906}
907
John Zulauf43cc7462020-12-03 12:33:12 -0700908AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
909 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -0600910}
911
John Zulauf1507ee42020-05-18 11:33:09 -0600912static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
913 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
914 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
915 return stage_access;
916}
917static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
918 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
919 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
920 return stage_access;
921}
922
John Zulauf7635de32020-05-29 17:14:15 -0600923// Caller must manage returned pointer
924static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
925 uint32_t subpass, const VkRect2D &render_area,
926 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
927 auto *proxy = new AccessContext(context);
928 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600929 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600930 return proxy;
931}
932
John Zulaufb02c1eb2020-10-06 16:33:36 -0600933template <typename BarrierAction>
John Zulauf52446eb2020-10-22 16:40:08 -0600934class ResolveAccessRangeFunctor {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600935 public:
John Zulauf43cc7462020-12-03 12:33:12 -0700936 ResolveAccessRangeFunctor(const AccessContext &context, AccessAddressType address_type, ResourceAccessRangeMap *descent_map,
937 const ResourceAccessState *infill_state, BarrierAction &barrier_action)
John Zulauf52446eb2020-10-22 16:40:08 -0600938 : context_(context),
939 address_type_(address_type),
940 descent_map_(descent_map),
941 infill_state_(infill_state),
942 barrier_action_(barrier_action) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600943 ResolveAccessRangeFunctor() = delete;
944 void operator()(const ResourceAccessRange &range) const {
945 context_.ResolveAccessRange(address_type_, range, barrier_action_, descent_map_, infill_state_);
946 }
947
948 private:
John Zulauf52446eb2020-10-22 16:40:08 -0600949 const AccessContext &context_;
John Zulauf43cc7462020-12-03 12:33:12 -0700950 const AccessAddressType address_type_;
John Zulauf52446eb2020-10-22 16:40:08 -0600951 ResourceAccessRangeMap *const descent_map_;
952 const ResourceAccessState *infill_state_;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600953 BarrierAction &barrier_action_;
954};
955
John Zulaufb02c1eb2020-10-06 16:33:36 -0600956template <typename BarrierAction>
957void AccessContext::ResolveAccessRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -0700958 BarrierAction &barrier_action, AccessAddressType address_type,
959 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600960 const ResolveAccessRangeFunctor<BarrierAction> action(*this, address_type, descent_map, infill_state, barrier_action);
961 ApplyOverImageRange(image_state, subresource_range, action);
John Zulauf62f10592020-04-03 12:20:02 -0600962}
963
John Zulauf7635de32020-05-29 17:14:15 -0600964// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600965bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600966 const VkRect2D &render_area, uint32_t subpass,
967 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
968 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600969 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600970 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
971 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
972 // those affects have not been recorded yet.
973 //
974 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
975 // to apply and only copy then, if this proves a hot spot.
976 std::unique_ptr<AccessContext> proxy_for_prev;
977 TrackBack proxy_track_back;
978
John Zulauf355e49b2020-04-24 15:11:15 -0600979 const auto &transitions = rp_state.subpass_transitions[subpass];
980 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600981 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
982
983 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
984 if (prev_needs_proxy) {
985 if (!proxy_for_prev) {
986 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
987 render_area, attachment_views));
988 proxy_track_back = *track_back;
989 proxy_track_back.context = proxy_for_prev.get();
990 }
991 track_back = &proxy_track_back;
992 }
993 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600994 if (hazard.hazard) {
John Zulauf389c34b2020-07-28 11:19:35 -0600995 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
996 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
997 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
998 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
999 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
1000 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001001 }
1002 }
1003 return skip;
1004}
1005
John Zulauf1507ee42020-05-18 11:33:09 -06001006bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001007 const VkRect2D &render_area, uint32_t subpass,
1008 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1009 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001010 bool skip = false;
1011 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1012 VkExtent3D extent = CastTo3D(render_area.extent);
1013 VkOffset3D offset = CastTo3D(render_area.offset);
John Zulaufa0a98292020-09-18 09:30:10 -06001014
John Zulauf1507ee42020-05-18 11:33:09 -06001015 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1016 if (subpass == rp_state.attachment_first_subpass[i]) {
1017 if (attachment_views[i] == nullptr) continue;
1018 const IMAGE_VIEW_STATE &view = *attachment_views[i];
1019 const IMAGE_STATE *image = view.image_state.get();
1020 if (image == nullptr) continue;
1021 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001022
1023 // Need check in the following way
1024 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1025 // vs. transition
1026 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1027 // for each aspect loaded.
1028
1029 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001030 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001031 const bool is_color = !(has_depth || has_stencil);
1032
1033 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001034 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001035
John Zulaufaff20662020-06-01 14:07:58 -06001036 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001037 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001038
John Zulaufb02c1eb2020-10-06 16:33:36 -06001039 auto hazard_range = view.normalized_subresource_range;
1040 bool checked_stencil = false;
1041 if (is_color) {
John Zulauf859089b2020-10-29 17:37:03 -06001042 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, kColorAttachmentRasterOrder, offset,
1043 extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001044 aspect = "color";
1045 } else {
1046 if (has_depth) {
1047 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf859089b2020-10-29 17:37:03 -06001048 hazard = DetectHazard(*image, load_index, hazard_range, kDepthStencilAttachmentRasterOrder, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001049 aspect = "depth";
1050 }
1051 if (!hazard.hazard && has_stencil) {
1052 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf859089b2020-10-29 17:37:03 -06001053 hazard =
1054 DetectHazard(*image, stencil_load_index, hazard_range, kDepthStencilAttachmentRasterOrder, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001055 aspect = "stencil";
1056 checked_stencil = true;
1057 }
1058 }
1059
1060 if (hazard.hazard) {
1061 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
1062 if (hazard.tag == kCurrentCommandTag) {
1063 // Hazard vs. ILT
1064 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1065 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1066 " aspect %s during load with loadOp %s.",
1067 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1068 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06001069 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1070 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001071 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001072 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -06001073 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001074 }
1075 }
1076 }
1077 }
1078 return skip;
1079}
1080
John Zulaufaff20662020-06-01 14:07:58 -06001081// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1082// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1083// store is part of the same Next/End operation.
1084// The latter is handled in layout transistion validation directly
1085bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
1086 const VkRect2D &render_area, uint32_t subpass,
1087 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1088 const char *func_name) const {
1089 bool skip = false;
1090 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1091 VkExtent3D extent = CastTo3D(render_area.extent);
1092 VkOffset3D offset = CastTo3D(render_area.offset);
1093
1094 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1095 if (subpass == rp_state.attachment_last_subpass[i]) {
1096 if (attachment_views[i] == nullptr) continue;
1097 const IMAGE_VIEW_STATE &view = *attachment_views[i];
1098 const IMAGE_STATE *image = view.image_state.get();
1099 if (image == nullptr) continue;
1100 const auto &ci = attachment_ci[i];
1101
1102 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1103 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1104 // sake, we treat DONT_CARE as writing.
1105 const bool has_depth = FormatHasDepth(ci.format);
1106 const bool has_stencil = FormatHasStencil(ci.format);
1107 const bool is_color = !(has_depth || has_stencil);
1108 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1109 if (!has_stencil && !store_op_stores) continue;
1110
1111 HazardResult hazard;
1112 const char *aspect = nullptr;
1113 bool checked_stencil = false;
1114 if (is_color) {
1115 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1116 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
1117 aspect = "color";
1118 } else {
1119 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1120 auto hazard_range = view.normalized_subresource_range;
1121 if (has_depth && store_op_stores) {
1122 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1123 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
1124 kAttachmentRasterOrder, offset, extent);
1125 aspect = "depth";
1126 }
1127 if (!hazard.hazard && has_stencil && stencil_op_stores) {
1128 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1129 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
1130 kAttachmentRasterOrder, offset, extent);
1131 aspect = "stencil";
1132 checked_stencil = true;
1133 }
1134 }
1135
1136 if (hazard.hazard) {
1137 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1138 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -06001139 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1140 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001141 " %s aspect during store with %s %s. Access info %s",
John Zulauf1dae9192020-06-16 15:46:44 -06001142 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -06001143 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001144 }
1145 }
1146 }
1147 return skip;
1148}
1149
John Zulaufb027cdb2020-05-21 14:25:22 -06001150bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
1151 const VkRect2D &render_area,
1152 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
1153 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -06001154 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
1155 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
1156 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001157}
1158
John Zulauf3d84f1b2020-03-09 13:33:25 -06001159class HazardDetector {
1160 SyncStageAccessIndex usage_index_;
1161
1162 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001163 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001164 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1165 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001166 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001167 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001168};
1169
John Zulauf69133422020-05-20 14:55:53 -06001170class HazardDetectorWithOrdering {
1171 const SyncStageAccessIndex usage_index_;
1172 const SyncOrderingBarrier &ordering_;
1173
1174 public:
1175 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1176 return pos->second.DetectHazard(usage_index_, ordering_);
1177 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001178 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1179 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001180 }
1181 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
1182 : usage_index_(usage), ordering_(ordering) {}
1183};
1184
John Zulauf16adfc92020-04-08 10:28:33 -06001185HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001186 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001187 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001188 const auto base_address = ResourceBaseAddress(buffer);
1189 HazardDetector detector(usage_index);
1190 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001191}
1192
John Zulauf69133422020-05-20 14:55:53 -06001193template <typename Detector>
1194HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1195 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1196 const VkExtent3D &extent, DetectOptions options) const {
1197 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001198 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001199 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1200 base_address);
1201 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001202 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001203 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001204 if (hazard.hazard) return hazard;
1205 }
1206 return HazardResult();
1207}
1208
John Zulauf540266b2020-04-06 18:54:53 -06001209HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1210 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1211 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001212 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1213 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -06001214 return DetectHazard(image, current_usage, subresource_range, offset, extent);
1215}
1216
1217HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1218 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1219 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001220 HazardDetector detector(current_usage);
1221 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1222}
1223
1224HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1225 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
1226 const VkOffset3D &offset, const VkExtent3D &extent) const {
1227 HazardDetectorWithOrdering detector(current_usage, ordering);
1228 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001229}
1230
John Zulaufb027cdb2020-05-21 14:25:22 -06001231// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1232// should have reported the issue regarding an invalid attachment entry
1233HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
1234 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
1235 VkImageAspectFlags aspect_mask) const {
1236 if (view != nullptr) {
1237 const IMAGE_STATE *image = view->image_state.get();
1238 if (image != nullptr) {
1239 auto *detect_range = &view->normalized_subresource_range;
1240 VkImageSubresourceRange masked_range;
1241 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1242 masked_range = view->normalized_subresource_range;
1243 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1244 detect_range = &masked_range;
1245 }
1246
1247 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1248 if (detect_range->aspectMask) {
1249 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
1250 }
1251 }
1252 }
1253 return HazardResult();
1254}
John Zulauf43cc7462020-12-03 12:33:12 -07001255
John Zulauf3d84f1b2020-03-09 13:33:25 -06001256class BarrierHazardDetector {
1257 public:
1258 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1259 SyncStageAccessFlags src_access_scope)
1260 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1261
John Zulauf5f13a792020-03-10 07:31:21 -06001262 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1263 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001264 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001265 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001266 // 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 -07001267 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001268 }
1269
1270 private:
1271 SyncStageAccessIndex usage_index_;
1272 VkPipelineStageFlags src_exec_scope_;
1273 SyncStageAccessFlags src_access_scope_;
1274};
1275
John Zulauf4a6105a2020-11-17 15:11:05 -07001276class EventBarrierHazardDetector {
1277 public:
1278 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1279 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
1280 const ResourceUsageTag &scope_tag)
1281 : usage_index_(usage_index),
1282 src_exec_scope_(src_exec_scope),
1283 src_access_scope_(src_access_scope),
1284 event_scope_(event_scope),
1285 scope_pos_(event_scope.cbegin()),
1286 scope_end_(event_scope.cend()),
1287 scope_tag_(scope_tag) {}
1288
1289 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1290 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1291 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1292 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1293 if (scope_pos_ == scope_end_) return HazardResult();
1294 if (!scope_pos_->first.intersects(pos->first)) {
1295 event_scope_.lower_bound(pos->first);
1296 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1297 }
1298
1299 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1300 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1301 }
1302 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1303 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1304 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1305 }
1306
1307 private:
1308 SyncStageAccessIndex usage_index_;
1309 VkPipelineStageFlags src_exec_scope_;
1310 SyncStageAccessFlags src_access_scope_;
1311 const SyncEventState::ScopeMap &event_scope_;
1312 SyncEventState::ScopeMap::const_iterator scope_pos_;
1313 SyncEventState::ScopeMap::const_iterator scope_end_;
1314 const ResourceUsageTag &scope_tag_;
1315};
1316
1317HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1318 const SyncStageAccessFlags &src_access_scope,
1319 const VkImageSubresourceRange &subresource_range,
1320 const SyncEventState &sync_event, DetectOptions options) const {
1321 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1322 // first access scope map to use, and there's no easy way to plumb it in below.
1323 const auto address_type = ImageAddressType(image);
1324 const auto &event_scope = sync_event.FirstScope(address_type);
1325
1326 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1327 event_scope, sync_event.first_scope_tag);
1328 VkOffset3D zero_offset = {0, 0, 0};
1329 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
1330}
1331
John Zulauf16adfc92020-04-08 10:28:33 -06001332HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001333 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001334 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001335 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001336 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1337 VkOffset3D zero_offset = {0, 0, 0};
1338 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001339}
1340
John Zulauf355e49b2020-04-24 15:11:15 -06001341HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001342 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001343 const VkImageMemoryBarrier &barrier) const {
1344 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1345 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1346 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1347}
1348
John Zulauf9cb530d2019-09-30 14:14:10 -06001349template <typename Flags, typename Map>
1350SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1351 SyncStageAccessFlags scope = 0;
1352 for (const auto &bit_scope : map) {
1353 if (flag_mask < bit_scope.first) break;
1354
1355 if (flag_mask & bit_scope.first) {
1356 scope |= bit_scope.second;
1357 }
1358 }
1359 return scope;
1360}
1361
1362SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1363 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1364}
1365
1366SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1367 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1368}
1369
1370// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1371SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001372 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1373 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1374 // 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 -06001375 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1376}
1377
1378template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001379void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001380 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1381 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001382 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001383 auto pos = accesses->lower_bound(range);
1384 if (pos == accesses->end() || !pos->first.intersects(range)) {
1385 // The range is empty, fill it with a default value.
1386 pos = action.Infill(accesses, pos, range);
1387 } else if (range.begin < pos->first.begin) {
1388 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001389 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001390 } else if (pos->first.begin < range.begin) {
1391 // Trim the beginning if needed
1392 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1393 ++pos;
1394 }
1395
1396 const auto the_end = accesses->end();
1397 while ((pos != the_end) && pos->first.intersects(range)) {
1398 if (pos->first.end > range.end) {
1399 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1400 }
1401
1402 pos = action(accesses, pos);
1403 if (pos == the_end) break;
1404
1405 auto next = pos;
1406 ++next;
1407 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1408 // Need to infill if next is disjoint
1409 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001410 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001411 next = action.Infill(accesses, next, new_range);
1412 }
1413 pos = next;
1414 }
1415}
John Zulauf4a6105a2020-11-17 15:11:05 -07001416template <typename Action, typename RangeGen>
1417void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1418 assert(range_gen_arg);
1419 auto &range_gen = *range_gen_arg; // Non-const references must be * by style requirement but deref-ing * iterator is a pain
1420 for (; range_gen->non_empty(); ++range_gen) {
1421 UpdateMemoryAccessState(accesses, *range_gen, action);
1422 }
1423}
John Zulauf9cb530d2019-09-30 14:14:10 -06001424
1425struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001426 using Iterator = ResourceAccessRangeMap::iterator;
1427 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001428 // this is only called on gaps, and never returns a gap.
1429 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001430 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001431 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001432 }
John Zulauf5f13a792020-03-10 07:31:21 -06001433
John Zulauf5c5e88d2019-12-26 11:22:02 -07001434 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001435 auto &access_state = pos->second;
1436 access_state.Update(usage, tag);
1437 return pos;
1438 }
1439
John Zulauf43cc7462020-12-03 12:33:12 -07001440 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001441 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001442 : type(type_), context(context_), usage(usage_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001443 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001444 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001445 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001446 const ResourceUsageTag &tag;
1447};
1448
John Zulauf4a6105a2020-11-17 15:11:05 -07001449// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001450struct PipelineBarrierOp {
1451 SyncBarrier barrier;
1452 bool layout_transition;
1453 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1454 : barrier(barrier_), layout_transition(layout_transition_) {}
1455 PipelineBarrierOp() = default;
1456 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1457};
John Zulauf4a6105a2020-11-17 15:11:05 -07001458// The barrier operation for wait events
1459struct WaitEventBarrierOp {
1460 const ResourceUsageTag *scope_tag;
1461 SyncBarrier barrier;
1462 bool layout_transition;
1463 WaitEventBarrierOp(const ResourceUsageTag &scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1464 : scope_tag(&scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
1465 WaitEventBarrierOp() = default;
1466 void operator()(ResourceAccessState *access_state) const {
1467 assert(scope_tag); // Not valid to have a non-scope op executed, default construct included for std::vector support
1468 access_state->ApplyBarrier(*scope_tag, barrier, layout_transition);
1469 }
1470};
John Zulauf1e331ec2020-12-04 18:29:38 -07001471
John Zulauf4a6105a2020-11-17 15:11:05 -07001472// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1473// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1474// of a collection is known/present.
John Zulauf1e331ec2020-12-04 18:29:38 -07001475template <typename BarrierOp>
John Zulauf89311b42020-09-29 16:28:47 -06001476class ApplyBarrierOpsFunctor {
1477 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001478 using Iterator = ResourceAccessRangeMap::iterator;
1479 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001480
John Zulauf5c5e88d2019-12-26 11:22:02 -07001481 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001482 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001483 for (const auto &op : barrier_ops_) {
1484 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001485 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001486
John Zulauf89311b42020-09-29 16:28:47 -06001487 if (resolve_) {
1488 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1489 // another walk
1490 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001491 }
1492 return pos;
1493 }
1494
John Zulauf89311b42020-09-29 16:28:47 -06001495 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulauf1e331ec2020-12-04 18:29:38 -07001496 ApplyBarrierOpsFunctor(bool resolve, const std::vector<BarrierOp> &barrier_ops, const ResourceUsageTag &tag)
1497 : resolve_(resolve), barrier_ops_(barrier_ops), tag_(tag) {}
John Zulauf89311b42020-09-29 16:28:47 -06001498
1499 private:
1500 bool resolve_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001501 const std::vector<BarrierOp> &barrier_ops_;
1502 const ResourceUsageTag &tag_;
1503};
1504
John Zulauf4a6105a2020-11-17 15:11:05 -07001505// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1506// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1507template <typename BarrierOp>
1508class ApplyBarrierFunctor {
1509 public:
1510 using Iterator = ResourceAccessRangeMap::iterator;
1511 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1512
1513 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1514 auto &access_state = pos->second;
1515 barrier_op_(&access_state);
1516 return pos;
1517 }
1518
1519 ApplyBarrierFunctor(const BarrierOp &barrier_op) : barrier_op_(barrier_op) {}
1520
1521 private:
1522 const BarrierOp barrier_op_;
1523};
1524
John Zulauf1e331ec2020-12-04 18:29:38 -07001525// This functor resolves the pendinging state.
1526class ResolvePendingBarrierFunctor {
1527 public:
1528 using Iterator = ResourceAccessRangeMap::iterator;
1529 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1530
1531 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1532 auto &access_state = pos->second;
1533 access_state.ApplyPendingBarriers(tag_);
1534 return pos;
1535 }
1536
1537 ResolvePendingBarrierFunctor(const ResourceUsageTag &tag) : tag_(tag) {}
1538
1539 private:
John Zulauf89311b42020-09-29 16:28:47 -06001540 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001541};
1542
John Zulauf43cc7462020-12-03 12:33:12 -07001543void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -06001544 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001545 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1546 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001547}
1548
John Zulauf16adfc92020-04-08 10:28:33 -06001549void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001550 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001551 if (!SimpleBinding(buffer)) return;
1552 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf43cc7462020-12-03 12:33:12 -07001553 UpdateAccessState(AccessAddressType::kLinear, current_usage, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001554}
John Zulauf355e49b2020-04-24 15:11:15 -06001555
John Zulauf540266b2020-04-06 18:54:53 -06001556void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001557 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001558 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001559 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001560 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001561 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1562 base_address);
1563 const auto address_type = ImageAddressType(image);
John Zulauf16adfc92020-04-08 10:28:33 -06001564 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001565 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001566 UpdateMemoryAccessState(&GetAccessStateMap(address_type), *range_gen, action);
John Zulauf5f13a792020-03-10 07:31:21 -06001567 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001568}
John Zulauf7635de32020-05-29 17:14:15 -06001569void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1570 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1571 if (view != nullptr) {
1572 const IMAGE_STATE *image = view->image_state.get();
1573 if (image != nullptr) {
1574 auto *update_range = &view->normalized_subresource_range;
1575 VkImageSubresourceRange masked_range;
1576 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1577 masked_range = view->normalized_subresource_range;
1578 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1579 update_range = &masked_range;
1580 }
1581 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1582 }
1583 }
1584}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001585
John Zulauf355e49b2020-04-24 15:11:15 -06001586void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1587 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1588 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001589 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1590 subresource.layerCount};
1591 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1592}
1593
John Zulauf540266b2020-04-06 18:54:53 -06001594template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001595void AccessContext::UpdateResourceAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001596 if (!SimpleBinding(buffer)) return;
1597 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf43cc7462020-12-03 12:33:12 -07001598 UpdateMemoryAccessState(&GetAccessStateMap(AccessAddressType::kLinear), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001599}
1600
1601template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001602void AccessContext::UpdateResourceAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1603 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001604 if (!SimpleBinding(image)) return;
1605 const auto address_type = ImageAddressType(image);
1606 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001607
John Zulauf16adfc92020-04-08 10:28:33 -06001608 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001609 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
1610 image.createInfo.extent, base_address);
1611
John Zulauf540266b2020-04-06 18:54:53 -06001612 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001613 UpdateMemoryAccessState(accesses, *range_gen, action);
John Zulauf540266b2020-04-06 18:54:53 -06001614 }
1615}
1616
John Zulauf7635de32020-05-29 17:14:15 -06001617void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1618 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1619 const ResourceUsageTag &tag) {
1620 UpdateStateResolveAction update(*this, tag);
1621 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1622}
1623
John Zulaufaff20662020-06-01 14:07:58 -06001624void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1625 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1626 const ResourceUsageTag &tag) {
1627 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1628 VkExtent3D extent = CastTo3D(render_area.extent);
1629 VkOffset3D offset = CastTo3D(render_area.offset);
1630
1631 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1632 if (rp_state.attachment_last_subpass[i] == subpass) {
1633 if (attachment_views[i] == nullptr) continue; // UNUSED
1634 const auto &view = *attachment_views[i];
1635 const IMAGE_STATE *image = view.image_state.get();
1636 if (image == nullptr) continue;
1637
1638 const auto &ci = attachment_ci[i];
1639 const bool has_depth = FormatHasDepth(ci.format);
1640 const bool has_stencil = FormatHasStencil(ci.format);
1641 const bool is_color = !(has_depth || has_stencil);
1642 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1643
1644 if (is_color && store_op_stores) {
1645 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1646 offset, extent, tag);
1647 } else {
1648 auto update_range = view.normalized_subresource_range;
1649 if (has_depth && store_op_stores) {
1650 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1651 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1652 tag);
1653 }
1654 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1655 if (has_stencil && stencil_op_stores) {
1656 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1657 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1658 tag);
1659 }
1660 }
1661 }
1662 }
1663}
1664
John Zulauf540266b2020-04-06 18:54:53 -06001665template <typename Action>
1666void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1667 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001668 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001669 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001670 }
1671}
1672
1673void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001674 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1675 auto &context = contexts[subpass_index];
John Zulaufb02c1eb2020-10-06 16:33:36 -06001676 ApplyTrackbackBarriersAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001677 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001678 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001679 }
1680 }
1681}
1682
John Zulauf355e49b2020-04-24 15:11:15 -06001683// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001684HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001685 if (!attach_view) return HazardResult();
1686 const auto image_state = attach_view->image_state.get();
1687 if (!image_state) return HazardResult();
1688
John Zulauf355e49b2020-04-24 15:11:15 -06001689 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001690 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001691
1692 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001693 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1694 const auto merged_barrier = MergeBarriers(track_back.barriers);
1695 HazardResult hazard =
1696 track_back.context->DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
1697 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001698 if (!hazard.hazard) {
1699 // The Async hazard check is against the current context's async set.
John Zulaufa0a98292020-09-18 09:30:10 -06001700 hazard = DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001701 attach_view->normalized_subresource_range, kDetectAsync);
1702 }
John Zulaufa0a98292020-09-18 09:30:10 -06001703
John Zulauf355e49b2020-04-24 15:11:15 -06001704 return hazard;
1705}
1706
John Zulaufb02c1eb2020-10-06 16:33:36 -06001707void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
1708 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1709 const ResourceUsageTag &tag) {
1710 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001711 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001712 for (const auto &transition : transitions) {
1713 const auto prev_pass = transition.prev_pass;
1714 const auto attachment_view = attachment_views[transition.attachment];
1715 if (!attachment_view) continue;
1716 const auto *image = attachment_view->image_state.get();
1717 if (!image) continue;
1718 if (!SimpleBinding(*image)) continue;
1719
1720 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1721 assert(trackback);
1722
1723 // Import the attachments into the current context
1724 const auto *prev_context = trackback->context;
1725 assert(prev_context);
1726 const auto address_type = ImageAddressType(*image);
1727 auto &target_map = GetAccessStateMap(address_type);
1728 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
1729 prev_context->ResolveAccessRange(*image, attachment_view->normalized_subresource_range, barrier_action, address_type,
John Zulauf646cc292020-10-23 09:16:45 -06001730 &target_map, &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001731 }
1732
John Zulauf86356ca2020-10-19 11:46:41 -06001733 // If there were no transitions skip this global map walk
1734 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001735 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulauf86356ca2020-10-19 11:46:41 -06001736 ApplyGlobalBarriers(apply_pending_action);
1737 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001738}
John Zulauf4a6105a2020-11-17 15:11:05 -07001739void CommandBufferAccessContext::ApplyBufferBarriers(const SyncEventState &sync_event, VkPipelineStageFlags dst_exec_scope,
1740 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
1741 const VkBufferMemoryBarrier *barriers) {
1742 const auto &scope_tag = sync_event.first_scope_tag;
1743 auto *access_context = GetCurrentAccessContext();
1744 const auto address_type = AccessAddressType::kLinear;
1745 for (uint32_t index = 0; index < barrier_count; index++) {
1746 auto barrier = barriers[index]; // barrier is a copy
1747 const auto *buffer = sync_state_->Get<BUFFER_STATE>(barrier.buffer);
1748 if (!buffer) continue;
1749 const auto base_address = ResourceBaseAddress(*buffer);
1750 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
1751 const ResourceAccessRange range = MakeRange(barrier) + base_address;
1752 const auto src_access_scope = SyncStageAccess::AccessScope(sync_event.stage_accesses, barrier.srcAccessMask);
1753 const auto dst_access_scope = SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1754 const SyncBarrier sync_barrier(sync_event.exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1755 const ApplyBarrierFunctor<WaitEventBarrierOp> barrier_action({scope_tag, sync_barrier, false /* layout_transition */});
1756 EventSimpleRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), range);
1757 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barrier_action, &filtered_range_gen);
1758 }
1759}
1760
1761void CommandBufferAccessContext::ApplyGlobalBarriers(SyncEventState &sync_event, VkPipelineStageFlags dstStageMask,
1762 VkPipelineStageFlags dst_exec_scope,
1763 const SyncStageAccessFlags &dst_stage_accesses, uint32_t memory_barrier_count,
1764 const VkMemoryBarrier *pMemoryBarriers, const ResourceUsageTag &tag) {
1765 std::vector<WaitEventBarrierOp> barrier_ops;
1766 barrier_ops.reserve(std::min<uint32_t>(memory_barrier_count, 1));
1767 const auto &scope_tag = sync_event.first_scope_tag;
1768 auto *access_context = GetCurrentAccessContext();
1769 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
1770 const auto &barrier = pMemoryBarriers[barrier_index];
1771 SyncBarrier sync_barrier(sync_event.exec_scope,
1772 SyncStageAccess::AccessScope(sync_event.stage_accesses, barrier.srcAccessMask), dst_exec_scope,
1773 SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
1774 barrier_ops.emplace_back(scope_tag, sync_barrier, false);
1775 }
1776 if (0 == memory_barrier_count) {
1777 // If there are no global memory barriers, force an exec barrier
1778 barrier_ops.emplace_back(scope_tag, SyncBarrier(sync_event.exec_scope, 0, dst_exec_scope, 0), false);
1779 }
1780 ApplyBarrierOpsFunctor<WaitEventBarrierOp> barriers_functor(false /* don't resolve */, barrier_ops, tag);
1781 for (const auto address_type : kAddressTypes) {
1782 EventSimpleRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), kFullRange);
1783 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &filtered_range_gen);
1784 }
1785
1786 // Apply the global barrier to the event itself (for race condition tracking)
1787 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
1788 sync_event.barriers = dstStageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1789 sync_event.barriers |= dst_exec_scope;
1790}
1791
1792void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags src_exec_scope,
1793 VkPipelineStageFlags dstStageMask,
1794 VkPipelineStageFlags dst_exec_scope) {
1795 const bool all_commands_bit = 0 != (srcStageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
1796 for (auto &event_pair : event_state_) {
1797 assert(event_pair.second); // Shouldn't be storing empty
1798 auto &sync_event = *event_pair.second;
1799 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
1800 if ((sync_event.barriers & src_exec_scope) || all_commands_bit) {
1801 sync_event.barriers |= dst_exec_scope;
1802 sync_event.barriers |= dstStageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1803 }
1804 }
1805}
1806
1807void CommandBufferAccessContext::ApplyImageBarriers(const SyncEventState &sync_event, VkPipelineStageFlags dst_exec_scope,
1808 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
1809 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
1810 const auto &scope_tag = sync_event.first_scope_tag;
1811 auto *access_context = GetCurrentAccessContext();
1812 for (uint32_t index = 0; index < barrier_count; index++) {
1813 const auto &barrier = barriers[index];
1814 const auto *image = sync_state_->Get<IMAGE_STATE>(barrier.image);
1815 if (!image) continue;
1816 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
1817 bool layout_transition = barrier.oldLayout != barrier.newLayout;
1818 const auto src_access_scope = SyncStageAccess::AccessScope(sync_event.stage_accesses, barrier.srcAccessMask);
1819 const auto dst_access_scope = SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1820 const SyncBarrier sync_barrier(sync_event.exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1821 const ApplyBarrierFunctor<WaitEventBarrierOp> barrier_action({scope_tag, sync_barrier, layout_transition});
1822 const auto base_address = ResourceBaseAddress(*image);
1823 subresource_adapter::ImageRangeGenerator range_gen(*image->fragment_encoder.get(), subresource_range, {0, 0, 0},
1824 image->createInfo.extent, base_address);
1825 const auto address_type = AccessContext::ImageAddressType(*image);
1826 EventImageRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), range_gen);
1827 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barrier_action, &filtered_range_gen);
1828 }
1829}
John Zulaufb02c1eb2020-10-06 16:33:36 -06001830
John Zulauf355e49b2020-04-24 15:11:15 -06001831// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1832bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1833
1834 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001835 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001836 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1837 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06001838
John Zulauf86356ca2020-10-19 11:46:41 -06001839 assert(pRenderPassBegin);
1840 if (nullptr == pRenderPassBegin) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06001841
John Zulauf86356ca2020-10-19 11:46:41 -06001842 const uint32_t subpass = 0;
John Zulauf355e49b2020-04-24 15:11:15 -06001843
John Zulauf86356ca2020-10-19 11:46:41 -06001844 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
1845 // hasn't happened yet)
1846 const std::vector<AccessContext> empty_context_vector;
1847 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1848 const_cast<AccessContext *>(&cb_access_context_));
John Zulauf355e49b2020-04-24 15:11:15 -06001849
John Zulauf86356ca2020-10-19 11:46:41 -06001850 // Create a view list
1851 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1852 assert(fb_state);
1853 if (nullptr == fb_state) return skip;
1854 // NOTE: Must not use COMMAND_BUFFER_STATE variant of this as RecordCmdBeginRenderPass hasn't run and thus
1855 // the activeRenderPass.* fields haven't been set.
1856 const auto views = sync_state_->GetAttachmentViews(*pRenderPassBegin, *fb_state);
1857
1858 // Validate transitions
1859 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
1860
1861 // Validate load operations if there were no layout transition hazards
1862 if (!skip) {
1863 temp_context.RecordLayoutTransitions(rp_state, subpass, views, kCurrentCommandTag);
1864 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001865 }
John Zulauf86356ca2020-10-19 11:46:41 -06001866
John Zulauf355e49b2020-04-24 15:11:15 -06001867 return skip;
1868}
1869
locke-lunarg61870c22020-06-09 14:51:50 -06001870bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1871 const char *func_name) const {
1872 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001873 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001874 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001875 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1876 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001877 return skip;
1878 }
1879
1880 using DescriptorClass = cvdescriptorset::DescriptorClass;
1881 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1882 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1883 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1884 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1885
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001886 for (const auto &stage_state : pipe->stage_state) {
1887 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1888 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001889 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001890 }
locke-lunarg61870c22020-06-09 14:51:50 -06001891 for (const auto &set_binding : stage_state.descriptor_uses) {
1892 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1893 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1894 set_binding.first.second);
1895 const auto descriptor_type = binding_it.GetType();
1896 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1897 auto array_idx = 0;
1898
1899 if (binding_it.IsVariableDescriptorCount()) {
1900 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1901 }
1902 SyncStageAccessIndex sync_index =
1903 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1904
1905 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1906 uint32_t index = i - index_range.start;
1907 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1908 switch (descriptor->GetClass()) {
1909 case DescriptorClass::ImageSampler:
1910 case DescriptorClass::Image: {
1911 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001912 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001913 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001914 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1915 img_view_state = image_sampler_descriptor->GetImageViewState();
1916 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001917 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001918 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1919 img_view_state = image_descriptor->GetImageViewState();
1920 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001921 }
1922 if (!img_view_state) continue;
1923 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1924 VkExtent3D extent = {};
1925 VkOffset3D offset = {};
1926 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1927 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1928 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1929 } else {
1930 extent = img_state->createInfo.extent;
1931 }
John Zulauf361fb532020-07-22 10:45:39 -06001932 HazardResult hazard;
1933 const auto &subresource_range = img_view_state->normalized_subresource_range;
1934 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1935 // Input attachments are subject to raster ordering rules
1936 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1937 kAttachmentRasterOrder, offset, extent);
1938 } else {
1939 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1940 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001941 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001942 skip |= sync_state_->LogError(
1943 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001944 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1945 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001946 func_name, string_SyncHazard(hazard.hazard),
1947 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1948 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001949 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001950 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1951 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1952 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001953 }
1954 break;
1955 }
1956 case DescriptorClass::TexelBuffer: {
1957 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1958 if (!buf_view_state) continue;
1959 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001960 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001961 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001962 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001963 skip |= sync_state_->LogError(
1964 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001965 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1966 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001967 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1968 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001969 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001970 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1971 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1972 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001973 }
1974 break;
1975 }
1976 case DescriptorClass::GeneralBuffer: {
1977 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1978 auto buf_state = buffer_descriptor->GetBufferState();
1979 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001980 const ResourceAccessRange range =
1981 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001982 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001983 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001984 skip |= sync_state_->LogError(
1985 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001986 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1987 func_name, string_SyncHazard(hazard.hazard),
1988 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001989 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001990 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001991 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1992 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1993 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001994 }
1995 break;
1996 }
1997 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1998 default:
1999 break;
2000 }
2001 }
2002 }
2003 }
2004 return skip;
2005}
2006
2007void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
2008 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002009 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06002010 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002011 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
2012 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06002013 return;
2014 }
2015
2016 using DescriptorClass = cvdescriptorset::DescriptorClass;
2017 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
2018 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
2019 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
2020 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
2021
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002022 for (const auto &stage_state : pipe->stage_state) {
2023 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
2024 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06002025 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002026 }
locke-lunarg61870c22020-06-09 14:51:50 -06002027 for (const auto &set_binding : stage_state.descriptor_uses) {
2028 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
2029 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
2030 set_binding.first.second);
2031 const auto descriptor_type = binding_it.GetType();
2032 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2033 auto array_idx = 0;
2034
2035 if (binding_it.IsVariableDescriptorCount()) {
2036 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2037 }
2038 SyncStageAccessIndex sync_index =
2039 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
2040
2041 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2042 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2043 switch (descriptor->GetClass()) {
2044 case DescriptorClass::ImageSampler:
2045 case DescriptorClass::Image: {
2046 const IMAGE_VIEW_STATE *img_view_state = nullptr;
2047 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
2048 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
2049 } else {
2050 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
2051 }
2052 if (!img_view_state) continue;
2053 const IMAGE_STATE *img_state = img_view_state->image_state.get();
2054 VkExtent3D extent = {};
2055 VkOffset3D offset = {};
2056 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
2057 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
2058 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
2059 } else {
2060 extent = img_state->createInfo.extent;
2061 }
2062 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
2063 offset, extent, tag);
2064 break;
2065 }
2066 case DescriptorClass::TexelBuffer: {
2067 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
2068 if (!buf_view_state) continue;
2069 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002070 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06002071 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
2072 break;
2073 }
2074 case DescriptorClass::GeneralBuffer: {
2075 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
2076 auto buf_state = buffer_descriptor->GetBufferState();
2077 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002078 const ResourceAccessRange range =
2079 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06002080 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
2081 break;
2082 }
2083 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2084 default:
2085 break;
2086 }
2087 }
2088 }
2089 }
2090}
2091
2092bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2093 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002094 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2095 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002096 return skip;
2097 }
2098
2099 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2100 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002101 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002102
2103 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002104 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002105 if (binding_description.binding < binding_buffers_size) {
2106 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002107 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002108
locke-lunarg1ae57d62020-11-18 10:49:19 -07002109 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002110 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2111 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06002112 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
2113 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002114 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002115 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 -06002116 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002117 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002118 }
2119 }
2120 }
2121 return skip;
2122}
2123
2124void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002125 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2126 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002127 return;
2128 }
2129 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2130 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002131 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002132
2133 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002134 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002135 if (binding_description.binding < binding_buffers_size) {
2136 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002137 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002138
locke-lunarg1ae57d62020-11-18 10:49:19 -07002139 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002140 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2141 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06002142 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
2143 }
2144 }
2145}
2146
2147bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2148 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002149 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002150 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002151 }
locke-lunarg61870c22020-06-09 14:51:50 -06002152
locke-lunarg1ae57d62020-11-18 10:49:19 -07002153 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002154 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002155 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2156 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06002157 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
2158 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002159 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002160 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 -06002161 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002162 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002163 }
2164
2165 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2166 // We will detect more accurate range in the future.
2167 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2168 return skip;
2169}
2170
2171void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002172 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 -06002173
locke-lunarg1ae57d62020-11-18 10:49:19 -07002174 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002175 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002176 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2177 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06002178 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
2179
2180 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2181 // We will detect more accurate range in the future.
2182 RecordDrawVertex(UINT32_MAX, 0, tag);
2183}
2184
2185bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002186 bool skip = false;
2187 if (!current_renderpass_context_) return skip;
2188 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
2189 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
2190 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002191}
2192
2193void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002194 if (current_renderpass_context_) {
locke-lunarg7077d502020-06-18 21:37:26 -06002195 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
2196 tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002197 }
locke-lunarg61870c22020-06-09 14:51:50 -06002198}
2199
John Zulauf355e49b2020-04-24 15:11:15 -06002200bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002201 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06002202 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06002203 skip |=
2204 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002205
2206 return skip;
2207}
2208
2209bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
2210 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06002211 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002212 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06002213 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06002214 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
2215 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002216
2217 return skip;
2218}
2219
2220void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
2221 assert(sync_state_);
2222 if (!cb_state_) return;
2223
2224 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06002225 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06002226 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06002227 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002228 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002229}
2230
John Zulauf355e49b2020-04-24 15:11:15 -06002231void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002232 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06002233 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002234 current_context_ = &current_renderpass_context_->CurrentContext();
2235}
2236
John Zulauf355e49b2020-04-24 15:11:15 -06002237void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002238 assert(current_renderpass_context_);
2239 if (!current_renderpass_context_) return;
2240
John Zulauf1a224292020-06-30 14:52:13 -06002241 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002242 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002243 current_renderpass_context_ = nullptr;
2244}
2245
John Zulauf49beb112020-11-04 16:06:31 -07002246bool CommandBufferAccessContext::ValidateSetEvent(VkCommandBuffer commandBuffer, VkEvent event,
2247 VkPipelineStageFlags stageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002248 // I'll put this here just in case we need to pass this in for future extension support
2249 const auto cmd = CMD_SETEVENT;
2250 bool skip = false;
2251 const auto *sync_event = GetEventState(event);
2252 if (!sync_event) return false; // Core, Lifetimes, or Param check needs to catch invalid events.
2253
2254 const char *const reset_set =
2255 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
2256 "hazards.";
2257 const char *const wait =
2258 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
2259
2260 const auto exec_scope = WithEarlierPipelineStages(ExpandPipelineStages(GetQueueFlags(), stageMask));
2261 if (!sync_event->HasBarrier(stageMask, exec_scope)) {
2262 const char *vuid = nullptr;
2263 const char *message = nullptr;
2264 switch (sync_event->last_command) {
2265 case CMD_RESETEVENT:
2266 // Needs a barrier between reset and set
2267 vuid = "SYNC-vkCmdSetEvent-missingbarrier-reset";
2268 message = reset_set;
2269 break;
2270 case CMD_SETEVENT:
2271 // Needs a barrier between set and set
2272 vuid = "SYNC-vkCmdSetEvent-missingbarrier-set";
2273 message = reset_set;
2274 break;
2275 case CMD_WAITEVENTS:
2276 // Needs a barrier or is in second execution scope
2277 vuid = "SYNC-vkCmdSetEvent-missingbarrier-wait";
2278 message = wait;
2279 break;
2280 default:
2281 // The only other valid last command that wasn't one.
2282 assert(sync_event->last_command == CMD_NONE);
2283 break;
2284 }
2285 if (vuid) {
2286 assert(nullptr != message);
2287 const char *const cmd_name = CommandTypeString(cmd);
2288 skip |= sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2289 cmd_name, CommandTypeString(sync_event->last_command));
2290 }
2291 }
2292
2293 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002294}
2295
John Zulauf4a6105a2020-11-17 15:11:05 -07002296void CommandBufferAccessContext::RecordSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask,
2297 const ResourceUsageTag &tag) {
2298 auto *sync_event = GetEventState(event);
2299 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
2300
2301 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
2302 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
2303 // any issues caused by naive scope setting here.
2304
2305 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
2306 // Given:
2307 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
2308 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
2309
2310 const auto stage_mask = ExpandPipelineStages(GetQueueFlags(), stageMask);
2311 const auto exec_scope = WithEarlierPipelineStages(stage_mask);
2312
2313 sync_event->stage_mask_param = stageMask;
2314
2315 if (!sync_event->HasBarrier(stageMask, exec_scope)) {
2316 sync_event->unsynchronized_set = sync_event->last_command;
2317 sync_event->ResetFirstScope();
2318 } else if (sync_event->exec_scope == 0) {
2319 // We only set the scope if there isn't one
2320 sync_event->stage_mask = stage_mask;
2321 sync_event->exec_scope = exec_scope;
2322 sync_event->stage_accesses = SyncStageAccess::AccessScopeByStage(sync_event->stage_mask);
2323
2324 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
2325 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
2326 if (access.second.InSourceScopeOrChain(sync_event->exec_scope, sync_event->stage_accesses)) {
2327 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
2328 }
2329 };
2330 GetCurrentAccessContext()->ForAll(set_scope);
2331 sync_event->unsynchronized_set = CMD_NONE;
2332 sync_event->first_scope_tag = tag;
2333 }
2334 sync_event->last_command = CMD_SETEVENT;
2335 sync_event->barriers = 0U;
2336}
John Zulauf49beb112020-11-04 16:06:31 -07002337
2338bool CommandBufferAccessContext::ValidateResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
2339 VkPipelineStageFlags stageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002340 // I'll put this here just in case we need to pass this in for future extension support
2341 const auto cmd = CMD_RESETEVENT;
2342
2343 bool skip = false;
2344 // TODO: EVENTS:
2345 // What is it we need to check... that we've had a reset since a set? Set/Set seems ill formed...
2346 const auto *sync_event = GetEventState(event);
2347 if (!sync_event) return false; // Core, Lifetimes, or Param check needs to catch invalid events.
2348
2349 const char *const set_wait =
2350 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
2351 "hazards.";
2352 const char *message = set_wait; // Only one message this call.
2353 const auto exec_scope = WithEarlierPipelineStages(ExpandPipelineStages(GetQueueFlags(), stageMask));
2354 if (!sync_event->HasBarrier(stageMask, exec_scope)) {
2355 const char *vuid = nullptr;
2356 switch (sync_event->last_command) {
2357 case CMD_SETEVENT:
2358 // Needs a barrier between set and reset
2359 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
2360 break;
2361 case CMD_WAITEVENTS: {
2362 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
2363 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
2364 break;
2365 }
2366 default:
2367 // The only other valid last command that wasn't one.
2368 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT));
2369 break;
2370 }
2371 if (vuid) {
2372 const char *const cmd_name = CommandTypeString(cmd);
2373 skip |= sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2374 cmd_name, CommandTypeString(sync_event->last_command));
2375 }
2376 }
2377 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002378}
2379
John Zulauf4a6105a2020-11-17 15:11:05 -07002380void CommandBufferAccessContext::RecordResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
2381 const auto cmd = CMD_RESETEVENT;
2382 auto *sync_event = GetEventState(event);
2383 if (!sync_event) return;
John Zulauf49beb112020-11-04 16:06:31 -07002384
John Zulauf4a6105a2020-11-17 15:11:05 -07002385 // Clear out the first sync scope, any races vs. wait or set are reported, so we'll keep the bookkeeping simple assuming
2386 // the safe case
2387 for (const auto address_type : kAddressTypes) {
2388 sync_event->first_scope[static_cast<size_t>(address_type)].clear();
2389 }
2390
2391 // Update the event state
2392 sync_event->last_command = cmd;
2393 sync_event->unsynchronized_set = CMD_NONE;
2394 sync_event->ResetFirstScope();
2395 sync_event->barriers = 0U;
2396}
2397
2398bool CommandBufferAccessContext::ValidateWaitEvents(uint32_t eventCount, const VkEvent *pEvents, VkPipelineStageFlags srcStageMask,
2399 VkPipelineStageFlags dstStageMask, uint32_t memoryBarrierCount,
2400 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
John Zulauf49beb112020-11-04 16:06:31 -07002401 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2402 uint32_t imageMemoryBarrierCount,
2403 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002404 const auto cmd = CMD_WAITEVENTS;
2405 const char *const ignored = "Wait operation is ignored for this event.";
2406 bool skip = false;
2407
2408 if (srcStageMask & VK_PIPELINE_STAGE_HOST_BIT) {
2409 const char *const cmd_name = CommandTypeString(cmd);
2410 const char *const vuid = "SYNC-vkCmdWaitEvents-hostevent-unsupported";
John Zulauffe757512020-12-18 12:17:47 -07002411 skip = sync_state_->LogInfo(cb_state_->commandBuffer, vuid,
2412 "%s, srcStageMask includes %s, unsupported by synchronization validaton.", cmd_name,
2413 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT), ignored);
John Zulauf4a6105a2020-11-17 15:11:05 -07002414 }
2415
2416 VkPipelineStageFlags event_stage_masks = 0U;
John Zulauffe757512020-12-18 12:17:47 -07002417 bool events_not_found = false;
John Zulauf4a6105a2020-11-17 15:11:05 -07002418 for (uint32_t event_index = 0; event_index < eventCount; event_index++) {
2419 const auto event = pEvents[event_index];
2420 const auto *sync_event = GetEventState(event);
John Zulauffe757512020-12-18 12:17:47 -07002421 if (!sync_event) {
2422 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
2423 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives.
2424
2425 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
2426 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002427
2428 event_stage_masks |= sync_event->stage_mask_param;
2429 const auto ignore_reason = sync_event->IsIgnoredByWait(srcStageMask);
2430 if (ignore_reason) {
2431 switch (ignore_reason) {
2432 case SyncEventState::ResetWaitRace: {
2433 const char *const cmd_name = CommandTypeString(cmd);
2434 const char *const vuid = "SYNC-vkCmdWaitEvents-missingbarrier-reset";
2435 const char *const message =
2436 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
2437 skip |=
2438 sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2439 cmd_name, CommandTypeString(sync_event->last_command), ignored);
2440 break;
2441 }
2442 case SyncEventState::SetRace: {
2443 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for this
2444 // event
2445 const char *const cmd_name = CommandTypeString(cmd);
2446 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
2447 const char *const message =
2448 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, % %s";
2449 const char *const reason = "First synchronization scope is undefined.";
2450 skip |=
2451 sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2452 CommandTypeString(sync_event->last_command), reason, ignored);
2453 break;
2454 }
2455 case SyncEventState::MissingStageBits: {
2456 const VkPipelineStageFlags missing_bits = sync_event->stage_mask_param & ~srcStageMask;
2457 // Issue error message that event waited for is not in wait events scope
2458 const char *const cmd_name = CommandTypeString(cmd);
2459 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
2460 const char *const message =
2461 "%s: %s stageMask 0x%" PRIx32 " includes bits not present in srcStageMask 0x%" PRIx32
2462 ". Bits missing from srcStageMask %s. %s";
2463 skip |= sync_state_->LogError(
2464 event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2465 sync_event->stage_mask_param, srcStageMask, string_VkPipelineStageFlags(missing_bits).c_str(), ignored);
2466 break;
2467 }
2468 default:
2469 assert(ignore_reason == SyncEventState::NotIgnored);
2470 }
2471 } else if (imageMemoryBarrierCount) {
2472 const auto *context = GetCurrentAccessContext();
2473 assert(context);
2474 for (uint32_t barrier_index = 0; barrier_index < imageMemoryBarrierCount; barrier_index++) {
2475 const auto &barrier = pImageMemoryBarriers[barrier_index];
2476 if (barrier.oldLayout == barrier.newLayout) continue;
2477 const auto *image_state = sync_state_->Get<IMAGE_STATE>(barrier.image);
2478 if (!image_state) continue;
2479 auto subresource_range = NormalizeSubresourceRange(image_state->createInfo, barrier.subresourceRange);
2480 const auto src_access_scope = SyncStageAccess::AccessScope(sync_event->stage_accesses, barrier.srcAccessMask);
2481 const auto hazard =
2482 context->DetectImageBarrierHazard(*image_state, sync_event->exec_scope, src_access_scope, subresource_range,
2483 *sync_event, AccessContext::DetectOptions::kDetectAll);
2484 if (hazard.hazard) {
2485 const char *const cmd_name = CommandTypeString(cmd);
2486 skip |= sync_state_->LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
2487 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", cmd_name,
2488 string_SyncHazard(hazard.hazard), barrier_index,
2489 sync_state_->report_data->FormatHandle(barrier.image).c_str(),
2490 string_UsageTag(hazard).c_str());
2491 break;
2492 }
2493 }
2494 }
2495 }
2496
2497 // Note that we can't check for HOST in pEvents as we don't track that set event type
2498 const auto extra_stage_bits = (srcStageMask & ~VK_PIPELINE_STAGE_HOST_BIT) & ~event_stage_masks;
2499 if (extra_stage_bits) {
2500 // Issue error message that event waited for is not in wait events scope
2501 const char *const cmd_name = CommandTypeString(cmd);
2502 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
2503 const char *const message =
John Zulauffe757512020-12-18 12:17:47 -07002504 "%s: srcStageMask 0x%" PRIx32 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
2505 if (events_not_found) {
2506 skip |= sync_state_->LogInfo(cb_state_->commandBuffer, vuid, message, cmd_name, srcStageMask,
2507 string_VkPipelineStageFlags(extra_stage_bits).c_str(),
2508 " vkCmdSetEvent may be in previously submitted command buffer.");
2509 } else {
2510 skip |= sync_state_->LogError(cb_state_->commandBuffer, vuid, message, cmd_name, srcStageMask,
2511 string_VkPipelineStageFlags(extra_stage_bits).c_str(), "");
2512 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002513 }
2514 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002515}
2516
2517void CommandBufferAccessContext::RecordWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
2518 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
2519 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2520 uint32_t bufferMemoryBarrierCount,
2521 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2522 uint32_t imageMemoryBarrierCount,
John Zulauf4a6105a2020-11-17 15:11:05 -07002523 const VkImageMemoryBarrier *pImageMemoryBarriers, const ResourceUsageTag &tag) {
2524 auto *access_context = GetCurrentAccessContext();
2525 assert(access_context);
2526 if (!access_context) return;
2527
2528 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
2529 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
2530 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
2531 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here
2532 access_context->ResolvePreviousAccesses();
2533
2534 const auto dst_stage_mask = ExpandPipelineStages(GetQueueFlags(), dstStageMask);
2535 auto dst_stage_accesses = SyncStageAccess::AccessScopeByStage(dst_stage_mask);
2536 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2537 for (uint32_t event_index = 0; event_index < eventCount; event_index++) {
2538 const auto event = pEvents[event_index];
2539 auto *sync_event = GetEventState(event);
2540 if (!sync_event) continue;
2541
2542 sync_event->last_command = CMD_WAITEVENTS;
2543
2544 if (!sync_event->IsIgnoredByWait(srcStageMask)) {
2545 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
2546 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
2547 // of the barriers is maintained.
2548 ApplyBufferBarriers(*sync_event, dst_exec_scope, dst_stage_accesses, bufferMemoryBarrierCount, pBufferMemoryBarriers);
2549 ApplyImageBarriers(*sync_event, dst_exec_scope, dst_stage_accesses, imageMemoryBarrierCount, pImageMemoryBarriers, tag);
2550 ApplyGlobalBarriers(*sync_event, dstStageMask, dst_exec_scope, dst_stage_accesses, memoryBarrierCount, pMemoryBarriers,
2551 tag);
2552 } else {
2553 // We ignored this wait, so we don't have any effective synchronization barriers for it.
2554 sync_event->barriers = 0U;
2555 }
2556 }
2557
2558 // Apply the pending barriers
2559 ResolvePendingBarrierFunctor apply_pending_action(tag);
2560 access_context->ApplyGlobalBarriers(apply_pending_action);
2561}
2562
2563void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2564 // Erase is okay with the key not being
2565 event_state_.erase(event);
2566}
2567
2568SyncEventState *CommandBufferAccessContext::GetEventState(VkEvent event) {
2569 auto &event_up = event_state_[event];
2570 if (!event_up) {
2571 auto event_atate = sync_state_->GetShared<EVENT_STATE>(event);
2572 event_up.reset(new SyncEventState(event_atate));
2573 }
2574 return event_up.get();
2575}
2576
2577const SyncEventState *CommandBufferAccessContext::GetEventState(VkEvent event) const {
2578 auto event_it = event_state_.find(event);
2579 if (event_it == event_state_.cend()) {
2580 return nullptr;
2581 }
2582 return event_it->second.get();
2583}
John Zulauf49beb112020-11-04 16:06:31 -07002584
locke-lunarg61870c22020-06-09 14:51:50 -06002585bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
2586 const VkRect2D &render_area, const char *func_name) const {
2587 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002588 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2589 if (!pipe ||
2590 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002591 return skip;
2592 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002593 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002594 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2595 VkExtent3D extent = CastTo3D(render_area.extent);
2596 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06002597
John Zulauf1a224292020-06-30 14:52:13 -06002598 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002599 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002600 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2601 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002602 if (location >= subpass.colorAttachmentCount ||
2603 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002604 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002605 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002606 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002607 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
2608 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06002609 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002610 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002611 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002612 func_name, string_SyncHazard(hazard.hazard),
2613 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2614 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002615 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002616 }
2617 }
2618 }
locke-lunarg37047832020-06-12 13:44:45 -06002619
2620 // PHASE1 TODO: Add layout based read/vs. write selection.
2621 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002622 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002623 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002624 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002625 bool depth_write = false, stencil_write = false;
2626
2627 // PHASE1 TODO: These validation should be in core_checks.
2628 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002629 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2630 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002631 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2632 depth_write = true;
2633 }
2634 // PHASE1 TODO: It needs to check if stencil is writable.
2635 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2636 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2637 // PHASE1 TODO: These validation should be in core_checks.
2638 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002639 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002640 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2641 stencil_write = true;
2642 }
2643
2644 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2645 if (depth_write) {
2646 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002647 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2648 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002649 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002650 skip |= sync_state.LogError(
2651 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002652 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002653 func_name, string_SyncHazard(hazard.hazard),
2654 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2655 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002656 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002657 }
2658 }
2659 if (stencil_write) {
2660 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002661 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2662 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002663 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002664 skip |= sync_state.LogError(
2665 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002666 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002667 func_name, string_SyncHazard(hazard.hazard),
2668 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2669 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002670 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002671 }
locke-lunarg61870c22020-06-09 14:51:50 -06002672 }
2673 }
2674 return skip;
2675}
2676
locke-lunarg96dc9632020-06-10 17:22:18 -06002677void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
2678 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002679 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2680 if (!pipe ||
2681 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002682 return;
2683 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002684 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002685 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2686 VkExtent3D extent = CastTo3D(render_area.extent);
2687 VkOffset3D offset = CastTo3D(render_area.offset);
2688
John Zulauf1a224292020-06-30 14:52:13 -06002689 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002690 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002691 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2692 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002693 if (location >= subpass.colorAttachmentCount ||
2694 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002695 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002696 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002697 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002698 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
2699 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002700 }
2701 }
locke-lunarg37047832020-06-12 13:44:45 -06002702
2703 // PHASE1 TODO: Add layout based read/vs. write selection.
2704 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002705 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002706 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002707 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002708 bool depth_write = false, stencil_write = false;
2709
2710 // PHASE1 TODO: These validation should be in core_checks.
2711 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002712 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2713 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002714 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2715 depth_write = true;
2716 }
2717 // PHASE1 TODO: It needs to check if stencil is writable.
2718 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2719 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2720 // PHASE1 TODO: These validation should be in core_checks.
2721 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002722 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002723 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2724 stencil_write = true;
2725 }
2726
2727 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2728 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002729 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2730 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002731 }
2732 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002733 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2734 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002735 }
locke-lunarg61870c22020-06-09 14:51:50 -06002736 }
2737}
2738
John Zulauf1507ee42020-05-18 11:33:09 -06002739bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
2740 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002741 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002742 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06002743 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2744 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002745 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2746 func_name);
2747
John Zulauf355e49b2020-04-24 15:11:15 -06002748 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002749 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06002750 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002751 if (!skip) {
2752 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2753 // on a copy of the (empty) next context.
2754 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2755 AccessContext temp_context(next_context);
2756 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
2757 skip |= temp_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2758 }
John Zulauf7635de32020-05-29 17:14:15 -06002759 return skip;
2760}
2761bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
2762 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002763 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002764 bool skip = false;
2765 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2766 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002767 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2768 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002769 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002770 return skip;
2771}
2772
John Zulauf7635de32020-05-29 17:14:15 -06002773AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2774 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2775}
2776
2777bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2778 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002779 bool skip = false;
2780
John Zulauf7635de32020-05-29 17:14:15 -06002781 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2782 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2783 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2784 // to apply and only copy then, if this proves a hot spot.
2785 std::unique_ptr<AccessContext> proxy_for_current;
2786
John Zulauf355e49b2020-04-24 15:11:15 -06002787 // Validate the "finalLayout" transitions to external
2788 // Get them from where there we're hidding in the extra entry.
2789 const auto &final_transitions = rp_state_->subpass_transitions.back();
2790 for (const auto &transition : final_transitions) {
2791 const auto &attach_view = attachment_views_[transition.attachment];
2792 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2793 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002794 auto *context = trackback.context;
2795
2796 if (transition.prev_pass == current_subpass_) {
2797 if (!proxy_for_current) {
2798 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2799 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2800 }
2801 context = proxy_for_current.get();
2802 }
2803
John Zulaufa0a98292020-09-18 09:30:10 -06002804 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2805 const auto merged_barrier = MergeBarriers(trackback.barriers);
2806 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2807 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2808 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002809 if (hazard.hazard) {
2810 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2811 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002812 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002813 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002814 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002815 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002816 }
2817 }
2818 return skip;
2819}
2820
2821void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2822 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002823 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002824}
2825
John Zulauf1507ee42020-05-18 11:33:09 -06002826void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2827 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2828 auto &subpass_context = subpass_contexts_[current_subpass_];
2829 VkExtent3D extent = CastTo3D(render_area.extent);
2830 VkOffset3D offset = CastTo3D(render_area.offset);
2831
2832 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2833 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2834 if (attachment_views_[i] == nullptr) continue; // UNUSED
2835 const auto &view = *attachment_views_[i];
2836 const IMAGE_STATE *image = view.image_state.get();
2837 if (image == nullptr) continue;
2838
2839 const auto &ci = attachment_ci[i];
2840 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002841 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002842 const bool is_color = !(has_depth || has_stencil);
2843
2844 if (is_color) {
2845 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2846 extent, tag);
2847 } else {
2848 auto update_range = view.normalized_subresource_range;
2849 if (has_depth) {
2850 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2851 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2852 }
2853 if (has_stencil) {
2854 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2855 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2856 tag);
2857 }
2858 }
2859 }
2860 }
2861}
2862
John Zulauf355e49b2020-04-24 15:11:15 -06002863void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002864 const AccessContext *external_context, VkQueueFlags queue_flags,
2865 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002866 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002867 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002868 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2869 // Add this for all subpasses here so that they exsist during next subpass validation
2870 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002871 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002872 }
2873 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2874
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002875 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002876 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002877 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002878}
John Zulauf1507ee42020-05-18 11:33:09 -06002879
2880void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002881 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2882 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002883 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002884
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002885 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2886 // subpass, so their tag needs to be different from the layout and load operations below.
Jeremy Gebben4bb73502020-12-14 11:17:50 -07002887 ResourceUsageTag next_tag = tag.NextSubCommand();
John Zulauf355e49b2020-04-24 15:11:15 -06002888 current_subpass_++;
2889 assert(current_subpass_ < subpass_contexts_.size());
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002890 subpass_contexts_[current_subpass_].SetStartTag(next_tag);
2891 RecordLayoutTransitions(next_tag);
2892 RecordLoadOperations(render_area, next_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002893}
2894
John Zulauf1a224292020-06-30 14:52:13 -06002895void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2896 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002897 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002898 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002899 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002900
John Zulauf355e49b2020-04-24 15:11:15 -06002901 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002902 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002903
2904 // Add the "finalLayout" transitions to external
2905 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002906 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2907 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2908 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002909 const auto &final_transitions = rp_state_->subpass_transitions.back();
2910 for (const auto &transition : final_transitions) {
2911 const auto &attachment = attachment_views_[transition.attachment];
2912 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002913 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf1e331ec2020-12-04 18:29:38 -07002914 std::vector<PipelineBarrierOp> barrier_ops;
2915 barrier_ops.reserve(last_trackback.barriers.size());
2916 for (const auto &barrier : last_trackback.barriers) {
2917 barrier_ops.emplace_back(barrier, true);
2918 }
2919 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, barrier_ops, tag);
2920 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002921 }
2922}
2923
John Zulauf3d84f1b2020-03-09 13:33:25 -06002924SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2925 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2926 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2927 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2928 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2929 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2930 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2931}
2932
John Zulaufb02c1eb2020-10-06 16:33:36 -06002933// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2934void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2935 for (const auto &barrier : barriers) {
2936 ApplyBarrier(barrier, layout_transition);
2937 }
2938}
2939
John Zulauf89311b42020-09-29 16:28:47 -06002940// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2941// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2942// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002943void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2944 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002945 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002946 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002947 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002948 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002949 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002950 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002951}
John Zulauf9cb530d2019-09-30 14:14:10 -06002952HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2953 HazardResult hazard;
2954 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002955 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002956 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002957 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002958 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002959 }
2960 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002961 // Write operation:
2962 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2963 // If reads exists -- test only against them because either:
2964 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2965 // * 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
2966 // the current write happens after the reads, so just test the write against the reades
2967 // Otherwise test against last_write
2968 //
2969 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002970 if (last_reads.size()) {
2971 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002972 if (IsReadHazard(usage_stage, read_access)) {
2973 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2974 break;
2975 }
2976 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002977 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002978 // Write-After-Write check -- if we have a previous write to test against
2979 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002980 }
2981 }
2982 return hazard;
2983}
2984
John Zulauf69133422020-05-20 14:55:53 -06002985HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2986 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2987 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002988 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002989 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002990 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2991 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002992 if (IsRead(usage_bit)) {
2993 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2994 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2995 if (is_raw_hazard) {
2996 // NOTE: we know last_write is non-zero
2997 // See if the ordering rules save us from the simple RAW check above
2998 // First check to see if the current usage is covered by the ordering rules
2999 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
3000 const bool usage_is_ordered =
3001 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
3002 if (usage_is_ordered) {
3003 // Now see of the most recent write (or a subsequent read) are ordered
3004 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
3005 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06003006 }
3007 }
John Zulauf4285ee92020-09-23 10:20:52 -06003008 if (is_raw_hazard) {
3009 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
3010 }
John Zulauf361fb532020-07-22 10:45:39 -06003011 } else {
3012 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003013 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07003014 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06003015 // Look for any WAR hazards outside the ordered set of stages
John Zulauf4285ee92020-09-23 10:20:52 -06003016 VkPipelineStageFlags ordered_stages = 0;
3017 if (usage_write_is_ordered) {
3018 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
3019 ordered_stages = GetOrderedStages(ordering);
3020 }
3021 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
3022 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003023 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06003024 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
3025 if (IsReadHazard(usage_stage, read_access)) {
3026 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3027 break;
3028 }
John Zulaufd14743a2020-07-03 09:42:39 -06003029 }
3030 }
John Zulauf4285ee92020-09-23 10:20:52 -06003031 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003032 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003033 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06003034 }
John Zulauf69133422020-05-20 14:55:53 -06003035 }
3036 }
3037 return hazard;
3038}
3039
John Zulauf2f952d22020-02-10 11:34:51 -07003040// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003041HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07003042 HazardResult hazard;
3043 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003044 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
3045 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
3046 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07003047 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003048 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06003049 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07003050 }
3051 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003052 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06003053 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07003054 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003055 // 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 -07003056 for (const auto &read_access : last_reads) {
3057 if (read_access.tag.index >= start_tag.index) {
3058 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003059 break;
3060 }
3061 }
John Zulauf2f952d22020-02-10 11:34:51 -07003062 }
3063 }
3064 return hazard;
3065}
3066
John Zulauf36bcf6a2020-02-03 15:12:52 -07003067HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003068 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07003069 // Only supporting image layout transitions for now
3070 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3071 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06003072 // only test for WAW if there no intervening read operations.
3073 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07003074 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06003075 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07003076 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003077 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06003078 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07003079 break;
3080 }
3081 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003082 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3083 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3084 }
3085
3086 return hazard;
3087}
3088
3089HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
3090 const SyncStageAccessFlags &src_access_scope,
3091 const ResourceUsageTag &event_tag) const {
3092 // Only supporting image layout transitions for now
3093 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3094 HazardResult hazard;
3095 // only test for WAW if there no intervening read operations.
3096 // See DetectHazard(SyncStagetAccessIndex) above for more details.
3097
John Zulaufab7756b2020-12-29 16:10:16 -07003098 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003099 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
3100 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07003101 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003102 if (read_access.tag.IsBefore(event_tag)) {
3103 // The read is in the events first synchronization scope, so we use a barrier hazard check
3104 // If the read stage is not in the src sync scope
3105 // *AND* not execution chained with an existing sync barrier (that's the or)
3106 // then the barrier access is unsafe (R/W after R)
3107 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3108 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3109 break;
3110 }
3111 } else {
3112 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3113 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3114 }
3115 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003116 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003117 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
3118 if (write_tag.IsBefore(event_tag)) {
3119 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3120 // So do a normal barrier hazard check
3121 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3122 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3123 }
3124 } else {
3125 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003126 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3127 }
John Zulaufd14743a2020-07-03 09:42:39 -06003128 }
John Zulauf361fb532020-07-22 10:45:39 -06003129
John Zulauf0cb5be22020-01-23 12:18:22 -07003130 return hazard;
3131}
3132
John Zulauf5f13a792020-03-10 07:31:21 -06003133// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3134// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3135// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3136void ResourceAccessState::Resolve(const ResourceAccessState &other) {
3137 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003138 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3139 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003140 *this = other;
3141 } else if (!other.write_tag.IsBefore(write_tag)) {
3142 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
3143 // dependency chaining logic or any stage expansion)
3144 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003145 pending_write_barriers |= other.pending_write_barriers;
3146 pending_layout_transition |= other.pending_layout_transition;
3147 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003148
John Zulaufd14743a2020-07-03 09:42:39 -06003149 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07003150 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06003151 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07003152 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003153 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003154 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003155 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003156 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3157 // but we should wait on profiling data for that.
3158 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003159 auto &my_read = last_reads[my_read_index];
3160 if (other_read.stage == my_read.stage) {
3161 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003162 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003163 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003164 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003165 my_read.pending_dep_chain = other_read.pending_dep_chain;
3166 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3167 // May require tracking more than one access per stage.
3168 my_read.barriers = other_read.barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003169 if (my_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
3170 // Since I'm overwriting the fragement stage read, also update the input attachment info
3171 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003172 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003173 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003174 } else if (other_read.tag.IsBefore(my_read.tag)) {
3175 // The read tags match so merge the barriers
3176 my_read.barriers |= other_read.barriers;
3177 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003178 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003179
John Zulauf5f13a792020-03-10 07:31:21 -06003180 break;
3181 }
3182 }
3183 } else {
3184 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07003185 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06003186 last_read_stages |= other_read.stage;
John Zulauf4285ee92020-09-23 10:20:52 -06003187 if (other_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06003188 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003189 }
John Zulauf5f13a792020-03-10 07:31:21 -06003190 }
3191 }
John Zulauf361fb532020-07-22 10:45:39 -06003192 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003193 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3194 // ignore it.
John Zulauf5f13a792020-03-10 07:31:21 -06003195}
3196
John Zulauf9cb530d2019-09-30 14:14:10 -06003197void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
3198 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3199 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003200 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003201 // Mulitple outstanding reads may be of interest and do dependency chains independently
3202 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3203 const auto usage_stage = PipelineStageBit(usage_index);
3204 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003205 for (auto &read_access : last_reads) {
3206 if (read_access.stage == usage_stage) {
3207 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003208 break;
3209 }
3210 }
3211 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07003212 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003213 last_read_stages |= usage_stage;
3214 }
John Zulauf4285ee92020-09-23 10:20:52 -06003215
3216 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
3217 if (usage_stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06003218 // TODO Revisit re: multiple reads for a given stage
3219 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003220 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003221 } else {
3222 // Assume write
3223 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003224 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003225 }
3226}
John Zulauf5f13a792020-03-10 07:31:21 -06003227
John Zulauf89311b42020-09-29 16:28:47 -06003228// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3229// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3230// We can overwrite them as *this* write is now after them.
3231//
3232// 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 -07003233void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003234 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06003235 last_read_stages = 0;
3236 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003237 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003238
3239 write_barriers = 0;
3240 write_dependency_chain = 0;
3241 write_tag = tag;
3242 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003243}
3244
John Zulauf89311b42020-09-29 16:28:47 -06003245// Apply the memory barrier without updating the existing barriers. The execution barrier
3246// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3247// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3248// replace the current write barriers or add to them, so accumulate to pending as well.
3249void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3250 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3251 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003252 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3253 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3254 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3255 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulauf4a6105a2020-11-17 15:11:05 -07003256 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003257 pending_write_barriers |= barrier.dst_access_scope;
3258 pending_write_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003259 }
John Zulauf89311b42020-09-29 16:28:47 -06003260 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3261 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003262
John Zulauf89311b42020-09-29 16:28:47 -06003263 if (!pending_layout_transition) {
3264 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3265 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003266 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06003267 // 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 -07003268 if (barrier.src_exec_scope & (read_access.stage | read_access.barriers)) {
3269 read_access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003270 }
3271 }
John Zulaufa0a98292020-09-18 09:30:10 -06003272 }
John Zulaufa0a98292020-09-18 09:30:10 -06003273}
3274
John Zulauf4a6105a2020-11-17 15:11:05 -07003275// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3276// changes the "chaining" state, but to keep barriers independent. See discussion above.
3277void ResourceAccessState::ApplyBarrier(const ResourceUsageTag &scope_tag, const SyncBarrier &barrier, bool layout_transition) {
3278 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3279 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3280 // in order to know if it's in the excecution scope
3281 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3282 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3283 // errors w.r.t. "most recent" accesses.
3284 if (layout_transition || ((write_tag.IsBefore(scope_tag)) && (barrier.src_access_scope & last_write).any())) {
3285 pending_write_barriers |= barrier.dst_access_scope;
3286 pending_write_dep_chain |= barrier.dst_exec_scope;
3287 }
3288 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3289 pending_layout_transition |= layout_transition;
3290
3291 if (!pending_layout_transition) {
3292 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3293 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003294 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003295 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3296 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3297 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3298 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3299 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3300 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3301 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulaufab7756b2020-12-29 16:10:16 -07003302 if (read_access.tag.IsBefore(scope_tag) && (barrier.src_exec_scope & (read_access.stage | read_access.barriers))) {
3303 read_access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003304 }
3305 }
3306 }
3307}
John Zulauf89311b42020-09-29 16:28:47 -06003308void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
3309 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06003310 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
3311 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
3312 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003313 }
John Zulauf89311b42020-09-29 16:28:47 -06003314
3315 // Apply the accumulate execution barriers (and thus update chaining information)
3316 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003317 for (auto &read_access : last_reads) {
3318 read_access.barriers |= read_access.pending_dep_chain;
3319 read_execution_barriers |= read_access.barriers;
3320 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003321 }
3322
3323 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3324 write_dependency_chain |= pending_write_dep_chain;
3325 write_barriers |= pending_write_barriers;
3326 pending_write_dep_chain = 0;
3327 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003328}
3329
John Zulauf59e25072020-07-17 10:55:21 -06003330// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003331VkPipelineStageFlags ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
John Zulauf59e25072020-07-17 10:55:21 -06003332 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003333
John Zulaufab7756b2020-12-29 16:10:16 -07003334 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003335 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003336 barriers = read_access.barriers;
3337 break;
John Zulauf59e25072020-07-17 10:55:21 -06003338 }
3339 }
John Zulauf4285ee92020-09-23 10:20:52 -06003340
John Zulauf59e25072020-07-17 10:55:21 -06003341 return barriers;
3342}
3343
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003344inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003345 assert(IsRead(usage));
3346 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3347 // * the previous reads are not hazards, and thus last_write must be visible and available to
3348 // any reads that happen after.
3349 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3350 // 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 -07003351 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003352}
3353
John Zulauf4285ee92020-09-23 10:20:52 -06003354VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const SyncOrderingBarrier &ordering) const {
3355 // Whether the stage are in the ordering scope only matters if the current write is ordered
3356 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
3357 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003358 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003359 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003360 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
3361 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
3362 }
3363
3364 return ordered_stages;
3365}
3366
John Zulaufd1f85d42020-04-15 12:23:15 -06003367void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003368 auto *access_context = GetAccessContextNoInsert(command_buffer);
3369 if (access_context) {
3370 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003371 }
3372}
3373
John Zulaufd1f85d42020-04-15 12:23:15 -06003374void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3375 auto access_found = cb_access_state.find(command_buffer);
3376 if (access_found != cb_access_state.end()) {
3377 access_found->second->Reset();
3378 cb_access_state.erase(access_found);
3379 }
3380}
3381
John Zulauf89311b42020-09-29 16:28:47 -06003382void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
3383 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags src_access_scope,
3384 SyncStageAccessFlags dst_access_scope, uint32_t memory_barrier_count,
3385 const VkMemoryBarrier *pMemoryBarriers, const ResourceUsageTag &tag) {
John Zulauf1e331ec2020-12-04 18:29:38 -07003386 std::vector<PipelineBarrierOp> barrier_ops;
3387 barrier_ops.reserve(std::min<uint32_t>(1, memory_barrier_count));
John Zulauf89311b42020-09-29 16:28:47 -06003388 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
3389 const auto &barrier = pMemoryBarriers[barrier_index];
3390 SyncBarrier sync_barrier(src_exec_scope, SyncStageAccess::AccessScope(src_access_scope, barrier.srcAccessMask),
3391 dst_exec_scope, SyncStageAccess::AccessScope(dst_access_scope, barrier.dstAccessMask));
John Zulauf1e331ec2020-12-04 18:29:38 -07003392 barrier_ops.emplace_back(sync_barrier, false);
John Zulauf89311b42020-09-29 16:28:47 -06003393 }
3394 if (0 == memory_barrier_count) {
3395 // If there are no global memory barriers, force an exec barrier
John Zulauf1e331ec2020-12-04 18:29:38 -07003396 barrier_ops.emplace_back(SyncBarrier(src_exec_scope, 0, dst_exec_scope, 0), false);
John Zulauf89311b42020-09-29 16:28:47 -06003397 }
John Zulauf1e331ec2020-12-04 18:29:38 -07003398 ApplyBarrierOpsFunctor<PipelineBarrierOp> barriers_functor(true /* resolve */, barrier_ops, tag);
John Zulauf540266b2020-04-06 18:54:53 -06003399 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06003400}
3401
John Zulauf540266b2020-04-06 18:54:53 -06003402void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003403 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
3404 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06003405 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003406 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003407 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06003408 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
3409 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06003410 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
3411 const ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06003412 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
3413 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06003414 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf4a6105a2020-11-17 15:11:05 -07003415 const ApplyBarrierFunctor<PipelineBarrierOp> update_action({sync_barrier, false /* layout_transition */});
John Zulauf89311b42020-09-29 16:28:47 -06003416 context->UpdateResourceAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06003417 }
3418}
3419
John Zulauf540266b2020-04-06 18:54:53 -06003420void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003421 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
3422 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06003423 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07003424 for (uint32_t index = 0; index < barrier_count; index++) {
3425 const auto &barrier = barriers[index];
3426 const auto *image = Get<IMAGE_STATE>(barrier.image);
3427 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06003428 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06003429 bool layout_transition = barrier.oldLayout != barrier.newLayout;
3430 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
3431 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06003432 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf4a6105a2020-11-17 15:11:05 -07003433 const ApplyBarrierFunctor<PipelineBarrierOp> barrier_action({sync_barrier, layout_transition});
John Zulauf89311b42020-09-29 16:28:47 -06003434 context->UpdateResourceAccess(*image, subresource_range, barrier_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06003435 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003436}
3437
3438bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3439 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3440 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003441 const auto *cb_context = GetAccessContext(commandBuffer);
3442 assert(cb_context);
3443 if (!cb_context) return skip;
3444 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003445
John Zulauf3d84f1b2020-03-09 13:33:25 -06003446 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003447 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003448 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003449
3450 for (uint32_t region = 0; region < regionCount; region++) {
3451 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003452 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003453 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003454 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003455 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003456 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003457 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003458 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003459 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003460 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003461 }
John Zulauf16adfc92020-04-08 10:28:33 -06003462 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003463 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06003464 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003465 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003466 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003467 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003468 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003469 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003470 }
3471 }
3472 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003473 }
3474 return skip;
3475}
3476
3477void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3478 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003479 auto *cb_context = GetAccessContext(commandBuffer);
3480 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003481 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003482 auto *context = cb_context->GetCurrentAccessContext();
3483
John Zulauf9cb530d2019-09-30 14:14:10 -06003484 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003485 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003486
3487 for (uint32_t region = 0; region < regionCount; region++) {
3488 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003489 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003490 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003491 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003492 }
John Zulauf16adfc92020-04-08 10:28:33 -06003493 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003494 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003495 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003496 }
3497 }
3498}
3499
John Zulauf4a6105a2020-11-17 15:11:05 -07003500void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3501 // Clear out events from the command buffer contexts
3502 for (auto &cb_context : cb_access_state) {
3503 cb_context.second->RecordDestroyEvent(event);
3504 }
3505}
3506
Jeff Leger178b1e52020-10-05 12:22:23 -04003507bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3508 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3509 bool skip = false;
3510 const auto *cb_context = GetAccessContext(commandBuffer);
3511 assert(cb_context);
3512 if (!cb_context) return skip;
3513 const auto *context = cb_context->GetCurrentAccessContext();
3514
3515 // If we have no previous accesses, we have no hazards
3516 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3517 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3518
3519 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3520 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3521 if (src_buffer) {
3522 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
3523 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
3524 if (hazard.hazard) {
3525 // TODO -- add tag information to log msg when useful.
3526 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3527 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3528 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
3529 region, string_UsageTag(hazard).c_str());
3530 }
3531 }
3532 if (dst_buffer && !skip) {
3533 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
3534 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
3535 if (hazard.hazard) {
3536 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3537 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3538 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
3539 region, string_UsageTag(hazard).c_str());
3540 }
3541 }
3542 if (skip) break;
3543 }
3544 return skip;
3545}
3546
3547void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3548 auto *cb_context = GetAccessContext(commandBuffer);
3549 assert(cb_context);
3550 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3551 auto *context = cb_context->GetCurrentAccessContext();
3552
3553 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3554 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3555
3556 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3557 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3558 if (src_buffer) {
3559 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
3560 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
3561 }
3562 if (dst_buffer) {
3563 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
3564 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
3565 }
3566 }
3567}
3568
John Zulauf5c5e88d2019-12-26 11:22:02 -07003569bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3570 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3571 const VkImageCopy *pRegions) const {
3572 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003573 const auto *cb_access_context = GetAccessContext(commandBuffer);
3574 assert(cb_access_context);
3575 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003576
John Zulauf3d84f1b2020-03-09 13:33:25 -06003577 const auto *context = cb_access_context->GetCurrentAccessContext();
3578 assert(context);
3579 if (!context) return skip;
3580
3581 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3582 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003583 for (uint32_t region = 0; region < regionCount; region++) {
3584 const auto &copy_region = pRegions[region];
3585 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003586 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003587 copy_region.srcOffset, copy_region.extent);
3588 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003589 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003590 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003591 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003592 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003593 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003594 }
3595
3596 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003597 VkExtent3D dst_copy_extent =
3598 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06003599 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003600 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003601 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003602 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003603 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003604 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003605 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003606 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003607 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003608 }
3609 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003610
John Zulauf5c5e88d2019-12-26 11:22:02 -07003611 return skip;
3612}
3613
3614void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3615 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3616 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003617 auto *cb_access_context = GetAccessContext(commandBuffer);
3618 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003619 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003620 auto *context = cb_access_context->GetCurrentAccessContext();
3621 assert(context);
3622
John Zulauf5c5e88d2019-12-26 11:22:02 -07003623 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003624 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003625
3626 for (uint32_t region = 0; region < regionCount; region++) {
3627 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003628 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003629 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
3630 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003631 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003632 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003633 VkExtent3D dst_copy_extent =
3634 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06003635 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
3636 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003637 }
3638 }
3639}
3640
Jeff Leger178b1e52020-10-05 12:22:23 -04003641bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3642 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3643 bool skip = false;
3644 const auto *cb_access_context = GetAccessContext(commandBuffer);
3645 assert(cb_access_context);
3646 if (!cb_access_context) return skip;
3647
3648 const auto *context = cb_access_context->GetCurrentAccessContext();
3649 assert(context);
3650 if (!context) return skip;
3651
3652 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3653 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3654 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3655 const auto &copy_region = pCopyImageInfo->pRegions[region];
3656 if (src_image) {
3657 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
3658 copy_region.srcOffset, copy_region.extent);
3659 if (hazard.hazard) {
3660 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3661 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3662 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
3663 region, string_UsageTag(hazard).c_str());
3664 }
3665 }
3666
3667 if (dst_image) {
3668 VkExtent3D dst_copy_extent =
3669 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
3670 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
3671 copy_region.dstOffset, dst_copy_extent);
3672 if (hazard.hazard) {
3673 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3674 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3675 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
3676 region, string_UsageTag(hazard).c_str());
3677 }
3678 if (skip) break;
3679 }
3680 }
3681
3682 return skip;
3683}
3684
3685void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3686 auto *cb_access_context = GetAccessContext(commandBuffer);
3687 assert(cb_access_context);
3688 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3689 auto *context = cb_access_context->GetCurrentAccessContext();
3690 assert(context);
3691
3692 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3693 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3694
3695 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3696 const auto &copy_region = pCopyImageInfo->pRegions[region];
3697 if (src_image) {
3698 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
3699 copy_region.extent, tag);
3700 }
3701 if (dst_image) {
3702 VkExtent3D dst_copy_extent =
3703 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
3704 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
3705 dst_copy_extent, tag);
3706 }
3707 }
3708}
3709
John Zulauf9cb530d2019-09-30 14:14:10 -06003710bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3711 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3712 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3713 uint32_t bufferMemoryBarrierCount,
3714 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3715 uint32_t imageMemoryBarrierCount,
3716 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3717 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003718 const auto *cb_access_context = GetAccessContext(commandBuffer);
3719 assert(cb_access_context);
3720 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003721
John Zulauf3d84f1b2020-03-09 13:33:25 -06003722 const auto *context = cb_access_context->GetCurrentAccessContext();
3723 assert(context);
3724 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003725
John Zulauf3d84f1b2020-03-09 13:33:25 -06003726 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003727 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3728 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07003729 // Validate Image Layout transitions
3730 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
3731 const auto &barrier = pImageMemoryBarriers[index];
3732 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
3733 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
3734 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06003735 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07003736 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003737 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003738 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003739 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003740 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003741 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07003742 }
3743 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003744
3745 return skip;
3746}
3747
3748void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3749 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3750 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3751 uint32_t bufferMemoryBarrierCount,
3752 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3753 uint32_t imageMemoryBarrierCount,
3754 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003755 auto *cb_access_context = GetAccessContext(commandBuffer);
3756 assert(cb_access_context);
3757 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06003758 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003759 auto access_context = cb_access_context->GetCurrentAccessContext();
3760 assert(access_context);
3761 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003762
John Zulauf3d84f1b2020-03-09 13:33:25 -06003763 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003764 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003765 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003766 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
3767 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3768 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf89311b42020-09-29 16:28:47 -06003769
3770 // These two apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
3771 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
3772 // of the barriers is maintained.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003773 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
3774 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06003775 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06003776 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003777
John Zulauf89311b42020-09-29 16:28:47 -06003778 // Apply the global barriers last as is it walks all memory, it can also clean up the "pending" state without requiring an
3779 // additional pass, updating the dependency chains *last* as it goes along.
3780 // This is needed to guarantee order independence of the three lists.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003781 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf89311b42020-09-29 16:28:47 -06003782 pMemoryBarriers, tag);
John Zulauf4a6105a2020-11-17 15:11:05 -07003783
3784 // Need to pass the unexpanded stage masks as the ALL_COMMANDS bit is removed during expansion.
3785 cb_access_context->ApplyGlobalBarriersToEvents(srcStageMask, src_exec_scope, dstStageMask, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06003786}
3787
3788void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3789 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3790 // The state tracker sets up the device state
3791 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3792
John Zulauf5f13a792020-03-10 07:31:21 -06003793 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3794 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003795 // TODO: Find a good way to do this hooklessly.
3796 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3797 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3798 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3799
John Zulaufd1f85d42020-04-15 12:23:15 -06003800 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3801 sync_device_state->ResetCommandBufferCallback(command_buffer);
3802 });
3803 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3804 sync_device_state->FreeCommandBufferCallback(command_buffer);
3805 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003806}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003807
John Zulauf355e49b2020-04-24 15:11:15 -06003808bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003809 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003810 bool skip = false;
3811 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
3812 auto cb_context = GetAccessContext(commandBuffer);
3813
3814 if (rp_state && cb_context) {
3815 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
3816 }
3817
3818 return skip;
3819}
3820
3821bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3822 VkSubpassContents contents) const {
3823 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003824 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003825 subpass_begin_info.contents = contents;
3826 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
3827 return skip;
3828}
3829
3830bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003831 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003832 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3833 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
3834 return skip;
3835}
3836
3837bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3838 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003839 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003840 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3841 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3842 return skip;
3843}
3844
John Zulauf3d84f1b2020-03-09 13:33:25 -06003845void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3846 VkResult result) {
3847 // The state tracker sets up the command buffer state
3848 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3849
3850 // Create/initialize the structure that trackers accesses at the command buffer scope.
3851 auto cb_access_context = GetAccessContext(commandBuffer);
3852 assert(cb_access_context);
3853 cb_access_context->Reset();
3854}
3855
3856void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003857 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003858 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003859 if (cb_context) {
3860 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003861 }
3862}
3863
3864void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3865 VkSubpassContents contents) {
3866 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003867 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003868 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003869 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003870}
3871
3872void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3873 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3874 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003875 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003876}
3877
3878void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3879 const VkRenderPassBeginInfo *pRenderPassBegin,
3880 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3881 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003882 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3883}
3884
Mike Schuchardt2df08912020-12-15 16:28:09 -08003885bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3886 const VkSubpassEndInfo *pSubpassEndInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003887 bool skip = false;
3888
3889 auto cb_context = GetAccessContext(commandBuffer);
3890 assert(cb_context);
3891 auto cb_state = cb_context->GetCommandBufferState();
3892 if (!cb_state) return skip;
3893
3894 auto rp_state = cb_state->activeRenderPass;
3895 if (!rp_state) return skip;
3896
3897 skip |= cb_context->ValidateNextSubpass(func_name);
3898
3899 return skip;
3900}
3901
3902bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3903 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003904 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003905 subpass_begin_info.contents = contents;
3906 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3907 return skip;
3908}
3909
Mike Schuchardt2df08912020-12-15 16:28:09 -08003910bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3911 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003912 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3913 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3914 return skip;
3915}
3916
3917bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3918 const VkSubpassEndInfo *pSubpassEndInfo) const {
3919 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3920 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3921 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003922}
3923
3924void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003925 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003926 auto cb_context = GetAccessContext(commandBuffer);
3927 assert(cb_context);
3928 auto cb_state = cb_context->GetCommandBufferState();
3929 if (!cb_state) return;
3930
3931 auto rp_state = cb_state->activeRenderPass;
3932 if (!rp_state) return;
3933
John Zulauf355e49b2020-04-24 15:11:15 -06003934 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003935}
3936
3937void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3938 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003939 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003940 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003941 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003942}
3943
3944void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3945 const VkSubpassEndInfo *pSubpassEndInfo) {
3946 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003947 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003948}
3949
3950void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3951 const VkSubpassEndInfo *pSubpassEndInfo) {
3952 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003953 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003954}
3955
Mike Schuchardt2df08912020-12-15 16:28:09 -08003956bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003957 const char *func_name) const {
3958 bool skip = false;
3959
3960 auto cb_context = GetAccessContext(commandBuffer);
3961 assert(cb_context);
3962 auto cb_state = cb_context->GetCommandBufferState();
3963 if (!cb_state) return skip;
3964
3965 auto rp_state = cb_state->activeRenderPass;
3966 if (!rp_state) return skip;
3967
3968 skip |= cb_context->ValidateEndRenderpass(func_name);
3969 return skip;
3970}
3971
3972bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3973 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
3974 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
3975 return skip;
3976}
3977
Mike Schuchardt2df08912020-12-15 16:28:09 -08003978bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003979 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
3980 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
3981 return skip;
3982}
3983
3984bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003985 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003986 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
3987 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
3988 return skip;
3989}
3990
3991void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3992 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003993 // Resolve the all subpass contexts to the command buffer contexts
3994 auto cb_context = GetAccessContext(commandBuffer);
3995 assert(cb_context);
3996 auto cb_state = cb_context->GetCommandBufferState();
3997 if (!cb_state) return;
3998
locke-lunargaecf2152020-05-12 17:15:41 -06003999 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06004000 if (!rp_state) return;
4001
John Zulauf355e49b2020-04-24 15:11:15 -06004002 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06004003}
John Zulauf3d84f1b2020-03-09 13:33:25 -06004004
John Zulauf33fc1d52020-07-17 11:01:10 -06004005// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
4006// updates to a resource which do not conflict at the byte level.
4007// TODO: Revisit this rule to see if it needs to be tighter or looser
4008// TODO: Add programatic control over suppression heuristics
4009bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
4010 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
4011}
4012
John Zulauf3d84f1b2020-03-09 13:33:25 -06004013void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004014 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06004015 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004016}
4017
4018void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06004019 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06004020 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004021}
4022
4023void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06004024 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06004025 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004026}
locke-lunarga19c71d2020-03-02 18:17:04 -07004027
Jeff Leger178b1e52020-10-05 12:22:23 -04004028template <typename BufferImageCopyRegionType>
4029bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4030 VkImageLayout dstImageLayout, uint32_t regionCount,
4031 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004032 bool skip = false;
4033 const auto *cb_access_context = GetAccessContext(commandBuffer);
4034 assert(cb_access_context);
4035 if (!cb_access_context) return skip;
4036
Jeff Leger178b1e52020-10-05 12:22:23 -04004037 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4038 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
4039
locke-lunarga19c71d2020-03-02 18:17:04 -07004040 const auto *context = cb_access_context->GetCurrentAccessContext();
4041 assert(context);
4042 if (!context) return skip;
4043
4044 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07004045 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4046
4047 for (uint32_t region = 0; region < regionCount; region++) {
4048 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06004049 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004050 ResourceAccessRange src_range =
4051 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004052 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07004053 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06004054 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06004055 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004056 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004057 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004058 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004059 }
4060 }
4061 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004062 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004063 copy_region.imageOffset, copy_region.imageExtent);
4064 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004065 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004066 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004067 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004068 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004069 }
4070 if (skip) break;
4071 }
4072 if (skip) break;
4073 }
4074 return skip;
4075}
4076
Jeff Leger178b1e52020-10-05 12:22:23 -04004077bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4078 VkImageLayout dstImageLayout, uint32_t regionCount,
4079 const VkBufferImageCopy *pRegions) const {
4080 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
4081 COPY_COMMAND_VERSION_1);
4082}
4083
4084bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4085 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
4086 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4087 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4088 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4089}
4090
4091template <typename BufferImageCopyRegionType>
4092void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4093 VkImageLayout dstImageLayout, uint32_t regionCount,
4094 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004095 auto *cb_access_context = GetAccessContext(commandBuffer);
4096 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004097
4098 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4099 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
4100
4101 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004102 auto *context = cb_access_context->GetCurrentAccessContext();
4103 assert(context);
4104
4105 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06004106 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004107
4108 for (uint32_t region = 0; region < regionCount; region++) {
4109 const auto &copy_region = pRegions[region];
4110 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004111 ResourceAccessRange src_range =
4112 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004113 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004114 }
4115 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004116 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06004117 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004118 }
4119 }
4120}
4121
Jeff Leger178b1e52020-10-05 12:22:23 -04004122void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4123 VkImageLayout dstImageLayout, uint32_t regionCount,
4124 const VkBufferImageCopy *pRegions) {
4125 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
4126 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4127}
4128
4129void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4130 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4131 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4132 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4133 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4134 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4135}
4136
4137template <typename BufferImageCopyRegionType>
4138bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4139 VkBuffer dstBuffer, uint32_t regionCount,
4140 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004141 bool skip = false;
4142 const auto *cb_access_context = GetAccessContext(commandBuffer);
4143 assert(cb_access_context);
4144 if (!cb_access_context) return skip;
4145
Jeff Leger178b1e52020-10-05 12:22:23 -04004146 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4147 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
4148
locke-lunarga19c71d2020-03-02 18:17:04 -07004149 const auto *context = cb_access_context->GetCurrentAccessContext();
4150 assert(context);
4151 if (!context) return skip;
4152
4153 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4154 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4155 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
4156 for (uint32_t region = 0; region < regionCount; region++) {
4157 const auto &copy_region = pRegions[region];
4158 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004159 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004160 copy_region.imageOffset, copy_region.imageExtent);
4161 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004162 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004163 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004164 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004165 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004166 }
4167 }
4168 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06004169 ResourceAccessRange dst_range =
4170 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004171 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07004172 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004173 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004174 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004175 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004176 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004177 }
4178 }
4179 if (skip) break;
4180 }
4181 return skip;
4182}
4183
Jeff Leger178b1e52020-10-05 12:22:23 -04004184bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4185 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4186 const VkBufferImageCopy *pRegions) const {
4187 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
4188 COPY_COMMAND_VERSION_1);
4189}
4190
4191bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4192 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4193 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4194 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4195 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4196}
4197
4198template <typename BufferImageCopyRegionType>
4199void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4200 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
4201 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004202 auto *cb_access_context = GetAccessContext(commandBuffer);
4203 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004204
4205 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4206 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
4207
4208 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004209 auto *context = cb_access_context->GetCurrentAccessContext();
4210 assert(context);
4211
4212 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004213 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4214 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 -06004215 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004216
4217 for (uint32_t region = 0; region < regionCount; region++) {
4218 const auto &copy_region = pRegions[region];
4219 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004220 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06004221 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004222 }
4223 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004224 ResourceAccessRange dst_range =
4225 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004226 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004227 }
4228 }
4229}
4230
Jeff Leger178b1e52020-10-05 12:22:23 -04004231void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4232 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4233 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
4234 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4235}
4236
4237void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4238 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4239 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4240 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4241 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4242 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4243}
4244
4245template <typename RegionType>
4246bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4247 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4248 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004249 bool skip = false;
4250 const auto *cb_access_context = GetAccessContext(commandBuffer);
4251 assert(cb_access_context);
4252 if (!cb_access_context) return skip;
4253
4254 const auto *context = cb_access_context->GetCurrentAccessContext();
4255 assert(context);
4256 if (!context) return skip;
4257
4258 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4259 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4260
4261 for (uint32_t region = 0; region < regionCount; region++) {
4262 const auto &blit_region = pRegions[region];
4263 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004264 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4265 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4266 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4267 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4268 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4269 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
4270 auto hazard =
4271 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004272 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004273 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004274 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004275 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004276 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004277 }
4278 }
4279
4280 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004281 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4282 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4283 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4284 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4285 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4286 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
4287 auto hazard =
4288 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004289 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004290 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004291 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004292 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004293 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004294 }
4295 if (skip) break;
4296 }
4297 }
4298
4299 return skip;
4300}
4301
Jeff Leger178b1e52020-10-05 12:22:23 -04004302bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4303 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4304 const VkImageBlit *pRegions, VkFilter filter) const {
4305 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4306 "vkCmdBlitImage");
4307}
4308
4309bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4310 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4311 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4312 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4313 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4314}
4315
4316template <typename RegionType>
4317void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4318 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4319 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004320 auto *cb_access_context = GetAccessContext(commandBuffer);
4321 assert(cb_access_context);
4322 auto *context = cb_access_context->GetCurrentAccessContext();
4323 assert(context);
4324
4325 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004326 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004327
4328 for (uint32_t region = 0; region < regionCount; region++) {
4329 const auto &blit_region = pRegions[region];
4330 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004331 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4332 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4333 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4334 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4335 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4336 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
4337 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004338 }
4339 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004340 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4341 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4342 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4343 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4344 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4345 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
4346 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004347 }
4348 }
4349}
locke-lunarg36ba2592020-04-03 09:42:04 -06004350
Jeff Leger178b1e52020-10-05 12:22:23 -04004351void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4352 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4353 const VkImageBlit *pRegions, VkFilter filter) {
4354 auto *cb_access_context = GetAccessContext(commandBuffer);
4355 assert(cb_access_context);
4356 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4357 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4358 pRegions, filter);
4359 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4360}
4361
4362void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4363 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4364 auto *cb_access_context = GetAccessContext(commandBuffer);
4365 assert(cb_access_context);
4366 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4367 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4368 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4369 pBlitImageInfo->filter, tag);
4370}
4371
locke-lunarg61870c22020-06-09 14:51:50 -06004372bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
4373 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
4374 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004375 bool skip = false;
4376 if (drawCount == 0) return skip;
4377
4378 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4379 VkDeviceSize size = struct_size;
4380 if (drawCount == 1 || stride == size) {
4381 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004382 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004383 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4384 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004385 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004386 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004387 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06004388 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004389 }
4390 } else {
4391 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004392 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004393 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4394 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004395 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004396 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4397 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
4398 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004399 break;
4400 }
4401 }
4402 }
4403 return skip;
4404}
4405
locke-lunarg61870c22020-06-09 14:51:50 -06004406void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
4407 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4408 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06004409 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4410 VkDeviceSize size = struct_size;
4411 if (drawCount == 1 || stride == size) {
4412 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004413 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004414 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
4415 } else {
4416 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004417 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004418 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
4419 }
4420 }
4421}
4422
locke-lunarg61870c22020-06-09 14:51:50 -06004423bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
4424 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004425 bool skip = false;
4426
4427 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004428 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004429 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4430 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004431 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004432 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004433 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06004434 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004435 }
4436 return skip;
4437}
4438
locke-lunarg61870c22020-06-09 14:51:50 -06004439void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06004440 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004441 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004442 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
4443}
4444
locke-lunarg36ba2592020-04-03 09:42:04 -06004445bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004446 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004447 const auto *cb_access_context = GetAccessContext(commandBuffer);
4448 assert(cb_access_context);
4449 if (!cb_access_context) return skip;
4450
locke-lunarg61870c22020-06-09 14:51:50 -06004451 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004452 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004453}
4454
4455void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004456 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004457 auto *cb_access_context = GetAccessContext(commandBuffer);
4458 assert(cb_access_context);
4459 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004460
locke-lunarg61870c22020-06-09 14:51:50 -06004461 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004462}
locke-lunarge1a67022020-04-29 00:15:36 -06004463
4464bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004465 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004466 const auto *cb_access_context = GetAccessContext(commandBuffer);
4467 assert(cb_access_context);
4468 if (!cb_access_context) return skip;
4469
4470 const auto *context = cb_access_context->GetCurrentAccessContext();
4471 assert(context);
4472 if (!context) return skip;
4473
locke-lunarg61870c22020-06-09 14:51:50 -06004474 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
4475 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
4476 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004477 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004478}
4479
4480void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004481 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004482 auto *cb_access_context = GetAccessContext(commandBuffer);
4483 assert(cb_access_context);
4484 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4485 auto *context = cb_access_context->GetCurrentAccessContext();
4486 assert(context);
4487
locke-lunarg61870c22020-06-09 14:51:50 -06004488 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4489 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004490}
4491
4492bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4493 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004494 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004495 const auto *cb_access_context = GetAccessContext(commandBuffer);
4496 assert(cb_access_context);
4497 if (!cb_access_context) return skip;
4498
locke-lunarg61870c22020-06-09 14:51:50 -06004499 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4500 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4501 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004502 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004503}
4504
4505void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4506 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004507 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004508 auto *cb_access_context = GetAccessContext(commandBuffer);
4509 assert(cb_access_context);
4510 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004511
locke-lunarg61870c22020-06-09 14:51:50 -06004512 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4513 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4514 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004515}
4516
4517bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4518 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004519 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004520 const auto *cb_access_context = GetAccessContext(commandBuffer);
4521 assert(cb_access_context);
4522 if (!cb_access_context) return skip;
4523
locke-lunarg61870c22020-06-09 14:51:50 -06004524 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4525 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4526 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004527 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004528}
4529
4530void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4531 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004532 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004533 auto *cb_access_context = GetAccessContext(commandBuffer);
4534 assert(cb_access_context);
4535 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004536
locke-lunarg61870c22020-06-09 14:51:50 -06004537 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4538 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4539 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004540}
4541
4542bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4543 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004544 bool skip = false;
4545 if (drawCount == 0) return skip;
4546
locke-lunargff255f92020-05-13 18:53:52 -06004547 const auto *cb_access_context = GetAccessContext(commandBuffer);
4548 assert(cb_access_context);
4549 if (!cb_access_context) return skip;
4550
4551 const auto *context = cb_access_context->GetCurrentAccessContext();
4552 assert(context);
4553 if (!context) return skip;
4554
locke-lunarg61870c22020-06-09 14:51:50 -06004555 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4556 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
4557 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
4558 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004559
4560 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4561 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4562 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004563 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004564 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004565}
4566
4567void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4568 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004569 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004570 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004571 auto *cb_access_context = GetAccessContext(commandBuffer);
4572 assert(cb_access_context);
4573 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4574 auto *context = cb_access_context->GetCurrentAccessContext();
4575 assert(context);
4576
locke-lunarg61870c22020-06-09 14:51:50 -06004577 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4578 cb_access_context->RecordDrawSubpassAttachment(tag);
4579 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004580
4581 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4582 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4583 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004584 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004585}
4586
4587bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4588 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004589 bool skip = false;
4590 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004591 const auto *cb_access_context = GetAccessContext(commandBuffer);
4592 assert(cb_access_context);
4593 if (!cb_access_context) return skip;
4594
4595 const auto *context = cb_access_context->GetCurrentAccessContext();
4596 assert(context);
4597 if (!context) return skip;
4598
locke-lunarg61870c22020-06-09 14:51:50 -06004599 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4600 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
4601 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
4602 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004603
4604 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4605 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4606 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004607 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004608 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004609}
4610
4611void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4612 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004613 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004614 auto *cb_access_context = GetAccessContext(commandBuffer);
4615 assert(cb_access_context);
4616 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4617 auto *context = cb_access_context->GetCurrentAccessContext();
4618 assert(context);
4619
locke-lunarg61870c22020-06-09 14:51:50 -06004620 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4621 cb_access_context->RecordDrawSubpassAttachment(tag);
4622 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004623
4624 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4625 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4626 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004627 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004628}
4629
4630bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4631 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4632 uint32_t stride, const char *function) const {
4633 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004634 const auto *cb_access_context = GetAccessContext(commandBuffer);
4635 assert(cb_access_context);
4636 if (!cb_access_context) return skip;
4637
4638 const auto *context = cb_access_context->GetCurrentAccessContext();
4639 assert(context);
4640 if (!context) return skip;
4641
locke-lunarg61870c22020-06-09 14:51:50 -06004642 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4643 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
4644 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
4645 function);
4646 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004647
4648 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4649 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4650 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004651 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004652 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004653}
4654
4655bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4656 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4657 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004658 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4659 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004660}
4661
4662void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4663 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4664 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004665 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4666 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004667 auto *cb_access_context = GetAccessContext(commandBuffer);
4668 assert(cb_access_context);
4669 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
4670 auto *context = cb_access_context->GetCurrentAccessContext();
4671 assert(context);
4672
locke-lunarg61870c22020-06-09 14:51:50 -06004673 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4674 cb_access_context->RecordDrawSubpassAttachment(tag);
4675 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4676 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004677
4678 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4679 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4680 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004681 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004682}
4683
4684bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4685 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4686 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004687 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4688 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004689}
4690
4691void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4692 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4693 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004694 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4695 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004696 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004697}
4698
4699bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4700 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4701 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004702 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4703 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004704}
4705
4706void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4707 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4708 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004709 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4710 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004711 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4712}
4713
4714bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4715 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4716 uint32_t stride, const char *function) const {
4717 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004718 const auto *cb_access_context = GetAccessContext(commandBuffer);
4719 assert(cb_access_context);
4720 if (!cb_access_context) return skip;
4721
4722 const auto *context = cb_access_context->GetCurrentAccessContext();
4723 assert(context);
4724 if (!context) return skip;
4725
locke-lunarg61870c22020-06-09 14:51:50 -06004726 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4727 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
4728 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
4729 stride, function);
4730 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004731
4732 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4733 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4734 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004735 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004736 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004737}
4738
4739bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4740 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4741 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004742 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4743 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004744}
4745
4746void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4747 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4748 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004749 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4750 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004751 auto *cb_access_context = GetAccessContext(commandBuffer);
4752 assert(cb_access_context);
4753 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4754 auto *context = cb_access_context->GetCurrentAccessContext();
4755 assert(context);
4756
locke-lunarg61870c22020-06-09 14:51:50 -06004757 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4758 cb_access_context->RecordDrawSubpassAttachment(tag);
4759 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4760 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004761
4762 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4763 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004764 // We will update the index and vertex buffer in SubmitQueue in the future.
4765 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004766}
4767
4768bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4769 VkDeviceSize offset, VkBuffer countBuffer,
4770 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4771 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004772 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4773 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004774}
4775
4776void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4777 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4778 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004779 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4780 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004781 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4782}
4783
4784bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4785 VkDeviceSize offset, VkBuffer countBuffer,
4786 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4787 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004788 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4789 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004790}
4791
4792void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4793 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4794 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004795 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4796 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004797 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4798}
4799
4800bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4801 const VkClearColorValue *pColor, uint32_t rangeCount,
4802 const VkImageSubresourceRange *pRanges) const {
4803 bool skip = false;
4804 const auto *cb_access_context = GetAccessContext(commandBuffer);
4805 assert(cb_access_context);
4806 if (!cb_access_context) return skip;
4807
4808 const auto *context = cb_access_context->GetCurrentAccessContext();
4809 assert(context);
4810 if (!context) return skip;
4811
4812 const auto *image_state = Get<IMAGE_STATE>(image);
4813
4814 for (uint32_t index = 0; index < rangeCount; index++) {
4815 const auto &range = pRanges[index];
4816 if (image_state) {
4817 auto hazard =
4818 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4819 if (hazard.hazard) {
4820 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004821 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004822 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004823 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004824 }
4825 }
4826 }
4827 return skip;
4828}
4829
4830void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4831 const VkClearColorValue *pColor, uint32_t rangeCount,
4832 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004833 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004834 auto *cb_access_context = GetAccessContext(commandBuffer);
4835 assert(cb_access_context);
4836 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4837 auto *context = cb_access_context->GetCurrentAccessContext();
4838 assert(context);
4839
4840 const auto *image_state = Get<IMAGE_STATE>(image);
4841
4842 for (uint32_t index = 0; index < rangeCount; index++) {
4843 const auto &range = pRanges[index];
4844 if (image_state) {
4845 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4846 tag);
4847 }
4848 }
4849}
4850
4851bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4852 VkImageLayout imageLayout,
4853 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4854 const VkImageSubresourceRange *pRanges) const {
4855 bool skip = false;
4856 const auto *cb_access_context = GetAccessContext(commandBuffer);
4857 assert(cb_access_context);
4858 if (!cb_access_context) return skip;
4859
4860 const auto *context = cb_access_context->GetCurrentAccessContext();
4861 assert(context);
4862 if (!context) return skip;
4863
4864 const auto *image_state = Get<IMAGE_STATE>(image);
4865
4866 for (uint32_t index = 0; index < rangeCount; index++) {
4867 const auto &range = pRanges[index];
4868 if (image_state) {
4869 auto hazard =
4870 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4871 if (hazard.hazard) {
4872 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004873 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004874 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004875 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004876 }
4877 }
4878 }
4879 return skip;
4880}
4881
4882void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4883 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4884 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004885 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004886 auto *cb_access_context = GetAccessContext(commandBuffer);
4887 assert(cb_access_context);
4888 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4889 auto *context = cb_access_context->GetCurrentAccessContext();
4890 assert(context);
4891
4892 const auto *image_state = Get<IMAGE_STATE>(image);
4893
4894 for (uint32_t index = 0; index < rangeCount; index++) {
4895 const auto &range = pRanges[index];
4896 if (image_state) {
4897 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4898 tag);
4899 }
4900 }
4901}
4902
4903bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4904 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4905 VkDeviceSize dstOffset, VkDeviceSize stride,
4906 VkQueryResultFlags flags) const {
4907 bool skip = false;
4908 const auto *cb_access_context = GetAccessContext(commandBuffer);
4909 assert(cb_access_context);
4910 if (!cb_access_context) return skip;
4911
4912 const auto *context = cb_access_context->GetCurrentAccessContext();
4913 assert(context);
4914 if (!context) return skip;
4915
4916 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4917
4918 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004919 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004920 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4921 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004922 skip |=
4923 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4924 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4925 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004926 }
4927 }
locke-lunargff255f92020-05-13 18:53:52 -06004928
4929 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004930 return skip;
4931}
4932
4933void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4934 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4935 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004936 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4937 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004938 auto *cb_access_context = GetAccessContext(commandBuffer);
4939 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004940 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004941 auto *context = cb_access_context->GetCurrentAccessContext();
4942 assert(context);
4943
4944 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4945
4946 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004947 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004948 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4949 }
locke-lunargff255f92020-05-13 18:53:52 -06004950
4951 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004952}
4953
4954bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4955 VkDeviceSize size, uint32_t data) const {
4956 bool skip = false;
4957 const auto *cb_access_context = GetAccessContext(commandBuffer);
4958 assert(cb_access_context);
4959 if (!cb_access_context) return skip;
4960
4961 const auto *context = cb_access_context->GetCurrentAccessContext();
4962 assert(context);
4963 if (!context) return skip;
4964
4965 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4966
4967 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004968 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004969 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4970 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004971 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004972 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004973 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004974 }
4975 }
4976 return skip;
4977}
4978
4979void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4980 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004981 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004982 auto *cb_access_context = GetAccessContext(commandBuffer);
4983 assert(cb_access_context);
4984 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4985 auto *context = cb_access_context->GetCurrentAccessContext();
4986 assert(context);
4987
4988 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4989
4990 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004991 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004992 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4993 }
4994}
4995
4996bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4997 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4998 const VkImageResolve *pRegions) const {
4999 bool skip = false;
5000 const auto *cb_access_context = GetAccessContext(commandBuffer);
5001 assert(cb_access_context);
5002 if (!cb_access_context) return skip;
5003
5004 const auto *context = cb_access_context->GetCurrentAccessContext();
5005 assert(context);
5006 if (!context) return skip;
5007
5008 const auto *src_image = Get<IMAGE_STATE>(srcImage);
5009 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
5010
5011 for (uint32_t region = 0; region < regionCount; region++) {
5012 const auto &resolve_region = pRegions[region];
5013 if (src_image) {
5014 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5015 resolve_region.srcOffset, resolve_region.extent);
5016 if (hazard.hazard) {
5017 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005018 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005019 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06005020 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005021 }
5022 }
5023
5024 if (dst_image) {
5025 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5026 resolve_region.dstOffset, resolve_region.extent);
5027 if (hazard.hazard) {
5028 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005029 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005030 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06005031 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005032 }
5033 if (skip) break;
5034 }
5035 }
5036
5037 return skip;
5038}
5039
5040void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5041 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5042 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005043 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
5044 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06005045 auto *cb_access_context = GetAccessContext(commandBuffer);
5046 assert(cb_access_context);
5047 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
5048 auto *context = cb_access_context->GetCurrentAccessContext();
5049 assert(context);
5050
5051 auto *src_image = Get<IMAGE_STATE>(srcImage);
5052 auto *dst_image = Get<IMAGE_STATE>(dstImage);
5053
5054 for (uint32_t region = 0; region < regionCount; region++) {
5055 const auto &resolve_region = pRegions[region];
5056 if (src_image) {
5057 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5058 resolve_region.srcOffset, resolve_region.extent, tag);
5059 }
5060 if (dst_image) {
5061 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5062 resolve_region.dstOffset, resolve_region.extent, tag);
5063 }
5064 }
5065}
5066
Jeff Leger178b1e52020-10-05 12:22:23 -04005067bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5068 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
5069 bool skip = false;
5070 const auto *cb_access_context = GetAccessContext(commandBuffer);
5071 assert(cb_access_context);
5072 if (!cb_access_context) return skip;
5073
5074 const auto *context = cb_access_context->GetCurrentAccessContext();
5075 assert(context);
5076 if (!context) return skip;
5077
5078 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5079 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5080
5081 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5082 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5083 if (src_image) {
5084 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5085 resolve_region.srcOffset, resolve_region.extent);
5086 if (hazard.hazard) {
5087 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
5088 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
5089 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
5090 region, string_UsageTag(hazard).c_str());
5091 }
5092 }
5093
5094 if (dst_image) {
5095 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5096 resolve_region.dstOffset, resolve_region.extent);
5097 if (hazard.hazard) {
5098 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
5099 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
5100 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
5101 region, string_UsageTag(hazard).c_str());
5102 }
5103 if (skip) break;
5104 }
5105 }
5106
5107 return skip;
5108}
5109
5110void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5111 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5112 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5113 auto *cb_access_context = GetAccessContext(commandBuffer);
5114 assert(cb_access_context);
5115 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
5116 auto *context = cb_access_context->GetCurrentAccessContext();
5117 assert(context);
5118
5119 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5120 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5121
5122 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5123 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5124 if (src_image) {
5125 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5126 resolve_region.srcOffset, resolve_region.extent, tag);
5127 }
5128 if (dst_image) {
5129 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5130 resolve_region.dstOffset, resolve_region.extent, tag);
5131 }
5132 }
5133}
5134
locke-lunarge1a67022020-04-29 00:15:36 -06005135bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5136 VkDeviceSize dataSize, const void *pData) const {
5137 bool skip = false;
5138 const auto *cb_access_context = GetAccessContext(commandBuffer);
5139 assert(cb_access_context);
5140 if (!cb_access_context) return skip;
5141
5142 const auto *context = cb_access_context->GetCurrentAccessContext();
5143 assert(context);
5144 if (!context) return skip;
5145
5146 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5147
5148 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005149 // VK_WHOLE_SIZE not allowed
5150 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06005151 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
5152 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005153 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005154 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06005155 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005156 }
5157 }
5158 return skip;
5159}
5160
5161void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5162 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005163 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005164 auto *cb_access_context = GetAccessContext(commandBuffer);
5165 assert(cb_access_context);
5166 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5167 auto *context = cb_access_context->GetCurrentAccessContext();
5168 assert(context);
5169
5170 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5171
5172 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005173 // VK_WHOLE_SIZE not allowed
5174 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06005175 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
5176 }
5177}
locke-lunargff255f92020-05-13 18:53:52 -06005178
5179bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5180 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5181 bool skip = false;
5182 const auto *cb_access_context = GetAccessContext(commandBuffer);
5183 assert(cb_access_context);
5184 if (!cb_access_context) return skip;
5185
5186 const auto *context = cb_access_context->GetCurrentAccessContext();
5187 assert(context);
5188 if (!context) return skip;
5189
5190 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5191
5192 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005193 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06005194 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
5195 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005196 skip |=
5197 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5198 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
5199 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005200 }
5201 }
5202 return skip;
5203}
5204
5205void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5206 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005207 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005208 auto *cb_access_context = GetAccessContext(commandBuffer);
5209 assert(cb_access_context);
5210 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5211 auto *context = cb_access_context->GetCurrentAccessContext();
5212 assert(context);
5213
5214 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5215
5216 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005217 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06005218 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
5219 }
5220}
John Zulauf49beb112020-11-04 16:06:31 -07005221
5222bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5223 bool skip = false;
5224 const auto *cb_context = GetAccessContext(commandBuffer);
5225 assert(cb_context);
5226 if (!cb_context) return skip;
5227
5228 return cb_context->ValidateSetEvent(commandBuffer, event, stageMask);
5229}
5230
5231void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5232 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5233 auto *cb_context = GetAccessContext(commandBuffer);
5234 assert(cb_context);
5235 if (!cb_context) return;
John Zulauf4a6105a2020-11-17 15:11:05 -07005236 const auto tag = cb_context->NextCommandTag(CMD_SETEVENT);
5237 cb_context->RecordSetEvent(commandBuffer, event, stageMask, tag);
John Zulauf49beb112020-11-04 16:06:31 -07005238}
5239
5240bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5241 VkPipelineStageFlags stageMask) const {
5242 bool skip = false;
5243 const auto *cb_context = GetAccessContext(commandBuffer);
5244 assert(cb_context);
5245 if (!cb_context) return skip;
5246
5247 return cb_context->ValidateResetEvent(commandBuffer, event, stageMask);
5248}
5249
5250void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5251 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5252 auto *cb_context = GetAccessContext(commandBuffer);
5253 assert(cb_context);
5254 if (!cb_context) return;
5255
5256 cb_context->RecordResetEvent(commandBuffer, event, stageMask);
5257}
5258
5259bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5260 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5261 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5262 uint32_t bufferMemoryBarrierCount,
5263 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5264 uint32_t imageMemoryBarrierCount,
5265 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5266 bool skip = false;
5267 const auto *cb_context = GetAccessContext(commandBuffer);
5268 assert(cb_context);
5269 if (!cb_context) return skip;
5270
John Zulauf4a6105a2020-11-17 15:11:05 -07005271 return cb_context->ValidateWaitEvents(eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount, pMemoryBarriers,
5272 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
John Zulauf49beb112020-11-04 16:06:31 -07005273 pImageMemoryBarriers);
5274}
5275
5276void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5277 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5278 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5279 uint32_t bufferMemoryBarrierCount,
5280 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5281 uint32_t imageMemoryBarrierCount,
5282 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5283 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5284 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5285 imageMemoryBarrierCount, pImageMemoryBarriers);
5286
5287 auto *cb_context = GetAccessContext(commandBuffer);
5288 assert(cb_context);
5289 if (!cb_context) return;
5290
John Zulauf4a6105a2020-11-17 15:11:05 -07005291 const auto tag = cb_context->NextCommandTag(CMD_WAITEVENTS);
John Zulauf49beb112020-11-04 16:06:31 -07005292 cb_context->RecordWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5293 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
John Zulauf4a6105a2020-11-17 15:11:05 -07005294 pImageMemoryBarriers, tag);
5295}
5296
5297void SyncEventState::ResetFirstScope() {
5298 for (const auto address_type : kAddressTypes) {
5299 first_scope[static_cast<size_t>(address_type)].clear();
5300 }
5301 stage_mask = 0U;
5302 exec_scope = 0U;
5303 stage_accesses.reset();
5304}
5305
5306// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
5307SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(VkPipelineStageFlags srcStageMask) const {
5308 IgnoreReason reason = NotIgnored;
5309
5310 if (last_command == CMD_RESETEVENT && !HasBarrier(0U, 0U)) {
5311 reason = ResetWaitRace;
5312 } else if (unsynchronized_set) {
5313 reason = SetRace;
5314 } else {
5315 const VkPipelineStageFlags missing_bits = stage_mask_param & ~srcStageMask;
5316 if (missing_bits) reason = MissingStageBits;
5317 }
5318
5319 return reason;
5320}
5321
5322bool SyncEventState::HasBarrier(VkPipelineStageFlags stageMask, VkPipelineStageFlags exec_scope_arg) const {
5323 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5324 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5325 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005326}