blob: d1f3d1c45cbabae7ae1a10d6a4e7eb1e4a4f15d3 [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());
John Zulauff4aecca2021-01-05 16:21:58 -0700155 const auto seq_id = tag.GetSequenceId();
156 out << ", seq_no: " << seq_id.seq_num << ", reset_no: " << seq_id.reset_count;
157 if (seq_id.sub_command != 0) {
158 out << ", subcmd: " << seq_id.sub_command;
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700159 }
160 return out.str();
161}
162
John Zulauf37ceaed2020-07-03 16:18:15 -0600163static std::string string_UsageTag(const HazardResult &hazard) {
164 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600165 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
166 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600167 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600168 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
169 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf59e25072020-07-17 10:55:21 -0600170 out << "(usage: " << usage_info.name << ", prior_usage: " << stage_access_name;
171 if (IsHazardVsRead(hazard.hazard)) {
172 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
173 out << ", read_barriers: " << string_VkPipelineStageFlags(barriers);
174 } else {
175 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
176 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
177 }
178
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700179 out << ", " << string_UsageTag(tag) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600180 return out.str();
181}
182
John Zulaufd14743a2020-07-03 09:42:39 -0600183// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
184// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
185// also reflects this special case for read hazard detection (using access instead of exec scope)
John Zulaufb027cdb2020-05-21 14:25:22 -0600186static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700187static const SyncStageAccessFlags kColorAttachmentAccessScope =
188 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
189 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
190 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
191 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600192static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
193 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700194static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
195 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
196 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
197 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulauf8e3c3e92021-01-06 11:19:36 -0700198static constexpr VkPipelineStageFlags kRasterAttachmentExecScope = kDepthStencilAttachmentExecScope | kColorAttachmentExecScope;
199static const SyncStageAccessFlags kRasterAttachmentAccessScope = kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope;
John Zulaufb027cdb2020-05-21 14:25:22 -0600200
John Zulauf8e3c3e92021-01-06 11:19:36 -0700201ResourceAccessState::OrderingBarriers ResourceAccessState::kOrderingRules = {
202 {{0U, SyncStageAccessFlags()},
203 {kColorAttachmentExecScope, kColorAttachmentAccessScope},
204 {kDepthStencilAttachmentExecScope, kDepthStencilAttachmentAccessScope},
205 {kRasterAttachmentExecScope, kRasterAttachmentAccessScope}}};
206
John Zulauf7635de32020-05-29 17:14:15 -0600207// 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 -0600208static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600209
John Zulaufb02c1eb2020-10-06 16:33:36 -0600210static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) {
211 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
212}
213
214static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
215
locke-lunarg3c038002020-04-30 23:08:08 -0600216inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
217 if (size == VK_WHOLE_SIZE) {
218 return (whole_size - offset);
219 }
220 return size;
221}
222
John Zulauf3e86bf02020-09-12 10:47:57 -0600223static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
224 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
225}
226
John Zulauf16adfc92020-04-08 10:28:33 -0600227template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600228static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600229 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
230}
231
John Zulauf355e49b2020-04-24 15:11:15 -0600232static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600233
John Zulauf3e86bf02020-09-12 10:47:57 -0600234static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
235 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
236}
237
238static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
239 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
240}
241
John Zulauf4a6105a2020-11-17 15:11:05 -0700242// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
243//
John Zulauf10f1f522020-12-18 12:00:35 -0700244// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
245//
John Zulauf4a6105a2020-11-17 15:11:05 -0700246// Usage:
247// Constructor() -- initializes the generator to point to the begin of the space declared.
248// * -- the current range of the generator empty signfies end
249// ++ -- advance to the next non-empty range (or end)
250
251// A wrapper for a single range with the same semantics as the actual generators below
252template <typename KeyType>
253class SingleRangeGenerator {
254 public:
255 SingleRangeGenerator(const KeyType &range) : current_(range) {}
256 KeyType &operator*() const { return *current_; }
257 KeyType *operator->() const { return &*current_; }
258 SingleRangeGenerator &operator++() {
259 current_ = KeyType(); // just one real range
260 return *this;
261 }
262
263 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
264
265 private:
266 SingleRangeGenerator() = default;
267 const KeyType range_;
268 KeyType current_;
269};
270
271// Generate the ranges that are the intersection of range and the entries in the FilterMap
272template <typename FilterMap, typename KeyType = typename FilterMap::key_type>
273class FilteredRangeGenerator {
274 public:
275 FilteredRangeGenerator(const FilterMap &filter, const KeyType &range)
276 : range_(range), filter_(&filter), filter_pos_(), current_() {
277 SeekBegin();
278 }
279 const KeyType &operator*() const { return current_; }
280 const KeyType *operator->() const { return &current_; }
281 FilteredRangeGenerator &operator++() {
282 ++filter_pos_;
283 UpdateCurrent();
284 return *this;
285 }
286
287 bool operator==(const FilteredRangeGenerator &other) const { return current_ == other.current_; }
288
289 private:
290 FilteredRangeGenerator() = default;
291 void UpdateCurrent() {
292 if (filter_pos_ != filter_->cend()) {
293 current_ = range_ & filter_pos_->first;
294 } else {
295 current_ = KeyType();
296 }
297 }
298 void SeekBegin() {
299 filter_pos_ = filter_->lower_bound(range_);
300 UpdateCurrent();
301 }
302 const KeyType range_;
303 const FilterMap *filter_;
304 typename FilterMap::const_iterator filter_pos_;
305 KeyType current_;
306};
307using EventSimpleRangeGenerator = FilteredRangeGenerator<SyncEventState::ScopeMap>;
308
309// Templated to allow for different Range generators or map sources...
310
311// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulauf4a6105a2020-11-17 15:11:05 -0700312template <typename FilterMap, typename RangeGen, typename KeyType = typename FilterMap::key_type>
313class FilteredGeneratorGenerator {
314 public:
315 FilteredGeneratorGenerator(const FilterMap &filter, RangeGen &gen) : filter_(&filter), gen_(&gen), filter_pos_(), current_() {
316 SeekBegin();
317 }
318 const KeyType &operator*() const { return current_; }
319 const KeyType *operator->() const { return &current_; }
320 FilteredGeneratorGenerator &operator++() {
321 KeyType gen_range = GenRange();
322 KeyType filter_range = FilterRange();
323 current_ = KeyType();
324 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
325 if (gen_range.end > filter_range.end) {
326 // if the generated range is beyond the filter_range, advance the filter range
327 filter_range = AdvanceFilter();
328 } else {
329 gen_range = AdvanceGen();
330 }
331 current_ = gen_range & filter_range;
332 }
333 return *this;
334 }
335
336 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
337
338 private:
339 KeyType AdvanceFilter() {
340 ++filter_pos_;
341 auto filter_range = FilterRange();
342 if (filter_range.valid()) {
343 FastForwardGen(filter_range);
344 }
345 return filter_range;
346 }
347 KeyType AdvanceGen() {
348 ++(*gen_);
349 auto gen_range = GenRange();
350 if (gen_range.valid()) {
351 FastForwardFilter(gen_range);
352 }
353 return gen_range;
354 }
355
356 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
357 KeyType GenRange() const { return *(*gen_); }
358
359 KeyType FastForwardFilter(const KeyType &range) {
360 auto filter_range = FilterRange();
361 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700362 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700363 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
364 if (retry_count < kRetryLimit) {
365 ++filter_pos_;
366 filter_range = FilterRange();
367 retry_count++;
368 } else {
369 // Okay we've tried walking, do a seek.
370 filter_pos_ = filter_->lower_bound(range);
371 break;
372 }
373 }
374 return FilterRange();
375 }
376
377 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
378 // faster.
379 KeyType FastForwardGen(const KeyType &range) {
380 auto gen_range = GenRange();
381 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
382 ++(*gen_);
383 gen_range = GenRange();
384 }
385 return gen_range;
386 }
387
388 void SeekBegin() {
389 auto gen_range = GenRange();
390 if (gen_range.empty()) {
391 current_ = KeyType();
392 filter_pos_ = filter_->cend();
393 } else {
394 filter_pos_ = filter_->lower_bound(gen_range);
395 current_ = gen_range & FilterRange();
396 }
397 }
398
399 FilteredGeneratorGenerator() = default;
400 const FilterMap *filter_;
401 RangeGen *const gen_;
402 typename FilterMap::const_iterator filter_pos_;
403 KeyType current_;
404};
405
406using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
407
John Zulauf0cb5be22020-01-23 12:18:22 -0700408// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
409VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
410 VkPipelineStageFlags expanded = stage_mask;
411 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
412 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
413 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
414 if (all_commands.first & queue_flags) {
415 expanded |= all_commands.second;
416 }
417 }
418 }
419 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
420 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
421 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
422 }
423 return expanded;
424}
425
John Zulauf36bcf6a2020-02-03 15:12:52 -0700426VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
Jeremy Gebben91c36902020-11-09 08:17:08 -0700427 const std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
John Zulauf36bcf6a2020-02-03 15:12:52 -0700428 VkPipelineStageFlags unscanned = stage_mask;
429 VkPipelineStageFlags related = 0;
Jonah Ryan-Davis185189c2020-07-14 10:28:52 -0400430 for (const auto &entry : map) {
431 const auto &stage = entry.first;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700432 if (stage & unscanned) {
433 related = related | entry.second;
434 unscanned = unscanned & ~stage;
435 if (!unscanned) break;
436 }
437 }
438 return related;
439}
440
441VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
442 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
443}
444
445VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
446 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
447}
448
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700449static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700450
John Zulauf3e86bf02020-09-12 10:47:57 -0600451ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
452 VkDeviceSize stride) {
453 VkDeviceSize range_start = offset + first_index * stride;
454 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600455 if (count == UINT32_MAX) {
456 range_size = buf_whole_size - range_start;
457 } else {
458 range_size = count * stride;
459 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600460 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600461}
462
locke-lunarg654e3692020-06-04 17:19:15 -0600463SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
464 VkShaderStageFlagBits stage_flag) {
465 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
466 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
467 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
468 }
469 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
470 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
471 assert(0);
472 }
473 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
474 return stage_access->second.uniform_read;
475 }
476
477 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
478 // Because if write hazard happens, read hazard might or might not happen.
479 // But if write hazard doesn't happen, read hazard is impossible to happen.
480 if (descriptor_data.is_writable) {
481 return stage_access->second.shader_write;
482 }
483 return stage_access->second.shader_read;
484}
485
locke-lunarg37047832020-06-12 13:44:45 -0600486bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
487 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
488 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
489 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
490 ? true
491 : false;
492}
493
494bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
495 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
496 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
497 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
498 ? true
499 : false;
500}
501
John Zulauf355e49b2020-04-24 15:11:15 -0600502// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600503template <typename Action>
504static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
505 Action &action) {
506 // At this point the "apply over range" logic only supports a single memory binding
507 if (!SimpleBinding(image_state)) return;
508 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600509 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700510 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
511 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600512 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700513 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600514 }
515}
516
John Zulauf7635de32020-05-29 17:14:15 -0600517// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
518// Used by both validation and record operations
519//
520// The signature for Action() reflect the needs of both uses.
521template <typename Action>
522void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
523 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
524 VkExtent3D extent = CastTo3D(render_area.extent);
525 VkOffset3D offset = CastTo3D(render_area.offset);
526 const auto &rp_ci = rp_state.createInfo;
527 const auto *attachment_ci = rp_ci.pAttachments;
528 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
529
530 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
531 const auto *color_attachments = subpass_ci.pColorAttachments;
532 const auto *color_resolve = subpass_ci.pResolveAttachments;
533 if (color_resolve && color_attachments) {
534 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
535 const auto &color_attach = color_attachments[i].attachment;
536 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
537 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
538 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulauf8e3c3e92021-01-06 11:19:36 -0700539 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kColorAttachment, offset, extent, 0);
John Zulauf7635de32020-05-29 17:14:15 -0600540 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulauf8e3c3e92021-01-06 11:19:36 -0700541 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment, offset, extent, 0);
John Zulauf7635de32020-05-29 17:14:15 -0600542 }
543 }
544 }
545
546 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700547 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600548 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
549 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
550 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
551 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
552 const auto src_ci = attachment_ci[src_at];
553 // The formats are required to match so we can pick either
554 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
555 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
556 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
557 VkImageAspectFlags aspect_mask = 0u;
558
559 // Figure out which aspects are actually touched during resolve operations
560 const char *aspect_string = nullptr;
561 if (resolve_depth && resolve_stencil) {
562 // Validate all aspects together
563 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
564 aspect_string = "depth/stencil";
565 } else if (resolve_depth) {
566 // Validate depth only
567 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
568 aspect_string = "depth";
569 } else if (resolve_stencil) {
570 // Validate all stencil only
571 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
572 aspect_string = "stencil";
573 }
574
575 if (aspect_mask) {
576 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
John Zulauf8e3c3e92021-01-06 11:19:36 -0700577 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster, offset, extent, aspect_mask);
John Zulauf7635de32020-05-29 17:14:15 -0600578 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
John Zulauf8e3c3e92021-01-06 11:19:36 -0700579 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, offset, extent, aspect_mask);
John Zulauf7635de32020-05-29 17:14:15 -0600580 }
581 }
582}
583
584// Action for validating resolve operations
585class ValidateResolveAction {
586 public:
587 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
588 const char *func_name)
589 : render_pass_(render_pass),
590 subpass_(subpass),
591 context_(context),
592 sync_state_(sync_state),
593 func_name_(func_name),
594 skip_(false) {}
595 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
John Zulauf8e3c3e92021-01-06 11:19:36 -0700596 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf7635de32020-05-29 17:14:15 -0600597 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
598 HazardResult hazard;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700599 hazard = context_.DetectHazard(view, current_usage, ordering_rule, offset, extent, aspect_mask);
John Zulauf7635de32020-05-29 17:14:15 -0600600 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600601 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
602 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600603 " to resolve attachment %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -0600604 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
John Zulauf37ceaed2020-07-03 16:18:15 -0600605 src_at, dst_at, string_UsageTag(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600606 }
607 }
608 // Providing a mechanism for the constructing caller to get the result of the validation
609 bool GetSkip() const { return skip_; }
610
611 private:
612 VkRenderPass render_pass_;
613 const uint32_t subpass_;
614 const AccessContext &context_;
615 const SyncValidator &sync_state_;
616 const char *func_name_;
617 bool skip_;
618};
619
620// Update action for resolve operations
621class UpdateStateResolveAction {
622 public:
623 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
624 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
John Zulauf8e3c3e92021-01-06 11:19:36 -0700625 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf7635de32020-05-29 17:14:15 -0600626 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
627 // Ignores validation only arguments...
John Zulauf8e3c3e92021-01-06 11:19:36 -0700628 context_.UpdateAccessState(view, current_usage, ordering_rule, offset, extent, aspect_mask, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600629 }
630
631 private:
632 AccessContext &context_;
633 const ResourceUsageTag &tag_;
634};
635
John Zulauf59e25072020-07-17 10:55:21 -0600636void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700637 const SyncStageAccessFlags &prior_, const ResourceUsageTag &tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600638 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
639 usage_index = usage_index_;
640 hazard = hazard_;
641 prior_access = prior_;
642 tag = tag_;
643}
644
John Zulauf540266b2020-04-06 18:54:53 -0600645AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
646 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600647 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600648 Reset();
649 const auto &subpass_dep = dependencies[subpass];
650 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600651 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600652 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600653 const auto prev_pass = prev_dep.first->pass;
654 const auto &prev_barriers = prev_dep.second;
655 assert(prev_dep.second.size());
656 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
657 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700658 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600659
660 async_.reserve(subpass_dep.async.size());
661 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700662 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600663 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600664 if (subpass_dep.barrier_from_external.size()) {
665 src_external_ = TrackBack(external_context, queue_flags, subpass_dep.barrier_from_external);
John Zulaufe5da6e52020-03-18 15:32:18 -0600666 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600667 if (subpass_dep.barrier_to_external.size()) {
668 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600669 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700670}
671
John Zulauf5f13a792020-03-10 07:31:21 -0600672template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700673HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600674 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600675 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600676 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600677
678 HazardResult hazard;
679 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
680 hazard = detector.Detect(prev);
681 }
682 return hazard;
683}
684
John Zulauf4a6105a2020-11-17 15:11:05 -0700685template <typename Action>
686void AccessContext::ForAll(Action &&action) {
687 for (const auto address_type : kAddressTypes) {
688 auto &accesses = GetAccessStateMap(address_type);
689 for (const auto &access : accesses) {
690 action(address_type, access);
691 }
692 }
693}
694
John Zulauf3d84f1b2020-03-09 13:33:25 -0600695// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
696// the DAG of the contexts (for example subpasses)
697template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700698HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600699 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600700 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600701
John Zulauf1a224292020-06-30 14:52:13 -0600702 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600703 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
704 // so we'll check these first
705 for (const auto &async_context : async_) {
706 hazard = async_context->DetectAsyncHazard(type, detector, range);
707 if (hazard.hazard) return hazard;
708 }
John Zulauf5f13a792020-03-10 07:31:21 -0600709 }
710
John Zulauf1a224292020-06-30 14:52:13 -0600711 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600712
John Zulauf69133422020-05-20 14:55:53 -0600713 const auto &accesses = GetAccessStateMap(type);
714 const auto from = accesses.lower_bound(range);
715 const auto to = accesses.upper_bound(range);
716 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600717
John Zulauf69133422020-05-20 14:55:53 -0600718 for (auto pos = from; pos != to; ++pos) {
719 // Cover any leading gap, or gap between entries
720 if (detect_prev) {
721 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
722 // Cover any leading gap, or gap between entries
723 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600724 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600725 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600726 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600727 if (hazard.hazard) return hazard;
728 }
John Zulauf69133422020-05-20 14:55:53 -0600729 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
730 gap.begin = pos->first.end;
731 }
732
733 hazard = detector.Detect(pos);
734 if (hazard.hazard) return hazard;
735 }
736
737 if (detect_prev) {
738 // Detect in the trailing empty as needed
739 gap.end = range.end;
740 if (gap.non_empty()) {
741 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600742 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600743 }
744
745 return hazard;
746}
747
748// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
749template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700750HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
751 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600752 auto &accesses = GetAccessStateMap(type);
753 const auto from = accesses.lower_bound(range);
754 const auto to = accesses.upper_bound(range);
755
John Zulauf3d84f1b2020-03-09 13:33:25 -0600756 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600757 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700758 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600759 }
John Zulauf16adfc92020-04-08 10:28:33 -0600760
John Zulauf3d84f1b2020-03-09 13:33:25 -0600761 return hazard;
762}
763
John Zulaufb02c1eb2020-10-06 16:33:36 -0600764struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700765 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600766 void operator()(ResourceAccessState *access) const {
767 assert(access);
768 access->ApplyBarriers(barriers, true);
769 }
770 const std::vector<SyncBarrier> &barriers;
771};
772
773struct ApplyTrackbackBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700774 explicit ApplyTrackbackBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600775 void operator()(ResourceAccessState *access) const {
776 assert(access);
777 assert(!access->HasPendingState());
778 access->ApplyBarriers(barriers, false);
779 access->ApplyPendingBarriers(kCurrentCommandTag);
780 }
781 const std::vector<SyncBarrier> &barriers;
782};
783
784// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
785// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
786// *different* map from dest.
787// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
788// range [first, last)
789template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600790static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
791 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600792 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600793 auto at = entry;
794 for (auto pos = first; pos != last; ++pos) {
795 // Every member of the input iterator range must fit within the remaining portion of entry
796 assert(at->first.includes(pos->first));
797 assert(at != dest->end());
798 // Trim up at to the same size as the entry to resolve
799 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600800 auto access = pos->second; // intentional copy
801 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600802 at->second.Resolve(access);
803 ++at; // Go to the remaining unused section of entry
804 }
805}
806
John Zulaufa0a98292020-09-18 09:30:10 -0600807static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
808 SyncBarrier merged = {};
809 for (const auto &barrier : barriers) {
810 merged.Merge(barrier);
811 }
812 return merged;
813}
814
John Zulaufb02c1eb2020-10-06 16:33:36 -0600815template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700816void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600817 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
818 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600819 if (!range.non_empty()) return;
820
John Zulauf355e49b2020-04-24 15:11:15 -0600821 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
822 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600823 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600824 if (current->pos_B->valid) {
825 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600826 auto access = src_pos->second; // intentional copy
827 barrier_action(&access);
828
John Zulauf16adfc92020-04-08 10:28:33 -0600829 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600830 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
831 trimmed->second.Resolve(access);
832 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600833 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600834 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600835 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600836 }
John Zulauf16adfc92020-04-08 10:28:33 -0600837 } else {
838 // we have to descend to fill this gap
839 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600840 if (current->pos_A->valid) {
841 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
842 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600843 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600844 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -0600845 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600846 // There isn't anything in dest in current)range, so we can accumulate directly into it.
847 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600848 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
849 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
850 barrier_action(&pos->second);
John Zulauf355e49b2020-04-24 15:11:15 -0600851 }
852 }
853 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
854 // iterator of the outer while.
855
856 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
857 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
858 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600859 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
860 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600861 current.seek(seek_to);
862 } else if (!current->pos_A->valid && infill_state) {
863 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
864 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
865 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600866 }
John Zulauf5f13a792020-03-10 07:31:21 -0600867 }
John Zulauf16adfc92020-04-08 10:28:33 -0600868 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600869 }
John Zulauf1a224292020-06-30 14:52:13 -0600870
871 // Infill if range goes passed both the current and resolve map prior contents
872 if (recur_to_infill && (current->range.end < range.end)) {
873 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
874 ResourceAccessRangeMap gap_map;
875 const auto the_end = resolve_map->end();
876 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
877 for (auto &access : gap_map) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600878 barrier_action(&access.second);
John Zulauf1a224292020-06-30 14:52:13 -0600879 resolve_map->insert(the_end, access);
880 }
881 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600882}
883
John Zulauf43cc7462020-12-03 12:33:12 -0700884void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
885 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600886 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600887 if (range.non_empty() && infill_state) {
888 descent_map->insert(std::make_pair(range, *infill_state));
889 }
890 } else {
891 // Look for something to fill the gap further along.
892 for (const auto &prev_dep : prev_) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600893 const ApplyTrackbackBarriersAction barrier_action(prev_dep.barriers);
894 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600895 }
896
John Zulaufe5da6e52020-03-18 15:32:18 -0600897 if (src_external_.context) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600898 const ApplyTrackbackBarriersAction barrier_action(src_external_.barriers);
899 src_external_.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600900 }
901 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600902}
903
John Zulauf4a6105a2020-11-17 15:11:05 -0700904// Non-lazy import of all accesses, WaitEvents needs this.
905void AccessContext::ResolvePreviousAccesses() {
906 ResourceAccessState default_state;
907 for (const auto address_type : kAddressTypes) {
908 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
909 }
910}
911
John Zulauf43cc7462020-12-03 12:33:12 -0700912AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
913 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -0600914}
915
John Zulauf1507ee42020-05-18 11:33:09 -0600916static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
917 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
918 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
919 return stage_access;
920}
921static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
922 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
923 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
924 return stage_access;
925}
926
John Zulauf7635de32020-05-29 17:14:15 -0600927// Caller must manage returned pointer
928static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
929 uint32_t subpass, const VkRect2D &render_area,
930 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
931 auto *proxy = new AccessContext(context);
932 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600933 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600934 return proxy;
935}
936
John Zulaufb02c1eb2020-10-06 16:33:36 -0600937template <typename BarrierAction>
John Zulauf52446eb2020-10-22 16:40:08 -0600938class ResolveAccessRangeFunctor {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600939 public:
John Zulauf43cc7462020-12-03 12:33:12 -0700940 ResolveAccessRangeFunctor(const AccessContext &context, AccessAddressType address_type, ResourceAccessRangeMap *descent_map,
941 const ResourceAccessState *infill_state, BarrierAction &barrier_action)
John Zulauf52446eb2020-10-22 16:40:08 -0600942 : context_(context),
943 address_type_(address_type),
944 descent_map_(descent_map),
945 infill_state_(infill_state),
946 barrier_action_(barrier_action) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600947 ResolveAccessRangeFunctor() = delete;
948 void operator()(const ResourceAccessRange &range) const {
949 context_.ResolveAccessRange(address_type_, range, barrier_action_, descent_map_, infill_state_);
950 }
951
952 private:
John Zulauf52446eb2020-10-22 16:40:08 -0600953 const AccessContext &context_;
John Zulauf43cc7462020-12-03 12:33:12 -0700954 const AccessAddressType address_type_;
John Zulauf52446eb2020-10-22 16:40:08 -0600955 ResourceAccessRangeMap *const descent_map_;
956 const ResourceAccessState *infill_state_;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600957 BarrierAction &barrier_action_;
958};
959
John Zulaufb02c1eb2020-10-06 16:33:36 -0600960template <typename BarrierAction>
961void AccessContext::ResolveAccessRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -0700962 BarrierAction &barrier_action, AccessAddressType address_type,
963 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600964 const ResolveAccessRangeFunctor<BarrierAction> action(*this, address_type, descent_map, infill_state, barrier_action);
965 ApplyOverImageRange(image_state, subresource_range, action);
John Zulauf62f10592020-04-03 12:20:02 -0600966}
967
John Zulauf7635de32020-05-29 17:14:15 -0600968// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600969bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600970 const VkRect2D &render_area, uint32_t subpass,
971 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
972 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600973 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600974 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
975 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
976 // those affects have not been recorded yet.
977 //
978 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
979 // to apply and only copy then, if this proves a hot spot.
980 std::unique_ptr<AccessContext> proxy_for_prev;
981 TrackBack proxy_track_back;
982
John Zulauf355e49b2020-04-24 15:11:15 -0600983 const auto &transitions = rp_state.subpass_transitions[subpass];
984 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600985 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
986
987 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
988 if (prev_needs_proxy) {
989 if (!proxy_for_prev) {
990 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
991 render_area, attachment_views));
992 proxy_track_back = *track_back;
993 proxy_track_back.context = proxy_for_prev.get();
994 }
995 track_back = &proxy_track_back;
996 }
997 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600998 if (hazard.hazard) {
John Zulauf389c34b2020-07-28 11:19:35 -0600999 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1000 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1001 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
1002 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1003 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
1004 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001005 }
1006 }
1007 return skip;
1008}
1009
John Zulauf1507ee42020-05-18 11:33:09 -06001010bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001011 const VkRect2D &render_area, uint32_t subpass,
1012 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1013 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001014 bool skip = false;
1015 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1016 VkExtent3D extent = CastTo3D(render_area.extent);
1017 VkOffset3D offset = CastTo3D(render_area.offset);
John Zulaufa0a98292020-09-18 09:30:10 -06001018
John Zulauf1507ee42020-05-18 11:33:09 -06001019 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1020 if (subpass == rp_state.attachment_first_subpass[i]) {
1021 if (attachment_views[i] == nullptr) continue;
1022 const IMAGE_VIEW_STATE &view = *attachment_views[i];
1023 const IMAGE_STATE *image = view.image_state.get();
1024 if (image == nullptr) continue;
1025 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001026
1027 // Need check in the following way
1028 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1029 // vs. transition
1030 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1031 // for each aspect loaded.
1032
1033 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001034 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001035 const bool is_color = !(has_depth || has_stencil);
1036
1037 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001038 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001039
John Zulaufaff20662020-06-01 14:07:58 -06001040 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001041 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001042
John Zulaufb02c1eb2020-10-06 16:33:36 -06001043 auto hazard_range = view.normalized_subresource_range;
1044 bool checked_stencil = false;
1045 if (is_color) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001046 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, SyncOrdering::kColorAttachment, offset,
John Zulauf859089b2020-10-29 17:37:03 -06001047 extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001048 aspect = "color";
1049 } else {
1050 if (has_depth) {
1051 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001052 hazard = DetectHazard(*image, load_index, hazard_range, SyncOrdering::kDepthStencilAttachment, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001053 aspect = "depth";
1054 }
1055 if (!hazard.hazard && has_stencil) {
1056 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001057 hazard = DetectHazard(*image, stencil_load_index, hazard_range, SyncOrdering::kDepthStencilAttachment, offset,
1058 extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001059 aspect = "stencil";
1060 checked_stencil = true;
1061 }
1062 }
1063
1064 if (hazard.hazard) {
1065 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
1066 if (hazard.tag == kCurrentCommandTag) {
1067 // Hazard vs. ILT
1068 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1069 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1070 " aspect %s during load with loadOp %s.",
1071 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1072 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06001073 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1074 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001075 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001076 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -06001077 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001078 }
1079 }
1080 }
1081 }
1082 return skip;
1083}
1084
John Zulaufaff20662020-06-01 14:07:58 -06001085// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1086// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1087// store is part of the same Next/End operation.
1088// The latter is handled in layout transistion validation directly
1089bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
1090 const VkRect2D &render_area, uint32_t subpass,
1091 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1092 const char *func_name) const {
1093 bool skip = false;
1094 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1095 VkExtent3D extent = CastTo3D(render_area.extent);
1096 VkOffset3D offset = CastTo3D(render_area.offset);
1097
1098 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1099 if (subpass == rp_state.attachment_last_subpass[i]) {
1100 if (attachment_views[i] == nullptr) continue;
1101 const IMAGE_VIEW_STATE &view = *attachment_views[i];
1102 const IMAGE_STATE *image = view.image_state.get();
1103 if (image == nullptr) continue;
1104 const auto &ci = attachment_ci[i];
1105
1106 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1107 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1108 // sake, we treat DONT_CARE as writing.
1109 const bool has_depth = FormatHasDepth(ci.format);
1110 const bool has_stencil = FormatHasStencil(ci.format);
1111 const bool is_color = !(has_depth || has_stencil);
1112 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1113 if (!has_stencil && !store_op_stores) continue;
1114
1115 HazardResult hazard;
1116 const char *aspect = nullptr;
1117 bool checked_stencil = false;
1118 if (is_color) {
1119 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001120 view.normalized_subresource_range, SyncOrdering::kRaster, offset, extent);
John Zulaufaff20662020-06-01 14:07:58 -06001121 aspect = "color";
1122 } else {
1123 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1124 auto hazard_range = view.normalized_subresource_range;
1125 if (has_depth && store_op_stores) {
1126 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1127 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001128 SyncOrdering::kRaster, offset, extent);
John Zulaufaff20662020-06-01 14:07:58 -06001129 aspect = "depth";
1130 }
1131 if (!hazard.hazard && has_stencil && stencil_op_stores) {
1132 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1133 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001134 SyncOrdering::kRaster, offset, extent);
John Zulaufaff20662020-06-01 14:07:58 -06001135 aspect = "stencil";
1136 checked_stencil = true;
1137 }
1138 }
1139
1140 if (hazard.hazard) {
1141 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1142 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -06001143 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1144 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001145 " %s aspect during store with %s %s. Access info %s",
John Zulauf1dae9192020-06-16 15:46:44 -06001146 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -06001147 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001148 }
1149 }
1150 }
1151 return skip;
1152}
1153
John Zulaufb027cdb2020-05-21 14:25:22 -06001154bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
1155 const VkRect2D &render_area,
1156 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
1157 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -06001158 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
1159 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
1160 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001161}
1162
John Zulauf3d84f1b2020-03-09 13:33:25 -06001163class HazardDetector {
1164 SyncStageAccessIndex usage_index_;
1165
1166 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001167 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001168 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1169 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001170 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001171 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001172};
1173
John Zulauf69133422020-05-20 14:55:53 -06001174class HazardDetectorWithOrdering {
1175 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001176 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001177
1178 public:
1179 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001180 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001181 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001182 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1183 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001184 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001185 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001186};
1187
John Zulauf16adfc92020-04-08 10:28:33 -06001188HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001189 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001190 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001191 const auto base_address = ResourceBaseAddress(buffer);
1192 HazardDetector detector(usage_index);
1193 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001194}
1195
John Zulauf69133422020-05-20 14:55:53 -06001196template <typename Detector>
1197HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1198 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1199 const VkExtent3D &extent, DetectOptions options) const {
1200 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001201 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001202 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1203 base_address);
1204 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001205 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001206 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001207 if (hazard.hazard) return hazard;
1208 }
1209 return HazardResult();
1210}
1211
John Zulauf540266b2020-04-06 18:54:53 -06001212HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1213 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1214 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001215 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1216 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -06001217 return DetectHazard(image, current_usage, subresource_range, offset, extent);
1218}
1219
1220HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1221 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1222 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001223 HazardDetector detector(current_usage);
1224 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1225}
1226
1227HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001228 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001229 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001230 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001231 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001232}
1233
John Zulaufb027cdb2020-05-21 14:25:22 -06001234// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1235// should have reported the issue regarding an invalid attachment entry
1236HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001237 SyncOrdering ordering_rule, const VkOffset3D &offset, const VkExtent3D &extent,
John Zulaufb027cdb2020-05-21 14:25:22 -06001238 VkImageAspectFlags aspect_mask) const {
1239 if (view != nullptr) {
1240 const IMAGE_STATE *image = view->image_state.get();
1241 if (image != nullptr) {
1242 auto *detect_range = &view->normalized_subresource_range;
1243 VkImageSubresourceRange masked_range;
1244 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1245 masked_range = view->normalized_subresource_range;
1246 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1247 detect_range = &masked_range;
1248 }
1249
1250 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1251 if (detect_range->aspectMask) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001252 return DetectHazard(*image, current_usage, *detect_range, ordering_rule, offset, extent);
John Zulaufb027cdb2020-05-21 14:25:22 -06001253 }
1254 }
1255 }
1256 return HazardResult();
1257}
John Zulauf43cc7462020-12-03 12:33:12 -07001258
John Zulauf3d84f1b2020-03-09 13:33:25 -06001259class BarrierHazardDetector {
1260 public:
1261 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1262 SyncStageAccessFlags src_access_scope)
1263 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1264
John Zulauf5f13a792020-03-10 07:31:21 -06001265 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1266 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001267 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001268 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001269 // 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 -07001270 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001271 }
1272
1273 private:
1274 SyncStageAccessIndex usage_index_;
1275 VkPipelineStageFlags src_exec_scope_;
1276 SyncStageAccessFlags src_access_scope_;
1277};
1278
John Zulauf4a6105a2020-11-17 15:11:05 -07001279class EventBarrierHazardDetector {
1280 public:
1281 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1282 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
1283 const ResourceUsageTag &scope_tag)
1284 : usage_index_(usage_index),
1285 src_exec_scope_(src_exec_scope),
1286 src_access_scope_(src_access_scope),
1287 event_scope_(event_scope),
1288 scope_pos_(event_scope.cbegin()),
1289 scope_end_(event_scope.cend()),
1290 scope_tag_(scope_tag) {}
1291
1292 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1293 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1294 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1295 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1296 if (scope_pos_ == scope_end_) return HazardResult();
1297 if (!scope_pos_->first.intersects(pos->first)) {
1298 event_scope_.lower_bound(pos->first);
1299 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1300 }
1301
1302 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1303 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1304 }
1305 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1306 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1307 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1308 }
1309
1310 private:
1311 SyncStageAccessIndex usage_index_;
1312 VkPipelineStageFlags src_exec_scope_;
1313 SyncStageAccessFlags src_access_scope_;
1314 const SyncEventState::ScopeMap &event_scope_;
1315 SyncEventState::ScopeMap::const_iterator scope_pos_;
1316 SyncEventState::ScopeMap::const_iterator scope_end_;
1317 const ResourceUsageTag &scope_tag_;
1318};
1319
1320HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1321 const SyncStageAccessFlags &src_access_scope,
1322 const VkImageSubresourceRange &subresource_range,
1323 const SyncEventState &sync_event, DetectOptions options) const {
1324 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1325 // first access scope map to use, and there's no easy way to plumb it in below.
1326 const auto address_type = ImageAddressType(image);
1327 const auto &event_scope = sync_event.FirstScope(address_type);
1328
1329 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1330 event_scope, sync_event.first_scope_tag);
1331 VkOffset3D zero_offset = {0, 0, 0};
1332 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
1333}
1334
John Zulauf16adfc92020-04-08 10:28:33 -06001335HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001336 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001337 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001338 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001339 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1340 VkOffset3D zero_offset = {0, 0, 0};
1341 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001342}
1343
John Zulauf355e49b2020-04-24 15:11:15 -06001344HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001345 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001346 const VkImageMemoryBarrier &barrier) const {
1347 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1348 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1349 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1350}
1351
John Zulauf9cb530d2019-09-30 14:14:10 -06001352template <typename Flags, typename Map>
1353SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1354 SyncStageAccessFlags scope = 0;
1355 for (const auto &bit_scope : map) {
1356 if (flag_mask < bit_scope.first) break;
1357
1358 if (flag_mask & bit_scope.first) {
1359 scope |= bit_scope.second;
1360 }
1361 }
1362 return scope;
1363}
1364
1365SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1366 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1367}
1368
1369SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1370 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1371}
1372
1373// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1374SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001375 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1376 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1377 // of the union of all stage/access types for all the stages and the same unions for the access mask...
John Zulauf9cb530d2019-09-30 14:14:10 -06001378 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1379}
1380
1381template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001382void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001383 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1384 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001385 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001386 auto pos = accesses->lower_bound(range);
1387 if (pos == accesses->end() || !pos->first.intersects(range)) {
1388 // The range is empty, fill it with a default value.
1389 pos = action.Infill(accesses, pos, range);
1390 } else if (range.begin < pos->first.begin) {
1391 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001392 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001393 } else if (pos->first.begin < range.begin) {
1394 // Trim the beginning if needed
1395 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1396 ++pos;
1397 }
1398
1399 const auto the_end = accesses->end();
1400 while ((pos != the_end) && pos->first.intersects(range)) {
1401 if (pos->first.end > range.end) {
1402 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1403 }
1404
1405 pos = action(accesses, pos);
1406 if (pos == the_end) break;
1407
1408 auto next = pos;
1409 ++next;
1410 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1411 // Need to infill if next is disjoint
1412 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001413 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001414 next = action.Infill(accesses, next, new_range);
1415 }
1416 pos = next;
1417 }
1418}
John Zulauf4a6105a2020-11-17 15:11:05 -07001419template <typename Action, typename RangeGen>
1420void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1421 assert(range_gen_arg);
1422 auto &range_gen = *range_gen_arg; // Non-const references must be * by style requirement but deref-ing * iterator is a pain
1423 for (; range_gen->non_empty(); ++range_gen) {
1424 UpdateMemoryAccessState(accesses, *range_gen, action);
1425 }
1426}
John Zulauf9cb530d2019-09-30 14:14:10 -06001427
1428struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001429 using Iterator = ResourceAccessRangeMap::iterator;
1430 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001431 // this is only called on gaps, and never returns a gap.
1432 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001433 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001434 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001435 }
John Zulauf5f13a792020-03-10 07:31:21 -06001436
John Zulauf5c5e88d2019-12-26 11:22:02 -07001437 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001438 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001439 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001440 return pos;
1441 }
1442
John Zulauf43cc7462020-12-03 12:33:12 -07001443 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001444 SyncOrdering ordering_rule_, const ResourceUsageTag &tag_)
1445 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001446 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001447 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001448 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001449 const SyncOrdering ordering_rule;
John Zulauf9cb530d2019-09-30 14:14:10 -06001450 const ResourceUsageTag &tag;
1451};
1452
John Zulauf4a6105a2020-11-17 15:11:05 -07001453// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001454struct PipelineBarrierOp {
1455 SyncBarrier barrier;
1456 bool layout_transition;
1457 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1458 : barrier(barrier_), layout_transition(layout_transition_) {}
1459 PipelineBarrierOp() = default;
1460 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1461};
John Zulauf4a6105a2020-11-17 15:11:05 -07001462// The barrier operation for wait events
1463struct WaitEventBarrierOp {
1464 const ResourceUsageTag *scope_tag;
1465 SyncBarrier barrier;
1466 bool layout_transition;
1467 WaitEventBarrierOp(const ResourceUsageTag &scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1468 : scope_tag(&scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
1469 WaitEventBarrierOp() = default;
1470 void operator()(ResourceAccessState *access_state) const {
1471 assert(scope_tag); // Not valid to have a non-scope op executed, default construct included for std::vector support
1472 access_state->ApplyBarrier(*scope_tag, barrier, layout_transition);
1473 }
1474};
John Zulauf1e331ec2020-12-04 18:29:38 -07001475
John Zulauf4a6105a2020-11-17 15:11:05 -07001476// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1477// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1478// of a collection is known/present.
John Zulauf1e331ec2020-12-04 18:29:38 -07001479template <typename BarrierOp>
John Zulauf89311b42020-09-29 16:28:47 -06001480class ApplyBarrierOpsFunctor {
1481 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001482 using Iterator = ResourceAccessRangeMap::iterator;
1483 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001484
John Zulauf5c5e88d2019-12-26 11:22:02 -07001485 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001486 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001487 for (const auto &op : barrier_ops_) {
1488 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001489 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001490
John Zulauf89311b42020-09-29 16:28:47 -06001491 if (resolve_) {
1492 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1493 // another walk
1494 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001495 }
1496 return pos;
1497 }
1498
John Zulauf89311b42020-09-29 16:28:47 -06001499 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulauf1e331ec2020-12-04 18:29:38 -07001500 ApplyBarrierOpsFunctor(bool resolve, const std::vector<BarrierOp> &barrier_ops, const ResourceUsageTag &tag)
1501 : resolve_(resolve), barrier_ops_(barrier_ops), tag_(tag) {}
John Zulauf89311b42020-09-29 16:28:47 -06001502
1503 private:
1504 bool resolve_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001505 const std::vector<BarrierOp> &barrier_ops_;
1506 const ResourceUsageTag &tag_;
1507};
1508
John Zulauf4a6105a2020-11-17 15:11:05 -07001509// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1510// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1511template <typename BarrierOp>
1512class ApplyBarrierFunctor {
1513 public:
1514 using Iterator = ResourceAccessRangeMap::iterator;
1515 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1516
1517 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1518 auto &access_state = pos->second;
1519 barrier_op_(&access_state);
1520 return pos;
1521 }
1522
1523 ApplyBarrierFunctor(const BarrierOp &barrier_op) : barrier_op_(barrier_op) {}
1524
1525 private:
1526 const BarrierOp barrier_op_;
1527};
1528
John Zulauf1e331ec2020-12-04 18:29:38 -07001529// This functor resolves the pendinging state.
1530class ResolvePendingBarrierFunctor {
1531 public:
1532 using Iterator = ResourceAccessRangeMap::iterator;
1533 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1534
1535 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1536 auto &access_state = pos->second;
1537 access_state.ApplyPendingBarriers(tag_);
1538 return pos;
1539 }
1540
1541 ResolvePendingBarrierFunctor(const ResourceUsageTag &tag) : tag_(tag) {}
1542
1543 private:
John Zulauf89311b42020-09-29 16:28:47 -06001544 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001545};
1546
John Zulauf8e3c3e92021-01-06 11:19:36 -07001547void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
1548 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
1549 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001550 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001551}
1552
John Zulauf8e3c3e92021-01-06 11:19:36 -07001553void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001554 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001555 if (!SimpleBinding(buffer)) return;
1556 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001557 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001558}
John Zulauf355e49b2020-04-24 15:11:15 -06001559
John Zulauf8e3c3e92021-01-06 11:19:36 -07001560void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001561 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001562 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001563 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001564 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001565 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1566 base_address);
1567 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001568 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001569 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001570 UpdateMemoryAccessState(&GetAccessStateMap(address_type), *range_gen, action);
John Zulauf5f13a792020-03-10 07:31:21 -06001571 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001572}
John Zulauf8e3c3e92021-01-06 11:19:36 -07001573void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
1574 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask,
1575 const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001576 if (view != nullptr) {
1577 const IMAGE_STATE *image = view->image_state.get();
1578 if (image != nullptr) {
1579 auto *update_range = &view->normalized_subresource_range;
1580 VkImageSubresourceRange masked_range;
1581 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1582 masked_range = view->normalized_subresource_range;
1583 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1584 update_range = &masked_range;
1585 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001586 UpdateAccessState(*image, current_usage, ordering_rule, *update_range, offset, extent, tag);
John Zulauf7635de32020-05-29 17:14:15 -06001587 }
1588 }
1589}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001590
John Zulauf8e3c3e92021-01-06 11:19:36 -07001591void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001592 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1593 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001594 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1595 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001596 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001597}
1598
John Zulauf540266b2020-04-06 18:54:53 -06001599template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001600void AccessContext::UpdateResourceAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001601 if (!SimpleBinding(buffer)) return;
1602 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf43cc7462020-12-03 12:33:12 -07001603 UpdateMemoryAccessState(&GetAccessStateMap(AccessAddressType::kLinear), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001604}
1605
1606template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001607void AccessContext::UpdateResourceAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1608 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001609 if (!SimpleBinding(image)) return;
1610 const auto address_type = ImageAddressType(image);
1611 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001612
John Zulauf16adfc92020-04-08 10:28:33 -06001613 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001614 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
1615 image.createInfo.extent, base_address);
1616
John Zulauf540266b2020-04-06 18:54:53 -06001617 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001618 UpdateMemoryAccessState(accesses, *range_gen, action);
John Zulauf540266b2020-04-06 18:54:53 -06001619 }
1620}
1621
John Zulauf7635de32020-05-29 17:14:15 -06001622void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1623 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1624 const ResourceUsageTag &tag) {
1625 UpdateStateResolveAction update(*this, tag);
1626 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1627}
1628
John Zulaufaff20662020-06-01 14:07:58 -06001629void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1630 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1631 const ResourceUsageTag &tag) {
1632 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1633 VkExtent3D extent = CastTo3D(render_area.extent);
1634 VkOffset3D offset = CastTo3D(render_area.offset);
1635
1636 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1637 if (rp_state.attachment_last_subpass[i] == subpass) {
1638 if (attachment_views[i] == nullptr) continue; // UNUSED
1639 const auto &view = *attachment_views[i];
1640 const IMAGE_STATE *image = view.image_state.get();
1641 if (image == nullptr) continue;
1642
1643 const auto &ci = attachment_ci[i];
1644 const bool has_depth = FormatHasDepth(ci.format);
1645 const bool has_stencil = FormatHasStencil(ci.format);
1646 const bool is_color = !(has_depth || has_stencil);
1647 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1648
1649 if (is_color && store_op_stores) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001650 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster,
1651 view.normalized_subresource_range, offset, extent, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001652 } else {
1653 auto update_range = view.normalized_subresource_range;
1654 if (has_depth && store_op_stores) {
1655 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001656 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster,
1657 update_range, offset, extent, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001658 }
1659 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1660 if (has_stencil && stencil_op_stores) {
1661 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001662 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster,
1663 update_range, offset, extent, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001664 }
1665 }
1666 }
1667 }
1668}
1669
John Zulauf540266b2020-04-06 18:54:53 -06001670template <typename Action>
1671void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1672 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001673 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001674 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001675 }
1676}
1677
1678void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001679 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1680 auto &context = contexts[subpass_index];
John Zulaufb02c1eb2020-10-06 16:33:36 -06001681 ApplyTrackbackBarriersAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001682 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001683 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001684 }
1685 }
1686}
1687
John Zulauf355e49b2020-04-24 15:11:15 -06001688// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001689HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001690 if (!attach_view) return HazardResult();
1691 const auto image_state = attach_view->image_state.get();
1692 if (!image_state) return HazardResult();
1693
John Zulauf355e49b2020-04-24 15:11:15 -06001694 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001695 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001696
1697 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001698 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1699 const auto merged_barrier = MergeBarriers(track_back.barriers);
1700 HazardResult hazard =
1701 track_back.context->DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
1702 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001703 if (!hazard.hazard) {
1704 // The Async hazard check is against the current context's async set.
John Zulaufa0a98292020-09-18 09:30:10 -06001705 hazard = DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001706 attach_view->normalized_subresource_range, kDetectAsync);
1707 }
John Zulaufa0a98292020-09-18 09:30:10 -06001708
John Zulauf355e49b2020-04-24 15:11:15 -06001709 return hazard;
1710}
1711
John Zulaufb02c1eb2020-10-06 16:33:36 -06001712void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
1713 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1714 const ResourceUsageTag &tag) {
1715 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001716 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001717 for (const auto &transition : transitions) {
1718 const auto prev_pass = transition.prev_pass;
1719 const auto attachment_view = attachment_views[transition.attachment];
1720 if (!attachment_view) continue;
1721 const auto *image = attachment_view->image_state.get();
1722 if (!image) continue;
1723 if (!SimpleBinding(*image)) continue;
1724
1725 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1726 assert(trackback);
1727
1728 // Import the attachments into the current context
1729 const auto *prev_context = trackback->context;
1730 assert(prev_context);
1731 const auto address_type = ImageAddressType(*image);
1732 auto &target_map = GetAccessStateMap(address_type);
1733 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
1734 prev_context->ResolveAccessRange(*image, attachment_view->normalized_subresource_range, barrier_action, address_type,
John Zulauf646cc292020-10-23 09:16:45 -06001735 &target_map, &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001736 }
1737
John Zulauf86356ca2020-10-19 11:46:41 -06001738 // If there were no transitions skip this global map walk
1739 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001740 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulauf86356ca2020-10-19 11:46:41 -06001741 ApplyGlobalBarriers(apply_pending_action);
1742 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001743}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001744
1745void CommandBufferAccessContext::ApplyBufferBarriers(const SyncEventState &sync_event, const SyncExecScope &dst,
1746 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
John Zulauf4a6105a2020-11-17 15:11:05 -07001747 const auto &scope_tag = sync_event.first_scope_tag;
1748 auto *access_context = GetCurrentAccessContext();
1749 const auto address_type = AccessAddressType::kLinear;
1750 for (uint32_t index = 0; index < barrier_count; index++) {
1751 auto barrier = barriers[index]; // barrier is a copy
1752 const auto *buffer = sync_state_->Get<BUFFER_STATE>(barrier.buffer);
1753 if (!buffer) continue;
1754 const auto base_address = ResourceBaseAddress(*buffer);
1755 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
1756 const ResourceAccessRange range = MakeRange(barrier) + base_address;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001757 const SyncBarrier sync_barrier(barrier, sync_event.scope, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001758 const ApplyBarrierFunctor<WaitEventBarrierOp> barrier_action({scope_tag, sync_barrier, false /* layout_transition */});
1759 EventSimpleRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), range);
1760 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barrier_action, &filtered_range_gen);
1761 }
1762}
1763
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001764void CommandBufferAccessContext::ApplyGlobalBarriers(SyncEventState &sync_event, const SyncExecScope &dst,
1765 uint32_t memory_barrier_count, const VkMemoryBarrier *pMemoryBarriers,
1766 const ResourceUsageTag &tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07001767 std::vector<WaitEventBarrierOp> barrier_ops;
1768 barrier_ops.reserve(std::min<uint32_t>(memory_barrier_count, 1));
1769 const auto &scope_tag = sync_event.first_scope_tag;
1770 auto *access_context = GetCurrentAccessContext();
1771 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
1772 const auto &barrier = pMemoryBarriers[barrier_index];
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001773 SyncBarrier sync_barrier(barrier, sync_event.scope, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001774 barrier_ops.emplace_back(scope_tag, sync_barrier, false);
1775 }
1776 if (0 == memory_barrier_count) {
1777 // If there are no global memory barriers, force an exec barrier
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001778 barrier_ops.emplace_back(scope_tag, SyncBarrier(sync_event.scope, dst), false);
John Zulauf4a6105a2020-11-17 15:11:05 -07001779 }
1780 ApplyBarrierOpsFunctor<WaitEventBarrierOp> barriers_functor(false /* don't resolve */, barrier_ops, tag);
1781 for (const auto address_type : kAddressTypes) {
1782 EventSimpleRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), kFullRange);
1783 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &filtered_range_gen);
1784 }
1785
1786 // Apply the global barrier to the event itself (for race condition tracking)
1787 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001788 sync_event.barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1789 sync_event.barriers |= dst.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07001790}
1791
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001792void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
1793 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
John Zulauf4a6105a2020-11-17 15:11:05 -07001794 for (auto &event_pair : event_state_) {
1795 assert(event_pair.second); // Shouldn't be storing empty
1796 auto &sync_event = *event_pair.second;
1797 // 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 -07001798 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
1799 sync_event.barriers |= dst.exec_scope;
1800 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
John Zulauf4a6105a2020-11-17 15:11:05 -07001801 }
1802 }
1803}
1804
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001805void CommandBufferAccessContext::ApplyImageBarriers(const SyncEventState &sync_event, const SyncExecScope &dst,
1806 uint32_t barrier_count, const VkImageMemoryBarrier *barriers,
1807 const ResourceUsageTag &tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07001808 const auto &scope_tag = sync_event.first_scope_tag;
1809 auto *access_context = GetCurrentAccessContext();
1810 for (uint32_t index = 0; index < barrier_count; index++) {
1811 const auto &barrier = barriers[index];
1812 const auto *image = sync_state_->Get<IMAGE_STATE>(barrier.image);
1813 if (!image) continue;
1814 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
1815 bool layout_transition = barrier.oldLayout != barrier.newLayout;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001816 const SyncBarrier sync_barrier(barrier, sync_event.scope, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001817 const ApplyBarrierFunctor<WaitEventBarrierOp> barrier_action({scope_tag, sync_barrier, layout_transition});
1818 const auto base_address = ResourceBaseAddress(*image);
1819 subresource_adapter::ImageRangeGenerator range_gen(*image->fragment_encoder.get(), subresource_range, {0, 0, 0},
1820 image->createInfo.extent, base_address);
1821 const auto address_type = AccessContext::ImageAddressType(*image);
1822 EventImageRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), range_gen);
1823 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barrier_action, &filtered_range_gen);
1824 }
1825}
John Zulaufb02c1eb2020-10-06 16:33:36 -06001826
John Zulauf355e49b2020-04-24 15:11:15 -06001827// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1828bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1829
1830 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001831 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001832 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1833 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06001834
John Zulauf86356ca2020-10-19 11:46:41 -06001835 assert(pRenderPassBegin);
1836 if (nullptr == pRenderPassBegin) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06001837
John Zulauf86356ca2020-10-19 11:46:41 -06001838 const uint32_t subpass = 0;
John Zulauf355e49b2020-04-24 15:11:15 -06001839
John Zulauf86356ca2020-10-19 11:46:41 -06001840 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
1841 // hasn't happened yet)
1842 const std::vector<AccessContext> empty_context_vector;
1843 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1844 const_cast<AccessContext *>(&cb_access_context_));
John Zulauf355e49b2020-04-24 15:11:15 -06001845
John Zulauf86356ca2020-10-19 11:46:41 -06001846 // Create a view list
1847 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1848 assert(fb_state);
1849 if (nullptr == fb_state) return skip;
1850 // NOTE: Must not use COMMAND_BUFFER_STATE variant of this as RecordCmdBeginRenderPass hasn't run and thus
1851 // the activeRenderPass.* fields haven't been set.
1852 const auto views = sync_state_->GetAttachmentViews(*pRenderPassBegin, *fb_state);
1853
1854 // Validate transitions
1855 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
1856
1857 // Validate load operations if there were no layout transition hazards
1858 if (!skip) {
1859 temp_context.RecordLayoutTransitions(rp_state, subpass, views, kCurrentCommandTag);
1860 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001861 }
John Zulauf86356ca2020-10-19 11:46:41 -06001862
John Zulauf355e49b2020-04-24 15:11:15 -06001863 return skip;
1864}
1865
locke-lunarg61870c22020-06-09 14:51:50 -06001866bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1867 const char *func_name) const {
1868 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001869 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001870 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001871 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1872 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001873 return skip;
1874 }
1875
1876 using DescriptorClass = cvdescriptorset::DescriptorClass;
1877 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1878 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1879 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1880 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1881
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001882 for (const auto &stage_state : pipe->stage_state) {
1883 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1884 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001885 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001886 }
locke-lunarg61870c22020-06-09 14:51:50 -06001887 for (const auto &set_binding : stage_state.descriptor_uses) {
1888 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1889 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1890 set_binding.first.second);
1891 const auto descriptor_type = binding_it.GetType();
1892 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1893 auto array_idx = 0;
1894
1895 if (binding_it.IsVariableDescriptorCount()) {
1896 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1897 }
1898 SyncStageAccessIndex sync_index =
1899 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1900
1901 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1902 uint32_t index = i - index_range.start;
1903 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1904 switch (descriptor->GetClass()) {
1905 case DescriptorClass::ImageSampler:
1906 case DescriptorClass::Image: {
1907 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001908 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001909 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001910 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1911 img_view_state = image_sampler_descriptor->GetImageViewState();
1912 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001913 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001914 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1915 img_view_state = image_descriptor->GetImageViewState();
1916 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001917 }
1918 if (!img_view_state) continue;
1919 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1920 VkExtent3D extent = {};
1921 VkOffset3D offset = {};
1922 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1923 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1924 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1925 } else {
1926 extent = img_state->createInfo.extent;
1927 }
John Zulauf361fb532020-07-22 10:45:39 -06001928 HazardResult hazard;
1929 const auto &subresource_range = img_view_state->normalized_subresource_range;
1930 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1931 // Input attachments are subject to raster ordering rules
1932 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001933 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001934 } else {
1935 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1936 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001937 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001938 skip |= sync_state_->LogError(
1939 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001940 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1941 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001942 func_name, string_SyncHazard(hazard.hazard),
1943 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1944 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001945 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001946 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1947 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1948 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001949 }
1950 break;
1951 }
1952 case DescriptorClass::TexelBuffer: {
1953 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1954 if (!buf_view_state) continue;
1955 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001956 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001957 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001958 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001959 skip |= sync_state_->LogError(
1960 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001961 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1962 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001963 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1964 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001965 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001966 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1967 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1968 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001969 }
1970 break;
1971 }
1972 case DescriptorClass::GeneralBuffer: {
1973 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1974 auto buf_state = buffer_descriptor->GetBufferState();
1975 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001976 const ResourceAccessRange range =
1977 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001978 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001979 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001980 skip |= sync_state_->LogError(
1981 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001982 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1983 func_name, string_SyncHazard(hazard.hazard),
1984 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001985 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001986 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001987 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1988 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1989 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001990 }
1991 break;
1992 }
1993 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1994 default:
1995 break;
1996 }
1997 }
1998 }
1999 }
2000 return skip;
2001}
2002
2003void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
2004 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002005 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06002006 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002007 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
2008 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06002009 return;
2010 }
2011
2012 using DescriptorClass = cvdescriptorset::DescriptorClass;
2013 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
2014 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
2015 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
2016 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
2017
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002018 for (const auto &stage_state : pipe->stage_state) {
2019 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
2020 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06002021 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002022 }
locke-lunarg61870c22020-06-09 14:51:50 -06002023 for (const auto &set_binding : stage_state.descriptor_uses) {
2024 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
2025 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
2026 set_binding.first.second);
2027 const auto descriptor_type = binding_it.GetType();
2028 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2029 auto array_idx = 0;
2030
2031 if (binding_it.IsVariableDescriptorCount()) {
2032 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2033 }
2034 SyncStageAccessIndex sync_index =
2035 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
2036
2037 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2038 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2039 switch (descriptor->GetClass()) {
2040 case DescriptorClass::ImageSampler:
2041 case DescriptorClass::Image: {
2042 const IMAGE_VIEW_STATE *img_view_state = nullptr;
2043 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
2044 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
2045 } else {
2046 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
2047 }
2048 if (!img_view_state) continue;
2049 const IMAGE_STATE *img_state = img_view_state->image_state.get();
2050 VkExtent3D extent = {};
2051 VkOffset3D offset = {};
2052 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
2053 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
2054 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
2055 } else {
2056 extent = img_state->createInfo.extent;
2057 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07002058 SyncOrdering ordering_rule = (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)
2059 ? SyncOrdering::kRaster
2060 : SyncOrdering::kNonAttachment;
2061 current_context_->UpdateAccessState(*img_state, sync_index, ordering_rule,
2062 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002063 break;
2064 }
2065 case DescriptorClass::TexelBuffer: {
2066 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
2067 if (!buf_view_state) continue;
2068 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002069 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07002070 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002071 break;
2072 }
2073 case DescriptorClass::GeneralBuffer: {
2074 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
2075 auto buf_state = buffer_descriptor->GetBufferState();
2076 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002077 const ResourceAccessRange range =
2078 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07002079 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002080 break;
2081 }
2082 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2083 default:
2084 break;
2085 }
2086 }
2087 }
2088 }
2089}
2090
2091bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2092 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002093 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2094 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002095 return skip;
2096 }
2097
2098 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2099 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002100 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002101
2102 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002103 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002104 if (binding_description.binding < binding_buffers_size) {
2105 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002106 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002107
locke-lunarg1ae57d62020-11-18 10:49:19 -07002108 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002109 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2110 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06002111 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
2112 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002113 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002114 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 -06002115 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002116 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002117 }
2118 }
2119 }
2120 return skip;
2121}
2122
2123void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002124 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2125 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002126 return;
2127 }
2128 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2129 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002130 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002131
2132 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002133 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002134 if (binding_description.binding < binding_buffers_size) {
2135 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002136 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002137
locke-lunarg1ae57d62020-11-18 10:49:19 -07002138 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002139 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2140 vertexCount, binding_description.stride);
John Zulauf8e3c3e92021-01-06 11:19:36 -07002141 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, SyncOrdering::kNonAttachment,
2142 range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002143 }
2144 }
2145}
2146
2147bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2148 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002149 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002150 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002151 }
locke-lunarg61870c22020-06-09 14:51:50 -06002152
locke-lunarg1ae57d62020-11-18 10:49:19 -07002153 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002154 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002155 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2156 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06002157 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
2158 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002159 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002160 index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002161 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002162 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002163 }
2164
2165 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2166 // We will detect more accurate range in the future.
2167 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2168 return skip;
2169}
2170
2171void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002172 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) return;
locke-lunarg61870c22020-06-09 14:51:50 -06002173
locke-lunarg1ae57d62020-11-18 10:49:19 -07002174 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002175 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002176 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2177 firstIndex, indexCount, index_size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07002178 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002179
2180 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2181 // We will detect more accurate range in the future.
2182 RecordDrawVertex(UINT32_MAX, 0, tag);
2183}
2184
2185bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002186 bool skip = false;
2187 if (!current_renderpass_context_) return skip;
2188 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
2189 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
2190 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002191}
2192
2193void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002194 if (current_renderpass_context_) {
locke-lunarg7077d502020-06-18 21:37:26 -06002195 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
2196 tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002197 }
locke-lunarg61870c22020-06-09 14:51:50 -06002198}
2199
John Zulauf355e49b2020-04-24 15:11:15 -06002200bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002201 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06002202 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06002203 skip |=
2204 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002205
2206 return skip;
2207}
2208
2209bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
2210 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06002211 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002212 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06002213 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06002214 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
2215 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002216
2217 return skip;
2218}
2219
2220void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
2221 assert(sync_state_);
2222 if (!cb_state_) return;
2223
2224 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06002225 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06002226 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06002227 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002228 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002229}
2230
John Zulauf355e49b2020-04-24 15:11:15 -06002231void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002232 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06002233 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002234 current_context_ = &current_renderpass_context_->CurrentContext();
2235}
2236
John Zulauf355e49b2020-04-24 15:11:15 -06002237void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002238 assert(current_renderpass_context_);
2239 if (!current_renderpass_context_) return;
2240
John Zulauf1a224292020-06-30 14:52:13 -06002241 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002242 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002243 current_renderpass_context_ = nullptr;
2244}
2245
John Zulauf49beb112020-11-04 16:06:31 -07002246bool CommandBufferAccessContext::ValidateSetEvent(VkCommandBuffer commandBuffer, VkEvent event,
2247 VkPipelineStageFlags stageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002248 // I'll put this here just in case we need to pass this in for future extension support
2249 const auto cmd = CMD_SETEVENT;
2250 bool skip = false;
2251 const auto *sync_event = GetEventState(event);
2252 if (!sync_event) return false; // Core, Lifetimes, or Param check needs to catch invalid events.
2253
2254 const char *const reset_set =
2255 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
2256 "hazards.";
2257 const char *const wait =
2258 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
2259
2260 const auto exec_scope = WithEarlierPipelineStages(ExpandPipelineStages(GetQueueFlags(), stageMask));
2261 if (!sync_event->HasBarrier(stageMask, exec_scope)) {
2262 const char *vuid = nullptr;
2263 const char *message = nullptr;
2264 switch (sync_event->last_command) {
2265 case CMD_RESETEVENT:
2266 // Needs a barrier between reset and set
2267 vuid = "SYNC-vkCmdSetEvent-missingbarrier-reset";
2268 message = reset_set;
2269 break;
2270 case CMD_SETEVENT:
2271 // Needs a barrier between set and set
2272 vuid = "SYNC-vkCmdSetEvent-missingbarrier-set";
2273 message = reset_set;
2274 break;
2275 case CMD_WAITEVENTS:
2276 // Needs a barrier or is in second execution scope
2277 vuid = "SYNC-vkCmdSetEvent-missingbarrier-wait";
2278 message = wait;
2279 break;
2280 default:
2281 // The only other valid last command that wasn't one.
2282 assert(sync_event->last_command == CMD_NONE);
2283 break;
2284 }
2285 if (vuid) {
2286 assert(nullptr != message);
2287 const char *const cmd_name = CommandTypeString(cmd);
2288 skip |= sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2289 cmd_name, CommandTypeString(sync_event->last_command));
2290 }
2291 }
2292
2293 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002294}
2295
John Zulauf4a6105a2020-11-17 15:11:05 -07002296void CommandBufferAccessContext::RecordSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask,
2297 const ResourceUsageTag &tag) {
2298 auto *sync_event = GetEventState(event);
2299 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
2300
2301 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
2302 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
2303 // any issues caused by naive scope setting here.
2304
2305 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
2306 // Given:
2307 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
2308 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002309 auto scope = SyncExecScope::MakeSrc(GetQueueFlags(), stageMask);
John Zulauf4a6105a2020-11-17 15:11:05 -07002310
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002311 if (!sync_event->HasBarrier(stageMask, scope.exec_scope)) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002312 sync_event->unsynchronized_set = sync_event->last_command;
2313 sync_event->ResetFirstScope();
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002314 } else if (sync_event->scope.exec_scope == 0) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002315 // We only set the scope if there isn't one
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002316 sync_event->scope = scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002317
2318 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
2319 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002320 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002321 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
2322 }
2323 };
2324 GetCurrentAccessContext()->ForAll(set_scope);
2325 sync_event->unsynchronized_set = CMD_NONE;
2326 sync_event->first_scope_tag = tag;
2327 }
2328 sync_event->last_command = CMD_SETEVENT;
2329 sync_event->barriers = 0U;
2330}
John Zulauf49beb112020-11-04 16:06:31 -07002331
2332bool CommandBufferAccessContext::ValidateResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
2333 VkPipelineStageFlags stageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002334 // I'll put this here just in case we need to pass this in for future extension support
2335 const auto cmd = CMD_RESETEVENT;
2336
2337 bool skip = false;
2338 // TODO: EVENTS:
2339 // What is it we need to check... that we've had a reset since a set? Set/Set seems ill formed...
2340 const auto *sync_event = GetEventState(event);
2341 if (!sync_event) return false; // Core, Lifetimes, or Param check needs to catch invalid events.
2342
2343 const char *const set_wait =
2344 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
2345 "hazards.";
2346 const char *message = set_wait; // Only one message this call.
2347 const auto exec_scope = WithEarlierPipelineStages(ExpandPipelineStages(GetQueueFlags(), stageMask));
2348 if (!sync_event->HasBarrier(stageMask, exec_scope)) {
2349 const char *vuid = nullptr;
2350 switch (sync_event->last_command) {
2351 case CMD_SETEVENT:
2352 // Needs a barrier between set and reset
2353 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
2354 break;
2355 case CMD_WAITEVENTS: {
2356 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
2357 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
2358 break;
2359 }
2360 default:
2361 // The only other valid last command that wasn't one.
2362 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT));
2363 break;
2364 }
2365 if (vuid) {
2366 const char *const cmd_name = CommandTypeString(cmd);
2367 skip |= sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2368 cmd_name, CommandTypeString(sync_event->last_command));
2369 }
2370 }
2371 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002372}
2373
John Zulauf4a6105a2020-11-17 15:11:05 -07002374void CommandBufferAccessContext::RecordResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
2375 const auto cmd = CMD_RESETEVENT;
2376 auto *sync_event = GetEventState(event);
2377 if (!sync_event) return;
John Zulauf49beb112020-11-04 16:06:31 -07002378
John Zulauf4a6105a2020-11-17 15:11:05 -07002379 // Clear out the first sync scope, any races vs. wait or set are reported, so we'll keep the bookkeeping simple assuming
2380 // the safe case
2381 for (const auto address_type : kAddressTypes) {
2382 sync_event->first_scope[static_cast<size_t>(address_type)].clear();
2383 }
2384
2385 // Update the event state
2386 sync_event->last_command = cmd;
2387 sync_event->unsynchronized_set = CMD_NONE;
2388 sync_event->ResetFirstScope();
2389 sync_event->barriers = 0U;
2390}
2391
2392bool CommandBufferAccessContext::ValidateWaitEvents(uint32_t eventCount, const VkEvent *pEvents, VkPipelineStageFlags srcStageMask,
2393 VkPipelineStageFlags dstStageMask, uint32_t memoryBarrierCount,
2394 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
John Zulauf49beb112020-11-04 16:06:31 -07002395 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2396 uint32_t imageMemoryBarrierCount,
2397 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002398 const auto cmd = CMD_WAITEVENTS;
2399 const char *const ignored = "Wait operation is ignored for this event.";
2400 bool skip = false;
2401
2402 if (srcStageMask & VK_PIPELINE_STAGE_HOST_BIT) {
2403 const char *const cmd_name = CommandTypeString(cmd);
2404 const char *const vuid = "SYNC-vkCmdWaitEvents-hostevent-unsupported";
John Zulauffe757512020-12-18 12:17:47 -07002405 skip = sync_state_->LogInfo(cb_state_->commandBuffer, vuid,
2406 "%s, srcStageMask includes %s, unsupported by synchronization validaton.", cmd_name,
2407 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT), ignored);
John Zulauf4a6105a2020-11-17 15:11:05 -07002408 }
2409
2410 VkPipelineStageFlags event_stage_masks = 0U;
John Zulauffe757512020-12-18 12:17:47 -07002411 bool events_not_found = false;
John Zulauf4a6105a2020-11-17 15:11:05 -07002412 for (uint32_t event_index = 0; event_index < eventCount; event_index++) {
2413 const auto event = pEvents[event_index];
2414 const auto *sync_event = GetEventState(event);
John Zulauffe757512020-12-18 12:17:47 -07002415 if (!sync_event) {
2416 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
2417 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives.
2418
2419 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
2420 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002421
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002422 event_stage_masks |= sync_event->scope.mask_param;
John Zulauf4a6105a2020-11-17 15:11:05 -07002423 const auto ignore_reason = sync_event->IsIgnoredByWait(srcStageMask);
2424 if (ignore_reason) {
2425 switch (ignore_reason) {
2426 case SyncEventState::ResetWaitRace: {
2427 const char *const cmd_name = CommandTypeString(cmd);
2428 const char *const vuid = "SYNC-vkCmdWaitEvents-missingbarrier-reset";
2429 const char *const message =
2430 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
2431 skip |=
2432 sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2433 cmd_name, CommandTypeString(sync_event->last_command), ignored);
2434 break;
2435 }
2436 case SyncEventState::SetRace: {
2437 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for this
2438 // event
2439 const char *const cmd_name = CommandTypeString(cmd);
2440 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
2441 const char *const message =
2442 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, % %s";
2443 const char *const reason = "First synchronization scope is undefined.";
2444 skip |=
2445 sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2446 CommandTypeString(sync_event->last_command), reason, ignored);
2447 break;
2448 }
2449 case SyncEventState::MissingStageBits: {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002450 const VkPipelineStageFlags missing_bits = sync_event->scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07002451 // Issue error message that event waited for is not in wait events scope
2452 const char *const cmd_name = CommandTypeString(cmd);
2453 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
2454 const char *const message =
2455 "%s: %s stageMask 0x%" PRIx32 " includes bits not present in srcStageMask 0x%" PRIx32
2456 ". Bits missing from srcStageMask %s. %s";
2457 skip |= sync_state_->LogError(
2458 event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002459 sync_event->scope.mask_param, srcStageMask, string_VkPipelineStageFlags(missing_bits).c_str(), ignored);
John Zulauf4a6105a2020-11-17 15:11:05 -07002460 break;
2461 }
2462 default:
2463 assert(ignore_reason == SyncEventState::NotIgnored);
2464 }
2465 } else if (imageMemoryBarrierCount) {
2466 const auto *context = GetCurrentAccessContext();
2467 assert(context);
2468 for (uint32_t barrier_index = 0; barrier_index < imageMemoryBarrierCount; barrier_index++) {
2469 const auto &barrier = pImageMemoryBarriers[barrier_index];
2470 if (barrier.oldLayout == barrier.newLayout) continue;
2471 const auto *image_state = sync_state_->Get<IMAGE_STATE>(barrier.image);
2472 if (!image_state) continue;
2473 auto subresource_range = NormalizeSubresourceRange(image_state->createInfo, barrier.subresourceRange);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002474 const auto src_access_scope = SyncStageAccess::AccessScope(sync_event->scope.valid_accesses, barrier.srcAccessMask);
John Zulauf4a6105a2020-11-17 15:11:05 -07002475 const auto hazard =
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002476 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
2477 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
John Zulauf4a6105a2020-11-17 15:11:05 -07002478 if (hazard.hazard) {
2479 const char *const cmd_name = CommandTypeString(cmd);
2480 skip |= sync_state_->LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
2481 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", cmd_name,
2482 string_SyncHazard(hazard.hazard), barrier_index,
2483 sync_state_->report_data->FormatHandle(barrier.image).c_str(),
2484 string_UsageTag(hazard).c_str());
2485 break;
2486 }
2487 }
2488 }
2489 }
2490
2491 // Note that we can't check for HOST in pEvents as we don't track that set event type
2492 const auto extra_stage_bits = (srcStageMask & ~VK_PIPELINE_STAGE_HOST_BIT) & ~event_stage_masks;
2493 if (extra_stage_bits) {
2494 // Issue error message that event waited for is not in wait events scope
2495 const char *const cmd_name = CommandTypeString(cmd);
2496 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
2497 const char *const message =
John Zulauffe757512020-12-18 12:17:47 -07002498 "%s: srcStageMask 0x%" PRIx32 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
2499 if (events_not_found) {
2500 skip |= sync_state_->LogInfo(cb_state_->commandBuffer, vuid, message, cmd_name, srcStageMask,
2501 string_VkPipelineStageFlags(extra_stage_bits).c_str(),
2502 " vkCmdSetEvent may be in previously submitted command buffer.");
2503 } else {
2504 skip |= sync_state_->LogError(cb_state_->commandBuffer, vuid, message, cmd_name, srcStageMask,
2505 string_VkPipelineStageFlags(extra_stage_bits).c_str(), "");
2506 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002507 }
2508 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002509}
2510
2511void CommandBufferAccessContext::RecordWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
2512 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
2513 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2514 uint32_t bufferMemoryBarrierCount,
2515 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2516 uint32_t imageMemoryBarrierCount,
John Zulauf4a6105a2020-11-17 15:11:05 -07002517 const VkImageMemoryBarrier *pImageMemoryBarriers, const ResourceUsageTag &tag) {
2518 auto *access_context = GetCurrentAccessContext();
2519 assert(access_context);
2520 if (!access_context) return;
2521
2522 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
2523 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
2524 // 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 -07002525 access_context->ResolvePreviousAccesses();
2526
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002527 auto dst = SyncExecScope::MakeDst(GetQueueFlags(), dstStageMask);
John Zulauf4a6105a2020-11-17 15:11:05 -07002528 for (uint32_t event_index = 0; event_index < eventCount; event_index++) {
2529 const auto event = pEvents[event_index];
2530 auto *sync_event = GetEventState(event);
2531 if (!sync_event) continue;
2532
2533 sync_event->last_command = CMD_WAITEVENTS;
2534
2535 if (!sync_event->IsIgnoredByWait(srcStageMask)) {
2536 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
2537 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
2538 // of the barriers is maintained.
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002539 ApplyBufferBarriers(*sync_event, dst, bufferMemoryBarrierCount, pBufferMemoryBarriers);
2540 ApplyImageBarriers(*sync_event, dst, imageMemoryBarrierCount, pImageMemoryBarriers, tag);
2541 ApplyGlobalBarriers(*sync_event, dst, memoryBarrierCount, pMemoryBarriers, tag);
John Zulauf4a6105a2020-11-17 15:11:05 -07002542 } else {
2543 // We ignored this wait, so we don't have any effective synchronization barriers for it.
2544 sync_event->barriers = 0U;
2545 }
2546 }
2547
2548 // Apply the pending barriers
2549 ResolvePendingBarrierFunctor apply_pending_action(tag);
2550 access_context->ApplyGlobalBarriers(apply_pending_action);
2551}
2552
2553void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2554 // Erase is okay with the key not being
2555 event_state_.erase(event);
2556}
2557
2558SyncEventState *CommandBufferAccessContext::GetEventState(VkEvent event) {
2559 auto &event_up = event_state_[event];
2560 if (!event_up) {
2561 auto event_atate = sync_state_->GetShared<EVENT_STATE>(event);
2562 event_up.reset(new SyncEventState(event_atate));
2563 }
2564 return event_up.get();
2565}
2566
2567const SyncEventState *CommandBufferAccessContext::GetEventState(VkEvent event) const {
2568 auto event_it = event_state_.find(event);
2569 if (event_it == event_state_.cend()) {
2570 return nullptr;
2571 }
2572 return event_it->second.get();
2573}
John Zulauf49beb112020-11-04 16:06:31 -07002574
locke-lunarg61870c22020-06-09 14:51:50 -06002575bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
2576 const VkRect2D &render_area, const char *func_name) const {
2577 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002578 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2579 if (!pipe ||
2580 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002581 return skip;
2582 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002583 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002584 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2585 VkExtent3D extent = CastTo3D(render_area.extent);
2586 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06002587
John Zulauf1a224292020-06-30 14:52:13 -06002588 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002589 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002590 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2591 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002592 if (location >= subpass.colorAttachmentCount ||
2593 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002594 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002595 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002596 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002597 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07002598 SyncOrdering::kColorAttachment, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06002599 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002600 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002601 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002602 func_name, string_SyncHazard(hazard.hazard),
2603 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2604 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002605 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002606 }
2607 }
2608 }
locke-lunarg37047832020-06-12 13:44:45 -06002609
2610 // PHASE1 TODO: Add layout based read/vs. write selection.
2611 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002612 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002613 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002614 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002615 bool depth_write = false, stencil_write = false;
2616
2617 // PHASE1 TODO: These validation should be in core_checks.
2618 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002619 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2620 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002621 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2622 depth_write = true;
2623 }
2624 // PHASE1 TODO: It needs to check if stencil is writable.
2625 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2626 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2627 // PHASE1 TODO: These validation should be in core_checks.
2628 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002629 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002630 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2631 stencil_write = true;
2632 }
2633
2634 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2635 if (depth_write) {
2636 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002637 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07002638 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002639 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002640 skip |= sync_state.LogError(
2641 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002642 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002643 func_name, string_SyncHazard(hazard.hazard),
2644 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2645 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002646 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002647 }
2648 }
2649 if (stencil_write) {
2650 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002651 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
John Zulauf8e3c3e92021-01-06 11:19:36 -07002652 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002653 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002654 skip |= sync_state.LogError(
2655 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002656 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002657 func_name, string_SyncHazard(hazard.hazard),
2658 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2659 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002660 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002661 }
locke-lunarg61870c22020-06-09 14:51:50 -06002662 }
2663 }
2664 return skip;
2665}
2666
locke-lunarg96dc9632020-06-10 17:22:18 -06002667void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
2668 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002669 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2670 if (!pipe ||
2671 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002672 return;
2673 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002674 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002675 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2676 VkExtent3D extent = CastTo3D(render_area.extent);
2677 VkOffset3D offset = CastTo3D(render_area.offset);
2678
John Zulauf1a224292020-06-30 14:52:13 -06002679 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002680 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002681 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2682 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002683 if (location >= subpass.colorAttachmentCount ||
2684 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002685 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002686 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002687 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf8e3c3e92021-01-06 11:19:36 -07002688 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
2689 SyncOrdering::kColorAttachment, offset, extent, 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002690 }
2691 }
locke-lunarg37047832020-06-12 13:44:45 -06002692
2693 // PHASE1 TODO: Add layout based read/vs. write selection.
2694 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002695 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002696 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002697 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002698 bool depth_write = false, stencil_write = false;
2699
2700 // PHASE1 TODO: These validation should be in core_checks.
2701 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002702 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2703 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002704 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2705 depth_write = true;
2706 }
2707 // PHASE1 TODO: It needs to check if stencil is writable.
2708 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2709 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2710 // PHASE1 TODO: These validation should be in core_checks.
2711 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002712 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002713 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2714 stencil_write = true;
2715 }
2716
2717 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2718 if (depth_write) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002719 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2720 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT,
2721 tag);
locke-lunarg37047832020-06-12 13:44:45 -06002722 }
2723 if (stencil_write) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002724 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2725 SyncOrdering::kDepthStencilAttachment, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT,
2726 tag);
locke-lunarg37047832020-06-12 13:44:45 -06002727 }
locke-lunarg61870c22020-06-09 14:51:50 -06002728 }
2729}
2730
John Zulauf1507ee42020-05-18 11:33:09 -06002731bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
2732 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002733 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002734 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06002735 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2736 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002737 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2738 func_name);
2739
John Zulauf355e49b2020-04-24 15:11:15 -06002740 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002741 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06002742 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002743 if (!skip) {
2744 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2745 // on a copy of the (empty) next context.
2746 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2747 AccessContext temp_context(next_context);
2748 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
2749 skip |= temp_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2750 }
John Zulauf7635de32020-05-29 17:14:15 -06002751 return skip;
2752}
2753bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
2754 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002755 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002756 bool skip = false;
2757 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2758 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002759 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2760 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002761 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002762 return skip;
2763}
2764
John Zulauf7635de32020-05-29 17:14:15 -06002765AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2766 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2767}
2768
2769bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2770 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002771 bool skip = false;
2772
John Zulauf7635de32020-05-29 17:14:15 -06002773 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2774 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2775 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2776 // to apply and only copy then, if this proves a hot spot.
2777 std::unique_ptr<AccessContext> proxy_for_current;
2778
John Zulauf355e49b2020-04-24 15:11:15 -06002779 // Validate the "finalLayout" transitions to external
2780 // Get them from where there we're hidding in the extra entry.
2781 const auto &final_transitions = rp_state_->subpass_transitions.back();
2782 for (const auto &transition : final_transitions) {
2783 const auto &attach_view = attachment_views_[transition.attachment];
2784 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2785 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002786 auto *context = trackback.context;
2787
2788 if (transition.prev_pass == current_subpass_) {
2789 if (!proxy_for_current) {
2790 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2791 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2792 }
2793 context = proxy_for_current.get();
2794 }
2795
John Zulaufa0a98292020-09-18 09:30:10 -06002796 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2797 const auto merged_barrier = MergeBarriers(trackback.barriers);
2798 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2799 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2800 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002801 if (hazard.hazard) {
2802 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2803 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002804 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002805 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002806 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002807 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002808 }
2809 }
2810 return skip;
2811}
2812
2813void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2814 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002815 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002816}
2817
John Zulauf1507ee42020-05-18 11:33:09 -06002818void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2819 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2820 auto &subpass_context = subpass_contexts_[current_subpass_];
2821 VkExtent3D extent = CastTo3D(render_area.extent);
2822 VkOffset3D offset = CastTo3D(render_area.offset);
2823
2824 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2825 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2826 if (attachment_views_[i] == nullptr) continue; // UNUSED
2827 const auto &view = *attachment_views_[i];
2828 const IMAGE_STATE *image = view.image_state.get();
2829 if (image == nullptr) continue;
2830
2831 const auto &ci = attachment_ci[i];
2832 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002833 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002834 const bool is_color = !(has_depth || has_stencil);
2835
2836 if (is_color) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002837 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), SyncOrdering::kColorAttachment,
2838 view.normalized_subresource_range, offset, extent, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002839 } else {
2840 auto update_range = view.normalized_subresource_range;
2841 if (has_depth) {
2842 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07002843 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp),
2844 SyncOrdering::kDepthStencilAttachment, update_range, offset, extent, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002845 }
2846 if (has_stencil) {
2847 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf8e3c3e92021-01-06 11:19:36 -07002848 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp),
2849 SyncOrdering::kDepthStencilAttachment, update_range, offset, extent, tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002850 }
2851 }
2852 }
2853 }
2854}
2855
John Zulauf355e49b2020-04-24 15:11:15 -06002856void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002857 const AccessContext *external_context, VkQueueFlags queue_flags,
2858 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002859 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002860 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002861 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2862 // Add this for all subpasses here so that they exsist during next subpass validation
2863 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002864 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002865 }
2866 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2867
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002868 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002869 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002870 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002871}
John Zulauf1507ee42020-05-18 11:33:09 -06002872
2873void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002874 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2875 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002876 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002877
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002878 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2879 // subpass, so their tag needs to be different from the layout and load operations below.
Jeremy Gebben4bb73502020-12-14 11:17:50 -07002880 ResourceUsageTag next_tag = tag.NextSubCommand();
John Zulauf355e49b2020-04-24 15:11:15 -06002881 current_subpass_++;
2882 assert(current_subpass_ < subpass_contexts_.size());
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002883 subpass_contexts_[current_subpass_].SetStartTag(next_tag);
2884 RecordLayoutTransitions(next_tag);
2885 RecordLoadOperations(render_area, next_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002886}
2887
John Zulauf1a224292020-06-30 14:52:13 -06002888void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2889 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002890 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002891 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002892 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002893
John Zulauf355e49b2020-04-24 15:11:15 -06002894 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002895 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002896
2897 // Add the "finalLayout" transitions to external
2898 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002899 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2900 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2901 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002902 const auto &final_transitions = rp_state_->subpass_transitions.back();
2903 for (const auto &transition : final_transitions) {
2904 const auto &attachment = attachment_views_[transition.attachment];
2905 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002906 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf1e331ec2020-12-04 18:29:38 -07002907 std::vector<PipelineBarrierOp> barrier_ops;
2908 barrier_ops.reserve(last_trackback.barriers.size());
2909 for (const auto &barrier : last_trackback.barriers) {
2910 barrier_ops.emplace_back(barrier, true);
2911 }
2912 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, barrier_ops, tag);
2913 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002914 }
2915}
2916
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002917SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags mask_param) {
2918 SyncExecScope result;
2919 result.mask_param = mask_param;
2920 result.expanded_mask = ExpandPipelineStages(queue_flags, mask_param);
2921 result.exec_scope = WithEarlierPipelineStages(result.expanded_mask);
2922 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2923 return result;
2924}
2925
2926SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags mask_param) {
2927 SyncExecScope result;
2928 result.mask_param = mask_param;
2929 result.expanded_mask = ExpandPipelineStages(queue_flags, mask_param);
2930 result.exec_scope = WithLaterPipelineStages(result.expanded_mask);
2931 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2932 return result;
2933}
2934
2935SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
2936 src_exec_scope = src.exec_scope;
2937 src_access_scope = 0;
2938 dst_exec_scope = dst.exec_scope;
2939 dst_access_scope = 0;
2940}
2941
2942template <typename Barrier>
2943SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
2944 src_exec_scope = src.exec_scope;
2945 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2946 dst_exec_scope = dst.exec_scope;
2947 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2948}
2949
2950SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
2951 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
2952 src_exec_scope = src.exec_scope;
2953 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2954
2955 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
2956 dst_exec_scope = dst.exec_scope;
2957 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002958}
2959
John Zulaufb02c1eb2020-10-06 16:33:36 -06002960// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2961void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2962 for (const auto &barrier : barriers) {
2963 ApplyBarrier(barrier, layout_transition);
2964 }
2965}
2966
John Zulauf89311b42020-09-29 16:28:47 -06002967// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2968// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2969// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002970void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2971 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002972 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002973 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002974 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002975 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002976 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002977 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002978}
John Zulauf9cb530d2019-09-30 14:14:10 -06002979HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2980 HazardResult hazard;
2981 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002982 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002983 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002984 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002985 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002986 }
2987 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002988 // Write operation:
2989 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2990 // If reads exists -- test only against them because either:
2991 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2992 // * 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
2993 // the current write happens after the reads, so just test the write against the reades
2994 // Otherwise test against last_write
2995 //
2996 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002997 if (last_reads.size()) {
2998 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002999 if (IsReadHazard(usage_stage, read_access)) {
3000 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3001 break;
3002 }
3003 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003004 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06003005 // Write-After-Write check -- if we have a previous write to test against
3006 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003007 }
3008 }
3009 return hazard;
3010}
3011
John Zulauf8e3c3e92021-01-06 11:19:36 -07003012HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering &ordering_rule) const {
3013 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06003014 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
3015 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06003016 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06003017 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003018 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
3019 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06003020 if (IsRead(usage_bit)) {
3021 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
3022 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
3023 if (is_raw_hazard) {
3024 // NOTE: we know last_write is non-zero
3025 // See if the ordering rules save us from the simple RAW check above
3026 // First check to see if the current usage is covered by the ordering rules
3027 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
3028 const bool usage_is_ordered =
3029 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
3030 if (usage_is_ordered) {
3031 // Now see of the most recent write (or a subsequent read) are ordered
3032 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
3033 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06003034 }
3035 }
John Zulauf4285ee92020-09-23 10:20:52 -06003036 if (is_raw_hazard) {
3037 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
3038 }
John Zulauf361fb532020-07-22 10:45:39 -06003039 } else {
3040 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003041 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07003042 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06003043 // Look for any WAR hazards outside the ordered set of stages
John Zulauf4285ee92020-09-23 10:20:52 -06003044 VkPipelineStageFlags ordered_stages = 0;
3045 if (usage_write_is_ordered) {
3046 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
3047 ordered_stages = GetOrderedStages(ordering);
3048 }
3049 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
3050 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003051 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06003052 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
3053 if (IsReadHazard(usage_stage, read_access)) {
3054 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3055 break;
3056 }
John Zulaufd14743a2020-07-03 09:42:39 -06003057 }
3058 }
John Zulauf4285ee92020-09-23 10:20:52 -06003059 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003060 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003061 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06003062 }
John Zulauf69133422020-05-20 14:55:53 -06003063 }
3064 }
3065 return hazard;
3066}
3067
John Zulauf2f952d22020-02-10 11:34:51 -07003068// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003069HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07003070 HazardResult hazard;
3071 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003072 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
3073 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
3074 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07003075 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003076 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06003077 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07003078 }
3079 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003080 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06003081 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07003082 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003083 // 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 -07003084 for (const auto &read_access : last_reads) {
3085 if (read_access.tag.index >= start_tag.index) {
3086 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003087 break;
3088 }
3089 }
John Zulauf2f952d22020-02-10 11:34:51 -07003090 }
3091 }
3092 return hazard;
3093}
3094
John Zulauf36bcf6a2020-02-03 15:12:52 -07003095HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003096 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07003097 // Only supporting image layout transitions for now
3098 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3099 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06003100 // only test for WAW if there no intervening read operations.
3101 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07003102 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06003103 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07003104 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003105 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06003106 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07003107 break;
3108 }
3109 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003110 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3111 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3112 }
3113
3114 return hazard;
3115}
3116
3117HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
3118 const SyncStageAccessFlags &src_access_scope,
3119 const ResourceUsageTag &event_tag) const {
3120 // Only supporting image layout transitions for now
3121 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3122 HazardResult hazard;
3123 // only test for WAW if there no intervening read operations.
3124 // See DetectHazard(SyncStagetAccessIndex) above for more details.
3125
John Zulaufab7756b2020-12-29 16:10:16 -07003126 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003127 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
3128 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07003129 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003130 if (read_access.tag.IsBefore(event_tag)) {
3131 // The read is in the events first synchronization scope, so we use a barrier hazard check
3132 // If the read stage is not in the src sync scope
3133 // *AND* not execution chained with an existing sync barrier (that's the or)
3134 // then the barrier access is unsafe (R/W after R)
3135 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3136 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3137 break;
3138 }
3139 } else {
3140 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3141 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3142 }
3143 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003144 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003145 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
3146 if (write_tag.IsBefore(event_tag)) {
3147 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3148 // So do a normal barrier hazard check
3149 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3150 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3151 }
3152 } else {
3153 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003154 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3155 }
John Zulaufd14743a2020-07-03 09:42:39 -06003156 }
John Zulauf361fb532020-07-22 10:45:39 -06003157
John Zulauf0cb5be22020-01-23 12:18:22 -07003158 return hazard;
3159}
3160
John Zulauf5f13a792020-03-10 07:31:21 -06003161// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3162// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3163// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3164void ResourceAccessState::Resolve(const ResourceAccessState &other) {
3165 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003166 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3167 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003168 *this = other;
3169 } else if (!other.write_tag.IsBefore(write_tag)) {
3170 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
3171 // dependency chaining logic or any stage expansion)
3172 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003173 pending_write_barriers |= other.pending_write_barriers;
3174 pending_layout_transition |= other.pending_layout_transition;
3175 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003176
John Zulaufd14743a2020-07-03 09:42:39 -06003177 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07003178 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06003179 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07003180 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003181 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003182 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003183 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003184 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3185 // but we should wait on profiling data for that.
3186 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003187 auto &my_read = last_reads[my_read_index];
3188 if (other_read.stage == my_read.stage) {
3189 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003190 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003191 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003192 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003193 my_read.pending_dep_chain = other_read.pending_dep_chain;
3194 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3195 // May require tracking more than one access per stage.
3196 my_read.barriers = other_read.barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003197 if (my_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
3198 // Since I'm overwriting the fragement stage read, also update the input attachment info
3199 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003200 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003201 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003202 } else if (other_read.tag.IsBefore(my_read.tag)) {
3203 // The read tags match so merge the barriers
3204 my_read.barriers |= other_read.barriers;
3205 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003206 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003207
John Zulauf5f13a792020-03-10 07:31:21 -06003208 break;
3209 }
3210 }
3211 } else {
3212 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07003213 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06003214 last_read_stages |= other_read.stage;
John Zulauf4285ee92020-09-23 10:20:52 -06003215 if (other_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06003216 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003217 }
John Zulauf5f13a792020-03-10 07:31:21 -06003218 }
3219 }
John Zulauf361fb532020-07-22 10:45:39 -06003220 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003221 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3222 // ignore it.
John Zulauf5f13a792020-03-10 07:31:21 -06003223}
3224
John Zulauf8e3c3e92021-01-06 11:19:36 -07003225void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag &tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003226 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3227 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003228 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003229 // Mulitple outstanding reads may be of interest and do dependency chains independently
3230 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3231 const auto usage_stage = PipelineStageBit(usage_index);
3232 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003233 for (auto &read_access : last_reads) {
3234 if (read_access.stage == usage_stage) {
3235 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003236 break;
3237 }
3238 }
3239 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07003240 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003241 last_read_stages |= usage_stage;
3242 }
John Zulauf4285ee92020-09-23 10:20:52 -06003243
3244 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
3245 if (usage_stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06003246 // TODO Revisit re: multiple reads for a given stage
3247 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003248 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003249 } else {
3250 // Assume write
3251 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003252 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003253 }
3254}
John Zulauf5f13a792020-03-10 07:31:21 -06003255
John Zulauf89311b42020-09-29 16:28:47 -06003256// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3257// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3258// We can overwrite them as *this* write is now after them.
3259//
3260// 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 -07003261void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003262 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06003263 last_read_stages = 0;
3264 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003265 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003266
3267 write_barriers = 0;
3268 write_dependency_chain = 0;
3269 write_tag = tag;
3270 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003271}
3272
John Zulauf89311b42020-09-29 16:28:47 -06003273// Apply the memory barrier without updating the existing barriers. The execution barrier
3274// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3275// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3276// replace the current write barriers or add to them, so accumulate to pending as well.
3277void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3278 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3279 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003280 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3281 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3282 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3283 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulauf4a6105a2020-11-17 15:11:05 -07003284 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003285 pending_write_barriers |= barrier.dst_access_scope;
3286 pending_write_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003287 }
John Zulauf89311b42020-09-29 16:28:47 -06003288 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3289 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003290
John Zulauf89311b42020-09-29 16:28:47 -06003291 if (!pending_layout_transition) {
3292 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3293 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003294 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06003295 // 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 -07003296 if (barrier.src_exec_scope & (read_access.stage | read_access.barriers)) {
3297 read_access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003298 }
3299 }
John Zulaufa0a98292020-09-18 09:30:10 -06003300 }
John Zulaufa0a98292020-09-18 09:30:10 -06003301}
3302
John Zulauf4a6105a2020-11-17 15:11:05 -07003303// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3304// changes the "chaining" state, but to keep barriers independent. See discussion above.
3305void ResourceAccessState::ApplyBarrier(const ResourceUsageTag &scope_tag, const SyncBarrier &barrier, bool layout_transition) {
3306 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3307 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3308 // in order to know if it's in the excecution scope
3309 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3310 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3311 // errors w.r.t. "most recent" accesses.
3312 if (layout_transition || ((write_tag.IsBefore(scope_tag)) && (barrier.src_access_scope & last_write).any())) {
3313 pending_write_barriers |= barrier.dst_access_scope;
3314 pending_write_dep_chain |= barrier.dst_exec_scope;
3315 }
3316 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3317 pending_layout_transition |= layout_transition;
3318
3319 if (!pending_layout_transition) {
3320 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3321 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003322 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003323 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3324 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3325 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3326 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3327 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3328 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3329 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulaufab7756b2020-12-29 16:10:16 -07003330 if (read_access.tag.IsBefore(scope_tag) && (barrier.src_exec_scope & (read_access.stage | read_access.barriers))) {
3331 read_access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003332 }
3333 }
3334 }
3335}
John Zulauf89311b42020-09-29 16:28:47 -06003336void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
3337 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06003338 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
3339 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
3340 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003341 }
John Zulauf89311b42020-09-29 16:28:47 -06003342
3343 // Apply the accumulate execution barriers (and thus update chaining information)
3344 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003345 for (auto &read_access : last_reads) {
3346 read_access.barriers |= read_access.pending_dep_chain;
3347 read_execution_barriers |= read_access.barriers;
3348 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003349 }
3350
3351 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3352 write_dependency_chain |= pending_write_dep_chain;
3353 write_barriers |= pending_write_barriers;
3354 pending_write_dep_chain = 0;
3355 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003356}
3357
John Zulauf59e25072020-07-17 10:55:21 -06003358// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003359VkPipelineStageFlags ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
John Zulauf59e25072020-07-17 10:55:21 -06003360 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003361
John Zulaufab7756b2020-12-29 16:10:16 -07003362 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003363 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003364 barriers = read_access.barriers;
3365 break;
John Zulauf59e25072020-07-17 10:55:21 -06003366 }
3367 }
John Zulauf4285ee92020-09-23 10:20:52 -06003368
John Zulauf59e25072020-07-17 10:55:21 -06003369 return barriers;
3370}
3371
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003372inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003373 assert(IsRead(usage));
3374 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3375 // * the previous reads are not hazards, and thus last_write must be visible and available to
3376 // any reads that happen after.
3377 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3378 // 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 -07003379 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003380}
3381
John Zulauf8e3c3e92021-01-06 11:19:36 -07003382VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003383 // Whether the stage are in the ordering scope only matters if the current write is ordered
3384 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
3385 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003386 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003387 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003388 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
3389 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
3390 }
3391
3392 return ordered_stages;
3393}
3394
John Zulaufd1f85d42020-04-15 12:23:15 -06003395void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003396 auto *access_context = GetAccessContextNoInsert(command_buffer);
3397 if (access_context) {
3398 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003399 }
3400}
3401
John Zulaufd1f85d42020-04-15 12:23:15 -06003402void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3403 auto access_found = cb_access_state.find(command_buffer);
3404 if (access_found != cb_access_state.end()) {
3405 access_found->second->Reset();
3406 cb_access_state.erase(access_found);
3407 }
3408}
3409
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003410void SyncValidator::ApplyGlobalBarriers(AccessContext *context, const SyncExecScope &src, const SyncExecScope &dst,
3411 uint32_t memory_barrier_count, const VkMemoryBarrier *pMemoryBarriers,
3412 const ResourceUsageTag &tag) {
John Zulauf1e331ec2020-12-04 18:29:38 -07003413 std::vector<PipelineBarrierOp> barrier_ops;
3414 barrier_ops.reserve(std::min<uint32_t>(1, memory_barrier_count));
John Zulauf89311b42020-09-29 16:28:47 -06003415 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
3416 const auto &barrier = pMemoryBarriers[barrier_index];
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003417 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf1e331ec2020-12-04 18:29:38 -07003418 barrier_ops.emplace_back(sync_barrier, false);
John Zulauf89311b42020-09-29 16:28:47 -06003419 }
3420 if (0 == memory_barrier_count) {
3421 // If there are no global memory barriers, force an exec barrier
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003422 barrier_ops.emplace_back(SyncBarrier(src, dst), false);
John Zulauf89311b42020-09-29 16:28:47 -06003423 }
John Zulauf1e331ec2020-12-04 18:29:38 -07003424 ApplyBarrierOpsFunctor<PipelineBarrierOp> barriers_functor(true /* resolve */, barrier_ops, tag);
John Zulauf540266b2020-04-06 18:54:53 -06003425 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06003426}
3427
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003428void SyncValidator::ApplyBufferBarriers(AccessContext *context, const SyncExecScope &src, const SyncExecScope &dst,
3429 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003430 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003431 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06003432 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
3433 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06003434 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
3435 const ResourceAccessRange range = MakeRange(barrier);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003436 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07003437 const ApplyBarrierFunctor<PipelineBarrierOp> update_action({sync_barrier, false /* layout_transition */});
John Zulauf89311b42020-09-29 16:28:47 -06003438 context->UpdateResourceAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06003439 }
3440}
3441
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003442void SyncValidator::ApplyImageBarriers(AccessContext *context, const SyncExecScope &src, const SyncExecScope &dst,
3443 uint32_t barrier_count, const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07003444 for (uint32_t index = 0; index < barrier_count; index++) {
3445 const auto &barrier = barriers[index];
3446 const auto *image = Get<IMAGE_STATE>(barrier.image);
3447 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06003448 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06003449 bool layout_transition = barrier.oldLayout != barrier.newLayout;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003450 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07003451 const ApplyBarrierFunctor<PipelineBarrierOp> barrier_action({sync_barrier, layout_transition});
John Zulauf89311b42020-09-29 16:28:47 -06003452 context->UpdateResourceAccess(*image, subresource_range, barrier_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06003453 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003454}
3455
3456bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3457 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3458 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003459 const auto *cb_context = GetAccessContext(commandBuffer);
3460 assert(cb_context);
3461 if (!cb_context) return skip;
3462 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003463
John Zulauf3d84f1b2020-03-09 13:33:25 -06003464 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003465 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003466 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003467
3468 for (uint32_t region = 0; region < regionCount; region++) {
3469 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003470 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003471 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003472 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003473 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003474 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003475 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003476 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003477 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003478 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003479 }
John Zulauf16adfc92020-04-08 10:28:33 -06003480 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003481 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06003482 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003483 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003484 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003485 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003486 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003487 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003488 }
3489 }
3490 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003491 }
3492 return skip;
3493}
3494
3495void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3496 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003497 auto *cb_context = GetAccessContext(commandBuffer);
3498 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003499 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003500 auto *context = cb_context->GetCurrentAccessContext();
3501
John Zulauf9cb530d2019-09-30 14:14:10 -06003502 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003503 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003504
3505 for (uint32_t region = 0; region < regionCount; region++) {
3506 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003507 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003508 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003509 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003510 }
John Zulauf16adfc92020-04-08 10:28:33 -06003511 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003512 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003513 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003514 }
3515 }
3516}
3517
John Zulauf4a6105a2020-11-17 15:11:05 -07003518void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3519 // Clear out events from the command buffer contexts
3520 for (auto &cb_context : cb_access_state) {
3521 cb_context.second->RecordDestroyEvent(event);
3522 }
3523}
3524
Jeff Leger178b1e52020-10-05 12:22:23 -04003525bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3526 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3527 bool skip = false;
3528 const auto *cb_context = GetAccessContext(commandBuffer);
3529 assert(cb_context);
3530 if (!cb_context) return skip;
3531 const auto *context = cb_context->GetCurrentAccessContext();
3532
3533 // If we have no previous accesses, we have no hazards
3534 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3535 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3536
3537 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3538 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3539 if (src_buffer) {
3540 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
3541 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
3542 if (hazard.hazard) {
3543 // TODO -- add tag information to log msg when useful.
3544 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3545 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3546 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
3547 region, string_UsageTag(hazard).c_str());
3548 }
3549 }
3550 if (dst_buffer && !skip) {
3551 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
3552 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
3553 if (hazard.hazard) {
3554 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3555 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3556 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
3557 region, string_UsageTag(hazard).c_str());
3558 }
3559 }
3560 if (skip) break;
3561 }
3562 return skip;
3563}
3564
3565void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3566 auto *cb_context = GetAccessContext(commandBuffer);
3567 assert(cb_context);
3568 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3569 auto *context = cb_context->GetCurrentAccessContext();
3570
3571 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3572 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3573
3574 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3575 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3576 if (src_buffer) {
3577 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003578 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003579 }
3580 if (dst_buffer) {
3581 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003582 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003583 }
3584 }
3585}
3586
John Zulauf5c5e88d2019-12-26 11:22:02 -07003587bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3588 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3589 const VkImageCopy *pRegions) const {
3590 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003591 const auto *cb_access_context = GetAccessContext(commandBuffer);
3592 assert(cb_access_context);
3593 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003594
John Zulauf3d84f1b2020-03-09 13:33:25 -06003595 const auto *context = cb_access_context->GetCurrentAccessContext();
3596 assert(context);
3597 if (!context) return skip;
3598
3599 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3600 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003601 for (uint32_t region = 0; region < regionCount; region++) {
3602 const auto &copy_region = pRegions[region];
3603 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003604 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003605 copy_region.srcOffset, copy_region.extent);
3606 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003607 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003608 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003609 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003610 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003611 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003612 }
3613
3614 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003615 VkExtent3D dst_copy_extent =
3616 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06003617 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003618 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003619 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003620 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003621 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003622 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003623 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003624 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003625 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003626 }
3627 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003628
John Zulauf5c5e88d2019-12-26 11:22:02 -07003629 return skip;
3630}
3631
3632void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3633 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3634 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003635 auto *cb_access_context = GetAccessContext(commandBuffer);
3636 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003637 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003638 auto *context = cb_access_context->GetCurrentAccessContext();
3639 assert(context);
3640
John Zulauf5c5e88d2019-12-26 11:22:02 -07003641 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003642 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003643
3644 for (uint32_t region = 0; region < regionCount; region++) {
3645 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003646 if (src_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07003647 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
3648 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003649 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003650 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003651 VkExtent3D dst_copy_extent =
3652 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003653 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
3654 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003655 }
3656 }
3657}
3658
Jeff Leger178b1e52020-10-05 12:22:23 -04003659bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3660 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3661 bool skip = false;
3662 const auto *cb_access_context = GetAccessContext(commandBuffer);
3663 assert(cb_access_context);
3664 if (!cb_access_context) return skip;
3665
3666 const auto *context = cb_access_context->GetCurrentAccessContext();
3667 assert(context);
3668 if (!context) return skip;
3669
3670 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3671 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3672 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3673 const auto &copy_region = pCopyImageInfo->pRegions[region];
3674 if (src_image) {
3675 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
3676 copy_region.srcOffset, copy_region.extent);
3677 if (hazard.hazard) {
3678 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3679 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3680 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
3681 region, string_UsageTag(hazard).c_str());
3682 }
3683 }
3684
3685 if (dst_image) {
3686 VkExtent3D dst_copy_extent =
3687 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
3688 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
3689 copy_region.dstOffset, dst_copy_extent);
3690 if (hazard.hazard) {
3691 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3692 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3693 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
3694 region, string_UsageTag(hazard).c_str());
3695 }
3696 if (skip) break;
3697 }
3698 }
3699
3700 return skip;
3701}
3702
3703void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3704 auto *cb_access_context = GetAccessContext(commandBuffer);
3705 assert(cb_access_context);
3706 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3707 auto *context = cb_access_context->GetCurrentAccessContext();
3708 assert(context);
3709
3710 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3711 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3712
3713 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3714 const auto &copy_region = pCopyImageInfo->pRegions[region];
3715 if (src_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07003716 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
3717 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003718 }
3719 if (dst_image) {
3720 VkExtent3D dst_copy_extent =
3721 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003722 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
3723 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003724 }
3725 }
3726}
3727
John Zulauf9cb530d2019-09-30 14:14:10 -06003728bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3729 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3730 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3731 uint32_t bufferMemoryBarrierCount,
3732 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3733 uint32_t imageMemoryBarrierCount,
3734 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3735 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003736 const auto *cb_access_context = GetAccessContext(commandBuffer);
3737 assert(cb_access_context);
3738 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003739
John Zulauf3d84f1b2020-03-09 13:33:25 -06003740 const auto *context = cb_access_context->GetCurrentAccessContext();
3741 assert(context);
3742 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003743
John Zulauf3d84f1b2020-03-09 13:33:25 -06003744 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003745 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3746 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07003747 // Validate Image Layout transitions
3748 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
3749 const auto &barrier = pImageMemoryBarriers[index];
3750 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
3751 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
3752 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06003753 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07003754 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003755 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003756 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003757 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003758 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003759 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07003760 }
3761 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003762
3763 return skip;
3764}
3765
3766void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3767 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3768 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3769 uint32_t bufferMemoryBarrierCount,
3770 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3771 uint32_t imageMemoryBarrierCount,
3772 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003773 auto *cb_access_context = GetAccessContext(commandBuffer);
3774 assert(cb_access_context);
3775 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06003776 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003777 auto access_context = cb_access_context->GetCurrentAccessContext();
3778 assert(access_context);
3779 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003780
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003781 auto src = SyncExecScope::MakeSrc(cb_access_context->GetQueueFlags(), srcStageMask);
3782 auto dst = SyncExecScope::MakeDst(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf89311b42020-09-29 16:28:47 -06003783
3784 // These two apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
3785 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
3786 // of the barriers is maintained.
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003787 ApplyBufferBarriers(access_context, src, dst, bufferMemoryBarrierCount, pBufferMemoryBarriers);
3788 ApplyImageBarriers(access_context, src, dst, imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003789
John Zulauf89311b42020-09-29 16:28:47 -06003790 // Apply the global barriers last as is it walks all memory, it can also clean up the "pending" state without requiring an
3791 // additional pass, updating the dependency chains *last* as it goes along.
3792 // This is needed to guarantee order independence of the three lists.
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003793 ApplyGlobalBarriers(access_context, src, dst, memoryBarrierCount, pMemoryBarriers, tag);
John Zulauf4a6105a2020-11-17 15:11:05 -07003794
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003795 cb_access_context->ApplyGlobalBarriersToEvents(src, dst);
John Zulauf9cb530d2019-09-30 14:14:10 -06003796}
3797
3798void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3799 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3800 // The state tracker sets up the device state
3801 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3802
John Zulauf5f13a792020-03-10 07:31:21 -06003803 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3804 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003805 // TODO: Find a good way to do this hooklessly.
3806 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3807 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3808 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3809
John Zulaufd1f85d42020-04-15 12:23:15 -06003810 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3811 sync_device_state->ResetCommandBufferCallback(command_buffer);
3812 });
3813 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3814 sync_device_state->FreeCommandBufferCallback(command_buffer);
3815 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003816}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003817
John Zulauf355e49b2020-04-24 15:11:15 -06003818bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003819 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003820 bool skip = false;
3821 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
3822 auto cb_context = GetAccessContext(commandBuffer);
3823
3824 if (rp_state && cb_context) {
3825 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
3826 }
3827
3828 return skip;
3829}
3830
3831bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3832 VkSubpassContents contents) const {
3833 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003834 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003835 subpass_begin_info.contents = contents;
3836 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
3837 return skip;
3838}
3839
3840bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003841 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003842 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3843 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
3844 return skip;
3845}
3846
3847bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3848 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003849 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003850 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3851 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3852 return skip;
3853}
3854
John Zulauf3d84f1b2020-03-09 13:33:25 -06003855void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3856 VkResult result) {
3857 // The state tracker sets up the command buffer state
3858 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3859
3860 // Create/initialize the structure that trackers accesses at the command buffer scope.
3861 auto cb_access_context = GetAccessContext(commandBuffer);
3862 assert(cb_access_context);
3863 cb_access_context->Reset();
3864}
3865
3866void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003867 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003868 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003869 if (cb_context) {
3870 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003871 }
3872}
3873
3874void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3875 VkSubpassContents contents) {
3876 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003877 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003878 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003879 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003880}
3881
3882void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3883 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3884 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003885 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003886}
3887
3888void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3889 const VkRenderPassBeginInfo *pRenderPassBegin,
3890 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3891 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003892 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3893}
3894
Mike Schuchardt2df08912020-12-15 16:28:09 -08003895bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3896 const VkSubpassEndInfo *pSubpassEndInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003897 bool skip = false;
3898
3899 auto cb_context = GetAccessContext(commandBuffer);
3900 assert(cb_context);
3901 auto cb_state = cb_context->GetCommandBufferState();
3902 if (!cb_state) return skip;
3903
3904 auto rp_state = cb_state->activeRenderPass;
3905 if (!rp_state) return skip;
3906
3907 skip |= cb_context->ValidateNextSubpass(func_name);
3908
3909 return skip;
3910}
3911
3912bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3913 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003914 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003915 subpass_begin_info.contents = contents;
3916 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3917 return skip;
3918}
3919
Mike Schuchardt2df08912020-12-15 16:28:09 -08003920bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3921 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003922 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3923 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3924 return skip;
3925}
3926
3927bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3928 const VkSubpassEndInfo *pSubpassEndInfo) const {
3929 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3930 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3931 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003932}
3933
3934void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003935 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003936 auto cb_context = GetAccessContext(commandBuffer);
3937 assert(cb_context);
3938 auto cb_state = cb_context->GetCommandBufferState();
3939 if (!cb_state) return;
3940
3941 auto rp_state = cb_state->activeRenderPass;
3942 if (!rp_state) return;
3943
John Zulauf355e49b2020-04-24 15:11:15 -06003944 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003945}
3946
3947void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3948 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003949 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003950 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003951 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003952}
3953
3954void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3955 const VkSubpassEndInfo *pSubpassEndInfo) {
3956 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003957 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003958}
3959
3960void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3961 const VkSubpassEndInfo *pSubpassEndInfo) {
3962 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003963 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003964}
3965
Mike Schuchardt2df08912020-12-15 16:28:09 -08003966bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003967 const char *func_name) const {
3968 bool skip = false;
3969
3970 auto cb_context = GetAccessContext(commandBuffer);
3971 assert(cb_context);
3972 auto cb_state = cb_context->GetCommandBufferState();
3973 if (!cb_state) return skip;
3974
3975 auto rp_state = cb_state->activeRenderPass;
3976 if (!rp_state) return skip;
3977
3978 skip |= cb_context->ValidateEndRenderpass(func_name);
3979 return skip;
3980}
3981
3982bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3983 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
3984 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
3985 return skip;
3986}
3987
Mike Schuchardt2df08912020-12-15 16:28:09 -08003988bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003989 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
3990 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
3991 return skip;
3992}
3993
3994bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003995 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003996 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
3997 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
3998 return skip;
3999}
4000
4001void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
4002 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06004003 // Resolve the all subpass contexts to the command buffer contexts
4004 auto cb_context = GetAccessContext(commandBuffer);
4005 assert(cb_context);
4006 auto cb_state = cb_context->GetCommandBufferState();
4007 if (!cb_state) return;
4008
locke-lunargaecf2152020-05-12 17:15:41 -06004009 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06004010 if (!rp_state) return;
4011
John Zulauf355e49b2020-04-24 15:11:15 -06004012 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06004013}
John Zulauf3d84f1b2020-03-09 13:33:25 -06004014
John Zulauf33fc1d52020-07-17 11:01:10 -06004015// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
4016// updates to a resource which do not conflict at the byte level.
4017// TODO: Revisit this rule to see if it needs to be tighter or looser
4018// TODO: Add programatic control over suppression heuristics
4019bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
4020 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
4021}
4022
John Zulauf3d84f1b2020-03-09 13:33:25 -06004023void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004024 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06004025 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004026}
4027
4028void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06004029 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06004030 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004031}
4032
4033void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06004034 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06004035 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004036}
locke-lunarga19c71d2020-03-02 18:17:04 -07004037
Jeff Leger178b1e52020-10-05 12:22:23 -04004038template <typename BufferImageCopyRegionType>
4039bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4040 VkImageLayout dstImageLayout, uint32_t regionCount,
4041 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004042 bool skip = false;
4043 const auto *cb_access_context = GetAccessContext(commandBuffer);
4044 assert(cb_access_context);
4045 if (!cb_access_context) return skip;
4046
Jeff Leger178b1e52020-10-05 12:22:23 -04004047 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4048 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
4049
locke-lunarga19c71d2020-03-02 18:17:04 -07004050 const auto *context = cb_access_context->GetCurrentAccessContext();
4051 assert(context);
4052 if (!context) return skip;
4053
4054 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07004055 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4056
4057 for (uint32_t region = 0; region < regionCount; region++) {
4058 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07004059 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07004060 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07004061 if (src_buffer) {
4062 ResourceAccessRange src_range =
4063 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
4064 hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
4065 if (hazard.hazard) {
4066 // PHASE1 TODO -- add tag information to log msg when useful.
4067 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
4068 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4069 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
4070 string_UsageTag(hazard).c_str());
4071 }
4072 }
4073
4074 hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
4075 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004076 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004077 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004078 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004079 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004080 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004081 }
4082 if (skip) break;
4083 }
4084 if (skip) break;
4085 }
4086 return skip;
4087}
4088
Jeff Leger178b1e52020-10-05 12:22:23 -04004089bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4090 VkImageLayout dstImageLayout, uint32_t regionCount,
4091 const VkBufferImageCopy *pRegions) const {
4092 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
4093 COPY_COMMAND_VERSION_1);
4094}
4095
4096bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4097 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
4098 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4099 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4100 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4101}
4102
4103template <typename BufferImageCopyRegionType>
4104void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4105 VkImageLayout dstImageLayout, uint32_t regionCount,
4106 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004107 auto *cb_access_context = GetAccessContext(commandBuffer);
4108 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004109
4110 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4111 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
4112
4113 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004114 auto *context = cb_access_context->GetCurrentAccessContext();
4115 assert(context);
4116
4117 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06004118 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004119
4120 for (uint32_t region = 0; region < regionCount; region++) {
4121 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07004122 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07004123 if (src_buffer) {
4124 ResourceAccessRange src_range =
4125 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
4126 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
4127 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07004128 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
4129 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004130 }
4131 }
4132}
4133
Jeff Leger178b1e52020-10-05 12:22:23 -04004134void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4135 VkImageLayout dstImageLayout, uint32_t regionCount,
4136 const VkBufferImageCopy *pRegions) {
4137 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
4138 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4139}
4140
4141void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4142 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4143 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4144 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4145 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4146 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4147}
4148
4149template <typename BufferImageCopyRegionType>
4150bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4151 VkBuffer dstBuffer, uint32_t regionCount,
4152 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004153 bool skip = false;
4154 const auto *cb_access_context = GetAccessContext(commandBuffer);
4155 assert(cb_access_context);
4156 if (!cb_access_context) return skip;
4157
Jeff Leger178b1e52020-10-05 12:22:23 -04004158 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4159 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
4160
locke-lunarga19c71d2020-03-02 18:17:04 -07004161 const auto *context = cb_access_context->GetCurrentAccessContext();
4162 assert(context);
4163 if (!context) return skip;
4164
4165 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4166 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4167 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
4168 for (uint32_t region = 0; region < regionCount; region++) {
4169 const auto &copy_region = pRegions[region];
4170 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004171 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004172 copy_region.imageOffset, copy_region.imageExtent);
4173 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004174 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004175 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004176 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004177 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004178 }
John Zulauf477700e2021-01-06 11:41:49 -07004179 if (dst_mem) {
4180 ResourceAccessRange dst_range =
4181 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
4182 hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
4183 if (hazard.hazard) {
4184 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4185 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4186 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
4187 string_UsageTag(hazard).c_str());
4188 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004189 }
4190 }
4191 if (skip) break;
4192 }
4193 return skip;
4194}
4195
Jeff Leger178b1e52020-10-05 12:22:23 -04004196bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4197 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4198 const VkBufferImageCopy *pRegions) const {
4199 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
4200 COPY_COMMAND_VERSION_1);
4201}
4202
4203bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4204 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4205 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4206 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4207 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4208}
4209
4210template <typename BufferImageCopyRegionType>
4211void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4212 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
4213 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004214 auto *cb_access_context = GetAccessContext(commandBuffer);
4215 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004216
4217 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4218 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
4219
4220 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004221 auto *context = cb_access_context->GetCurrentAccessContext();
4222 assert(context);
4223
4224 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004225 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4226 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 -06004227 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004228
4229 for (uint32_t region = 0; region < regionCount; region++) {
4230 const auto &copy_region = pRegions[region];
4231 if (src_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004232 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
4233 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004234 if (dst_buffer) {
4235 ResourceAccessRange dst_range =
4236 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
4237 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
4238 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004239 }
4240 }
4241}
4242
Jeff Leger178b1e52020-10-05 12:22:23 -04004243void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4244 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4245 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
4246 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4247}
4248
4249void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4250 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4251 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4252 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4253 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4254 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4255}
4256
4257template <typename RegionType>
4258bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4259 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4260 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004261 bool skip = false;
4262 const auto *cb_access_context = GetAccessContext(commandBuffer);
4263 assert(cb_access_context);
4264 if (!cb_access_context) return skip;
4265
4266 const auto *context = cb_access_context->GetCurrentAccessContext();
4267 assert(context);
4268 if (!context) return skip;
4269
4270 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4271 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4272
4273 for (uint32_t region = 0; region < regionCount; region++) {
4274 const auto &blit_region = pRegions[region];
4275 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004276 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4277 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4278 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4279 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4280 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4281 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
4282 auto hazard =
4283 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004284 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004285 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004286 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004287 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004288 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004289 }
4290 }
4291
4292 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004293 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4294 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4295 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4296 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4297 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4298 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
4299 auto hazard =
4300 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004301 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004302 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004303 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004304 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004305 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004306 }
4307 if (skip) break;
4308 }
4309 }
4310
4311 return skip;
4312}
4313
Jeff Leger178b1e52020-10-05 12:22:23 -04004314bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4315 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4316 const VkImageBlit *pRegions, VkFilter filter) const {
4317 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4318 "vkCmdBlitImage");
4319}
4320
4321bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4322 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4323 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4324 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4325 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4326}
4327
4328template <typename RegionType>
4329void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4330 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4331 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004332 auto *cb_access_context = GetAccessContext(commandBuffer);
4333 assert(cb_access_context);
4334 auto *context = cb_access_context->GetCurrentAccessContext();
4335 assert(context);
4336
4337 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004338 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004339
4340 for (uint32_t region = 0; region < regionCount; region++) {
4341 const auto &blit_region = pRegions[region];
4342 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004343 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4344 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4345 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4346 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4347 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4348 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
John Zulauf8e3c3e92021-01-06 11:19:36 -07004349 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
4350 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004351 }
4352 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004353 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4354 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4355 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4356 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4357 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4358 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
John Zulauf8e3c3e92021-01-06 11:19:36 -07004359 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
4360 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004361 }
4362 }
4363}
locke-lunarg36ba2592020-04-03 09:42:04 -06004364
Jeff Leger178b1e52020-10-05 12:22:23 -04004365void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4366 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4367 const VkImageBlit *pRegions, VkFilter filter) {
4368 auto *cb_access_context = GetAccessContext(commandBuffer);
4369 assert(cb_access_context);
4370 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4371 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4372 pRegions, filter);
4373 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4374}
4375
4376void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4377 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4378 auto *cb_access_context = GetAccessContext(commandBuffer);
4379 assert(cb_access_context);
4380 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4381 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4382 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4383 pBlitImageInfo->filter, tag);
4384}
4385
locke-lunarg61870c22020-06-09 14:51:50 -06004386bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
4387 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
4388 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004389 bool skip = false;
4390 if (drawCount == 0) return skip;
4391
4392 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4393 VkDeviceSize size = struct_size;
4394 if (drawCount == 1 || stride == size) {
4395 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004396 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004397 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4398 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004399 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004400 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004401 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06004402 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004403 }
4404 } else {
4405 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004406 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004407 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4408 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004409 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004410 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4411 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
4412 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004413 break;
4414 }
4415 }
4416 }
4417 return skip;
4418}
4419
locke-lunarg61870c22020-06-09 14:51:50 -06004420void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
4421 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4422 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06004423 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4424 VkDeviceSize size = struct_size;
4425 if (drawCount == 1 || stride == size) {
4426 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004427 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004428 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004429 } else {
4430 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004431 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004432 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4433 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004434 }
4435 }
4436}
4437
locke-lunarg61870c22020-06-09 14:51:50 -06004438bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
4439 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004440 bool skip = false;
4441
4442 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004443 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004444 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4445 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004446 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004447 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004448 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06004449 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004450 }
4451 return skip;
4452}
4453
locke-lunarg61870c22020-06-09 14:51:50 -06004454void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06004455 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004456 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004457 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004458}
4459
locke-lunarg36ba2592020-04-03 09:42:04 -06004460bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004461 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004462 const auto *cb_access_context = GetAccessContext(commandBuffer);
4463 assert(cb_access_context);
4464 if (!cb_access_context) return skip;
4465
locke-lunarg61870c22020-06-09 14:51:50 -06004466 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004467 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004468}
4469
4470void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004471 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004472 auto *cb_access_context = GetAccessContext(commandBuffer);
4473 assert(cb_access_context);
4474 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004475
locke-lunarg61870c22020-06-09 14:51:50 -06004476 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004477}
locke-lunarge1a67022020-04-29 00:15:36 -06004478
4479bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004480 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004481 const auto *cb_access_context = GetAccessContext(commandBuffer);
4482 assert(cb_access_context);
4483 if (!cb_access_context) return skip;
4484
4485 const auto *context = cb_access_context->GetCurrentAccessContext();
4486 assert(context);
4487 if (!context) return skip;
4488
locke-lunarg61870c22020-06-09 14:51:50 -06004489 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
4490 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
4491 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004492 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004493}
4494
4495void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004496 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004497 auto *cb_access_context = GetAccessContext(commandBuffer);
4498 assert(cb_access_context);
4499 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4500 auto *context = cb_access_context->GetCurrentAccessContext();
4501 assert(context);
4502
locke-lunarg61870c22020-06-09 14:51:50 -06004503 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4504 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004505}
4506
4507bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4508 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004509 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004510 const auto *cb_access_context = GetAccessContext(commandBuffer);
4511 assert(cb_access_context);
4512 if (!cb_access_context) return skip;
4513
locke-lunarg61870c22020-06-09 14:51:50 -06004514 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4515 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4516 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004517 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004518}
4519
4520void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4521 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004522 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004523 auto *cb_access_context = GetAccessContext(commandBuffer);
4524 assert(cb_access_context);
4525 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004526
locke-lunarg61870c22020-06-09 14:51:50 -06004527 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4528 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4529 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004530}
4531
4532bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4533 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004534 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004535 const auto *cb_access_context = GetAccessContext(commandBuffer);
4536 assert(cb_access_context);
4537 if (!cb_access_context) return skip;
4538
locke-lunarg61870c22020-06-09 14:51:50 -06004539 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4540 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4541 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004542 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004543}
4544
4545void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4546 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004547 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004548 auto *cb_access_context = GetAccessContext(commandBuffer);
4549 assert(cb_access_context);
4550 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004551
locke-lunarg61870c22020-06-09 14:51:50 -06004552 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4553 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4554 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004555}
4556
4557bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4558 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004559 bool skip = false;
4560 if (drawCount == 0) return skip;
4561
locke-lunargff255f92020-05-13 18:53:52 -06004562 const auto *cb_access_context = GetAccessContext(commandBuffer);
4563 assert(cb_access_context);
4564 if (!cb_access_context) return skip;
4565
4566 const auto *context = cb_access_context->GetCurrentAccessContext();
4567 assert(context);
4568 if (!context) return skip;
4569
locke-lunarg61870c22020-06-09 14:51:50 -06004570 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4571 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
4572 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
4573 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004574
4575 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4576 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4577 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004578 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004579 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004580}
4581
4582void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4583 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004584 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004585 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004586 auto *cb_access_context = GetAccessContext(commandBuffer);
4587 assert(cb_access_context);
4588 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4589 auto *context = cb_access_context->GetCurrentAccessContext();
4590 assert(context);
4591
locke-lunarg61870c22020-06-09 14:51:50 -06004592 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4593 cb_access_context->RecordDrawSubpassAttachment(tag);
4594 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004595
4596 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4597 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4598 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004599 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004600}
4601
4602bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4603 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004604 bool skip = false;
4605 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004606 const auto *cb_access_context = GetAccessContext(commandBuffer);
4607 assert(cb_access_context);
4608 if (!cb_access_context) return skip;
4609
4610 const auto *context = cb_access_context->GetCurrentAccessContext();
4611 assert(context);
4612 if (!context) return skip;
4613
locke-lunarg61870c22020-06-09 14:51:50 -06004614 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4615 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
4616 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
4617 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004618
4619 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4620 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4621 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004622 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004623 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004624}
4625
4626void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4627 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004628 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004629 auto *cb_access_context = GetAccessContext(commandBuffer);
4630 assert(cb_access_context);
4631 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4632 auto *context = cb_access_context->GetCurrentAccessContext();
4633 assert(context);
4634
locke-lunarg61870c22020-06-09 14:51:50 -06004635 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4636 cb_access_context->RecordDrawSubpassAttachment(tag);
4637 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004638
4639 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4640 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4641 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004642 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004643}
4644
4645bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4646 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4647 uint32_t stride, const char *function) const {
4648 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004649 const auto *cb_access_context = GetAccessContext(commandBuffer);
4650 assert(cb_access_context);
4651 if (!cb_access_context) return skip;
4652
4653 const auto *context = cb_access_context->GetCurrentAccessContext();
4654 assert(context);
4655 if (!context) return skip;
4656
locke-lunarg61870c22020-06-09 14:51:50 -06004657 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4658 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
4659 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
4660 function);
4661 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004662
4663 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4664 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4665 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004666 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004667 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004668}
4669
4670bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4671 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4672 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004673 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4674 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004675}
4676
4677void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4678 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4679 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004680 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4681 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004682 auto *cb_access_context = GetAccessContext(commandBuffer);
4683 assert(cb_access_context);
4684 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
4685 auto *context = cb_access_context->GetCurrentAccessContext();
4686 assert(context);
4687
locke-lunarg61870c22020-06-09 14:51:50 -06004688 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4689 cb_access_context->RecordDrawSubpassAttachment(tag);
4690 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4691 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004692
4693 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4694 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4695 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004696 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004697}
4698
4699bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4700 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4701 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004702 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4703 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004704}
4705
4706void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4707 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4708 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004709 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4710 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004711 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004712}
4713
4714bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4715 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4716 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004717 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4718 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004719}
4720
4721void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4722 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4723 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004724 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4725 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004726 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4727}
4728
4729bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4730 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4731 uint32_t stride, const char *function) const {
4732 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004733 const auto *cb_access_context = GetAccessContext(commandBuffer);
4734 assert(cb_access_context);
4735 if (!cb_access_context) return skip;
4736
4737 const auto *context = cb_access_context->GetCurrentAccessContext();
4738 assert(context);
4739 if (!context) return skip;
4740
locke-lunarg61870c22020-06-09 14:51:50 -06004741 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4742 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
4743 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
4744 stride, function);
4745 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004746
4747 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4748 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4749 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004750 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004751 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004752}
4753
4754bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4755 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4756 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004757 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4758 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004759}
4760
4761void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4762 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4763 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004764 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4765 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004766 auto *cb_access_context = GetAccessContext(commandBuffer);
4767 assert(cb_access_context);
4768 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4769 auto *context = cb_access_context->GetCurrentAccessContext();
4770 assert(context);
4771
locke-lunarg61870c22020-06-09 14:51:50 -06004772 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4773 cb_access_context->RecordDrawSubpassAttachment(tag);
4774 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4775 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004776
4777 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4778 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004779 // We will update the index and vertex buffer in SubmitQueue in the future.
4780 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004781}
4782
4783bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4784 VkDeviceSize offset, VkBuffer countBuffer,
4785 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4786 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004787 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4788 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004789}
4790
4791void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4792 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4793 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004794 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4795 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004796 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4797}
4798
4799bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4800 VkDeviceSize offset, VkBuffer countBuffer,
4801 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4802 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004803 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4804 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004805}
4806
4807void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4808 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4809 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004810 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4811 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004812 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4813}
4814
4815bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4816 const VkClearColorValue *pColor, uint32_t rangeCount,
4817 const VkImageSubresourceRange *pRanges) const {
4818 bool skip = false;
4819 const auto *cb_access_context = GetAccessContext(commandBuffer);
4820 assert(cb_access_context);
4821 if (!cb_access_context) return skip;
4822
4823 const auto *context = cb_access_context->GetCurrentAccessContext();
4824 assert(context);
4825 if (!context) return skip;
4826
4827 const auto *image_state = Get<IMAGE_STATE>(image);
4828
4829 for (uint32_t index = 0; index < rangeCount; index++) {
4830 const auto &range = pRanges[index];
4831 if (image_state) {
4832 auto hazard =
4833 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4834 if (hazard.hazard) {
4835 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004836 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004837 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004838 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004839 }
4840 }
4841 }
4842 return skip;
4843}
4844
4845void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4846 const VkClearColorValue *pColor, uint32_t rangeCount,
4847 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004848 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004849 auto *cb_access_context = GetAccessContext(commandBuffer);
4850 assert(cb_access_context);
4851 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4852 auto *context = cb_access_context->GetCurrentAccessContext();
4853 assert(context);
4854
4855 const auto *image_state = Get<IMAGE_STATE>(image);
4856
4857 for (uint32_t index = 0; index < rangeCount; index++) {
4858 const auto &range = pRanges[index];
4859 if (image_state) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004860 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, {0, 0, 0},
4861 image_state->createInfo.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004862 }
4863 }
4864}
4865
4866bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4867 VkImageLayout imageLayout,
4868 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4869 const VkImageSubresourceRange *pRanges) const {
4870 bool skip = false;
4871 const auto *cb_access_context = GetAccessContext(commandBuffer);
4872 assert(cb_access_context);
4873 if (!cb_access_context) return skip;
4874
4875 const auto *context = cb_access_context->GetCurrentAccessContext();
4876 assert(context);
4877 if (!context) return skip;
4878
4879 const auto *image_state = Get<IMAGE_STATE>(image);
4880
4881 for (uint32_t index = 0; index < rangeCount; index++) {
4882 const auto &range = pRanges[index];
4883 if (image_state) {
4884 auto hazard =
4885 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4886 if (hazard.hazard) {
4887 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004888 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004889 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004890 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004891 }
4892 }
4893 }
4894 return skip;
4895}
4896
4897void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4898 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4899 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004900 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004901 auto *cb_access_context = GetAccessContext(commandBuffer);
4902 assert(cb_access_context);
4903 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4904 auto *context = cb_access_context->GetCurrentAccessContext();
4905 assert(context);
4906
4907 const auto *image_state = Get<IMAGE_STATE>(image);
4908
4909 for (uint32_t index = 0; index < rangeCount; index++) {
4910 const auto &range = pRanges[index];
4911 if (image_state) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004912 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, {0, 0, 0},
4913 image_state->createInfo.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004914 }
4915 }
4916}
4917
4918bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4919 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4920 VkDeviceSize dstOffset, VkDeviceSize stride,
4921 VkQueryResultFlags flags) const {
4922 bool skip = false;
4923 const auto *cb_access_context = GetAccessContext(commandBuffer);
4924 assert(cb_access_context);
4925 if (!cb_access_context) return skip;
4926
4927 const auto *context = cb_access_context->GetCurrentAccessContext();
4928 assert(context);
4929 if (!context) return skip;
4930
4931 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4932
4933 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004934 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004935 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4936 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004937 skip |=
4938 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4939 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4940 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004941 }
4942 }
locke-lunargff255f92020-05-13 18:53:52 -06004943
4944 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004945 return skip;
4946}
4947
4948void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4949 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4950 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004951 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4952 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004953 auto *cb_access_context = GetAccessContext(commandBuffer);
4954 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004955 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004956 auto *context = cb_access_context->GetCurrentAccessContext();
4957 assert(context);
4958
4959 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4960
4961 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004962 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004963 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004964 }
locke-lunargff255f92020-05-13 18:53:52 -06004965
4966 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004967}
4968
4969bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4970 VkDeviceSize size, uint32_t data) const {
4971 bool skip = false;
4972 const auto *cb_access_context = GetAccessContext(commandBuffer);
4973 assert(cb_access_context);
4974 if (!cb_access_context) return skip;
4975
4976 const auto *context = cb_access_context->GetCurrentAccessContext();
4977 assert(context);
4978 if (!context) return skip;
4979
4980 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4981
4982 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004983 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004984 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4985 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004986 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004987 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004988 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004989 }
4990 }
4991 return skip;
4992}
4993
4994void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4995 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004996 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004997 auto *cb_access_context = GetAccessContext(commandBuffer);
4998 assert(cb_access_context);
4999 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
5000 auto *context = cb_access_context->GetCurrentAccessContext();
5001 assert(context);
5002
5003 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5004
5005 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005006 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07005007 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005008 }
5009}
5010
5011bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5012 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5013 const VkImageResolve *pRegions) const {
5014 bool skip = false;
5015 const auto *cb_access_context = GetAccessContext(commandBuffer);
5016 assert(cb_access_context);
5017 if (!cb_access_context) return skip;
5018
5019 const auto *context = cb_access_context->GetCurrentAccessContext();
5020 assert(context);
5021 if (!context) return skip;
5022
5023 const auto *src_image = Get<IMAGE_STATE>(srcImage);
5024 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
5025
5026 for (uint32_t region = 0; region < regionCount; region++) {
5027 const auto &resolve_region = pRegions[region];
5028 if (src_image) {
5029 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5030 resolve_region.srcOffset, resolve_region.extent);
5031 if (hazard.hazard) {
5032 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005033 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005034 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06005035 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005036 }
5037 }
5038
5039 if (dst_image) {
5040 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5041 resolve_region.dstOffset, resolve_region.extent);
5042 if (hazard.hazard) {
5043 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005044 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005045 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06005046 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005047 }
5048 if (skip) break;
5049 }
5050 }
5051
5052 return skip;
5053}
5054
5055void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5056 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5057 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005058 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
5059 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06005060 auto *cb_access_context = GetAccessContext(commandBuffer);
5061 assert(cb_access_context);
5062 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
5063 auto *context = cb_access_context->GetCurrentAccessContext();
5064 assert(context);
5065
5066 auto *src_image = Get<IMAGE_STATE>(srcImage);
5067 auto *dst_image = Get<IMAGE_STATE>(dstImage);
5068
5069 for (uint32_t region = 0; region < regionCount; region++) {
5070 const auto &resolve_region = pRegions[region];
5071 if (src_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07005072 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
5073 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005074 }
5075 if (dst_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07005076 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
5077 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005078 }
5079 }
5080}
5081
Jeff Leger178b1e52020-10-05 12:22:23 -04005082bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5083 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
5084 bool skip = false;
5085 const auto *cb_access_context = GetAccessContext(commandBuffer);
5086 assert(cb_access_context);
5087 if (!cb_access_context) return skip;
5088
5089 const auto *context = cb_access_context->GetCurrentAccessContext();
5090 assert(context);
5091 if (!context) return skip;
5092
5093 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5094 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5095
5096 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5097 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5098 if (src_image) {
5099 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5100 resolve_region.srcOffset, resolve_region.extent);
5101 if (hazard.hazard) {
5102 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
5103 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
5104 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
5105 region, string_UsageTag(hazard).c_str());
5106 }
5107 }
5108
5109 if (dst_image) {
5110 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5111 resolve_region.dstOffset, resolve_region.extent);
5112 if (hazard.hazard) {
5113 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
5114 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
5115 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
5116 region, string_UsageTag(hazard).c_str());
5117 }
5118 if (skip) break;
5119 }
5120 }
5121
5122 return skip;
5123}
5124
5125void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5126 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5127 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5128 auto *cb_access_context = GetAccessContext(commandBuffer);
5129 assert(cb_access_context);
5130 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
5131 auto *context = cb_access_context->GetCurrentAccessContext();
5132 assert(context);
5133
5134 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5135 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5136
5137 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5138 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5139 if (src_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07005140 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
5141 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005142 }
5143 if (dst_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07005144 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
5145 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005146 }
5147 }
5148}
5149
locke-lunarge1a67022020-04-29 00:15:36 -06005150bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5151 VkDeviceSize dataSize, const void *pData) const {
5152 bool skip = false;
5153 const auto *cb_access_context = GetAccessContext(commandBuffer);
5154 assert(cb_access_context);
5155 if (!cb_access_context) return skip;
5156
5157 const auto *context = cb_access_context->GetCurrentAccessContext();
5158 assert(context);
5159 if (!context) return skip;
5160
5161 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5162
5163 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005164 // VK_WHOLE_SIZE not allowed
5165 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06005166 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
5167 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005168 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005169 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06005170 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005171 }
5172 }
5173 return skip;
5174}
5175
5176void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5177 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005178 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005179 auto *cb_access_context = GetAccessContext(commandBuffer);
5180 assert(cb_access_context);
5181 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5182 auto *context = cb_access_context->GetCurrentAccessContext();
5183 assert(context);
5184
5185 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5186
5187 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005188 // VK_WHOLE_SIZE not allowed
5189 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
John Zulauf8e3c3e92021-01-06 11:19:36 -07005190 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005191 }
5192}
locke-lunargff255f92020-05-13 18:53:52 -06005193
5194bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5195 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5196 bool skip = false;
5197 const auto *cb_access_context = GetAccessContext(commandBuffer);
5198 assert(cb_access_context);
5199 if (!cb_access_context) return skip;
5200
5201 const auto *context = cb_access_context->GetCurrentAccessContext();
5202 assert(context);
5203 if (!context) return skip;
5204
5205 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5206
5207 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005208 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06005209 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
5210 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005211 skip |=
5212 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5213 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
5214 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005215 }
5216 }
5217 return skip;
5218}
5219
5220void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5221 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005222 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005223 auto *cb_access_context = GetAccessContext(commandBuffer);
5224 assert(cb_access_context);
5225 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5226 auto *context = cb_access_context->GetCurrentAccessContext();
5227 assert(context);
5228
5229 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5230
5231 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005232 const ResourceAccessRange range = MakeRange(dstOffset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07005233 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06005234 }
5235}
John Zulauf49beb112020-11-04 16:06:31 -07005236
5237bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5238 bool skip = false;
5239 const auto *cb_context = GetAccessContext(commandBuffer);
5240 assert(cb_context);
5241 if (!cb_context) return skip;
5242
5243 return cb_context->ValidateSetEvent(commandBuffer, event, stageMask);
5244}
5245
5246void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5247 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5248 auto *cb_context = GetAccessContext(commandBuffer);
5249 assert(cb_context);
5250 if (!cb_context) return;
John Zulauf4a6105a2020-11-17 15:11:05 -07005251 const auto tag = cb_context->NextCommandTag(CMD_SETEVENT);
5252 cb_context->RecordSetEvent(commandBuffer, event, stageMask, tag);
John Zulauf49beb112020-11-04 16:06:31 -07005253}
5254
5255bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5256 VkPipelineStageFlags stageMask) const {
5257 bool skip = false;
5258 const auto *cb_context = GetAccessContext(commandBuffer);
5259 assert(cb_context);
5260 if (!cb_context) return skip;
5261
5262 return cb_context->ValidateResetEvent(commandBuffer, event, stageMask);
5263}
5264
5265void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5266 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5267 auto *cb_context = GetAccessContext(commandBuffer);
5268 assert(cb_context);
5269 if (!cb_context) return;
5270
5271 cb_context->RecordResetEvent(commandBuffer, event, stageMask);
5272}
5273
5274bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5275 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5276 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5277 uint32_t bufferMemoryBarrierCount,
5278 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5279 uint32_t imageMemoryBarrierCount,
5280 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5281 bool skip = false;
5282 const auto *cb_context = GetAccessContext(commandBuffer);
5283 assert(cb_context);
5284 if (!cb_context) return skip;
5285
John Zulauf4a6105a2020-11-17 15:11:05 -07005286 return cb_context->ValidateWaitEvents(eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount, pMemoryBarriers,
5287 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
John Zulauf49beb112020-11-04 16:06:31 -07005288 pImageMemoryBarriers);
5289}
5290
5291void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5292 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5293 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5294 uint32_t bufferMemoryBarrierCount,
5295 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5296 uint32_t imageMemoryBarrierCount,
5297 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5298 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5299 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5300 imageMemoryBarrierCount, pImageMemoryBarriers);
5301
5302 auto *cb_context = GetAccessContext(commandBuffer);
5303 assert(cb_context);
5304 if (!cb_context) return;
5305
John Zulauf4a6105a2020-11-17 15:11:05 -07005306 const auto tag = cb_context->NextCommandTag(CMD_WAITEVENTS);
John Zulauf49beb112020-11-04 16:06:31 -07005307 cb_context->RecordWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5308 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
John Zulauf4a6105a2020-11-17 15:11:05 -07005309 pImageMemoryBarriers, tag);
5310}
5311
5312void SyncEventState::ResetFirstScope() {
5313 for (const auto address_type : kAddressTypes) {
5314 first_scope[static_cast<size_t>(address_type)].clear();
5315 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005316 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07005317}
5318
5319// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
5320SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(VkPipelineStageFlags srcStageMask) const {
5321 IgnoreReason reason = NotIgnored;
5322
5323 if (last_command == CMD_RESETEVENT && !HasBarrier(0U, 0U)) {
5324 reason = ResetWaitRace;
5325 } else if (unsynchronized_set) {
5326 reason = SetRace;
5327 } else {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005328 const VkPipelineStageFlags missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005329 if (missing_bits) reason = MissingStageBits;
5330 }
5331
5332 return reason;
5333}
5334
5335bool SyncEventState::HasBarrier(VkPipelineStageFlags stageMask, VkPipelineStageFlags exec_scope_arg) const {
5336 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5337 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5338 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005339}