blob: d0292bfc48215d5729cce0169da59acb0c20a393 [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}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001739
1740void CommandBufferAccessContext::ApplyBufferBarriers(const SyncEventState &sync_event, const SyncExecScope &dst,
1741 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
John Zulauf4a6105a2020-11-17 15:11:05 -07001742 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;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001752 const SyncBarrier sync_barrier(barrier, sync_event.scope, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001753 const ApplyBarrierFunctor<WaitEventBarrierOp> barrier_action({scope_tag, sync_barrier, false /* layout_transition */});
1754 EventSimpleRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), range);
1755 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barrier_action, &filtered_range_gen);
1756 }
1757}
1758
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001759void CommandBufferAccessContext::ApplyGlobalBarriers(SyncEventState &sync_event, const SyncExecScope &dst,
1760 uint32_t memory_barrier_count, const VkMemoryBarrier *pMemoryBarriers,
1761 const ResourceUsageTag &tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07001762 std::vector<WaitEventBarrierOp> barrier_ops;
1763 barrier_ops.reserve(std::min<uint32_t>(memory_barrier_count, 1));
1764 const auto &scope_tag = sync_event.first_scope_tag;
1765 auto *access_context = GetCurrentAccessContext();
1766 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
1767 const auto &barrier = pMemoryBarriers[barrier_index];
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001768 SyncBarrier sync_barrier(barrier, sync_event.scope, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001769 barrier_ops.emplace_back(scope_tag, sync_barrier, false);
1770 }
1771 if (0 == memory_barrier_count) {
1772 // If there are no global memory barriers, force an exec barrier
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001773 barrier_ops.emplace_back(scope_tag, SyncBarrier(sync_event.scope, dst), false);
John Zulauf4a6105a2020-11-17 15:11:05 -07001774 }
1775 ApplyBarrierOpsFunctor<WaitEventBarrierOp> barriers_functor(false /* don't resolve */, barrier_ops, tag);
1776 for (const auto address_type : kAddressTypes) {
1777 EventSimpleRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), kFullRange);
1778 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &filtered_range_gen);
1779 }
1780
1781 // Apply the global barrier to the event itself (for race condition tracking)
1782 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001783 sync_event.barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1784 sync_event.barriers |= dst.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07001785}
1786
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001787void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
1788 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
John Zulauf4a6105a2020-11-17 15:11:05 -07001789 for (auto &event_pair : event_state_) {
1790 assert(event_pair.second); // Shouldn't be storing empty
1791 auto &sync_event = *event_pair.second;
1792 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001793 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
1794 sync_event.barriers |= dst.exec_scope;
1795 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
John Zulauf4a6105a2020-11-17 15:11:05 -07001796 }
1797 }
1798}
1799
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001800void CommandBufferAccessContext::ApplyImageBarriers(const SyncEventState &sync_event, const SyncExecScope &dst,
1801 uint32_t barrier_count, const VkImageMemoryBarrier *barriers,
1802 const ResourceUsageTag &tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07001803 const auto &scope_tag = sync_event.first_scope_tag;
1804 auto *access_context = GetCurrentAccessContext();
1805 for (uint32_t index = 0; index < barrier_count; index++) {
1806 const auto &barrier = barriers[index];
1807 const auto *image = sync_state_->Get<IMAGE_STATE>(barrier.image);
1808 if (!image) continue;
1809 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
1810 bool layout_transition = barrier.oldLayout != barrier.newLayout;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001811 const SyncBarrier sync_barrier(barrier, sync_event.scope, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001812 const ApplyBarrierFunctor<WaitEventBarrierOp> barrier_action({scope_tag, sync_barrier, layout_transition});
1813 const auto base_address = ResourceBaseAddress(*image);
1814 subresource_adapter::ImageRangeGenerator range_gen(*image->fragment_encoder.get(), subresource_range, {0, 0, 0},
1815 image->createInfo.extent, base_address);
1816 const auto address_type = AccessContext::ImageAddressType(*image);
1817 EventImageRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), range_gen);
1818 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barrier_action, &filtered_range_gen);
1819 }
1820}
John Zulaufb02c1eb2020-10-06 16:33:36 -06001821
John Zulauf355e49b2020-04-24 15:11:15 -06001822// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1823bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1824
1825 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001826 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001827 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1828 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06001829
John Zulauf86356ca2020-10-19 11:46:41 -06001830 assert(pRenderPassBegin);
1831 if (nullptr == pRenderPassBegin) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06001832
John Zulauf86356ca2020-10-19 11:46:41 -06001833 const uint32_t subpass = 0;
John Zulauf355e49b2020-04-24 15:11:15 -06001834
John Zulauf86356ca2020-10-19 11:46:41 -06001835 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
1836 // hasn't happened yet)
1837 const std::vector<AccessContext> empty_context_vector;
1838 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1839 const_cast<AccessContext *>(&cb_access_context_));
John Zulauf355e49b2020-04-24 15:11:15 -06001840
John Zulauf86356ca2020-10-19 11:46:41 -06001841 // Create a view list
1842 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1843 assert(fb_state);
1844 if (nullptr == fb_state) return skip;
1845 // NOTE: Must not use COMMAND_BUFFER_STATE variant of this as RecordCmdBeginRenderPass hasn't run and thus
1846 // the activeRenderPass.* fields haven't been set.
1847 const auto views = sync_state_->GetAttachmentViews(*pRenderPassBegin, *fb_state);
1848
1849 // Validate transitions
1850 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
1851
1852 // Validate load operations if there were no layout transition hazards
1853 if (!skip) {
1854 temp_context.RecordLayoutTransitions(rp_state, subpass, views, kCurrentCommandTag);
1855 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001856 }
John Zulauf86356ca2020-10-19 11:46:41 -06001857
John Zulauf355e49b2020-04-24 15:11:15 -06001858 return skip;
1859}
1860
locke-lunarg61870c22020-06-09 14:51:50 -06001861bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1862 const char *func_name) const {
1863 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001864 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001865 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001866 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1867 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001868 return skip;
1869 }
1870
1871 using DescriptorClass = cvdescriptorset::DescriptorClass;
1872 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1873 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1874 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1875 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1876
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001877 for (const auto &stage_state : pipe->stage_state) {
1878 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1879 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001880 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001881 }
locke-lunarg61870c22020-06-09 14:51:50 -06001882 for (const auto &set_binding : stage_state.descriptor_uses) {
1883 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1884 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1885 set_binding.first.second);
1886 const auto descriptor_type = binding_it.GetType();
1887 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1888 auto array_idx = 0;
1889
1890 if (binding_it.IsVariableDescriptorCount()) {
1891 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1892 }
1893 SyncStageAccessIndex sync_index =
1894 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1895
1896 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1897 uint32_t index = i - index_range.start;
1898 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1899 switch (descriptor->GetClass()) {
1900 case DescriptorClass::ImageSampler:
1901 case DescriptorClass::Image: {
1902 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001903 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001904 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001905 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1906 img_view_state = image_sampler_descriptor->GetImageViewState();
1907 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001908 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001909 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1910 img_view_state = image_descriptor->GetImageViewState();
1911 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001912 }
1913 if (!img_view_state) continue;
1914 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1915 VkExtent3D extent = {};
1916 VkOffset3D offset = {};
1917 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1918 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1919 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1920 } else {
1921 extent = img_state->createInfo.extent;
1922 }
John Zulauf361fb532020-07-22 10:45:39 -06001923 HazardResult hazard;
1924 const auto &subresource_range = img_view_state->normalized_subresource_range;
1925 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1926 // Input attachments are subject to raster ordering rules
1927 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1928 kAttachmentRasterOrder, offset, extent);
1929 } else {
1930 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1931 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001932 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001933 skip |= sync_state_->LogError(
1934 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001935 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1936 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001937 func_name, string_SyncHazard(hazard.hazard),
1938 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1939 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001940 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001941 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1942 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1943 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001944 }
1945 break;
1946 }
1947 case DescriptorClass::TexelBuffer: {
1948 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1949 if (!buf_view_state) continue;
1950 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001951 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001952 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001953 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001954 skip |= sync_state_->LogError(
1955 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001956 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1957 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001958 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1959 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001960 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001961 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1962 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1963 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001964 }
1965 break;
1966 }
1967 case DescriptorClass::GeneralBuffer: {
1968 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1969 auto buf_state = buffer_descriptor->GetBufferState();
1970 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001971 const ResourceAccessRange range =
1972 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001973 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001974 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001975 skip |= sync_state_->LogError(
1976 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001977 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1978 func_name, string_SyncHazard(hazard.hazard),
1979 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001980 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001981 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001982 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1983 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1984 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001985 }
1986 break;
1987 }
1988 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1989 default:
1990 break;
1991 }
1992 }
1993 }
1994 }
1995 return skip;
1996}
1997
1998void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1999 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002000 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06002001 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002002 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
2003 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06002004 return;
2005 }
2006
2007 using DescriptorClass = cvdescriptorset::DescriptorClass;
2008 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
2009 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
2010 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
2011 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
2012
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002013 for (const auto &stage_state : pipe->stage_state) {
2014 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
2015 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06002016 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002017 }
locke-lunarg61870c22020-06-09 14:51:50 -06002018 for (const auto &set_binding : stage_state.descriptor_uses) {
2019 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
2020 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
2021 set_binding.first.second);
2022 const auto descriptor_type = binding_it.GetType();
2023 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2024 auto array_idx = 0;
2025
2026 if (binding_it.IsVariableDescriptorCount()) {
2027 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2028 }
2029 SyncStageAccessIndex sync_index =
2030 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
2031
2032 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2033 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2034 switch (descriptor->GetClass()) {
2035 case DescriptorClass::ImageSampler:
2036 case DescriptorClass::Image: {
2037 const IMAGE_VIEW_STATE *img_view_state = nullptr;
2038 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
2039 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
2040 } else {
2041 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
2042 }
2043 if (!img_view_state) continue;
2044 const IMAGE_STATE *img_state = img_view_state->image_state.get();
2045 VkExtent3D extent = {};
2046 VkOffset3D offset = {};
2047 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
2048 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
2049 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
2050 } else {
2051 extent = img_state->createInfo.extent;
2052 }
2053 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
2054 offset, extent, tag);
2055 break;
2056 }
2057 case DescriptorClass::TexelBuffer: {
2058 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
2059 if (!buf_view_state) continue;
2060 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002061 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06002062 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
2063 break;
2064 }
2065 case DescriptorClass::GeneralBuffer: {
2066 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
2067 auto buf_state = buffer_descriptor->GetBufferState();
2068 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002069 const ResourceAccessRange range =
2070 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06002071 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
2072 break;
2073 }
2074 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2075 default:
2076 break;
2077 }
2078 }
2079 }
2080 }
2081}
2082
2083bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2084 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002085 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2086 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002087 return skip;
2088 }
2089
2090 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2091 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002092 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002093
2094 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002095 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002096 if (binding_description.binding < binding_buffers_size) {
2097 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002098 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002099
locke-lunarg1ae57d62020-11-18 10:49:19 -07002100 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002101 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2102 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06002103 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
2104 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002105 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002106 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 -06002107 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002108 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002109 }
2110 }
2111 }
2112 return skip;
2113}
2114
2115void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002116 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2117 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002118 return;
2119 }
2120 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2121 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002122 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002123
2124 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002125 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002126 if (binding_description.binding < binding_buffers_size) {
2127 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002128 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002129
locke-lunarg1ae57d62020-11-18 10:49:19 -07002130 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002131 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2132 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06002133 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
2134 }
2135 }
2136}
2137
2138bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2139 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002140 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002141 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002142 }
locke-lunarg61870c22020-06-09 14:51:50 -06002143
locke-lunarg1ae57d62020-11-18 10:49:19 -07002144 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002145 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002146 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2147 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06002148 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
2149 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002150 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002151 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 -06002152 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002153 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002154 }
2155
2156 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2157 // We will detect more accurate range in the future.
2158 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2159 return skip;
2160}
2161
2162void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002163 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 -06002164
locke-lunarg1ae57d62020-11-18 10:49:19 -07002165 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002166 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002167 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2168 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06002169 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
2170
2171 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2172 // We will detect more accurate range in the future.
2173 RecordDrawVertex(UINT32_MAX, 0, tag);
2174}
2175
2176bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002177 bool skip = false;
2178 if (!current_renderpass_context_) return skip;
2179 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
2180 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
2181 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002182}
2183
2184void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002185 if (current_renderpass_context_) {
locke-lunarg7077d502020-06-18 21:37:26 -06002186 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
2187 tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002188 }
locke-lunarg61870c22020-06-09 14:51:50 -06002189}
2190
John Zulauf355e49b2020-04-24 15:11:15 -06002191bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002192 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06002193 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06002194 skip |=
2195 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002196
2197 return skip;
2198}
2199
2200bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
2201 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06002202 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002203 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06002204 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06002205 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
2206 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002207
2208 return skip;
2209}
2210
2211void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
2212 assert(sync_state_);
2213 if (!cb_state_) return;
2214
2215 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06002216 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06002217 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06002218 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002219 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002220}
2221
John Zulauf355e49b2020-04-24 15:11:15 -06002222void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002223 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06002224 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002225 current_context_ = &current_renderpass_context_->CurrentContext();
2226}
2227
John Zulauf355e49b2020-04-24 15:11:15 -06002228void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002229 assert(current_renderpass_context_);
2230 if (!current_renderpass_context_) return;
2231
John Zulauf1a224292020-06-30 14:52:13 -06002232 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002233 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002234 current_renderpass_context_ = nullptr;
2235}
2236
John Zulauf49beb112020-11-04 16:06:31 -07002237bool CommandBufferAccessContext::ValidateSetEvent(VkCommandBuffer commandBuffer, VkEvent event,
2238 VkPipelineStageFlags stageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002239 // I'll put this here just in case we need to pass this in for future extension support
2240 const auto cmd = CMD_SETEVENT;
2241 bool skip = false;
2242 const auto *sync_event = GetEventState(event);
2243 if (!sync_event) return false; // Core, Lifetimes, or Param check needs to catch invalid events.
2244
2245 const char *const reset_set =
2246 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
2247 "hazards.";
2248 const char *const wait =
2249 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
2250
2251 const auto exec_scope = WithEarlierPipelineStages(ExpandPipelineStages(GetQueueFlags(), stageMask));
2252 if (!sync_event->HasBarrier(stageMask, exec_scope)) {
2253 const char *vuid = nullptr;
2254 const char *message = nullptr;
2255 switch (sync_event->last_command) {
2256 case CMD_RESETEVENT:
2257 // Needs a barrier between reset and set
2258 vuid = "SYNC-vkCmdSetEvent-missingbarrier-reset";
2259 message = reset_set;
2260 break;
2261 case CMD_SETEVENT:
2262 // Needs a barrier between set and set
2263 vuid = "SYNC-vkCmdSetEvent-missingbarrier-set";
2264 message = reset_set;
2265 break;
2266 case CMD_WAITEVENTS:
2267 // Needs a barrier or is in second execution scope
2268 vuid = "SYNC-vkCmdSetEvent-missingbarrier-wait";
2269 message = wait;
2270 break;
2271 default:
2272 // The only other valid last command that wasn't one.
2273 assert(sync_event->last_command == CMD_NONE);
2274 break;
2275 }
2276 if (vuid) {
2277 assert(nullptr != message);
2278 const char *const cmd_name = CommandTypeString(cmd);
2279 skip |= sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2280 cmd_name, CommandTypeString(sync_event->last_command));
2281 }
2282 }
2283
2284 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002285}
2286
John Zulauf4a6105a2020-11-17 15:11:05 -07002287void CommandBufferAccessContext::RecordSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask,
2288 const ResourceUsageTag &tag) {
2289 auto *sync_event = GetEventState(event);
2290 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
2291
2292 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
2293 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
2294 // any issues caused by naive scope setting here.
2295
2296 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
2297 // Given:
2298 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
2299 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002300 auto scope = SyncExecScope::MakeSrc(GetQueueFlags(), stageMask);
John Zulauf4a6105a2020-11-17 15:11:05 -07002301
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002302 if (!sync_event->HasBarrier(stageMask, scope.exec_scope)) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002303 sync_event->unsynchronized_set = sync_event->last_command;
2304 sync_event->ResetFirstScope();
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002305 } else if (sync_event->scope.exec_scope == 0) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002306 // We only set the scope if there isn't one
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002307 sync_event->scope = scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002308
2309 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
2310 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002311 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002312 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
2313 }
2314 };
2315 GetCurrentAccessContext()->ForAll(set_scope);
2316 sync_event->unsynchronized_set = CMD_NONE;
2317 sync_event->first_scope_tag = tag;
2318 }
2319 sync_event->last_command = CMD_SETEVENT;
2320 sync_event->barriers = 0U;
2321}
John Zulauf49beb112020-11-04 16:06:31 -07002322
2323bool CommandBufferAccessContext::ValidateResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
2324 VkPipelineStageFlags stageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002325 // I'll put this here just in case we need to pass this in for future extension support
2326 const auto cmd = CMD_RESETEVENT;
2327
2328 bool skip = false;
2329 // TODO: EVENTS:
2330 // What is it we need to check... that we've had a reset since a set? Set/Set seems ill formed...
2331 const auto *sync_event = GetEventState(event);
2332 if (!sync_event) return false; // Core, Lifetimes, or Param check needs to catch invalid events.
2333
2334 const char *const set_wait =
2335 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
2336 "hazards.";
2337 const char *message = set_wait; // Only one message this call.
2338 const auto exec_scope = WithEarlierPipelineStages(ExpandPipelineStages(GetQueueFlags(), stageMask));
2339 if (!sync_event->HasBarrier(stageMask, exec_scope)) {
2340 const char *vuid = nullptr;
2341 switch (sync_event->last_command) {
2342 case CMD_SETEVENT:
2343 // Needs a barrier between set and reset
2344 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
2345 break;
2346 case CMD_WAITEVENTS: {
2347 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
2348 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
2349 break;
2350 }
2351 default:
2352 // The only other valid last command that wasn't one.
2353 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT));
2354 break;
2355 }
2356 if (vuid) {
2357 const char *const cmd_name = CommandTypeString(cmd);
2358 skip |= sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2359 cmd_name, CommandTypeString(sync_event->last_command));
2360 }
2361 }
2362 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002363}
2364
John Zulauf4a6105a2020-11-17 15:11:05 -07002365void CommandBufferAccessContext::RecordResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
2366 const auto cmd = CMD_RESETEVENT;
2367 auto *sync_event = GetEventState(event);
2368 if (!sync_event) return;
John Zulauf49beb112020-11-04 16:06:31 -07002369
John Zulauf4a6105a2020-11-17 15:11:05 -07002370 // Clear out the first sync scope, any races vs. wait or set are reported, so we'll keep the bookkeeping simple assuming
2371 // the safe case
2372 for (const auto address_type : kAddressTypes) {
2373 sync_event->first_scope[static_cast<size_t>(address_type)].clear();
2374 }
2375
2376 // Update the event state
2377 sync_event->last_command = cmd;
2378 sync_event->unsynchronized_set = CMD_NONE;
2379 sync_event->ResetFirstScope();
2380 sync_event->barriers = 0U;
2381}
2382
2383bool CommandBufferAccessContext::ValidateWaitEvents(uint32_t eventCount, const VkEvent *pEvents, VkPipelineStageFlags srcStageMask,
2384 VkPipelineStageFlags dstStageMask, uint32_t memoryBarrierCount,
2385 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
John Zulauf49beb112020-11-04 16:06:31 -07002386 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2387 uint32_t imageMemoryBarrierCount,
2388 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002389 const auto cmd = CMD_WAITEVENTS;
2390 const char *const ignored = "Wait operation is ignored for this event.";
2391 bool skip = false;
2392
2393 if (srcStageMask & VK_PIPELINE_STAGE_HOST_BIT) {
2394 const char *const cmd_name = CommandTypeString(cmd);
2395 const char *const vuid = "SYNC-vkCmdWaitEvents-hostevent-unsupported";
John Zulauffe757512020-12-18 12:17:47 -07002396 skip = sync_state_->LogInfo(cb_state_->commandBuffer, vuid,
2397 "%s, srcStageMask includes %s, unsupported by synchronization validaton.", cmd_name,
2398 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT), ignored);
John Zulauf4a6105a2020-11-17 15:11:05 -07002399 }
2400
2401 VkPipelineStageFlags event_stage_masks = 0U;
John Zulauffe757512020-12-18 12:17:47 -07002402 bool events_not_found = false;
John Zulauf4a6105a2020-11-17 15:11:05 -07002403 for (uint32_t event_index = 0; event_index < eventCount; event_index++) {
2404 const auto event = pEvents[event_index];
2405 const auto *sync_event = GetEventState(event);
John Zulauffe757512020-12-18 12:17:47 -07002406 if (!sync_event) {
2407 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
2408 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives.
2409
2410 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
2411 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002412
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002413 event_stage_masks |= sync_event->scope.mask_param;
John Zulauf4a6105a2020-11-17 15:11:05 -07002414 const auto ignore_reason = sync_event->IsIgnoredByWait(srcStageMask);
2415 if (ignore_reason) {
2416 switch (ignore_reason) {
2417 case SyncEventState::ResetWaitRace: {
2418 const char *const cmd_name = CommandTypeString(cmd);
2419 const char *const vuid = "SYNC-vkCmdWaitEvents-missingbarrier-reset";
2420 const char *const message =
2421 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
2422 skip |=
2423 sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2424 cmd_name, CommandTypeString(sync_event->last_command), ignored);
2425 break;
2426 }
2427 case SyncEventState::SetRace: {
2428 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for this
2429 // event
2430 const char *const cmd_name = CommandTypeString(cmd);
2431 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
2432 const char *const message =
2433 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, % %s";
2434 const char *const reason = "First synchronization scope is undefined.";
2435 skip |=
2436 sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2437 CommandTypeString(sync_event->last_command), reason, ignored);
2438 break;
2439 }
2440 case SyncEventState::MissingStageBits: {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002441 const VkPipelineStageFlags missing_bits = sync_event->scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07002442 // Issue error message that event waited for is not in wait events scope
2443 const char *const cmd_name = CommandTypeString(cmd);
2444 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
2445 const char *const message =
2446 "%s: %s stageMask 0x%" PRIx32 " includes bits not present in srcStageMask 0x%" PRIx32
2447 ". Bits missing from srcStageMask %s. %s";
2448 skip |= sync_state_->LogError(
2449 event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002450 sync_event->scope.mask_param, srcStageMask, string_VkPipelineStageFlags(missing_bits).c_str(), ignored);
John Zulauf4a6105a2020-11-17 15:11:05 -07002451 break;
2452 }
2453 default:
2454 assert(ignore_reason == SyncEventState::NotIgnored);
2455 }
2456 } else if (imageMemoryBarrierCount) {
2457 const auto *context = GetCurrentAccessContext();
2458 assert(context);
2459 for (uint32_t barrier_index = 0; barrier_index < imageMemoryBarrierCount; barrier_index++) {
2460 const auto &barrier = pImageMemoryBarriers[barrier_index];
2461 if (barrier.oldLayout == barrier.newLayout) continue;
2462 const auto *image_state = sync_state_->Get<IMAGE_STATE>(barrier.image);
2463 if (!image_state) continue;
2464 auto subresource_range = NormalizeSubresourceRange(image_state->createInfo, barrier.subresourceRange);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002465 const auto src_access_scope = SyncStageAccess::AccessScope(sync_event->scope.valid_accesses, barrier.srcAccessMask);
John Zulauf4a6105a2020-11-17 15:11:05 -07002466 const auto hazard =
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002467 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
2468 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
John Zulauf4a6105a2020-11-17 15:11:05 -07002469 if (hazard.hazard) {
2470 const char *const cmd_name = CommandTypeString(cmd);
2471 skip |= sync_state_->LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
2472 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", cmd_name,
2473 string_SyncHazard(hazard.hazard), barrier_index,
2474 sync_state_->report_data->FormatHandle(barrier.image).c_str(),
2475 string_UsageTag(hazard).c_str());
2476 break;
2477 }
2478 }
2479 }
2480 }
2481
2482 // Note that we can't check for HOST in pEvents as we don't track that set event type
2483 const auto extra_stage_bits = (srcStageMask & ~VK_PIPELINE_STAGE_HOST_BIT) & ~event_stage_masks;
2484 if (extra_stage_bits) {
2485 // Issue error message that event waited for is not in wait events scope
2486 const char *const cmd_name = CommandTypeString(cmd);
2487 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
2488 const char *const message =
John Zulauffe757512020-12-18 12:17:47 -07002489 "%s: srcStageMask 0x%" PRIx32 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
2490 if (events_not_found) {
2491 skip |= sync_state_->LogInfo(cb_state_->commandBuffer, vuid, message, cmd_name, srcStageMask,
2492 string_VkPipelineStageFlags(extra_stage_bits).c_str(),
2493 " vkCmdSetEvent may be in previously submitted command buffer.");
2494 } else {
2495 skip |= sync_state_->LogError(cb_state_->commandBuffer, vuid, message, cmd_name, srcStageMask,
2496 string_VkPipelineStageFlags(extra_stage_bits).c_str(), "");
2497 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002498 }
2499 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002500}
2501
2502void CommandBufferAccessContext::RecordWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
2503 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
2504 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2505 uint32_t bufferMemoryBarrierCount,
2506 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2507 uint32_t imageMemoryBarrierCount,
John Zulauf4a6105a2020-11-17 15:11:05 -07002508 const VkImageMemoryBarrier *pImageMemoryBarriers, const ResourceUsageTag &tag) {
2509 auto *access_context = GetCurrentAccessContext();
2510 assert(access_context);
2511 if (!access_context) return;
2512
2513 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
2514 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
2515 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
John Zulauf4a6105a2020-11-17 15:11:05 -07002516 access_context->ResolvePreviousAccesses();
2517
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002518 auto dst = SyncExecScope::MakeDst(GetQueueFlags(), dstStageMask);
John Zulauf4a6105a2020-11-17 15:11:05 -07002519 for (uint32_t event_index = 0; event_index < eventCount; event_index++) {
2520 const auto event = pEvents[event_index];
2521 auto *sync_event = GetEventState(event);
2522 if (!sync_event) continue;
2523
2524 sync_event->last_command = CMD_WAITEVENTS;
2525
2526 if (!sync_event->IsIgnoredByWait(srcStageMask)) {
2527 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
2528 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
2529 // of the barriers is maintained.
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002530 ApplyBufferBarriers(*sync_event, dst, bufferMemoryBarrierCount, pBufferMemoryBarriers);
2531 ApplyImageBarriers(*sync_event, dst, imageMemoryBarrierCount, pImageMemoryBarriers, tag);
2532 ApplyGlobalBarriers(*sync_event, dst, memoryBarrierCount, pMemoryBarriers, tag);
John Zulauf4a6105a2020-11-17 15:11:05 -07002533 } else {
2534 // We ignored this wait, so we don't have any effective synchronization barriers for it.
2535 sync_event->barriers = 0U;
2536 }
2537 }
2538
2539 // Apply the pending barriers
2540 ResolvePendingBarrierFunctor apply_pending_action(tag);
2541 access_context->ApplyGlobalBarriers(apply_pending_action);
2542}
2543
2544void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2545 // Erase is okay with the key not being
2546 event_state_.erase(event);
2547}
2548
2549SyncEventState *CommandBufferAccessContext::GetEventState(VkEvent event) {
2550 auto &event_up = event_state_[event];
2551 if (!event_up) {
2552 auto event_atate = sync_state_->GetShared<EVENT_STATE>(event);
2553 event_up.reset(new SyncEventState(event_atate));
2554 }
2555 return event_up.get();
2556}
2557
2558const SyncEventState *CommandBufferAccessContext::GetEventState(VkEvent event) const {
2559 auto event_it = event_state_.find(event);
2560 if (event_it == event_state_.cend()) {
2561 return nullptr;
2562 }
2563 return event_it->second.get();
2564}
John Zulauf49beb112020-11-04 16:06:31 -07002565
locke-lunarg61870c22020-06-09 14:51:50 -06002566bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
2567 const VkRect2D &render_area, const char *func_name) const {
2568 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002569 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2570 if (!pipe ||
2571 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002572 return skip;
2573 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002574 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002575 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2576 VkExtent3D extent = CastTo3D(render_area.extent);
2577 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06002578
John Zulauf1a224292020-06-30 14:52:13 -06002579 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002580 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002581 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2582 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002583 if (location >= subpass.colorAttachmentCount ||
2584 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002585 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002586 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002587 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002588 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
2589 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06002590 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002591 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002592 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002593 func_name, string_SyncHazard(hazard.hazard),
2594 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2595 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002596 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002597 }
2598 }
2599 }
locke-lunarg37047832020-06-12 13:44:45 -06002600
2601 // PHASE1 TODO: Add layout based read/vs. write selection.
2602 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002603 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002604 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002605 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002606 bool depth_write = false, stencil_write = false;
2607
2608 // PHASE1 TODO: These validation should be in core_checks.
2609 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002610 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2611 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002612 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2613 depth_write = true;
2614 }
2615 // PHASE1 TODO: It needs to check if stencil is writable.
2616 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2617 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2618 // PHASE1 TODO: These validation should be in core_checks.
2619 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002620 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002621 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2622 stencil_write = true;
2623 }
2624
2625 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2626 if (depth_write) {
2627 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002628 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2629 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002630 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002631 skip |= sync_state.LogError(
2632 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002633 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002634 func_name, string_SyncHazard(hazard.hazard),
2635 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2636 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002637 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002638 }
2639 }
2640 if (stencil_write) {
2641 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002642 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2643 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002644 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002645 skip |= sync_state.LogError(
2646 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002647 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002648 func_name, string_SyncHazard(hazard.hazard),
2649 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2650 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002651 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002652 }
locke-lunarg61870c22020-06-09 14:51:50 -06002653 }
2654 }
2655 return skip;
2656}
2657
locke-lunarg96dc9632020-06-10 17:22:18 -06002658void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
2659 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002660 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2661 if (!pipe ||
2662 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002663 return;
2664 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002665 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002666 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2667 VkExtent3D extent = CastTo3D(render_area.extent);
2668 VkOffset3D offset = CastTo3D(render_area.offset);
2669
John Zulauf1a224292020-06-30 14:52:13 -06002670 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002671 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002672 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2673 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002674 if (location >= subpass.colorAttachmentCount ||
2675 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002676 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002677 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002678 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002679 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
2680 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002681 }
2682 }
locke-lunarg37047832020-06-12 13:44:45 -06002683
2684 // PHASE1 TODO: Add layout based read/vs. write selection.
2685 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002686 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002687 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002688 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002689 bool depth_write = false, stencil_write = false;
2690
2691 // PHASE1 TODO: These validation should be in core_checks.
2692 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002693 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2694 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002695 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2696 depth_write = true;
2697 }
2698 // PHASE1 TODO: It needs to check if stencil is writable.
2699 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2700 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2701 // PHASE1 TODO: These validation should be in core_checks.
2702 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002703 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002704 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2705 stencil_write = true;
2706 }
2707
2708 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2709 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002710 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2711 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002712 }
2713 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002714 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2715 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002716 }
locke-lunarg61870c22020-06-09 14:51:50 -06002717 }
2718}
2719
John Zulauf1507ee42020-05-18 11:33:09 -06002720bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
2721 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002722 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002723 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06002724 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2725 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002726 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2727 func_name);
2728
John Zulauf355e49b2020-04-24 15:11:15 -06002729 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002730 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06002731 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002732 if (!skip) {
2733 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2734 // on a copy of the (empty) next context.
2735 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2736 AccessContext temp_context(next_context);
2737 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
2738 skip |= temp_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2739 }
John Zulauf7635de32020-05-29 17:14:15 -06002740 return skip;
2741}
2742bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
2743 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002744 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002745 bool skip = false;
2746 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2747 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002748 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2749 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002750 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002751 return skip;
2752}
2753
John Zulauf7635de32020-05-29 17:14:15 -06002754AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2755 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2756}
2757
2758bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2759 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002760 bool skip = false;
2761
John Zulauf7635de32020-05-29 17:14:15 -06002762 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2763 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2764 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2765 // to apply and only copy then, if this proves a hot spot.
2766 std::unique_ptr<AccessContext> proxy_for_current;
2767
John Zulauf355e49b2020-04-24 15:11:15 -06002768 // Validate the "finalLayout" transitions to external
2769 // Get them from where there we're hidding in the extra entry.
2770 const auto &final_transitions = rp_state_->subpass_transitions.back();
2771 for (const auto &transition : final_transitions) {
2772 const auto &attach_view = attachment_views_[transition.attachment];
2773 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2774 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002775 auto *context = trackback.context;
2776
2777 if (transition.prev_pass == current_subpass_) {
2778 if (!proxy_for_current) {
2779 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2780 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2781 }
2782 context = proxy_for_current.get();
2783 }
2784
John Zulaufa0a98292020-09-18 09:30:10 -06002785 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2786 const auto merged_barrier = MergeBarriers(trackback.barriers);
2787 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2788 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2789 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002790 if (hazard.hazard) {
2791 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2792 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002793 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002794 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002795 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002796 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002797 }
2798 }
2799 return skip;
2800}
2801
2802void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2803 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002804 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002805}
2806
John Zulauf1507ee42020-05-18 11:33:09 -06002807void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2808 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2809 auto &subpass_context = subpass_contexts_[current_subpass_];
2810 VkExtent3D extent = CastTo3D(render_area.extent);
2811 VkOffset3D offset = CastTo3D(render_area.offset);
2812
2813 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2814 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2815 if (attachment_views_[i] == nullptr) continue; // UNUSED
2816 const auto &view = *attachment_views_[i];
2817 const IMAGE_STATE *image = view.image_state.get();
2818 if (image == nullptr) continue;
2819
2820 const auto &ci = attachment_ci[i];
2821 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002822 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002823 const bool is_color = !(has_depth || has_stencil);
2824
2825 if (is_color) {
2826 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2827 extent, tag);
2828 } else {
2829 auto update_range = view.normalized_subresource_range;
2830 if (has_depth) {
2831 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2832 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2833 }
2834 if (has_stencil) {
2835 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2836 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2837 tag);
2838 }
2839 }
2840 }
2841 }
2842}
2843
John Zulauf355e49b2020-04-24 15:11:15 -06002844void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002845 const AccessContext *external_context, VkQueueFlags queue_flags,
2846 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002847 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002848 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002849 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2850 // Add this for all subpasses here so that they exsist during next subpass validation
2851 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002852 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002853 }
2854 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2855
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002856 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002857 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002858 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002859}
John Zulauf1507ee42020-05-18 11:33:09 -06002860
2861void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002862 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2863 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002864 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002865
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002866 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2867 // subpass, so their tag needs to be different from the layout and load operations below.
Jeremy Gebben4bb73502020-12-14 11:17:50 -07002868 ResourceUsageTag next_tag = tag.NextSubCommand();
John Zulauf355e49b2020-04-24 15:11:15 -06002869 current_subpass_++;
2870 assert(current_subpass_ < subpass_contexts_.size());
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002871 subpass_contexts_[current_subpass_].SetStartTag(next_tag);
2872 RecordLayoutTransitions(next_tag);
2873 RecordLoadOperations(render_area, next_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002874}
2875
John Zulauf1a224292020-06-30 14:52:13 -06002876void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2877 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002878 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002879 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002880 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002881
John Zulauf355e49b2020-04-24 15:11:15 -06002882 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002883 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002884
2885 // Add the "finalLayout" transitions to external
2886 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002887 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2888 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2889 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002890 const auto &final_transitions = rp_state_->subpass_transitions.back();
2891 for (const auto &transition : final_transitions) {
2892 const auto &attachment = attachment_views_[transition.attachment];
2893 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002894 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf1e331ec2020-12-04 18:29:38 -07002895 std::vector<PipelineBarrierOp> barrier_ops;
2896 barrier_ops.reserve(last_trackback.barriers.size());
2897 for (const auto &barrier : last_trackback.barriers) {
2898 barrier_ops.emplace_back(barrier, true);
2899 }
2900 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, barrier_ops, tag);
2901 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002902 }
2903}
2904
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002905SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags mask_param) {
2906 SyncExecScope result;
2907 result.mask_param = mask_param;
2908 result.expanded_mask = ExpandPipelineStages(queue_flags, mask_param);
2909 result.exec_scope = WithEarlierPipelineStages(result.expanded_mask);
2910 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2911 return result;
2912}
2913
2914SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags mask_param) {
2915 SyncExecScope result;
2916 result.mask_param = mask_param;
2917 result.expanded_mask = ExpandPipelineStages(queue_flags, mask_param);
2918 result.exec_scope = WithLaterPipelineStages(result.expanded_mask);
2919 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2920 return result;
2921}
2922
2923SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
2924 src_exec_scope = src.exec_scope;
2925 src_access_scope = 0;
2926 dst_exec_scope = dst.exec_scope;
2927 dst_access_scope = 0;
2928}
2929
2930template <typename Barrier>
2931SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
2932 src_exec_scope = src.exec_scope;
2933 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2934 dst_exec_scope = dst.exec_scope;
2935 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2936}
2937
2938SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
2939 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
2940 src_exec_scope = src.exec_scope;
2941 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2942
2943 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
2944 dst_exec_scope = dst.exec_scope;
2945 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002946}
2947
John Zulaufb02c1eb2020-10-06 16:33:36 -06002948// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2949void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2950 for (const auto &barrier : barriers) {
2951 ApplyBarrier(barrier, layout_transition);
2952 }
2953}
2954
John Zulauf89311b42020-09-29 16:28:47 -06002955// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2956// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2957// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002958void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2959 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002960 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002961 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002962 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002963 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002964 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002965 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002966}
John Zulauf9cb530d2019-09-30 14:14:10 -06002967HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2968 HazardResult hazard;
2969 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002970 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002971 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002972 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002973 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002974 }
2975 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002976 // Write operation:
2977 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2978 // If reads exists -- test only against them because either:
2979 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2980 // * 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
2981 // the current write happens after the reads, so just test the write against the reades
2982 // Otherwise test against last_write
2983 //
2984 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002985 if (last_reads.size()) {
2986 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002987 if (IsReadHazard(usage_stage, read_access)) {
2988 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2989 break;
2990 }
2991 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002992 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002993 // Write-After-Write check -- if we have a previous write to test against
2994 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002995 }
2996 }
2997 return hazard;
2998}
2999
John Zulauf69133422020-05-20 14:55:53 -06003000HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
3001 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
3002 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06003003 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06003004 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003005 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
3006 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06003007 if (IsRead(usage_bit)) {
3008 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
3009 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
3010 if (is_raw_hazard) {
3011 // NOTE: we know last_write is non-zero
3012 // See if the ordering rules save us from the simple RAW check above
3013 // First check to see if the current usage is covered by the ordering rules
3014 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
3015 const bool usage_is_ordered =
3016 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
3017 if (usage_is_ordered) {
3018 // Now see of the most recent write (or a subsequent read) are ordered
3019 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
3020 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06003021 }
3022 }
John Zulauf4285ee92020-09-23 10:20:52 -06003023 if (is_raw_hazard) {
3024 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
3025 }
John Zulauf361fb532020-07-22 10:45:39 -06003026 } else {
3027 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003028 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07003029 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06003030 // Look for any WAR hazards outside the ordered set of stages
John Zulauf4285ee92020-09-23 10:20:52 -06003031 VkPipelineStageFlags ordered_stages = 0;
3032 if (usage_write_is_ordered) {
3033 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
3034 ordered_stages = GetOrderedStages(ordering);
3035 }
3036 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
3037 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003038 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06003039 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
3040 if (IsReadHazard(usage_stage, read_access)) {
3041 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3042 break;
3043 }
John Zulaufd14743a2020-07-03 09:42:39 -06003044 }
3045 }
John Zulauf4285ee92020-09-23 10:20:52 -06003046 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003047 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003048 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06003049 }
John Zulauf69133422020-05-20 14:55:53 -06003050 }
3051 }
3052 return hazard;
3053}
3054
John Zulauf2f952d22020-02-10 11:34:51 -07003055// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003056HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07003057 HazardResult hazard;
3058 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003059 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
3060 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
3061 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07003062 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003063 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06003064 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07003065 }
3066 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003067 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06003068 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07003069 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003070 // 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 -07003071 for (const auto &read_access : last_reads) {
3072 if (read_access.tag.index >= start_tag.index) {
3073 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003074 break;
3075 }
3076 }
John Zulauf2f952d22020-02-10 11:34:51 -07003077 }
3078 }
3079 return hazard;
3080}
3081
John Zulauf36bcf6a2020-02-03 15:12:52 -07003082HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003083 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07003084 // Only supporting image layout transitions for now
3085 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3086 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06003087 // only test for WAW if there no intervening read operations.
3088 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07003089 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06003090 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07003091 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003092 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06003093 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07003094 break;
3095 }
3096 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003097 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3098 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3099 }
3100
3101 return hazard;
3102}
3103
3104HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
3105 const SyncStageAccessFlags &src_access_scope,
3106 const ResourceUsageTag &event_tag) const {
3107 // Only supporting image layout transitions for now
3108 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3109 HazardResult hazard;
3110 // only test for WAW if there no intervening read operations.
3111 // See DetectHazard(SyncStagetAccessIndex) above for more details.
3112
John Zulaufab7756b2020-12-29 16:10:16 -07003113 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003114 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
3115 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07003116 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003117 if (read_access.tag.IsBefore(event_tag)) {
3118 // The read is in the events first synchronization scope, so we use a barrier hazard check
3119 // If the read stage is not in the src sync scope
3120 // *AND* not execution chained with an existing sync barrier (that's the or)
3121 // then the barrier access is unsafe (R/W after R)
3122 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3123 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3124 break;
3125 }
3126 } else {
3127 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3128 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3129 }
3130 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003131 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003132 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
3133 if (write_tag.IsBefore(event_tag)) {
3134 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3135 // So do a normal barrier hazard check
3136 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3137 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3138 }
3139 } else {
3140 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003141 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3142 }
John Zulaufd14743a2020-07-03 09:42:39 -06003143 }
John Zulauf361fb532020-07-22 10:45:39 -06003144
John Zulauf0cb5be22020-01-23 12:18:22 -07003145 return hazard;
3146}
3147
John Zulauf5f13a792020-03-10 07:31:21 -06003148// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3149// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3150// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3151void ResourceAccessState::Resolve(const ResourceAccessState &other) {
3152 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003153 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3154 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003155 *this = other;
3156 } else if (!other.write_tag.IsBefore(write_tag)) {
3157 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
3158 // dependency chaining logic or any stage expansion)
3159 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003160 pending_write_barriers |= other.pending_write_barriers;
3161 pending_layout_transition |= other.pending_layout_transition;
3162 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003163
John Zulaufd14743a2020-07-03 09:42:39 -06003164 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07003165 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06003166 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07003167 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003168 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003169 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003170 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003171 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3172 // but we should wait on profiling data for that.
3173 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003174 auto &my_read = last_reads[my_read_index];
3175 if (other_read.stage == my_read.stage) {
3176 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003177 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003178 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003179 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003180 my_read.pending_dep_chain = other_read.pending_dep_chain;
3181 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3182 // May require tracking more than one access per stage.
3183 my_read.barriers = other_read.barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003184 if (my_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
3185 // Since I'm overwriting the fragement stage read, also update the input attachment info
3186 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003187 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003188 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003189 } else if (other_read.tag.IsBefore(my_read.tag)) {
3190 // The read tags match so merge the barriers
3191 my_read.barriers |= other_read.barriers;
3192 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003193 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003194
John Zulauf5f13a792020-03-10 07:31:21 -06003195 break;
3196 }
3197 }
3198 } else {
3199 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07003200 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06003201 last_read_stages |= other_read.stage;
John Zulauf4285ee92020-09-23 10:20:52 -06003202 if (other_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06003203 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003204 }
John Zulauf5f13a792020-03-10 07:31:21 -06003205 }
3206 }
John Zulauf361fb532020-07-22 10:45:39 -06003207 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003208 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3209 // ignore it.
John Zulauf5f13a792020-03-10 07:31:21 -06003210}
3211
John Zulauf9cb530d2019-09-30 14:14:10 -06003212void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
3213 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3214 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003215 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003216 // Mulitple outstanding reads may be of interest and do dependency chains independently
3217 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3218 const auto usage_stage = PipelineStageBit(usage_index);
3219 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003220 for (auto &read_access : last_reads) {
3221 if (read_access.stage == usage_stage) {
3222 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003223 break;
3224 }
3225 }
3226 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07003227 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003228 last_read_stages |= usage_stage;
3229 }
John Zulauf4285ee92020-09-23 10:20:52 -06003230
3231 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
3232 if (usage_stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06003233 // TODO Revisit re: multiple reads for a given stage
3234 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003235 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003236 } else {
3237 // Assume write
3238 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003239 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003240 }
3241}
John Zulauf5f13a792020-03-10 07:31:21 -06003242
John Zulauf89311b42020-09-29 16:28:47 -06003243// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3244// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3245// We can overwrite them as *this* write is now after them.
3246//
3247// 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 -07003248void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003249 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06003250 last_read_stages = 0;
3251 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003252 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003253
3254 write_barriers = 0;
3255 write_dependency_chain = 0;
3256 write_tag = tag;
3257 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003258}
3259
John Zulauf89311b42020-09-29 16:28:47 -06003260// Apply the memory barrier without updating the existing barriers. The execution barrier
3261// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3262// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3263// replace the current write barriers or add to them, so accumulate to pending as well.
3264void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3265 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3266 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003267 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3268 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3269 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3270 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulauf4a6105a2020-11-17 15:11:05 -07003271 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003272 pending_write_barriers |= barrier.dst_access_scope;
3273 pending_write_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003274 }
John Zulauf89311b42020-09-29 16:28:47 -06003275 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3276 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003277
John Zulauf89311b42020-09-29 16:28:47 -06003278 if (!pending_layout_transition) {
3279 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3280 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003281 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06003282 // 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 -07003283 if (barrier.src_exec_scope & (read_access.stage | read_access.barriers)) {
3284 read_access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003285 }
3286 }
John Zulaufa0a98292020-09-18 09:30:10 -06003287 }
John Zulaufa0a98292020-09-18 09:30:10 -06003288}
3289
John Zulauf4a6105a2020-11-17 15:11:05 -07003290// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3291// changes the "chaining" state, but to keep barriers independent. See discussion above.
3292void ResourceAccessState::ApplyBarrier(const ResourceUsageTag &scope_tag, const SyncBarrier &barrier, bool layout_transition) {
3293 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3294 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3295 // in order to know if it's in the excecution scope
3296 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3297 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3298 // errors w.r.t. "most recent" accesses.
3299 if (layout_transition || ((write_tag.IsBefore(scope_tag)) && (barrier.src_access_scope & last_write).any())) {
3300 pending_write_barriers |= barrier.dst_access_scope;
3301 pending_write_dep_chain |= barrier.dst_exec_scope;
3302 }
3303 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3304 pending_layout_transition |= layout_transition;
3305
3306 if (!pending_layout_transition) {
3307 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3308 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003309 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003310 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3311 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3312 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3313 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3314 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3315 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3316 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulaufab7756b2020-12-29 16:10:16 -07003317 if (read_access.tag.IsBefore(scope_tag) && (barrier.src_exec_scope & (read_access.stage | read_access.barriers))) {
3318 read_access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003319 }
3320 }
3321 }
3322}
John Zulauf89311b42020-09-29 16:28:47 -06003323void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
3324 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06003325 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
3326 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
3327 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003328 }
John Zulauf89311b42020-09-29 16:28:47 -06003329
3330 // Apply the accumulate execution barriers (and thus update chaining information)
3331 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003332 for (auto &read_access : last_reads) {
3333 read_access.barriers |= read_access.pending_dep_chain;
3334 read_execution_barriers |= read_access.barriers;
3335 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003336 }
3337
3338 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3339 write_dependency_chain |= pending_write_dep_chain;
3340 write_barriers |= pending_write_barriers;
3341 pending_write_dep_chain = 0;
3342 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003343}
3344
John Zulauf59e25072020-07-17 10:55:21 -06003345// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003346VkPipelineStageFlags ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
John Zulauf59e25072020-07-17 10:55:21 -06003347 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003348
John Zulaufab7756b2020-12-29 16:10:16 -07003349 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003350 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003351 barriers = read_access.barriers;
3352 break;
John Zulauf59e25072020-07-17 10:55:21 -06003353 }
3354 }
John Zulauf4285ee92020-09-23 10:20:52 -06003355
John Zulauf59e25072020-07-17 10:55:21 -06003356 return barriers;
3357}
3358
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003359inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003360 assert(IsRead(usage));
3361 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3362 // * the previous reads are not hazards, and thus last_write must be visible and available to
3363 // any reads that happen after.
3364 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3365 // 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 -07003366 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003367}
3368
John Zulauf4285ee92020-09-23 10:20:52 -06003369VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const SyncOrderingBarrier &ordering) const {
3370 // Whether the stage are in the ordering scope only matters if the current write is ordered
3371 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
3372 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003373 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003374 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003375 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
3376 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
3377 }
3378
3379 return ordered_stages;
3380}
3381
John Zulaufd1f85d42020-04-15 12:23:15 -06003382void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003383 auto *access_context = GetAccessContextNoInsert(command_buffer);
3384 if (access_context) {
3385 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003386 }
3387}
3388
John Zulaufd1f85d42020-04-15 12:23:15 -06003389void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3390 auto access_found = cb_access_state.find(command_buffer);
3391 if (access_found != cb_access_state.end()) {
3392 access_found->second->Reset();
3393 cb_access_state.erase(access_found);
3394 }
3395}
3396
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003397void SyncValidator::ApplyGlobalBarriers(AccessContext *context, const SyncExecScope &src, const SyncExecScope &dst,
3398 uint32_t memory_barrier_count, const VkMemoryBarrier *pMemoryBarriers,
3399 const ResourceUsageTag &tag) {
John Zulauf1e331ec2020-12-04 18:29:38 -07003400 std::vector<PipelineBarrierOp> barrier_ops;
3401 barrier_ops.reserve(std::min<uint32_t>(1, memory_barrier_count));
John Zulauf89311b42020-09-29 16:28:47 -06003402 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
3403 const auto &barrier = pMemoryBarriers[barrier_index];
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003404 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf1e331ec2020-12-04 18:29:38 -07003405 barrier_ops.emplace_back(sync_barrier, false);
John Zulauf89311b42020-09-29 16:28:47 -06003406 }
3407 if (0 == memory_barrier_count) {
3408 // If there are no global memory barriers, force an exec barrier
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003409 barrier_ops.emplace_back(SyncBarrier(src, dst), false);
John Zulauf89311b42020-09-29 16:28:47 -06003410 }
John Zulauf1e331ec2020-12-04 18:29:38 -07003411 ApplyBarrierOpsFunctor<PipelineBarrierOp> barriers_functor(true /* resolve */, barrier_ops, tag);
John Zulauf540266b2020-04-06 18:54:53 -06003412 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06003413}
3414
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003415void SyncValidator::ApplyBufferBarriers(AccessContext *context, const SyncExecScope &src, const SyncExecScope &dst,
3416 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003417 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003418 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06003419 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
3420 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06003421 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
3422 const ResourceAccessRange range = MakeRange(barrier);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003423 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07003424 const ApplyBarrierFunctor<PipelineBarrierOp> update_action({sync_barrier, false /* layout_transition */});
John Zulauf89311b42020-09-29 16:28:47 -06003425 context->UpdateResourceAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06003426 }
3427}
3428
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003429void SyncValidator::ApplyImageBarriers(AccessContext *context, const SyncExecScope &src, const SyncExecScope &dst,
3430 uint32_t barrier_count, const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07003431 for (uint32_t index = 0; index < barrier_count; index++) {
3432 const auto &barrier = barriers[index];
3433 const auto *image = Get<IMAGE_STATE>(barrier.image);
3434 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06003435 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06003436 bool layout_transition = barrier.oldLayout != barrier.newLayout;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003437 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07003438 const ApplyBarrierFunctor<PipelineBarrierOp> barrier_action({sync_barrier, layout_transition});
John Zulauf89311b42020-09-29 16:28:47 -06003439 context->UpdateResourceAccess(*image, subresource_range, barrier_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06003440 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003441}
3442
3443bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3444 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3445 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003446 const auto *cb_context = GetAccessContext(commandBuffer);
3447 assert(cb_context);
3448 if (!cb_context) return skip;
3449 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003450
John Zulauf3d84f1b2020-03-09 13:33:25 -06003451 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003452 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003453 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003454
3455 for (uint32_t region = 0; region < regionCount; region++) {
3456 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003457 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003458 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003459 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003460 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003461 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003462 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003463 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003464 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003465 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003466 }
John Zulauf16adfc92020-04-08 10:28:33 -06003467 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003468 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06003469 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003470 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003471 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003472 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003473 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003474 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003475 }
3476 }
3477 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003478 }
3479 return skip;
3480}
3481
3482void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3483 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003484 auto *cb_context = GetAccessContext(commandBuffer);
3485 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003486 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003487 auto *context = cb_context->GetCurrentAccessContext();
3488
John Zulauf9cb530d2019-09-30 14:14:10 -06003489 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003490 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003491
3492 for (uint32_t region = 0; region < regionCount; region++) {
3493 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003494 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003495 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003496 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003497 }
John Zulauf16adfc92020-04-08 10:28:33 -06003498 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003499 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003500 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003501 }
3502 }
3503}
3504
John Zulauf4a6105a2020-11-17 15:11:05 -07003505void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3506 // Clear out events from the command buffer contexts
3507 for (auto &cb_context : cb_access_state) {
3508 cb_context.second->RecordDestroyEvent(event);
3509 }
3510}
3511
Jeff Leger178b1e52020-10-05 12:22:23 -04003512bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3513 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3514 bool skip = false;
3515 const auto *cb_context = GetAccessContext(commandBuffer);
3516 assert(cb_context);
3517 if (!cb_context) return skip;
3518 const auto *context = cb_context->GetCurrentAccessContext();
3519
3520 // If we have no previous accesses, we have no hazards
3521 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3522 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3523
3524 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3525 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3526 if (src_buffer) {
3527 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
3528 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
3529 if (hazard.hazard) {
3530 // TODO -- add tag information to log msg when useful.
3531 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3532 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3533 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
3534 region, string_UsageTag(hazard).c_str());
3535 }
3536 }
3537 if (dst_buffer && !skip) {
3538 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
3539 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
3540 if (hazard.hazard) {
3541 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3542 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3543 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
3544 region, string_UsageTag(hazard).c_str());
3545 }
3546 }
3547 if (skip) break;
3548 }
3549 return skip;
3550}
3551
3552void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3553 auto *cb_context = GetAccessContext(commandBuffer);
3554 assert(cb_context);
3555 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3556 auto *context = cb_context->GetCurrentAccessContext();
3557
3558 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3559 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3560
3561 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3562 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3563 if (src_buffer) {
3564 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
3565 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
3566 }
3567 if (dst_buffer) {
3568 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
3569 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
3570 }
3571 }
3572}
3573
John Zulauf5c5e88d2019-12-26 11:22:02 -07003574bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3575 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3576 const VkImageCopy *pRegions) const {
3577 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003578 const auto *cb_access_context = GetAccessContext(commandBuffer);
3579 assert(cb_access_context);
3580 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003581
John Zulauf3d84f1b2020-03-09 13:33:25 -06003582 const auto *context = cb_access_context->GetCurrentAccessContext();
3583 assert(context);
3584 if (!context) return skip;
3585
3586 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3587 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003588 for (uint32_t region = 0; region < regionCount; region++) {
3589 const auto &copy_region = pRegions[region];
3590 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003591 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003592 copy_region.srcOffset, copy_region.extent);
3593 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003594 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003595 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003596 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003597 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003598 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003599 }
3600
3601 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003602 VkExtent3D dst_copy_extent =
3603 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06003604 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003605 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003606 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003607 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003608 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003609 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003610 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003611 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003612 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003613 }
3614 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003615
John Zulauf5c5e88d2019-12-26 11:22:02 -07003616 return skip;
3617}
3618
3619void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3620 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3621 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003622 auto *cb_access_context = GetAccessContext(commandBuffer);
3623 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003624 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003625 auto *context = cb_access_context->GetCurrentAccessContext();
3626 assert(context);
3627
John Zulauf5c5e88d2019-12-26 11:22:02 -07003628 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003629 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003630
3631 for (uint32_t region = 0; region < regionCount; region++) {
3632 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003633 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003634 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
3635 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003636 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003637 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003638 VkExtent3D dst_copy_extent =
3639 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06003640 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
3641 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003642 }
3643 }
3644}
3645
Jeff Leger178b1e52020-10-05 12:22:23 -04003646bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3647 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3648 bool skip = false;
3649 const auto *cb_access_context = GetAccessContext(commandBuffer);
3650 assert(cb_access_context);
3651 if (!cb_access_context) return skip;
3652
3653 const auto *context = cb_access_context->GetCurrentAccessContext();
3654 assert(context);
3655 if (!context) return skip;
3656
3657 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3658 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3659 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3660 const auto &copy_region = pCopyImageInfo->pRegions[region];
3661 if (src_image) {
3662 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
3663 copy_region.srcOffset, copy_region.extent);
3664 if (hazard.hazard) {
3665 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3666 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3667 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
3668 region, string_UsageTag(hazard).c_str());
3669 }
3670 }
3671
3672 if (dst_image) {
3673 VkExtent3D dst_copy_extent =
3674 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
3675 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
3676 copy_region.dstOffset, dst_copy_extent);
3677 if (hazard.hazard) {
3678 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3679 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3680 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
3681 region, string_UsageTag(hazard).c_str());
3682 }
3683 if (skip) break;
3684 }
3685 }
3686
3687 return skip;
3688}
3689
3690void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3691 auto *cb_access_context = GetAccessContext(commandBuffer);
3692 assert(cb_access_context);
3693 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3694 auto *context = cb_access_context->GetCurrentAccessContext();
3695 assert(context);
3696
3697 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3698 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3699
3700 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3701 const auto &copy_region = pCopyImageInfo->pRegions[region];
3702 if (src_image) {
3703 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
3704 copy_region.extent, tag);
3705 }
3706 if (dst_image) {
3707 VkExtent3D dst_copy_extent =
3708 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
3709 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
3710 dst_copy_extent, tag);
3711 }
3712 }
3713}
3714
John Zulauf9cb530d2019-09-30 14:14:10 -06003715bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3716 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3717 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3718 uint32_t bufferMemoryBarrierCount,
3719 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3720 uint32_t imageMemoryBarrierCount,
3721 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3722 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003723 const auto *cb_access_context = GetAccessContext(commandBuffer);
3724 assert(cb_access_context);
3725 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003726
John Zulauf3d84f1b2020-03-09 13:33:25 -06003727 const auto *context = cb_access_context->GetCurrentAccessContext();
3728 assert(context);
3729 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003730
John Zulauf3d84f1b2020-03-09 13:33:25 -06003731 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003732 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3733 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07003734 // Validate Image Layout transitions
3735 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
3736 const auto &barrier = pImageMemoryBarriers[index];
3737 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
3738 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
3739 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06003740 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07003741 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003742 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003743 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003744 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003745 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003746 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07003747 }
3748 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003749
3750 return skip;
3751}
3752
3753void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3754 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3755 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3756 uint32_t bufferMemoryBarrierCount,
3757 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3758 uint32_t imageMemoryBarrierCount,
3759 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003760 auto *cb_access_context = GetAccessContext(commandBuffer);
3761 assert(cb_access_context);
3762 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06003763 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003764 auto access_context = cb_access_context->GetCurrentAccessContext();
3765 assert(access_context);
3766 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003767
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003768 auto src = SyncExecScope::MakeSrc(cb_access_context->GetQueueFlags(), srcStageMask);
3769 auto dst = SyncExecScope::MakeDst(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf89311b42020-09-29 16:28:47 -06003770
3771 // These two apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
3772 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
3773 // of the barriers is maintained.
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003774 ApplyBufferBarriers(access_context, src, dst, bufferMemoryBarrierCount, pBufferMemoryBarriers);
3775 ApplyImageBarriers(access_context, src, dst, imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003776
John Zulauf89311b42020-09-29 16:28:47 -06003777 // Apply the global barriers last as is it walks all memory, it can also clean up the "pending" state without requiring an
3778 // additional pass, updating the dependency chains *last* as it goes along.
3779 // This is needed to guarantee order independence of the three lists.
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003780 ApplyGlobalBarriers(access_context, src, dst, memoryBarrierCount, pMemoryBarriers, tag);
John Zulauf4a6105a2020-11-17 15:11:05 -07003781
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003782 cb_access_context->ApplyGlobalBarriersToEvents(src, dst);
John Zulauf9cb530d2019-09-30 14:14:10 -06003783}
3784
3785void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3786 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3787 // The state tracker sets up the device state
3788 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3789
John Zulauf5f13a792020-03-10 07:31:21 -06003790 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3791 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003792 // TODO: Find a good way to do this hooklessly.
3793 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3794 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3795 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3796
John Zulaufd1f85d42020-04-15 12:23:15 -06003797 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3798 sync_device_state->ResetCommandBufferCallback(command_buffer);
3799 });
3800 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3801 sync_device_state->FreeCommandBufferCallback(command_buffer);
3802 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003803}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003804
John Zulauf355e49b2020-04-24 15:11:15 -06003805bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003806 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003807 bool skip = false;
3808 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
3809 auto cb_context = GetAccessContext(commandBuffer);
3810
3811 if (rp_state && cb_context) {
3812 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
3813 }
3814
3815 return skip;
3816}
3817
3818bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3819 VkSubpassContents contents) const {
3820 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003821 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003822 subpass_begin_info.contents = contents;
3823 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
3824 return skip;
3825}
3826
3827bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003828 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003829 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3830 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
3831 return skip;
3832}
3833
3834bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3835 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003836 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003837 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3838 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3839 return skip;
3840}
3841
John Zulauf3d84f1b2020-03-09 13:33:25 -06003842void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3843 VkResult result) {
3844 // The state tracker sets up the command buffer state
3845 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3846
3847 // Create/initialize the structure that trackers accesses at the command buffer scope.
3848 auto cb_access_context = GetAccessContext(commandBuffer);
3849 assert(cb_access_context);
3850 cb_access_context->Reset();
3851}
3852
3853void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003854 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003855 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003856 if (cb_context) {
3857 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003858 }
3859}
3860
3861void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3862 VkSubpassContents contents) {
3863 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003864 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003865 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003866 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003867}
3868
3869void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3870 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3871 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003872 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003873}
3874
3875void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3876 const VkRenderPassBeginInfo *pRenderPassBegin,
3877 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3878 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003879 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3880}
3881
Mike Schuchardt2df08912020-12-15 16:28:09 -08003882bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3883 const VkSubpassEndInfo *pSubpassEndInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003884 bool skip = false;
3885
3886 auto cb_context = GetAccessContext(commandBuffer);
3887 assert(cb_context);
3888 auto cb_state = cb_context->GetCommandBufferState();
3889 if (!cb_state) return skip;
3890
3891 auto rp_state = cb_state->activeRenderPass;
3892 if (!rp_state) return skip;
3893
3894 skip |= cb_context->ValidateNextSubpass(func_name);
3895
3896 return skip;
3897}
3898
3899bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3900 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003901 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003902 subpass_begin_info.contents = contents;
3903 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3904 return skip;
3905}
3906
Mike Schuchardt2df08912020-12-15 16:28:09 -08003907bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3908 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003909 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3910 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3911 return skip;
3912}
3913
3914bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3915 const VkSubpassEndInfo *pSubpassEndInfo) const {
3916 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3917 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3918 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003919}
3920
3921void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003922 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003923 auto cb_context = GetAccessContext(commandBuffer);
3924 assert(cb_context);
3925 auto cb_state = cb_context->GetCommandBufferState();
3926 if (!cb_state) return;
3927
3928 auto rp_state = cb_state->activeRenderPass;
3929 if (!rp_state) return;
3930
John Zulauf355e49b2020-04-24 15:11:15 -06003931 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003932}
3933
3934void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3935 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003936 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003937 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003938 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003939}
3940
3941void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3942 const VkSubpassEndInfo *pSubpassEndInfo) {
3943 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003944 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003945}
3946
3947void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3948 const VkSubpassEndInfo *pSubpassEndInfo) {
3949 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003950 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003951}
3952
Mike Schuchardt2df08912020-12-15 16:28:09 -08003953bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003954 const char *func_name) const {
3955 bool skip = false;
3956
3957 auto cb_context = GetAccessContext(commandBuffer);
3958 assert(cb_context);
3959 auto cb_state = cb_context->GetCommandBufferState();
3960 if (!cb_state) return skip;
3961
3962 auto rp_state = cb_state->activeRenderPass;
3963 if (!rp_state) return skip;
3964
3965 skip |= cb_context->ValidateEndRenderpass(func_name);
3966 return skip;
3967}
3968
3969bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3970 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
3971 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
3972 return skip;
3973}
3974
Mike Schuchardt2df08912020-12-15 16:28:09 -08003975bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003976 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
3977 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
3978 return skip;
3979}
3980
3981bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003982 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003983 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
3984 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
3985 return skip;
3986}
3987
3988void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3989 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003990 // Resolve the all subpass contexts to the command buffer contexts
3991 auto cb_context = GetAccessContext(commandBuffer);
3992 assert(cb_context);
3993 auto cb_state = cb_context->GetCommandBufferState();
3994 if (!cb_state) return;
3995
locke-lunargaecf2152020-05-12 17:15:41 -06003996 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06003997 if (!rp_state) return;
3998
John Zulauf355e49b2020-04-24 15:11:15 -06003999 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06004000}
John Zulauf3d84f1b2020-03-09 13:33:25 -06004001
John Zulauf33fc1d52020-07-17 11:01:10 -06004002// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
4003// updates to a resource which do not conflict at the byte level.
4004// TODO: Revisit this rule to see if it needs to be tighter or looser
4005// TODO: Add programatic control over suppression heuristics
4006bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
4007 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
4008}
4009
John Zulauf3d84f1b2020-03-09 13:33:25 -06004010void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004011 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06004012 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004013}
4014
4015void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06004016 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06004017 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004018}
4019
4020void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06004021 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06004022 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004023}
locke-lunarga19c71d2020-03-02 18:17:04 -07004024
Jeff Leger178b1e52020-10-05 12:22:23 -04004025template <typename BufferImageCopyRegionType>
4026bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4027 VkImageLayout dstImageLayout, uint32_t regionCount,
4028 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004029 bool skip = false;
4030 const auto *cb_access_context = GetAccessContext(commandBuffer);
4031 assert(cb_access_context);
4032 if (!cb_access_context) return skip;
4033
Jeff Leger178b1e52020-10-05 12:22:23 -04004034 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4035 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
4036
locke-lunarga19c71d2020-03-02 18:17:04 -07004037 const auto *context = cb_access_context->GetCurrentAccessContext();
4038 assert(context);
4039 if (!context) return skip;
4040
4041 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07004042 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4043
4044 for (uint32_t region = 0; region < regionCount; region++) {
4045 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06004046 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004047 ResourceAccessRange src_range =
4048 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004049 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07004050 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06004051 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06004052 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004053 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004054 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004055 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004056 }
4057 }
4058 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004059 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004060 copy_region.imageOffset, copy_region.imageExtent);
4061 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004062 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004063 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004064 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004065 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004066 }
4067 if (skip) break;
4068 }
4069 if (skip) break;
4070 }
4071 return skip;
4072}
4073
Jeff Leger178b1e52020-10-05 12:22:23 -04004074bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4075 VkImageLayout dstImageLayout, uint32_t regionCount,
4076 const VkBufferImageCopy *pRegions) const {
4077 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
4078 COPY_COMMAND_VERSION_1);
4079}
4080
4081bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4082 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
4083 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4084 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4085 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4086}
4087
4088template <typename BufferImageCopyRegionType>
4089void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4090 VkImageLayout dstImageLayout, uint32_t regionCount,
4091 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004092 auto *cb_access_context = GetAccessContext(commandBuffer);
4093 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004094
4095 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4096 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
4097
4098 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004099 auto *context = cb_access_context->GetCurrentAccessContext();
4100 assert(context);
4101
4102 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06004103 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004104
4105 for (uint32_t region = 0; region < regionCount; region++) {
4106 const auto &copy_region = pRegions[region];
4107 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004108 ResourceAccessRange src_range =
4109 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004110 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004111 }
4112 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004113 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06004114 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004115 }
4116 }
4117}
4118
Jeff Leger178b1e52020-10-05 12:22:23 -04004119void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4120 VkImageLayout dstImageLayout, uint32_t regionCount,
4121 const VkBufferImageCopy *pRegions) {
4122 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
4123 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4124}
4125
4126void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4127 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4128 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4129 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4130 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4131 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4132}
4133
4134template <typename BufferImageCopyRegionType>
4135bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4136 VkBuffer dstBuffer, uint32_t regionCount,
4137 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004138 bool skip = false;
4139 const auto *cb_access_context = GetAccessContext(commandBuffer);
4140 assert(cb_access_context);
4141 if (!cb_access_context) return skip;
4142
Jeff Leger178b1e52020-10-05 12:22:23 -04004143 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4144 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
4145
locke-lunarga19c71d2020-03-02 18:17:04 -07004146 const auto *context = cb_access_context->GetCurrentAccessContext();
4147 assert(context);
4148 if (!context) return skip;
4149
4150 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4151 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4152 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
4153 for (uint32_t region = 0; region < regionCount; region++) {
4154 const auto &copy_region = pRegions[region];
4155 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004156 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004157 copy_region.imageOffset, copy_region.imageExtent);
4158 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004159 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004160 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004161 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004162 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004163 }
4164 }
4165 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06004166 ResourceAccessRange dst_range =
4167 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004168 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07004169 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004170 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004171 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004172 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004173 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004174 }
4175 }
4176 if (skip) break;
4177 }
4178 return skip;
4179}
4180
Jeff Leger178b1e52020-10-05 12:22:23 -04004181bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4182 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4183 const VkBufferImageCopy *pRegions) const {
4184 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
4185 COPY_COMMAND_VERSION_1);
4186}
4187
4188bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4189 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4190 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4191 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4192 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4193}
4194
4195template <typename BufferImageCopyRegionType>
4196void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4197 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
4198 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004199 auto *cb_access_context = GetAccessContext(commandBuffer);
4200 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004201
4202 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4203 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
4204
4205 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004206 auto *context = cb_access_context->GetCurrentAccessContext();
4207 assert(context);
4208
4209 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004210 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4211 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 -06004212 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004213
4214 for (uint32_t region = 0; region < regionCount; region++) {
4215 const auto &copy_region = pRegions[region];
4216 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004217 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06004218 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004219 }
4220 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004221 ResourceAccessRange dst_range =
4222 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004223 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004224 }
4225 }
4226}
4227
Jeff Leger178b1e52020-10-05 12:22:23 -04004228void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4229 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4230 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
4231 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4232}
4233
4234void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4235 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4236 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4237 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4238 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4239 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4240}
4241
4242template <typename RegionType>
4243bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4244 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4245 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004246 bool skip = false;
4247 const auto *cb_access_context = GetAccessContext(commandBuffer);
4248 assert(cb_access_context);
4249 if (!cb_access_context) return skip;
4250
4251 const auto *context = cb_access_context->GetCurrentAccessContext();
4252 assert(context);
4253 if (!context) return skip;
4254
4255 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4256 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4257
4258 for (uint32_t region = 0; region < regionCount; region++) {
4259 const auto &blit_region = pRegions[region];
4260 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004261 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4262 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4263 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4264 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4265 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4266 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
4267 auto hazard =
4268 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004269 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004270 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004271 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004272 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004273 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004274 }
4275 }
4276
4277 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004278 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4279 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4280 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4281 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4282 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4283 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
4284 auto hazard =
4285 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004286 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004287 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004288 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004289 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004290 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004291 }
4292 if (skip) break;
4293 }
4294 }
4295
4296 return skip;
4297}
4298
Jeff Leger178b1e52020-10-05 12:22:23 -04004299bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4300 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4301 const VkImageBlit *pRegions, VkFilter filter) const {
4302 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4303 "vkCmdBlitImage");
4304}
4305
4306bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4307 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4308 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4309 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4310 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4311}
4312
4313template <typename RegionType>
4314void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4315 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4316 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004317 auto *cb_access_context = GetAccessContext(commandBuffer);
4318 assert(cb_access_context);
4319 auto *context = cb_access_context->GetCurrentAccessContext();
4320 assert(context);
4321
4322 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004323 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004324
4325 for (uint32_t region = 0; region < regionCount; region++) {
4326 const auto &blit_region = pRegions[region];
4327 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004328 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4329 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4330 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4331 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4332 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4333 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
4334 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004335 }
4336 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004337 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4338 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4339 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4340 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4341 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4342 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
4343 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004344 }
4345 }
4346}
locke-lunarg36ba2592020-04-03 09:42:04 -06004347
Jeff Leger178b1e52020-10-05 12:22:23 -04004348void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4349 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4350 const VkImageBlit *pRegions, VkFilter filter) {
4351 auto *cb_access_context = GetAccessContext(commandBuffer);
4352 assert(cb_access_context);
4353 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4354 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4355 pRegions, filter);
4356 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4357}
4358
4359void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4360 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4361 auto *cb_access_context = GetAccessContext(commandBuffer);
4362 assert(cb_access_context);
4363 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4364 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4365 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4366 pBlitImageInfo->filter, tag);
4367}
4368
locke-lunarg61870c22020-06-09 14:51:50 -06004369bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
4370 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
4371 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004372 bool skip = false;
4373 if (drawCount == 0) return skip;
4374
4375 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4376 VkDeviceSize size = struct_size;
4377 if (drawCount == 1 || stride == size) {
4378 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004379 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004380 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4381 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004382 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004383 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004384 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06004385 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004386 }
4387 } else {
4388 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004389 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004390 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4391 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004392 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004393 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4394 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
4395 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004396 break;
4397 }
4398 }
4399 }
4400 return skip;
4401}
4402
locke-lunarg61870c22020-06-09 14:51:50 -06004403void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
4404 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4405 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06004406 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4407 VkDeviceSize size = struct_size;
4408 if (drawCount == 1 || stride == size) {
4409 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004410 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004411 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
4412 } else {
4413 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004414 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004415 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
4416 }
4417 }
4418}
4419
locke-lunarg61870c22020-06-09 14:51:50 -06004420bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
4421 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004422 bool skip = false;
4423
4424 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004425 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004426 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4427 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004428 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004429 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004430 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06004431 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004432 }
4433 return skip;
4434}
4435
locke-lunarg61870c22020-06-09 14:51:50 -06004436void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06004437 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004438 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004439 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
4440}
4441
locke-lunarg36ba2592020-04-03 09:42:04 -06004442bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004443 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004444 const auto *cb_access_context = GetAccessContext(commandBuffer);
4445 assert(cb_access_context);
4446 if (!cb_access_context) return skip;
4447
locke-lunarg61870c22020-06-09 14:51:50 -06004448 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004449 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004450}
4451
4452void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004453 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004454 auto *cb_access_context = GetAccessContext(commandBuffer);
4455 assert(cb_access_context);
4456 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004457
locke-lunarg61870c22020-06-09 14:51:50 -06004458 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004459}
locke-lunarge1a67022020-04-29 00:15:36 -06004460
4461bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004462 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004463 const auto *cb_access_context = GetAccessContext(commandBuffer);
4464 assert(cb_access_context);
4465 if (!cb_access_context) return skip;
4466
4467 const auto *context = cb_access_context->GetCurrentAccessContext();
4468 assert(context);
4469 if (!context) return skip;
4470
locke-lunarg61870c22020-06-09 14:51:50 -06004471 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
4472 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
4473 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004474 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004475}
4476
4477void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004478 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004479 auto *cb_access_context = GetAccessContext(commandBuffer);
4480 assert(cb_access_context);
4481 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4482 auto *context = cb_access_context->GetCurrentAccessContext();
4483 assert(context);
4484
locke-lunarg61870c22020-06-09 14:51:50 -06004485 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4486 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004487}
4488
4489bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4490 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004491 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004492 const auto *cb_access_context = GetAccessContext(commandBuffer);
4493 assert(cb_access_context);
4494 if (!cb_access_context) return skip;
4495
locke-lunarg61870c22020-06-09 14:51:50 -06004496 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4497 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4498 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004499 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004500}
4501
4502void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4503 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004504 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004505 auto *cb_access_context = GetAccessContext(commandBuffer);
4506 assert(cb_access_context);
4507 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004508
locke-lunarg61870c22020-06-09 14:51:50 -06004509 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4510 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4511 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004512}
4513
4514bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4515 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004516 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004517 const auto *cb_access_context = GetAccessContext(commandBuffer);
4518 assert(cb_access_context);
4519 if (!cb_access_context) return skip;
4520
locke-lunarg61870c22020-06-09 14:51:50 -06004521 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4522 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4523 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004524 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004525}
4526
4527void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4528 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004529 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004530 auto *cb_access_context = GetAccessContext(commandBuffer);
4531 assert(cb_access_context);
4532 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004533
locke-lunarg61870c22020-06-09 14:51:50 -06004534 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4535 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4536 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004537}
4538
4539bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4540 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004541 bool skip = false;
4542 if (drawCount == 0) return skip;
4543
locke-lunargff255f92020-05-13 18:53:52 -06004544 const auto *cb_access_context = GetAccessContext(commandBuffer);
4545 assert(cb_access_context);
4546 if (!cb_access_context) return skip;
4547
4548 const auto *context = cb_access_context->GetCurrentAccessContext();
4549 assert(context);
4550 if (!context) return skip;
4551
locke-lunarg61870c22020-06-09 14:51:50 -06004552 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4553 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
4554 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
4555 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004556
4557 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4558 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4559 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004560 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004561 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004562}
4563
4564void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4565 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004566 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004567 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004568 auto *cb_access_context = GetAccessContext(commandBuffer);
4569 assert(cb_access_context);
4570 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4571 auto *context = cb_access_context->GetCurrentAccessContext();
4572 assert(context);
4573
locke-lunarg61870c22020-06-09 14:51:50 -06004574 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4575 cb_access_context->RecordDrawSubpassAttachment(tag);
4576 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004577
4578 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4579 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4580 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004581 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004582}
4583
4584bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4585 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004586 bool skip = false;
4587 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004588 const auto *cb_access_context = GetAccessContext(commandBuffer);
4589 assert(cb_access_context);
4590 if (!cb_access_context) return skip;
4591
4592 const auto *context = cb_access_context->GetCurrentAccessContext();
4593 assert(context);
4594 if (!context) return skip;
4595
locke-lunarg61870c22020-06-09 14:51:50 -06004596 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4597 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
4598 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
4599 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004600
4601 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4602 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4603 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004604 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004605 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004606}
4607
4608void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4609 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004610 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004611 auto *cb_access_context = GetAccessContext(commandBuffer);
4612 assert(cb_access_context);
4613 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4614 auto *context = cb_access_context->GetCurrentAccessContext();
4615 assert(context);
4616
locke-lunarg61870c22020-06-09 14:51:50 -06004617 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4618 cb_access_context->RecordDrawSubpassAttachment(tag);
4619 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004620
4621 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4622 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4623 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004624 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004625}
4626
4627bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4628 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4629 uint32_t stride, const char *function) const {
4630 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004631 const auto *cb_access_context = GetAccessContext(commandBuffer);
4632 assert(cb_access_context);
4633 if (!cb_access_context) return skip;
4634
4635 const auto *context = cb_access_context->GetCurrentAccessContext();
4636 assert(context);
4637 if (!context) return skip;
4638
locke-lunarg61870c22020-06-09 14:51:50 -06004639 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4640 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
4641 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
4642 function);
4643 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004644
4645 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4646 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4647 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004648 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004649 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004650}
4651
4652bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4653 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4654 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004655 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4656 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004657}
4658
4659void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4660 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4661 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004662 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4663 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004664 auto *cb_access_context = GetAccessContext(commandBuffer);
4665 assert(cb_access_context);
4666 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
4667 auto *context = cb_access_context->GetCurrentAccessContext();
4668 assert(context);
4669
locke-lunarg61870c22020-06-09 14:51:50 -06004670 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4671 cb_access_context->RecordDrawSubpassAttachment(tag);
4672 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4673 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004674
4675 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4676 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4677 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004678 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004679}
4680
4681bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4682 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4683 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004684 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4685 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004686}
4687
4688void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4689 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4690 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004691 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4692 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004693 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004694}
4695
4696bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4697 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4698 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004699 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4700 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004701}
4702
4703void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4704 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4705 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004706 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4707 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004708 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4709}
4710
4711bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4712 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4713 uint32_t stride, const char *function) const {
4714 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004715 const auto *cb_access_context = GetAccessContext(commandBuffer);
4716 assert(cb_access_context);
4717 if (!cb_access_context) return skip;
4718
4719 const auto *context = cb_access_context->GetCurrentAccessContext();
4720 assert(context);
4721 if (!context) return skip;
4722
locke-lunarg61870c22020-06-09 14:51:50 -06004723 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4724 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
4725 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
4726 stride, function);
4727 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004728
4729 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4730 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4731 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004732 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004733 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004734}
4735
4736bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4737 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4738 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004739 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4740 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004741}
4742
4743void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4744 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4745 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004746 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4747 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004748 auto *cb_access_context = GetAccessContext(commandBuffer);
4749 assert(cb_access_context);
4750 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4751 auto *context = cb_access_context->GetCurrentAccessContext();
4752 assert(context);
4753
locke-lunarg61870c22020-06-09 14:51:50 -06004754 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4755 cb_access_context->RecordDrawSubpassAttachment(tag);
4756 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4757 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004758
4759 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4760 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004761 // We will update the index and vertex buffer in SubmitQueue in the future.
4762 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004763}
4764
4765bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4766 VkDeviceSize offset, VkBuffer countBuffer,
4767 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4768 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004769 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4770 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004771}
4772
4773void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4774 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4775 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004776 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4777 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004778 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4779}
4780
4781bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4782 VkDeviceSize offset, VkBuffer countBuffer,
4783 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4784 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004785 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4786 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004787}
4788
4789void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4790 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4791 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004792 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4793 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004794 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4795}
4796
4797bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4798 const VkClearColorValue *pColor, uint32_t rangeCount,
4799 const VkImageSubresourceRange *pRanges) const {
4800 bool skip = false;
4801 const auto *cb_access_context = GetAccessContext(commandBuffer);
4802 assert(cb_access_context);
4803 if (!cb_access_context) return skip;
4804
4805 const auto *context = cb_access_context->GetCurrentAccessContext();
4806 assert(context);
4807 if (!context) return skip;
4808
4809 const auto *image_state = Get<IMAGE_STATE>(image);
4810
4811 for (uint32_t index = 0; index < rangeCount; index++) {
4812 const auto &range = pRanges[index];
4813 if (image_state) {
4814 auto hazard =
4815 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4816 if (hazard.hazard) {
4817 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004818 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004819 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004820 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004821 }
4822 }
4823 }
4824 return skip;
4825}
4826
4827void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4828 const VkClearColorValue *pColor, uint32_t rangeCount,
4829 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004830 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004831 auto *cb_access_context = GetAccessContext(commandBuffer);
4832 assert(cb_access_context);
4833 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4834 auto *context = cb_access_context->GetCurrentAccessContext();
4835 assert(context);
4836
4837 const auto *image_state = Get<IMAGE_STATE>(image);
4838
4839 for (uint32_t index = 0; index < rangeCount; index++) {
4840 const auto &range = pRanges[index];
4841 if (image_state) {
4842 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4843 tag);
4844 }
4845 }
4846}
4847
4848bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4849 VkImageLayout imageLayout,
4850 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4851 const VkImageSubresourceRange *pRanges) const {
4852 bool skip = false;
4853 const auto *cb_access_context = GetAccessContext(commandBuffer);
4854 assert(cb_access_context);
4855 if (!cb_access_context) return skip;
4856
4857 const auto *context = cb_access_context->GetCurrentAccessContext();
4858 assert(context);
4859 if (!context) return skip;
4860
4861 const auto *image_state = Get<IMAGE_STATE>(image);
4862
4863 for (uint32_t index = 0; index < rangeCount; index++) {
4864 const auto &range = pRanges[index];
4865 if (image_state) {
4866 auto hazard =
4867 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4868 if (hazard.hazard) {
4869 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004870 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004871 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004872 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004873 }
4874 }
4875 }
4876 return skip;
4877}
4878
4879void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4880 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4881 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004882 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004883 auto *cb_access_context = GetAccessContext(commandBuffer);
4884 assert(cb_access_context);
4885 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4886 auto *context = cb_access_context->GetCurrentAccessContext();
4887 assert(context);
4888
4889 const auto *image_state = Get<IMAGE_STATE>(image);
4890
4891 for (uint32_t index = 0; index < rangeCount; index++) {
4892 const auto &range = pRanges[index];
4893 if (image_state) {
4894 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4895 tag);
4896 }
4897 }
4898}
4899
4900bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4901 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4902 VkDeviceSize dstOffset, VkDeviceSize stride,
4903 VkQueryResultFlags flags) const {
4904 bool skip = false;
4905 const auto *cb_access_context = GetAccessContext(commandBuffer);
4906 assert(cb_access_context);
4907 if (!cb_access_context) return skip;
4908
4909 const auto *context = cb_access_context->GetCurrentAccessContext();
4910 assert(context);
4911 if (!context) return skip;
4912
4913 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4914
4915 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004916 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004917 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4918 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004919 skip |=
4920 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4921 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4922 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004923 }
4924 }
locke-lunargff255f92020-05-13 18:53:52 -06004925
4926 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004927 return skip;
4928}
4929
4930void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4931 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4932 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004933 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4934 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004935 auto *cb_access_context = GetAccessContext(commandBuffer);
4936 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004937 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004938 auto *context = cb_access_context->GetCurrentAccessContext();
4939 assert(context);
4940
4941 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4942
4943 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004944 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004945 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4946 }
locke-lunargff255f92020-05-13 18:53:52 -06004947
4948 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004949}
4950
4951bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4952 VkDeviceSize size, uint32_t data) const {
4953 bool skip = false;
4954 const auto *cb_access_context = GetAccessContext(commandBuffer);
4955 assert(cb_access_context);
4956 if (!cb_access_context) return skip;
4957
4958 const auto *context = cb_access_context->GetCurrentAccessContext();
4959 assert(context);
4960 if (!context) return skip;
4961
4962 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4963
4964 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004965 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004966 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4967 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004968 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004969 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004970 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004971 }
4972 }
4973 return skip;
4974}
4975
4976void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4977 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004978 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004979 auto *cb_access_context = GetAccessContext(commandBuffer);
4980 assert(cb_access_context);
4981 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4982 auto *context = cb_access_context->GetCurrentAccessContext();
4983 assert(context);
4984
4985 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4986
4987 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004988 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004989 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4990 }
4991}
4992
4993bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4994 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4995 const VkImageResolve *pRegions) const {
4996 bool skip = false;
4997 const auto *cb_access_context = GetAccessContext(commandBuffer);
4998 assert(cb_access_context);
4999 if (!cb_access_context) return skip;
5000
5001 const auto *context = cb_access_context->GetCurrentAccessContext();
5002 assert(context);
5003 if (!context) return skip;
5004
5005 const auto *src_image = Get<IMAGE_STATE>(srcImage);
5006 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
5007
5008 for (uint32_t region = 0; region < regionCount; region++) {
5009 const auto &resolve_region = pRegions[region];
5010 if (src_image) {
5011 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5012 resolve_region.srcOffset, resolve_region.extent);
5013 if (hazard.hazard) {
5014 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005015 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005016 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06005017 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005018 }
5019 }
5020
5021 if (dst_image) {
5022 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5023 resolve_region.dstOffset, resolve_region.extent);
5024 if (hazard.hazard) {
5025 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005026 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005027 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06005028 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005029 }
5030 if (skip) break;
5031 }
5032 }
5033
5034 return skip;
5035}
5036
5037void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5038 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5039 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005040 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
5041 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06005042 auto *cb_access_context = GetAccessContext(commandBuffer);
5043 assert(cb_access_context);
5044 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
5045 auto *context = cb_access_context->GetCurrentAccessContext();
5046 assert(context);
5047
5048 auto *src_image = Get<IMAGE_STATE>(srcImage);
5049 auto *dst_image = Get<IMAGE_STATE>(dstImage);
5050
5051 for (uint32_t region = 0; region < regionCount; region++) {
5052 const auto &resolve_region = pRegions[region];
5053 if (src_image) {
5054 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5055 resolve_region.srcOffset, resolve_region.extent, tag);
5056 }
5057 if (dst_image) {
5058 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5059 resolve_region.dstOffset, resolve_region.extent, tag);
5060 }
5061 }
5062}
5063
Jeff Leger178b1e52020-10-05 12:22:23 -04005064bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5065 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
5066 bool skip = false;
5067 const auto *cb_access_context = GetAccessContext(commandBuffer);
5068 assert(cb_access_context);
5069 if (!cb_access_context) return skip;
5070
5071 const auto *context = cb_access_context->GetCurrentAccessContext();
5072 assert(context);
5073 if (!context) return skip;
5074
5075 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5076 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5077
5078 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5079 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5080 if (src_image) {
5081 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5082 resolve_region.srcOffset, resolve_region.extent);
5083 if (hazard.hazard) {
5084 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
5085 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
5086 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
5087 region, string_UsageTag(hazard).c_str());
5088 }
5089 }
5090
5091 if (dst_image) {
5092 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5093 resolve_region.dstOffset, resolve_region.extent);
5094 if (hazard.hazard) {
5095 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
5096 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
5097 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
5098 region, string_UsageTag(hazard).c_str());
5099 }
5100 if (skip) break;
5101 }
5102 }
5103
5104 return skip;
5105}
5106
5107void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5108 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5109 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5110 auto *cb_access_context = GetAccessContext(commandBuffer);
5111 assert(cb_access_context);
5112 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
5113 auto *context = cb_access_context->GetCurrentAccessContext();
5114 assert(context);
5115
5116 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5117 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5118
5119 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5120 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5121 if (src_image) {
5122 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5123 resolve_region.srcOffset, resolve_region.extent, tag);
5124 }
5125 if (dst_image) {
5126 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5127 resolve_region.dstOffset, resolve_region.extent, tag);
5128 }
5129 }
5130}
5131
locke-lunarge1a67022020-04-29 00:15:36 -06005132bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5133 VkDeviceSize dataSize, const void *pData) const {
5134 bool skip = false;
5135 const auto *cb_access_context = GetAccessContext(commandBuffer);
5136 assert(cb_access_context);
5137 if (!cb_access_context) return skip;
5138
5139 const auto *context = cb_access_context->GetCurrentAccessContext();
5140 assert(context);
5141 if (!context) return skip;
5142
5143 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5144
5145 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005146 // VK_WHOLE_SIZE not allowed
5147 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06005148 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
5149 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005150 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005151 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06005152 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005153 }
5154 }
5155 return skip;
5156}
5157
5158void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5159 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005160 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005161 auto *cb_access_context = GetAccessContext(commandBuffer);
5162 assert(cb_access_context);
5163 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5164 auto *context = cb_access_context->GetCurrentAccessContext();
5165 assert(context);
5166
5167 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5168
5169 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005170 // VK_WHOLE_SIZE not allowed
5171 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06005172 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
5173 }
5174}
locke-lunargff255f92020-05-13 18:53:52 -06005175
5176bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5177 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5178 bool skip = false;
5179 const auto *cb_access_context = GetAccessContext(commandBuffer);
5180 assert(cb_access_context);
5181 if (!cb_access_context) return skip;
5182
5183 const auto *context = cb_access_context->GetCurrentAccessContext();
5184 assert(context);
5185 if (!context) return skip;
5186
5187 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5188
5189 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005190 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06005191 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
5192 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005193 skip |=
5194 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5195 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
5196 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005197 }
5198 }
5199 return skip;
5200}
5201
5202void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5203 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005204 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005205 auto *cb_access_context = GetAccessContext(commandBuffer);
5206 assert(cb_access_context);
5207 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5208 auto *context = cb_access_context->GetCurrentAccessContext();
5209 assert(context);
5210
5211 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5212
5213 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005214 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06005215 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
5216 }
5217}
John Zulauf49beb112020-11-04 16:06:31 -07005218
5219bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5220 bool skip = false;
5221 const auto *cb_context = GetAccessContext(commandBuffer);
5222 assert(cb_context);
5223 if (!cb_context) return skip;
5224
5225 return cb_context->ValidateSetEvent(commandBuffer, event, stageMask);
5226}
5227
5228void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5229 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5230 auto *cb_context = GetAccessContext(commandBuffer);
5231 assert(cb_context);
5232 if (!cb_context) return;
John Zulauf4a6105a2020-11-17 15:11:05 -07005233 const auto tag = cb_context->NextCommandTag(CMD_SETEVENT);
5234 cb_context->RecordSetEvent(commandBuffer, event, stageMask, tag);
John Zulauf49beb112020-11-04 16:06:31 -07005235}
5236
5237bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5238 VkPipelineStageFlags stageMask) const {
5239 bool skip = false;
5240 const auto *cb_context = GetAccessContext(commandBuffer);
5241 assert(cb_context);
5242 if (!cb_context) return skip;
5243
5244 return cb_context->ValidateResetEvent(commandBuffer, event, stageMask);
5245}
5246
5247void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5248 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5249 auto *cb_context = GetAccessContext(commandBuffer);
5250 assert(cb_context);
5251 if (!cb_context) return;
5252
5253 cb_context->RecordResetEvent(commandBuffer, event, stageMask);
5254}
5255
5256bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5257 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5258 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5259 uint32_t bufferMemoryBarrierCount,
5260 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5261 uint32_t imageMemoryBarrierCount,
5262 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5263 bool skip = false;
5264 const auto *cb_context = GetAccessContext(commandBuffer);
5265 assert(cb_context);
5266 if (!cb_context) return skip;
5267
John Zulauf4a6105a2020-11-17 15:11:05 -07005268 return cb_context->ValidateWaitEvents(eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount, pMemoryBarriers,
5269 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
John Zulauf49beb112020-11-04 16:06:31 -07005270 pImageMemoryBarriers);
5271}
5272
5273void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5274 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5275 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5276 uint32_t bufferMemoryBarrierCount,
5277 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5278 uint32_t imageMemoryBarrierCount,
5279 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5280 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5281 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5282 imageMemoryBarrierCount, pImageMemoryBarriers);
5283
5284 auto *cb_context = GetAccessContext(commandBuffer);
5285 assert(cb_context);
5286 if (!cb_context) return;
5287
John Zulauf4a6105a2020-11-17 15:11:05 -07005288 const auto tag = cb_context->NextCommandTag(CMD_WAITEVENTS);
John Zulauf49beb112020-11-04 16:06:31 -07005289 cb_context->RecordWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5290 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
John Zulauf4a6105a2020-11-17 15:11:05 -07005291 pImageMemoryBarriers, tag);
5292}
5293
5294void SyncEventState::ResetFirstScope() {
5295 for (const auto address_type : kAddressTypes) {
5296 first_scope[static_cast<size_t>(address_type)].clear();
5297 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005298 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07005299}
5300
5301// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
5302SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(VkPipelineStageFlags srcStageMask) const {
5303 IgnoreReason reason = NotIgnored;
5304
5305 if (last_command == CMD_RESETEVENT && !HasBarrier(0U, 0U)) {
5306 reason = ResetWaitRace;
5307 } else if (unsynchronized_set) {
5308 reason = SetRace;
5309 } else {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005310 const VkPipelineStageFlags missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005311 if (missing_bits) reason = MissingStageBits;
5312 }
5313
5314 return reason;
5315}
5316
5317bool SyncEventState::HasBarrier(VkPipelineStageFlags stageMask, VkPipelineStageFlags exec_scope_arg) const {
5318 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5319 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5320 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005321}