blob: dc134f71a268f057614cd01935083d535644e64e [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 Zulauf16adfc92020-04-08 10:28:33 -06004059 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004060 ResourceAccessRange src_range =
4061 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004062 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07004063 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06004064 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06004065 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004066 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004067 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004068 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004069 }
4070 }
4071 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004072 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004073 copy_region.imageOffset, copy_region.imageExtent);
4074 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004075 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004076 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004077 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004078 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004079 }
4080 if (skip) break;
4081 }
4082 if (skip) break;
4083 }
4084 return skip;
4085}
4086
Jeff Leger178b1e52020-10-05 12:22:23 -04004087bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4088 VkImageLayout dstImageLayout, uint32_t regionCount,
4089 const VkBufferImageCopy *pRegions) const {
4090 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
4091 COPY_COMMAND_VERSION_1);
4092}
4093
4094bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4095 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
4096 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4097 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4098 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4099}
4100
4101template <typename BufferImageCopyRegionType>
4102void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4103 VkImageLayout dstImageLayout, uint32_t regionCount,
4104 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004105 auto *cb_access_context = GetAccessContext(commandBuffer);
4106 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004107
4108 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4109 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
4110
4111 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004112 auto *context = cb_access_context->GetCurrentAccessContext();
4113 assert(context);
4114
4115 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06004116 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004117
4118 for (uint32_t region = 0; region < regionCount; region++) {
4119 const auto &copy_region = pRegions[region];
4120 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004121 ResourceAccessRange src_range =
4122 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf8e3c3e92021-01-06 11:19:36 -07004123 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004124 }
4125 if (dst_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004126 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
4127 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004128 }
4129 }
4130}
4131
Jeff Leger178b1e52020-10-05 12:22:23 -04004132void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4133 VkImageLayout dstImageLayout, uint32_t regionCount,
4134 const VkBufferImageCopy *pRegions) {
4135 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
4136 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4137}
4138
4139void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4140 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4141 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4142 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4143 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4144 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4145}
4146
4147template <typename BufferImageCopyRegionType>
4148bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4149 VkBuffer dstBuffer, uint32_t regionCount,
4150 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004151 bool skip = false;
4152 const auto *cb_access_context = GetAccessContext(commandBuffer);
4153 assert(cb_access_context);
4154 if (!cb_access_context) return skip;
4155
Jeff Leger178b1e52020-10-05 12:22:23 -04004156 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4157 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
4158
locke-lunarga19c71d2020-03-02 18:17:04 -07004159 const auto *context = cb_access_context->GetCurrentAccessContext();
4160 assert(context);
4161 if (!context) return skip;
4162
4163 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4164 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4165 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
4166 for (uint32_t region = 0; region < regionCount; region++) {
4167 const auto &copy_region = pRegions[region];
4168 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004169 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004170 copy_region.imageOffset, copy_region.imageExtent);
4171 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004172 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004173 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004174 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004175 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004176 }
4177 }
4178 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06004179 ResourceAccessRange dst_range =
4180 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004181 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07004182 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004183 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004184 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004185 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004186 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004187 }
4188 }
4189 if (skip) break;
4190 }
4191 return skip;
4192}
4193
Jeff Leger178b1e52020-10-05 12:22:23 -04004194bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4195 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4196 const VkBufferImageCopy *pRegions) const {
4197 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
4198 COPY_COMMAND_VERSION_1);
4199}
4200
4201bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4202 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4203 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4204 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4205 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4206}
4207
4208template <typename BufferImageCopyRegionType>
4209void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4210 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
4211 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004212 auto *cb_access_context = GetAccessContext(commandBuffer);
4213 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004214
4215 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4216 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
4217
4218 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004219 auto *context = cb_access_context->GetCurrentAccessContext();
4220 assert(context);
4221
4222 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004223 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4224 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 -06004225 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004226
4227 for (uint32_t region = 0; region < regionCount; region++) {
4228 const auto &copy_region = pRegions[region];
4229 if (src_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004230 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
4231 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004232 }
4233 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004234 ResourceAccessRange dst_range =
4235 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf8e3c3e92021-01-06 11:19:36 -07004236 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004237 }
4238 }
4239}
4240
Jeff Leger178b1e52020-10-05 12:22:23 -04004241void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4242 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4243 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
4244 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4245}
4246
4247void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4248 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4249 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4250 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4251 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4252 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4253}
4254
4255template <typename RegionType>
4256bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4257 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4258 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004259 bool skip = false;
4260 const auto *cb_access_context = GetAccessContext(commandBuffer);
4261 assert(cb_access_context);
4262 if (!cb_access_context) return skip;
4263
4264 const auto *context = cb_access_context->GetCurrentAccessContext();
4265 assert(context);
4266 if (!context) return skip;
4267
4268 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4269 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4270
4271 for (uint32_t region = 0; region < regionCount; region++) {
4272 const auto &blit_region = pRegions[region];
4273 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004274 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4275 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4276 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4277 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4278 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4279 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
4280 auto hazard =
4281 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004282 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004283 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004284 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004285 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004286 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004287 }
4288 }
4289
4290 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004291 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4292 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4293 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4294 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4295 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4296 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
4297 auto hazard =
4298 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004299 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004300 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004301 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004302 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004303 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004304 }
4305 if (skip) break;
4306 }
4307 }
4308
4309 return skip;
4310}
4311
Jeff Leger178b1e52020-10-05 12:22:23 -04004312bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4313 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4314 const VkImageBlit *pRegions, VkFilter filter) const {
4315 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4316 "vkCmdBlitImage");
4317}
4318
4319bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4320 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4321 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4322 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4323 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4324}
4325
4326template <typename RegionType>
4327void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4328 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4329 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004330 auto *cb_access_context = GetAccessContext(commandBuffer);
4331 assert(cb_access_context);
4332 auto *context = cb_access_context->GetCurrentAccessContext();
4333 assert(context);
4334
4335 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004336 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004337
4338 for (uint32_t region = 0; region < regionCount; region++) {
4339 const auto &blit_region = pRegions[region];
4340 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004341 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4342 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4343 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4344 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4345 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4346 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
John Zulauf8e3c3e92021-01-06 11:19:36 -07004347 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
4348 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004349 }
4350 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004351 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4352 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4353 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4354 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4355 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4356 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
John Zulauf8e3c3e92021-01-06 11:19:36 -07004357 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
4358 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004359 }
4360 }
4361}
locke-lunarg36ba2592020-04-03 09:42:04 -06004362
Jeff Leger178b1e52020-10-05 12:22:23 -04004363void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4364 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4365 const VkImageBlit *pRegions, VkFilter filter) {
4366 auto *cb_access_context = GetAccessContext(commandBuffer);
4367 assert(cb_access_context);
4368 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4369 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4370 pRegions, filter);
4371 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4372}
4373
4374void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4375 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4376 auto *cb_access_context = GetAccessContext(commandBuffer);
4377 assert(cb_access_context);
4378 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4379 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4380 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4381 pBlitImageInfo->filter, tag);
4382}
4383
locke-lunarg61870c22020-06-09 14:51:50 -06004384bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
4385 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
4386 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004387 bool skip = false;
4388 if (drawCount == 0) return skip;
4389
4390 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4391 VkDeviceSize size = struct_size;
4392 if (drawCount == 1 || stride == size) {
4393 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004394 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004395 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4396 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004397 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004398 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004399 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06004400 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004401 }
4402 } else {
4403 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004404 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004405 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4406 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004407 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004408 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4409 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
4410 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004411 break;
4412 }
4413 }
4414 }
4415 return skip;
4416}
4417
locke-lunarg61870c22020-06-09 14:51:50 -06004418void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
4419 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4420 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06004421 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4422 VkDeviceSize size = struct_size;
4423 if (drawCount == 1 || stride == size) {
4424 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004425 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004426 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004427 } else {
4428 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004429 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004430 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4431 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004432 }
4433 }
4434}
4435
locke-lunarg61870c22020-06-09 14:51:50 -06004436bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
4437 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004438 bool skip = false;
4439
4440 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004441 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004442 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4443 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004444 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004445 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004446 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06004447 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004448 }
4449 return skip;
4450}
4451
locke-lunarg61870c22020-06-09 14:51:50 -06004452void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06004453 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004454 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004455 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004456}
4457
locke-lunarg36ba2592020-04-03 09:42:04 -06004458bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004459 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004460 const auto *cb_access_context = GetAccessContext(commandBuffer);
4461 assert(cb_access_context);
4462 if (!cb_access_context) return skip;
4463
locke-lunarg61870c22020-06-09 14:51:50 -06004464 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004465 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004466}
4467
4468void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004469 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004470 auto *cb_access_context = GetAccessContext(commandBuffer);
4471 assert(cb_access_context);
4472 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004473
locke-lunarg61870c22020-06-09 14:51:50 -06004474 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004475}
locke-lunarge1a67022020-04-29 00:15:36 -06004476
4477bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004478 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004479 const auto *cb_access_context = GetAccessContext(commandBuffer);
4480 assert(cb_access_context);
4481 if (!cb_access_context) return skip;
4482
4483 const auto *context = cb_access_context->GetCurrentAccessContext();
4484 assert(context);
4485 if (!context) return skip;
4486
locke-lunarg61870c22020-06-09 14:51:50 -06004487 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
4488 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
4489 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004490 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004491}
4492
4493void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004494 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004495 auto *cb_access_context = GetAccessContext(commandBuffer);
4496 assert(cb_access_context);
4497 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4498 auto *context = cb_access_context->GetCurrentAccessContext();
4499 assert(context);
4500
locke-lunarg61870c22020-06-09 14:51:50 -06004501 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4502 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004503}
4504
4505bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4506 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004507 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004508 const auto *cb_access_context = GetAccessContext(commandBuffer);
4509 assert(cb_access_context);
4510 if (!cb_access_context) return skip;
4511
locke-lunarg61870c22020-06-09 14:51:50 -06004512 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4513 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4514 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004515 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004516}
4517
4518void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4519 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004520 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004521 auto *cb_access_context = GetAccessContext(commandBuffer);
4522 assert(cb_access_context);
4523 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004524
locke-lunarg61870c22020-06-09 14:51:50 -06004525 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4526 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4527 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004528}
4529
4530bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4531 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004532 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004533 const auto *cb_access_context = GetAccessContext(commandBuffer);
4534 assert(cb_access_context);
4535 if (!cb_access_context) return skip;
4536
locke-lunarg61870c22020-06-09 14:51:50 -06004537 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4538 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4539 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004540 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004541}
4542
4543void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4544 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004545 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004546 auto *cb_access_context = GetAccessContext(commandBuffer);
4547 assert(cb_access_context);
4548 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004549
locke-lunarg61870c22020-06-09 14:51:50 -06004550 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4551 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4552 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004553}
4554
4555bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4556 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004557 bool skip = false;
4558 if (drawCount == 0) return skip;
4559
locke-lunargff255f92020-05-13 18:53:52 -06004560 const auto *cb_access_context = GetAccessContext(commandBuffer);
4561 assert(cb_access_context);
4562 if (!cb_access_context) return skip;
4563
4564 const auto *context = cb_access_context->GetCurrentAccessContext();
4565 assert(context);
4566 if (!context) return skip;
4567
locke-lunarg61870c22020-06-09 14:51:50 -06004568 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4569 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
4570 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
4571 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004572
4573 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4574 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4575 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004576 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004577 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004578}
4579
4580void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4581 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004582 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004583 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004584 auto *cb_access_context = GetAccessContext(commandBuffer);
4585 assert(cb_access_context);
4586 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4587 auto *context = cb_access_context->GetCurrentAccessContext();
4588 assert(context);
4589
locke-lunarg61870c22020-06-09 14:51:50 -06004590 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4591 cb_access_context->RecordDrawSubpassAttachment(tag);
4592 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004593
4594 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4595 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4596 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004597 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004598}
4599
4600bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4601 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004602 bool skip = false;
4603 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004604 const auto *cb_access_context = GetAccessContext(commandBuffer);
4605 assert(cb_access_context);
4606 if (!cb_access_context) return skip;
4607
4608 const auto *context = cb_access_context->GetCurrentAccessContext();
4609 assert(context);
4610 if (!context) return skip;
4611
locke-lunarg61870c22020-06-09 14:51:50 -06004612 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4613 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
4614 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
4615 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004616
4617 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4618 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4619 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004620 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004621 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004622}
4623
4624void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4625 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004626 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004627 auto *cb_access_context = GetAccessContext(commandBuffer);
4628 assert(cb_access_context);
4629 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4630 auto *context = cb_access_context->GetCurrentAccessContext();
4631 assert(context);
4632
locke-lunarg61870c22020-06-09 14:51:50 -06004633 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4634 cb_access_context->RecordDrawSubpassAttachment(tag);
4635 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004636
4637 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4638 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4639 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004640 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004641}
4642
4643bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4644 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4645 uint32_t stride, const char *function) const {
4646 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004647 const auto *cb_access_context = GetAccessContext(commandBuffer);
4648 assert(cb_access_context);
4649 if (!cb_access_context) return skip;
4650
4651 const auto *context = cb_access_context->GetCurrentAccessContext();
4652 assert(context);
4653 if (!context) return skip;
4654
locke-lunarg61870c22020-06-09 14:51:50 -06004655 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4656 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
4657 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
4658 function);
4659 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004660
4661 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4662 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4663 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004664 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004665 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004666}
4667
4668bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4669 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4670 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004671 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4672 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004673}
4674
4675void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4676 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4677 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004678 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4679 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004680 auto *cb_access_context = GetAccessContext(commandBuffer);
4681 assert(cb_access_context);
4682 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
4683 auto *context = cb_access_context->GetCurrentAccessContext();
4684 assert(context);
4685
locke-lunarg61870c22020-06-09 14:51:50 -06004686 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4687 cb_access_context->RecordDrawSubpassAttachment(tag);
4688 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4689 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004690
4691 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4692 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4693 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004694 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004695}
4696
4697bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4698 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4699 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004700 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4701 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004702}
4703
4704void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4705 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4706 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004707 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4708 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004709 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004710}
4711
4712bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4713 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4714 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004715 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4716 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004717}
4718
4719void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4720 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4721 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004722 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4723 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004724 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4725}
4726
4727bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4728 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4729 uint32_t stride, const char *function) const {
4730 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004731 const auto *cb_access_context = GetAccessContext(commandBuffer);
4732 assert(cb_access_context);
4733 if (!cb_access_context) return skip;
4734
4735 const auto *context = cb_access_context->GetCurrentAccessContext();
4736 assert(context);
4737 if (!context) return skip;
4738
locke-lunarg61870c22020-06-09 14:51:50 -06004739 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4740 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
4741 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
4742 stride, function);
4743 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004744
4745 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4746 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4747 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004748 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004749 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004750}
4751
4752bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4753 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4754 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004755 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4756 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004757}
4758
4759void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4760 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4761 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004762 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4763 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004764 auto *cb_access_context = GetAccessContext(commandBuffer);
4765 assert(cb_access_context);
4766 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4767 auto *context = cb_access_context->GetCurrentAccessContext();
4768 assert(context);
4769
locke-lunarg61870c22020-06-09 14:51:50 -06004770 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4771 cb_access_context->RecordDrawSubpassAttachment(tag);
4772 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4773 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004774
4775 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4776 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004777 // We will update the index and vertex buffer in SubmitQueue in the future.
4778 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004779}
4780
4781bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4782 VkDeviceSize offset, VkBuffer countBuffer,
4783 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4784 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004785 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4786 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004787}
4788
4789void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4790 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4791 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004792 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4793 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004794 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4795}
4796
4797bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4798 VkDeviceSize offset, VkBuffer countBuffer,
4799 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4800 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004801 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4802 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004803}
4804
4805void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4806 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4807 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004808 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4809 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004810 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4811}
4812
4813bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4814 const VkClearColorValue *pColor, uint32_t rangeCount,
4815 const VkImageSubresourceRange *pRanges) const {
4816 bool skip = false;
4817 const auto *cb_access_context = GetAccessContext(commandBuffer);
4818 assert(cb_access_context);
4819 if (!cb_access_context) return skip;
4820
4821 const auto *context = cb_access_context->GetCurrentAccessContext();
4822 assert(context);
4823 if (!context) return skip;
4824
4825 const auto *image_state = Get<IMAGE_STATE>(image);
4826
4827 for (uint32_t index = 0; index < rangeCount; index++) {
4828 const auto &range = pRanges[index];
4829 if (image_state) {
4830 auto hazard =
4831 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4832 if (hazard.hazard) {
4833 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004834 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004835 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004836 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004837 }
4838 }
4839 }
4840 return skip;
4841}
4842
4843void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4844 const VkClearColorValue *pColor, uint32_t rangeCount,
4845 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004846 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004847 auto *cb_access_context = GetAccessContext(commandBuffer);
4848 assert(cb_access_context);
4849 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4850 auto *context = cb_access_context->GetCurrentAccessContext();
4851 assert(context);
4852
4853 const auto *image_state = Get<IMAGE_STATE>(image);
4854
4855 for (uint32_t index = 0; index < rangeCount; index++) {
4856 const auto &range = pRanges[index];
4857 if (image_state) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004858 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, {0, 0, 0},
4859 image_state->createInfo.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004860 }
4861 }
4862}
4863
4864bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4865 VkImageLayout imageLayout,
4866 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4867 const VkImageSubresourceRange *pRanges) const {
4868 bool skip = false;
4869 const auto *cb_access_context = GetAccessContext(commandBuffer);
4870 assert(cb_access_context);
4871 if (!cb_access_context) return skip;
4872
4873 const auto *context = cb_access_context->GetCurrentAccessContext();
4874 assert(context);
4875 if (!context) return skip;
4876
4877 const auto *image_state = Get<IMAGE_STATE>(image);
4878
4879 for (uint32_t index = 0; index < rangeCount; index++) {
4880 const auto &range = pRanges[index];
4881 if (image_state) {
4882 auto hazard =
4883 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4884 if (hazard.hazard) {
4885 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004886 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004887 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004888 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004889 }
4890 }
4891 }
4892 return skip;
4893}
4894
4895void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4896 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4897 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004898 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004899 auto *cb_access_context = GetAccessContext(commandBuffer);
4900 assert(cb_access_context);
4901 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4902 auto *context = cb_access_context->GetCurrentAccessContext();
4903 assert(context);
4904
4905 const auto *image_state = Get<IMAGE_STATE>(image);
4906
4907 for (uint32_t index = 0; index < rangeCount; index++) {
4908 const auto &range = pRanges[index];
4909 if (image_state) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07004910 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, {0, 0, 0},
4911 image_state->createInfo.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004912 }
4913 }
4914}
4915
4916bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4917 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4918 VkDeviceSize dstOffset, VkDeviceSize stride,
4919 VkQueryResultFlags flags) const {
4920 bool skip = false;
4921 const auto *cb_access_context = GetAccessContext(commandBuffer);
4922 assert(cb_access_context);
4923 if (!cb_access_context) return skip;
4924
4925 const auto *context = cb_access_context->GetCurrentAccessContext();
4926 assert(context);
4927 if (!context) return skip;
4928
4929 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4930
4931 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004932 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004933 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4934 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004935 skip |=
4936 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4937 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4938 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004939 }
4940 }
locke-lunargff255f92020-05-13 18:53:52 -06004941
4942 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004943 return skip;
4944}
4945
4946void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4947 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4948 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004949 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4950 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004951 auto *cb_access_context = GetAccessContext(commandBuffer);
4952 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004953 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004954 auto *context = cb_access_context->GetCurrentAccessContext();
4955 assert(context);
4956
4957 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4958
4959 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004960 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004961 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004962 }
locke-lunargff255f92020-05-13 18:53:52 -06004963
4964 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004965}
4966
4967bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4968 VkDeviceSize size, uint32_t data) const {
4969 bool skip = false;
4970 const auto *cb_access_context = GetAccessContext(commandBuffer);
4971 assert(cb_access_context);
4972 if (!cb_access_context) return skip;
4973
4974 const auto *context = cb_access_context->GetCurrentAccessContext();
4975 assert(context);
4976 if (!context) return skip;
4977
4978 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4979
4980 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004981 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004982 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4983 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004984 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004985 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004986 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004987 }
4988 }
4989 return skip;
4990}
4991
4992void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4993 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004994 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004995 auto *cb_access_context = GetAccessContext(commandBuffer);
4996 assert(cb_access_context);
4997 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4998 auto *context = cb_access_context->GetCurrentAccessContext();
4999 assert(context);
5000
5001 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5002
5003 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005004 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07005005 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005006 }
5007}
5008
5009bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5010 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5011 const VkImageResolve *pRegions) const {
5012 bool skip = false;
5013 const auto *cb_access_context = GetAccessContext(commandBuffer);
5014 assert(cb_access_context);
5015 if (!cb_access_context) return skip;
5016
5017 const auto *context = cb_access_context->GetCurrentAccessContext();
5018 assert(context);
5019 if (!context) return skip;
5020
5021 const auto *src_image = Get<IMAGE_STATE>(srcImage);
5022 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
5023
5024 for (uint32_t region = 0; region < regionCount; region++) {
5025 const auto &resolve_region = pRegions[region];
5026 if (src_image) {
5027 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5028 resolve_region.srcOffset, resolve_region.extent);
5029 if (hazard.hazard) {
5030 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005031 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005032 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06005033 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005034 }
5035 }
5036
5037 if (dst_image) {
5038 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5039 resolve_region.dstOffset, resolve_region.extent);
5040 if (hazard.hazard) {
5041 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005042 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005043 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06005044 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005045 }
5046 if (skip) break;
5047 }
5048 }
5049
5050 return skip;
5051}
5052
5053void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5054 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5055 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005056 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
5057 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06005058 auto *cb_access_context = GetAccessContext(commandBuffer);
5059 assert(cb_access_context);
5060 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
5061 auto *context = cb_access_context->GetCurrentAccessContext();
5062 assert(context);
5063
5064 auto *src_image = Get<IMAGE_STATE>(srcImage);
5065 auto *dst_image = Get<IMAGE_STATE>(dstImage);
5066
5067 for (uint32_t region = 0; region < regionCount; region++) {
5068 const auto &resolve_region = pRegions[region];
5069 if (src_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07005070 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
5071 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005072 }
5073 if (dst_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07005074 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
5075 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005076 }
5077 }
5078}
5079
Jeff Leger178b1e52020-10-05 12:22:23 -04005080bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5081 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
5082 bool skip = false;
5083 const auto *cb_access_context = GetAccessContext(commandBuffer);
5084 assert(cb_access_context);
5085 if (!cb_access_context) return skip;
5086
5087 const auto *context = cb_access_context->GetCurrentAccessContext();
5088 assert(context);
5089 if (!context) return skip;
5090
5091 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5092 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5093
5094 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5095 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5096 if (src_image) {
5097 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5098 resolve_region.srcOffset, resolve_region.extent);
5099 if (hazard.hazard) {
5100 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
5101 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
5102 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
5103 region, string_UsageTag(hazard).c_str());
5104 }
5105 }
5106
5107 if (dst_image) {
5108 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5109 resolve_region.dstOffset, resolve_region.extent);
5110 if (hazard.hazard) {
5111 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
5112 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
5113 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
5114 region, string_UsageTag(hazard).c_str());
5115 }
5116 if (skip) break;
5117 }
5118 }
5119
5120 return skip;
5121}
5122
5123void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5124 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5125 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5126 auto *cb_access_context = GetAccessContext(commandBuffer);
5127 assert(cb_access_context);
5128 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
5129 auto *context = cb_access_context->GetCurrentAccessContext();
5130 assert(context);
5131
5132 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5133 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5134
5135 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5136 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5137 if (src_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07005138 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, SyncOrdering::kNonAttachment,
5139 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005140 }
5141 if (dst_image) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07005142 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
5143 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005144 }
5145 }
5146}
5147
locke-lunarge1a67022020-04-29 00:15:36 -06005148bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5149 VkDeviceSize dataSize, const void *pData) const {
5150 bool skip = false;
5151 const auto *cb_access_context = GetAccessContext(commandBuffer);
5152 assert(cb_access_context);
5153 if (!cb_access_context) return skip;
5154
5155 const auto *context = cb_access_context->GetCurrentAccessContext();
5156 assert(context);
5157 if (!context) return skip;
5158
5159 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5160
5161 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005162 // VK_WHOLE_SIZE not allowed
5163 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06005164 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
5165 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005166 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005167 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06005168 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005169 }
5170 }
5171 return skip;
5172}
5173
5174void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5175 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005176 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005177 auto *cb_access_context = GetAccessContext(commandBuffer);
5178 assert(cb_access_context);
5179 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5180 auto *context = cb_access_context->GetCurrentAccessContext();
5181 assert(context);
5182
5183 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5184
5185 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005186 // VK_WHOLE_SIZE not allowed
5187 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
John Zulauf8e3c3e92021-01-06 11:19:36 -07005188 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005189 }
5190}
locke-lunargff255f92020-05-13 18:53:52 -06005191
5192bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5193 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5194 bool skip = false;
5195 const auto *cb_access_context = GetAccessContext(commandBuffer);
5196 assert(cb_access_context);
5197 if (!cb_access_context) return skip;
5198
5199 const auto *context = cb_access_context->GetCurrentAccessContext();
5200 assert(context);
5201 if (!context) return skip;
5202
5203 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5204
5205 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005206 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06005207 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
5208 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005209 skip |=
5210 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5211 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
5212 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005213 }
5214 }
5215 return skip;
5216}
5217
5218void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5219 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005220 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005221 auto *cb_access_context = GetAccessContext(commandBuffer);
5222 assert(cb_access_context);
5223 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5224 auto *context = cb_access_context->GetCurrentAccessContext();
5225 assert(context);
5226
5227 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5228
5229 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005230 const ResourceAccessRange range = MakeRange(dstOffset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07005231 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06005232 }
5233}
John Zulauf49beb112020-11-04 16:06:31 -07005234
5235bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5236 bool skip = false;
5237 const auto *cb_context = GetAccessContext(commandBuffer);
5238 assert(cb_context);
5239 if (!cb_context) return skip;
5240
5241 return cb_context->ValidateSetEvent(commandBuffer, event, stageMask);
5242}
5243
5244void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5245 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5246 auto *cb_context = GetAccessContext(commandBuffer);
5247 assert(cb_context);
5248 if (!cb_context) return;
John Zulauf4a6105a2020-11-17 15:11:05 -07005249 const auto tag = cb_context->NextCommandTag(CMD_SETEVENT);
5250 cb_context->RecordSetEvent(commandBuffer, event, stageMask, tag);
John Zulauf49beb112020-11-04 16:06:31 -07005251}
5252
5253bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5254 VkPipelineStageFlags stageMask) const {
5255 bool skip = false;
5256 const auto *cb_context = GetAccessContext(commandBuffer);
5257 assert(cb_context);
5258 if (!cb_context) return skip;
5259
5260 return cb_context->ValidateResetEvent(commandBuffer, event, stageMask);
5261}
5262
5263void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5264 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5265 auto *cb_context = GetAccessContext(commandBuffer);
5266 assert(cb_context);
5267 if (!cb_context) return;
5268
5269 cb_context->RecordResetEvent(commandBuffer, event, stageMask);
5270}
5271
5272bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5273 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5274 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5275 uint32_t bufferMemoryBarrierCount,
5276 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5277 uint32_t imageMemoryBarrierCount,
5278 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5279 bool skip = false;
5280 const auto *cb_context = GetAccessContext(commandBuffer);
5281 assert(cb_context);
5282 if (!cb_context) return skip;
5283
John Zulauf4a6105a2020-11-17 15:11:05 -07005284 return cb_context->ValidateWaitEvents(eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount, pMemoryBarriers,
5285 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
John Zulauf49beb112020-11-04 16:06:31 -07005286 pImageMemoryBarriers);
5287}
5288
5289void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5290 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5291 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5292 uint32_t bufferMemoryBarrierCount,
5293 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5294 uint32_t imageMemoryBarrierCount,
5295 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5296 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5297 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5298 imageMemoryBarrierCount, pImageMemoryBarriers);
5299
5300 auto *cb_context = GetAccessContext(commandBuffer);
5301 assert(cb_context);
5302 if (!cb_context) return;
5303
John Zulauf4a6105a2020-11-17 15:11:05 -07005304 const auto tag = cb_context->NextCommandTag(CMD_WAITEVENTS);
John Zulauf49beb112020-11-04 16:06:31 -07005305 cb_context->RecordWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5306 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
John Zulauf4a6105a2020-11-17 15:11:05 -07005307 pImageMemoryBarriers, tag);
5308}
5309
5310void SyncEventState::ResetFirstScope() {
5311 for (const auto address_type : kAddressTypes) {
5312 first_scope[static_cast<size_t>(address_type)].clear();
5313 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005314 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07005315}
5316
5317// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
5318SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(VkPipelineStageFlags srcStageMask) const {
5319 IgnoreReason reason = NotIgnored;
5320
5321 if (last_command == CMD_RESETEVENT && !HasBarrier(0U, 0U)) {
5322 reason = ResetWaitRace;
5323 } else if (unsynchronized_set) {
5324 reason = SetRace;
5325 } else {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005326 const VkPipelineStageFlags missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005327 if (missing_bits) reason = MissingStageBits;
5328 }
5329
5330 return reason;
5331}
5332
5333bool SyncEventState::HasBarrier(VkPipelineStageFlags stageMask, VkPipelineStageFlags exec_scope_arg) const {
5334 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5335 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5336 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005337}