blob: ab9ec7f8ec3d84bb7b1adb2aa14e5babedae9e20 [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 Zulaufb027cdb2020-05-21 14:25:22 -0600198
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700199static const SyncOrderingBarrier kColorAttachmentRasterOrder = {kColorAttachmentExecScope, kColorAttachmentAccessScope};
200static const SyncOrderingBarrier kDepthStencilAttachmentRasterOrder = {kDepthStencilAttachmentExecScope,
201 kDepthStencilAttachmentAccessScope};
202static const SyncOrderingBarrier kAttachmentRasterOrder = {kDepthStencilAttachmentExecScope | kColorAttachmentExecScope,
203 kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope};
John Zulauf7635de32020-05-29 17:14:15 -0600204// 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 -0600205static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600206
John Zulaufb02c1eb2020-10-06 16:33:36 -0600207static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) {
208 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
209}
210
211static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
212
locke-lunarg3c038002020-04-30 23:08:08 -0600213inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
214 if (size == VK_WHOLE_SIZE) {
215 return (whole_size - offset);
216 }
217 return size;
218}
219
John Zulauf3e86bf02020-09-12 10:47:57 -0600220static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
221 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
222}
223
John Zulauf16adfc92020-04-08 10:28:33 -0600224template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600225static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600226 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
227}
228
John Zulauf355e49b2020-04-24 15:11:15 -0600229static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600230
John Zulauf3e86bf02020-09-12 10:47:57 -0600231static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
232 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
233}
234
235static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
236 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
237}
238
John Zulauf4a6105a2020-11-17 15:11:05 -0700239// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
240//
John Zulauf10f1f522020-12-18 12:00:35 -0700241// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
242//
John Zulauf4a6105a2020-11-17 15:11:05 -0700243// Usage:
244// Constructor() -- initializes the generator to point to the begin of the space declared.
245// * -- the current range of the generator empty signfies end
246// ++ -- advance to the next non-empty range (or end)
247
248// A wrapper for a single range with the same semantics as the actual generators below
249template <typename KeyType>
250class SingleRangeGenerator {
251 public:
252 SingleRangeGenerator(const KeyType &range) : current_(range) {}
253 KeyType &operator*() const { return *current_; }
254 KeyType *operator->() const { return &*current_; }
255 SingleRangeGenerator &operator++() {
256 current_ = KeyType(); // just one real range
257 return *this;
258 }
259
260 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
261
262 private:
263 SingleRangeGenerator() = default;
264 const KeyType range_;
265 KeyType current_;
266};
267
268// Generate the ranges that are the intersection of range and the entries in the FilterMap
269template <typename FilterMap, typename KeyType = typename FilterMap::key_type>
270class FilteredRangeGenerator {
271 public:
272 FilteredRangeGenerator(const FilterMap &filter, const KeyType &range)
273 : range_(range), filter_(&filter), filter_pos_(), current_() {
274 SeekBegin();
275 }
276 const KeyType &operator*() const { return current_; }
277 const KeyType *operator->() const { return &current_; }
278 FilteredRangeGenerator &operator++() {
279 ++filter_pos_;
280 UpdateCurrent();
281 return *this;
282 }
283
284 bool operator==(const FilteredRangeGenerator &other) const { return current_ == other.current_; }
285
286 private:
287 FilteredRangeGenerator() = default;
288 void UpdateCurrent() {
289 if (filter_pos_ != filter_->cend()) {
290 current_ = range_ & filter_pos_->first;
291 } else {
292 current_ = KeyType();
293 }
294 }
295 void SeekBegin() {
296 filter_pos_ = filter_->lower_bound(range_);
297 UpdateCurrent();
298 }
299 const KeyType range_;
300 const FilterMap *filter_;
301 typename FilterMap::const_iterator filter_pos_;
302 KeyType current_;
303};
304using EventSimpleRangeGenerator = FilteredRangeGenerator<SyncEventState::ScopeMap>;
305
306// Templated to allow for different Range generators or map sources...
307
308// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulauf4a6105a2020-11-17 15:11:05 -0700309template <typename FilterMap, typename RangeGen, typename KeyType = typename FilterMap::key_type>
310class FilteredGeneratorGenerator {
311 public:
312 FilteredGeneratorGenerator(const FilterMap &filter, RangeGen &gen) : filter_(&filter), gen_(&gen), filter_pos_(), current_() {
313 SeekBegin();
314 }
315 const KeyType &operator*() const { return current_; }
316 const KeyType *operator->() const { return &current_; }
317 FilteredGeneratorGenerator &operator++() {
318 KeyType gen_range = GenRange();
319 KeyType filter_range = FilterRange();
320 current_ = KeyType();
321 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
322 if (gen_range.end > filter_range.end) {
323 // if the generated range is beyond the filter_range, advance the filter range
324 filter_range = AdvanceFilter();
325 } else {
326 gen_range = AdvanceGen();
327 }
328 current_ = gen_range & filter_range;
329 }
330 return *this;
331 }
332
333 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
334
335 private:
336 KeyType AdvanceFilter() {
337 ++filter_pos_;
338 auto filter_range = FilterRange();
339 if (filter_range.valid()) {
340 FastForwardGen(filter_range);
341 }
342 return filter_range;
343 }
344 KeyType AdvanceGen() {
345 ++(*gen_);
346 auto gen_range = GenRange();
347 if (gen_range.valid()) {
348 FastForwardFilter(gen_range);
349 }
350 return gen_range;
351 }
352
353 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
354 KeyType GenRange() const { return *(*gen_); }
355
356 KeyType FastForwardFilter(const KeyType &range) {
357 auto filter_range = FilterRange();
358 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700359 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700360 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
361 if (retry_count < kRetryLimit) {
362 ++filter_pos_;
363 filter_range = FilterRange();
364 retry_count++;
365 } else {
366 // Okay we've tried walking, do a seek.
367 filter_pos_ = filter_->lower_bound(range);
368 break;
369 }
370 }
371 return FilterRange();
372 }
373
374 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
375 // faster.
376 KeyType FastForwardGen(const KeyType &range) {
377 auto gen_range = GenRange();
378 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
379 ++(*gen_);
380 gen_range = GenRange();
381 }
382 return gen_range;
383 }
384
385 void SeekBegin() {
386 auto gen_range = GenRange();
387 if (gen_range.empty()) {
388 current_ = KeyType();
389 filter_pos_ = filter_->cend();
390 } else {
391 filter_pos_ = filter_->lower_bound(gen_range);
392 current_ = gen_range & FilterRange();
393 }
394 }
395
396 FilteredGeneratorGenerator() = default;
397 const FilterMap *filter_;
398 RangeGen *const gen_;
399 typename FilterMap::const_iterator filter_pos_;
400 KeyType current_;
401};
402
403using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
404
John Zulauf0cb5be22020-01-23 12:18:22 -0700405// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
406VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
407 VkPipelineStageFlags expanded = stage_mask;
408 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
409 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
410 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
411 if (all_commands.first & queue_flags) {
412 expanded |= all_commands.second;
413 }
414 }
415 }
416 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
417 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
418 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
419 }
420 return expanded;
421}
422
John Zulauf36bcf6a2020-02-03 15:12:52 -0700423VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
Jeremy Gebben91c36902020-11-09 08:17:08 -0700424 const std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
John Zulauf36bcf6a2020-02-03 15:12:52 -0700425 VkPipelineStageFlags unscanned = stage_mask;
426 VkPipelineStageFlags related = 0;
Jonah Ryan-Davis185189c2020-07-14 10:28:52 -0400427 for (const auto &entry : map) {
428 const auto &stage = entry.first;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700429 if (stage & unscanned) {
430 related = related | entry.second;
431 unscanned = unscanned & ~stage;
432 if (!unscanned) break;
433 }
434 }
435 return related;
436}
437
438VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
439 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
440}
441
442VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
443 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
444}
445
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700446static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700447
John Zulauf3e86bf02020-09-12 10:47:57 -0600448ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
449 VkDeviceSize stride) {
450 VkDeviceSize range_start = offset + first_index * stride;
451 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600452 if (count == UINT32_MAX) {
453 range_size = buf_whole_size - range_start;
454 } else {
455 range_size = count * stride;
456 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600457 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600458}
459
locke-lunarg654e3692020-06-04 17:19:15 -0600460SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
461 VkShaderStageFlagBits stage_flag) {
462 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
463 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
464 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
465 }
466 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
467 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
468 assert(0);
469 }
470 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
471 return stage_access->second.uniform_read;
472 }
473
474 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
475 // Because if write hazard happens, read hazard might or might not happen.
476 // But if write hazard doesn't happen, read hazard is impossible to happen.
477 if (descriptor_data.is_writable) {
478 return stage_access->second.shader_write;
479 }
480 return stage_access->second.shader_read;
481}
482
locke-lunarg37047832020-06-12 13:44:45 -0600483bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
484 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
485 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
486 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
487 ? true
488 : false;
489}
490
491bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
492 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
493 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
494 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
495 ? true
496 : false;
497}
498
John Zulauf355e49b2020-04-24 15:11:15 -0600499// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600500template <typename Action>
501static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
502 Action &action) {
503 // At this point the "apply over range" logic only supports a single memory binding
504 if (!SimpleBinding(image_state)) return;
505 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600506 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700507 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
508 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600509 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700510 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600511 }
512}
513
John Zulauf7635de32020-05-29 17:14:15 -0600514// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
515// Used by both validation and record operations
516//
517// The signature for Action() reflect the needs of both uses.
518template <typename Action>
519void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
520 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
521 VkExtent3D extent = CastTo3D(render_area.extent);
522 VkOffset3D offset = CastTo3D(render_area.offset);
523 const auto &rp_ci = rp_state.createInfo;
524 const auto *attachment_ci = rp_ci.pAttachments;
525 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
526
527 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
528 const auto *color_attachments = subpass_ci.pColorAttachments;
529 const auto *color_resolve = subpass_ci.pResolveAttachments;
530 if (color_resolve && color_attachments) {
531 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
532 const auto &color_attach = color_attachments[i].attachment;
533 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
534 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
535 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
536 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
537 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
538 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
539 }
540 }
541 }
542
543 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700544 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600545 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
546 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
547 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
548 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
549 const auto src_ci = attachment_ci[src_at];
550 // The formats are required to match so we can pick either
551 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
552 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
553 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
554 VkImageAspectFlags aspect_mask = 0u;
555
556 // Figure out which aspects are actually touched during resolve operations
557 const char *aspect_string = nullptr;
558 if (resolve_depth && resolve_stencil) {
559 // Validate all aspects together
560 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
561 aspect_string = "depth/stencil";
562 } else if (resolve_depth) {
563 // Validate depth only
564 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
565 aspect_string = "depth";
566 } else if (resolve_stencil) {
567 // Validate all stencil only
568 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
569 aspect_string = "stencil";
570 }
571
572 if (aspect_mask) {
573 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700574 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kAttachmentRasterOrder, offset, extent, aspect_mask);
John Zulauf7635de32020-05-29 17:14:15 -0600575 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
576 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
577 }
578 }
579}
580
581// Action for validating resolve operations
582class ValidateResolveAction {
583 public:
584 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
585 const char *func_name)
586 : render_pass_(render_pass),
587 subpass_(subpass),
588 context_(context),
589 sync_state_(sync_state),
590 func_name_(func_name),
591 skip_(false) {}
592 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
593 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
594 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
595 HazardResult hazard;
596 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
597 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600598 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
599 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600600 " to resolve attachment %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -0600601 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
John Zulauf37ceaed2020-07-03 16:18:15 -0600602 src_at, dst_at, string_UsageTag(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600603 }
604 }
605 // Providing a mechanism for the constructing caller to get the result of the validation
606 bool GetSkip() const { return skip_; }
607
608 private:
609 VkRenderPass render_pass_;
610 const uint32_t subpass_;
611 const AccessContext &context_;
612 const SyncValidator &sync_state_;
613 const char *func_name_;
614 bool skip_;
615};
616
617// Update action for resolve operations
618class UpdateStateResolveAction {
619 public:
620 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
621 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
622 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
623 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
624 // Ignores validation only arguments...
625 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
626 }
627
628 private:
629 AccessContext &context_;
630 const ResourceUsageTag &tag_;
631};
632
John Zulauf59e25072020-07-17 10:55:21 -0600633void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700634 const SyncStageAccessFlags &prior_, const ResourceUsageTag &tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600635 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
636 usage_index = usage_index_;
637 hazard = hazard_;
638 prior_access = prior_;
639 tag = tag_;
640}
641
John Zulauf540266b2020-04-06 18:54:53 -0600642AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
643 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600644 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600645 Reset();
646 const auto &subpass_dep = dependencies[subpass];
647 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600648 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600649 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600650 const auto prev_pass = prev_dep.first->pass;
651 const auto &prev_barriers = prev_dep.second;
652 assert(prev_dep.second.size());
653 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
654 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700655 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600656
657 async_.reserve(subpass_dep.async.size());
658 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700659 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600660 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600661 if (subpass_dep.barrier_from_external.size()) {
662 src_external_ = TrackBack(external_context, queue_flags, subpass_dep.barrier_from_external);
John Zulaufe5da6e52020-03-18 15:32:18 -0600663 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600664 if (subpass_dep.barrier_to_external.size()) {
665 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600666 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700667}
668
John Zulauf5f13a792020-03-10 07:31:21 -0600669template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700670HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600671 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600672 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600673 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600674
675 HazardResult hazard;
676 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
677 hazard = detector.Detect(prev);
678 }
679 return hazard;
680}
681
John Zulauf4a6105a2020-11-17 15:11:05 -0700682template <typename Action>
683void AccessContext::ForAll(Action &&action) {
684 for (const auto address_type : kAddressTypes) {
685 auto &accesses = GetAccessStateMap(address_type);
686 for (const auto &access : accesses) {
687 action(address_type, access);
688 }
689 }
690}
691
John Zulauf3d84f1b2020-03-09 13:33:25 -0600692// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
693// the DAG of the contexts (for example subpasses)
694template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700695HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600696 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600697 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600698
John Zulauf1a224292020-06-30 14:52:13 -0600699 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600700 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
701 // so we'll check these first
702 for (const auto &async_context : async_) {
703 hazard = async_context->DetectAsyncHazard(type, detector, range);
704 if (hazard.hazard) return hazard;
705 }
John Zulauf5f13a792020-03-10 07:31:21 -0600706 }
707
John Zulauf1a224292020-06-30 14:52:13 -0600708 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600709
John Zulauf69133422020-05-20 14:55:53 -0600710 const auto &accesses = GetAccessStateMap(type);
711 const auto from = accesses.lower_bound(range);
712 const auto to = accesses.upper_bound(range);
713 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600714
John Zulauf69133422020-05-20 14:55:53 -0600715 for (auto pos = from; pos != to; ++pos) {
716 // Cover any leading gap, or gap between entries
717 if (detect_prev) {
718 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
719 // Cover any leading gap, or gap between entries
720 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600721 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600722 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600723 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600724 if (hazard.hazard) return hazard;
725 }
John Zulauf69133422020-05-20 14:55:53 -0600726 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
727 gap.begin = pos->first.end;
728 }
729
730 hazard = detector.Detect(pos);
731 if (hazard.hazard) return hazard;
732 }
733
734 if (detect_prev) {
735 // Detect in the trailing empty as needed
736 gap.end = range.end;
737 if (gap.non_empty()) {
738 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600739 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600740 }
741
742 return hazard;
743}
744
745// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
746template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700747HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
748 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600749 auto &accesses = GetAccessStateMap(type);
750 const auto from = accesses.lower_bound(range);
751 const auto to = accesses.upper_bound(range);
752
John Zulauf3d84f1b2020-03-09 13:33:25 -0600753 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600754 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700755 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600756 }
John Zulauf16adfc92020-04-08 10:28:33 -0600757
John Zulauf3d84f1b2020-03-09 13:33:25 -0600758 return hazard;
759}
760
John Zulaufb02c1eb2020-10-06 16:33:36 -0600761struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700762 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600763 void operator()(ResourceAccessState *access) const {
764 assert(access);
765 access->ApplyBarriers(barriers, true);
766 }
767 const std::vector<SyncBarrier> &barriers;
768};
769
770struct ApplyTrackbackBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700771 explicit ApplyTrackbackBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600772 void operator()(ResourceAccessState *access) const {
773 assert(access);
774 assert(!access->HasPendingState());
775 access->ApplyBarriers(barriers, false);
776 access->ApplyPendingBarriers(kCurrentCommandTag);
777 }
778 const std::vector<SyncBarrier> &barriers;
779};
780
781// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
782// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
783// *different* map from dest.
784// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
785// range [first, last)
786template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600787static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
788 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600789 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600790 auto at = entry;
791 for (auto pos = first; pos != last; ++pos) {
792 // Every member of the input iterator range must fit within the remaining portion of entry
793 assert(at->first.includes(pos->first));
794 assert(at != dest->end());
795 // Trim up at to the same size as the entry to resolve
796 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600797 auto access = pos->second; // intentional copy
798 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600799 at->second.Resolve(access);
800 ++at; // Go to the remaining unused section of entry
801 }
802}
803
John Zulaufa0a98292020-09-18 09:30:10 -0600804static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
805 SyncBarrier merged = {};
806 for (const auto &barrier : barriers) {
807 merged.Merge(barrier);
808 }
809 return merged;
810}
811
John Zulaufb02c1eb2020-10-06 16:33:36 -0600812template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700813void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600814 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
815 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600816 if (!range.non_empty()) return;
817
John Zulauf355e49b2020-04-24 15:11:15 -0600818 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
819 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600820 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600821 if (current->pos_B->valid) {
822 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600823 auto access = src_pos->second; // intentional copy
824 barrier_action(&access);
825
John Zulauf16adfc92020-04-08 10:28:33 -0600826 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600827 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
828 trimmed->second.Resolve(access);
829 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600830 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600831 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600832 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600833 }
John Zulauf16adfc92020-04-08 10:28:33 -0600834 } else {
835 // we have to descend to fill this gap
836 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600837 if (current->pos_A->valid) {
838 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
839 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600840 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600841 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -0600842 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600843 // There isn't anything in dest in current)range, so we can accumulate directly into it.
844 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600845 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
846 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
847 barrier_action(&pos->second);
John Zulauf355e49b2020-04-24 15:11:15 -0600848 }
849 }
850 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
851 // iterator of the outer while.
852
853 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
854 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
855 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600856 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
857 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600858 current.seek(seek_to);
859 } else if (!current->pos_A->valid && infill_state) {
860 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
861 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
862 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600863 }
John Zulauf5f13a792020-03-10 07:31:21 -0600864 }
John Zulauf16adfc92020-04-08 10:28:33 -0600865 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600866 }
John Zulauf1a224292020-06-30 14:52:13 -0600867
868 // Infill if range goes passed both the current and resolve map prior contents
869 if (recur_to_infill && (current->range.end < range.end)) {
870 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
871 ResourceAccessRangeMap gap_map;
872 const auto the_end = resolve_map->end();
873 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
874 for (auto &access : gap_map) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600875 barrier_action(&access.second);
John Zulauf1a224292020-06-30 14:52:13 -0600876 resolve_map->insert(the_end, access);
877 }
878 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600879}
880
John Zulauf43cc7462020-12-03 12:33:12 -0700881void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
882 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600883 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600884 if (range.non_empty() && infill_state) {
885 descent_map->insert(std::make_pair(range, *infill_state));
886 }
887 } else {
888 // Look for something to fill the gap further along.
889 for (const auto &prev_dep : prev_) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600890 const ApplyTrackbackBarriersAction barrier_action(prev_dep.barriers);
891 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600892 }
893
John Zulaufe5da6e52020-03-18 15:32:18 -0600894 if (src_external_.context) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600895 const ApplyTrackbackBarriersAction barrier_action(src_external_.barriers);
896 src_external_.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600897 }
898 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600899}
900
John Zulauf4a6105a2020-11-17 15:11:05 -0700901// Non-lazy import of all accesses, WaitEvents needs this.
902void AccessContext::ResolvePreviousAccesses() {
903 ResourceAccessState default_state;
904 for (const auto address_type : kAddressTypes) {
905 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
906 }
907}
908
John Zulauf43cc7462020-12-03 12:33:12 -0700909AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
910 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -0600911}
912
John Zulauf1507ee42020-05-18 11:33:09 -0600913static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
914 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
915 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
916 return stage_access;
917}
918static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
919 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
920 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
921 return stage_access;
922}
923
John Zulauf7635de32020-05-29 17:14:15 -0600924// Caller must manage returned pointer
925static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
926 uint32_t subpass, const VkRect2D &render_area,
927 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
928 auto *proxy = new AccessContext(context);
929 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600930 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600931 return proxy;
932}
933
John Zulaufb02c1eb2020-10-06 16:33:36 -0600934template <typename BarrierAction>
John Zulauf52446eb2020-10-22 16:40:08 -0600935class ResolveAccessRangeFunctor {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600936 public:
John Zulauf43cc7462020-12-03 12:33:12 -0700937 ResolveAccessRangeFunctor(const AccessContext &context, AccessAddressType address_type, ResourceAccessRangeMap *descent_map,
938 const ResourceAccessState *infill_state, BarrierAction &barrier_action)
John Zulauf52446eb2020-10-22 16:40:08 -0600939 : context_(context),
940 address_type_(address_type),
941 descent_map_(descent_map),
942 infill_state_(infill_state),
943 barrier_action_(barrier_action) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600944 ResolveAccessRangeFunctor() = delete;
945 void operator()(const ResourceAccessRange &range) const {
946 context_.ResolveAccessRange(address_type_, range, barrier_action_, descent_map_, infill_state_);
947 }
948
949 private:
John Zulauf52446eb2020-10-22 16:40:08 -0600950 const AccessContext &context_;
John Zulauf43cc7462020-12-03 12:33:12 -0700951 const AccessAddressType address_type_;
John Zulauf52446eb2020-10-22 16:40:08 -0600952 ResourceAccessRangeMap *const descent_map_;
953 const ResourceAccessState *infill_state_;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600954 BarrierAction &barrier_action_;
955};
956
John Zulaufb02c1eb2020-10-06 16:33:36 -0600957template <typename BarrierAction>
958void AccessContext::ResolveAccessRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -0700959 BarrierAction &barrier_action, AccessAddressType address_type,
960 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600961 const ResolveAccessRangeFunctor<BarrierAction> action(*this, address_type, descent_map, infill_state, barrier_action);
962 ApplyOverImageRange(image_state, subresource_range, action);
John Zulauf62f10592020-04-03 12:20:02 -0600963}
964
John Zulauf7635de32020-05-29 17:14:15 -0600965// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600966bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600967 const VkRect2D &render_area, uint32_t subpass,
968 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
969 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600970 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600971 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
972 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
973 // those affects have not been recorded yet.
974 //
975 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
976 // to apply and only copy then, if this proves a hot spot.
977 std::unique_ptr<AccessContext> proxy_for_prev;
978 TrackBack proxy_track_back;
979
John Zulauf355e49b2020-04-24 15:11:15 -0600980 const auto &transitions = rp_state.subpass_transitions[subpass];
981 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600982 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
983
984 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
985 if (prev_needs_proxy) {
986 if (!proxy_for_prev) {
987 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
988 render_area, attachment_views));
989 proxy_track_back = *track_back;
990 proxy_track_back.context = proxy_for_prev.get();
991 }
992 track_back = &proxy_track_back;
993 }
994 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600995 if (hazard.hazard) {
John Zulauf389c34b2020-07-28 11:19:35 -0600996 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
997 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
998 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
999 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1000 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
1001 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001002 }
1003 }
1004 return skip;
1005}
1006
John Zulauf1507ee42020-05-18 11:33:09 -06001007bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001008 const VkRect2D &render_area, uint32_t subpass,
1009 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1010 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001011 bool skip = false;
1012 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1013 VkExtent3D extent = CastTo3D(render_area.extent);
1014 VkOffset3D offset = CastTo3D(render_area.offset);
John Zulaufa0a98292020-09-18 09:30:10 -06001015
John Zulauf1507ee42020-05-18 11:33:09 -06001016 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1017 if (subpass == rp_state.attachment_first_subpass[i]) {
1018 if (attachment_views[i] == nullptr) continue;
1019 const IMAGE_VIEW_STATE &view = *attachment_views[i];
1020 const IMAGE_STATE *image = view.image_state.get();
1021 if (image == nullptr) continue;
1022 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001023
1024 // Need check in the following way
1025 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1026 // vs. transition
1027 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1028 // for each aspect loaded.
1029
1030 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001031 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001032 const bool is_color = !(has_depth || has_stencil);
1033
1034 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001035 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001036
John Zulaufaff20662020-06-01 14:07:58 -06001037 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001038 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001039
John Zulaufb02c1eb2020-10-06 16:33:36 -06001040 auto hazard_range = view.normalized_subresource_range;
1041 bool checked_stencil = false;
1042 if (is_color) {
John Zulauf859089b2020-10-29 17:37:03 -06001043 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, kColorAttachmentRasterOrder, offset,
1044 extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001045 aspect = "color";
1046 } else {
1047 if (has_depth) {
1048 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf859089b2020-10-29 17:37:03 -06001049 hazard = DetectHazard(*image, load_index, hazard_range, kDepthStencilAttachmentRasterOrder, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001050 aspect = "depth";
1051 }
1052 if (!hazard.hazard && has_stencil) {
1053 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf859089b2020-10-29 17:37:03 -06001054 hazard =
1055 DetectHazard(*image, stencil_load_index, hazard_range, kDepthStencilAttachmentRasterOrder, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001056 aspect = "stencil";
1057 checked_stencil = true;
1058 }
1059 }
1060
1061 if (hazard.hazard) {
1062 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
1063 if (hazard.tag == kCurrentCommandTag) {
1064 // Hazard vs. ILT
1065 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1066 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1067 " aspect %s during load with loadOp %s.",
1068 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1069 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06001070 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1071 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001072 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001073 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -06001074 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001075 }
1076 }
1077 }
1078 }
1079 return skip;
1080}
1081
John Zulaufaff20662020-06-01 14:07:58 -06001082// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1083// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1084// store is part of the same Next/End operation.
1085// The latter is handled in layout transistion validation directly
1086bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
1087 const VkRect2D &render_area, uint32_t subpass,
1088 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1089 const char *func_name) const {
1090 bool skip = false;
1091 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1092 VkExtent3D extent = CastTo3D(render_area.extent);
1093 VkOffset3D offset = CastTo3D(render_area.offset);
1094
1095 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1096 if (subpass == rp_state.attachment_last_subpass[i]) {
1097 if (attachment_views[i] == nullptr) continue;
1098 const IMAGE_VIEW_STATE &view = *attachment_views[i];
1099 const IMAGE_STATE *image = view.image_state.get();
1100 if (image == nullptr) continue;
1101 const auto &ci = attachment_ci[i];
1102
1103 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1104 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1105 // sake, we treat DONT_CARE as writing.
1106 const bool has_depth = FormatHasDepth(ci.format);
1107 const bool has_stencil = FormatHasStencil(ci.format);
1108 const bool is_color = !(has_depth || has_stencil);
1109 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1110 if (!has_stencil && !store_op_stores) continue;
1111
1112 HazardResult hazard;
1113 const char *aspect = nullptr;
1114 bool checked_stencil = false;
1115 if (is_color) {
1116 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1117 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
1118 aspect = "color";
1119 } else {
1120 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1121 auto hazard_range = view.normalized_subresource_range;
1122 if (has_depth && store_op_stores) {
1123 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1124 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
1125 kAttachmentRasterOrder, offset, extent);
1126 aspect = "depth";
1127 }
1128 if (!hazard.hazard && has_stencil && stencil_op_stores) {
1129 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1130 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
1131 kAttachmentRasterOrder, offset, extent);
1132 aspect = "stencil";
1133 checked_stencil = true;
1134 }
1135 }
1136
1137 if (hazard.hazard) {
1138 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1139 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -06001140 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1141 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001142 " %s aspect during store with %s %s. Access info %s",
John Zulauf1dae9192020-06-16 15:46:44 -06001143 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -06001144 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001145 }
1146 }
1147 }
1148 return skip;
1149}
1150
John Zulaufb027cdb2020-05-21 14:25:22 -06001151bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
1152 const VkRect2D &render_area,
1153 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
1154 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -06001155 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
1156 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
1157 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001158}
1159
John Zulauf3d84f1b2020-03-09 13:33:25 -06001160class HazardDetector {
1161 SyncStageAccessIndex usage_index_;
1162
1163 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001164 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001165 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1166 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001167 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001168 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001169};
1170
John Zulauf69133422020-05-20 14:55:53 -06001171class HazardDetectorWithOrdering {
1172 const SyncStageAccessIndex usage_index_;
1173 const SyncOrderingBarrier &ordering_;
1174
1175 public:
1176 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1177 return pos->second.DetectHazard(usage_index_, ordering_);
1178 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001179 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1180 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001181 }
1182 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
1183 : usage_index_(usage), ordering_(ordering) {}
1184};
1185
John Zulauf16adfc92020-04-08 10:28:33 -06001186HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001187 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001188 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001189 const auto base_address = ResourceBaseAddress(buffer);
1190 HazardDetector detector(usage_index);
1191 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001192}
1193
John Zulauf69133422020-05-20 14:55:53 -06001194template <typename Detector>
1195HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1196 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1197 const VkExtent3D &extent, DetectOptions options) const {
1198 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001199 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001200 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1201 base_address);
1202 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001203 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001204 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001205 if (hazard.hazard) return hazard;
1206 }
1207 return HazardResult();
1208}
1209
John Zulauf540266b2020-04-06 18:54:53 -06001210HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1211 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1212 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001213 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1214 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -06001215 return DetectHazard(image, current_usage, subresource_range, offset, extent);
1216}
1217
1218HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1219 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1220 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001221 HazardDetector detector(current_usage);
1222 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1223}
1224
1225HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1226 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
1227 const VkOffset3D &offset, const VkExtent3D &extent) const {
1228 HazardDetectorWithOrdering detector(current_usage, ordering);
1229 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001230}
1231
John Zulaufb027cdb2020-05-21 14:25:22 -06001232// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1233// should have reported the issue regarding an invalid attachment entry
1234HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
1235 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
1236 VkImageAspectFlags aspect_mask) const {
1237 if (view != nullptr) {
1238 const IMAGE_STATE *image = view->image_state.get();
1239 if (image != nullptr) {
1240 auto *detect_range = &view->normalized_subresource_range;
1241 VkImageSubresourceRange masked_range;
1242 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1243 masked_range = view->normalized_subresource_range;
1244 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1245 detect_range = &masked_range;
1246 }
1247
1248 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1249 if (detect_range->aspectMask) {
1250 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
1251 }
1252 }
1253 }
1254 return HazardResult();
1255}
John Zulauf43cc7462020-12-03 12:33:12 -07001256
John Zulauf3d84f1b2020-03-09 13:33:25 -06001257class BarrierHazardDetector {
1258 public:
1259 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1260 SyncStageAccessFlags src_access_scope)
1261 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1262
John Zulauf5f13a792020-03-10 07:31:21 -06001263 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1264 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001265 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001266 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001267 // 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 -07001268 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001269 }
1270
1271 private:
1272 SyncStageAccessIndex usage_index_;
1273 VkPipelineStageFlags src_exec_scope_;
1274 SyncStageAccessFlags src_access_scope_;
1275};
1276
John Zulauf4a6105a2020-11-17 15:11:05 -07001277class EventBarrierHazardDetector {
1278 public:
1279 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1280 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
1281 const ResourceUsageTag &scope_tag)
1282 : usage_index_(usage_index),
1283 src_exec_scope_(src_exec_scope),
1284 src_access_scope_(src_access_scope),
1285 event_scope_(event_scope),
1286 scope_pos_(event_scope.cbegin()),
1287 scope_end_(event_scope.cend()),
1288 scope_tag_(scope_tag) {}
1289
1290 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1291 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1292 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1293 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1294 if (scope_pos_ == scope_end_) return HazardResult();
1295 if (!scope_pos_->first.intersects(pos->first)) {
1296 event_scope_.lower_bound(pos->first);
1297 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1298 }
1299
1300 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1301 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1302 }
1303 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1304 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1305 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1306 }
1307
1308 private:
1309 SyncStageAccessIndex usage_index_;
1310 VkPipelineStageFlags src_exec_scope_;
1311 SyncStageAccessFlags src_access_scope_;
1312 const SyncEventState::ScopeMap &event_scope_;
1313 SyncEventState::ScopeMap::const_iterator scope_pos_;
1314 SyncEventState::ScopeMap::const_iterator scope_end_;
1315 const ResourceUsageTag &scope_tag_;
1316};
1317
1318HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1319 const SyncStageAccessFlags &src_access_scope,
1320 const VkImageSubresourceRange &subresource_range,
1321 const SyncEventState &sync_event, DetectOptions options) const {
1322 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1323 // first access scope map to use, and there's no easy way to plumb it in below.
1324 const auto address_type = ImageAddressType(image);
1325 const auto &event_scope = sync_event.FirstScope(address_type);
1326
1327 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1328 event_scope, sync_event.first_scope_tag);
1329 VkOffset3D zero_offset = {0, 0, 0};
1330 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
1331}
1332
John Zulauf16adfc92020-04-08 10:28:33 -06001333HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001334 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001335 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001336 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001337 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1338 VkOffset3D zero_offset = {0, 0, 0};
1339 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001340}
1341
John Zulauf355e49b2020-04-24 15:11:15 -06001342HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001343 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001344 const VkImageMemoryBarrier &barrier) const {
1345 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1346 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1347 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1348}
1349
John Zulauf9cb530d2019-09-30 14:14:10 -06001350template <typename Flags, typename Map>
1351SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1352 SyncStageAccessFlags scope = 0;
1353 for (const auto &bit_scope : map) {
1354 if (flag_mask < bit_scope.first) break;
1355
1356 if (flag_mask & bit_scope.first) {
1357 scope |= bit_scope.second;
1358 }
1359 }
1360 return scope;
1361}
1362
1363SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1364 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1365}
1366
1367SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1368 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1369}
1370
1371// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1372SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001373 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1374 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1375 // 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 -06001376 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1377}
1378
1379template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001380void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001381 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1382 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001383 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001384 auto pos = accesses->lower_bound(range);
1385 if (pos == accesses->end() || !pos->first.intersects(range)) {
1386 // The range is empty, fill it with a default value.
1387 pos = action.Infill(accesses, pos, range);
1388 } else if (range.begin < pos->first.begin) {
1389 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001390 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001391 } else if (pos->first.begin < range.begin) {
1392 // Trim the beginning if needed
1393 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1394 ++pos;
1395 }
1396
1397 const auto the_end = accesses->end();
1398 while ((pos != the_end) && pos->first.intersects(range)) {
1399 if (pos->first.end > range.end) {
1400 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1401 }
1402
1403 pos = action(accesses, pos);
1404 if (pos == the_end) break;
1405
1406 auto next = pos;
1407 ++next;
1408 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1409 // Need to infill if next is disjoint
1410 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001411 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001412 next = action.Infill(accesses, next, new_range);
1413 }
1414 pos = next;
1415 }
1416}
John Zulauf4a6105a2020-11-17 15:11:05 -07001417template <typename Action, typename RangeGen>
1418void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1419 assert(range_gen_arg);
1420 auto &range_gen = *range_gen_arg; // Non-const references must be * by style requirement but deref-ing * iterator is a pain
1421 for (; range_gen->non_empty(); ++range_gen) {
1422 UpdateMemoryAccessState(accesses, *range_gen, action);
1423 }
1424}
John Zulauf9cb530d2019-09-30 14:14:10 -06001425
1426struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001427 using Iterator = ResourceAccessRangeMap::iterator;
1428 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001429 // this is only called on gaps, and never returns a gap.
1430 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001431 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001432 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001433 }
John Zulauf5f13a792020-03-10 07:31:21 -06001434
John Zulauf5c5e88d2019-12-26 11:22:02 -07001435 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001436 auto &access_state = pos->second;
1437 access_state.Update(usage, tag);
1438 return pos;
1439 }
1440
John Zulauf43cc7462020-12-03 12:33:12 -07001441 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001442 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001443 : type(type_), context(context_), usage(usage_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001444 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001445 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001446 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001447 const ResourceUsageTag &tag;
1448};
1449
John Zulauf4a6105a2020-11-17 15:11:05 -07001450// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001451struct PipelineBarrierOp {
1452 SyncBarrier barrier;
1453 bool layout_transition;
1454 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1455 : barrier(barrier_), layout_transition(layout_transition_) {}
1456 PipelineBarrierOp() = default;
1457 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1458};
John Zulauf4a6105a2020-11-17 15:11:05 -07001459// The barrier operation for wait events
1460struct WaitEventBarrierOp {
1461 const ResourceUsageTag *scope_tag;
1462 SyncBarrier barrier;
1463 bool layout_transition;
1464 WaitEventBarrierOp(const ResourceUsageTag &scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1465 : scope_tag(&scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
1466 WaitEventBarrierOp() = default;
1467 void operator()(ResourceAccessState *access_state) const {
1468 assert(scope_tag); // Not valid to have a non-scope op executed, default construct included for std::vector support
1469 access_state->ApplyBarrier(*scope_tag, barrier, layout_transition);
1470 }
1471};
John Zulauf1e331ec2020-12-04 18:29:38 -07001472
John Zulauf4a6105a2020-11-17 15:11:05 -07001473// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1474// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1475// of a collection is known/present.
John Zulauf1e331ec2020-12-04 18:29:38 -07001476template <typename BarrierOp>
John Zulauf89311b42020-09-29 16:28:47 -06001477class ApplyBarrierOpsFunctor {
1478 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001479 using Iterator = ResourceAccessRangeMap::iterator;
1480 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001481
John Zulauf5c5e88d2019-12-26 11:22:02 -07001482 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001483 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001484 for (const auto &op : barrier_ops_) {
1485 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001486 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001487
John Zulauf89311b42020-09-29 16:28:47 -06001488 if (resolve_) {
1489 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1490 // another walk
1491 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001492 }
1493 return pos;
1494 }
1495
John Zulauf89311b42020-09-29 16:28:47 -06001496 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulauf1e331ec2020-12-04 18:29:38 -07001497 ApplyBarrierOpsFunctor(bool resolve, const std::vector<BarrierOp> &barrier_ops, const ResourceUsageTag &tag)
1498 : resolve_(resolve), barrier_ops_(barrier_ops), tag_(tag) {}
John Zulauf89311b42020-09-29 16:28:47 -06001499
1500 private:
1501 bool resolve_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001502 const std::vector<BarrierOp> &barrier_ops_;
1503 const ResourceUsageTag &tag_;
1504};
1505
John Zulauf4a6105a2020-11-17 15:11:05 -07001506// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1507// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1508template <typename BarrierOp>
1509class ApplyBarrierFunctor {
1510 public:
1511 using Iterator = ResourceAccessRangeMap::iterator;
1512 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1513
1514 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1515 auto &access_state = pos->second;
1516 barrier_op_(&access_state);
1517 return pos;
1518 }
1519
1520 ApplyBarrierFunctor(const BarrierOp &barrier_op) : barrier_op_(barrier_op) {}
1521
1522 private:
1523 const BarrierOp barrier_op_;
1524};
1525
John Zulauf1e331ec2020-12-04 18:29:38 -07001526// This functor resolves the pendinging state.
1527class ResolvePendingBarrierFunctor {
1528 public:
1529 using Iterator = ResourceAccessRangeMap::iterator;
1530 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1531
1532 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1533 auto &access_state = pos->second;
1534 access_state.ApplyPendingBarriers(tag_);
1535 return pos;
1536 }
1537
1538 ResolvePendingBarrierFunctor(const ResourceUsageTag &tag) : tag_(tag) {}
1539
1540 private:
John Zulauf89311b42020-09-29 16:28:47 -06001541 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001542};
1543
John Zulauf43cc7462020-12-03 12:33:12 -07001544void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -06001545 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001546 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1547 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001548}
1549
John Zulauf16adfc92020-04-08 10:28:33 -06001550void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001551 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001552 if (!SimpleBinding(buffer)) return;
1553 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf43cc7462020-12-03 12:33:12 -07001554 UpdateAccessState(AccessAddressType::kLinear, current_usage, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001555}
John Zulauf355e49b2020-04-24 15:11:15 -06001556
John Zulauf540266b2020-04-06 18:54:53 -06001557void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001558 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001559 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001560 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001561 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001562 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1563 base_address);
1564 const auto address_type = ImageAddressType(image);
John Zulauf16adfc92020-04-08 10:28:33 -06001565 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001566 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001567 UpdateMemoryAccessState(&GetAccessStateMap(address_type), *range_gen, action);
John Zulauf5f13a792020-03-10 07:31:21 -06001568 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001569}
John Zulauf7635de32020-05-29 17:14:15 -06001570void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1571 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1572 if (view != nullptr) {
1573 const IMAGE_STATE *image = view->image_state.get();
1574 if (image != nullptr) {
1575 auto *update_range = &view->normalized_subresource_range;
1576 VkImageSubresourceRange masked_range;
1577 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1578 masked_range = view->normalized_subresource_range;
1579 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1580 update_range = &masked_range;
1581 }
1582 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1583 }
1584 }
1585}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001586
John Zulauf355e49b2020-04-24 15:11:15 -06001587void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1588 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1589 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001590 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1591 subresource.layerCount};
1592 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1593}
1594
John Zulauf540266b2020-04-06 18:54:53 -06001595template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001596void AccessContext::UpdateResourceAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001597 if (!SimpleBinding(buffer)) return;
1598 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf43cc7462020-12-03 12:33:12 -07001599 UpdateMemoryAccessState(&GetAccessStateMap(AccessAddressType::kLinear), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001600}
1601
1602template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001603void AccessContext::UpdateResourceAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1604 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001605 if (!SimpleBinding(image)) return;
1606 const auto address_type = ImageAddressType(image);
1607 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001608
John Zulauf16adfc92020-04-08 10:28:33 -06001609 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001610 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
1611 image.createInfo.extent, base_address);
1612
John Zulauf540266b2020-04-06 18:54:53 -06001613 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001614 UpdateMemoryAccessState(accesses, *range_gen, action);
John Zulauf540266b2020-04-06 18:54:53 -06001615 }
1616}
1617
John Zulauf7635de32020-05-29 17:14:15 -06001618void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1619 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1620 const ResourceUsageTag &tag) {
1621 UpdateStateResolveAction update(*this, tag);
1622 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1623}
1624
John Zulaufaff20662020-06-01 14:07:58 -06001625void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1626 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1627 const ResourceUsageTag &tag) {
1628 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1629 VkExtent3D extent = CastTo3D(render_area.extent);
1630 VkOffset3D offset = CastTo3D(render_area.offset);
1631
1632 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1633 if (rp_state.attachment_last_subpass[i] == subpass) {
1634 if (attachment_views[i] == nullptr) continue; // UNUSED
1635 const auto &view = *attachment_views[i];
1636 const IMAGE_STATE *image = view.image_state.get();
1637 if (image == nullptr) continue;
1638
1639 const auto &ci = attachment_ci[i];
1640 const bool has_depth = FormatHasDepth(ci.format);
1641 const bool has_stencil = FormatHasStencil(ci.format);
1642 const bool is_color = !(has_depth || has_stencil);
1643 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1644
1645 if (is_color && store_op_stores) {
1646 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1647 offset, extent, tag);
1648 } else {
1649 auto update_range = view.normalized_subresource_range;
1650 if (has_depth && store_op_stores) {
1651 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1652 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1653 tag);
1654 }
1655 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1656 if (has_stencil && stencil_op_stores) {
1657 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1658 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1659 tag);
1660 }
1661 }
1662 }
1663 }
1664}
1665
John Zulauf540266b2020-04-06 18:54:53 -06001666template <typename Action>
1667void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1668 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001669 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001670 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001671 }
1672}
1673
1674void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001675 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1676 auto &context = contexts[subpass_index];
John Zulaufb02c1eb2020-10-06 16:33:36 -06001677 ApplyTrackbackBarriersAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001678 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001679 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001680 }
1681 }
1682}
1683
John Zulauf355e49b2020-04-24 15:11:15 -06001684// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001685HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001686 if (!attach_view) return HazardResult();
1687 const auto image_state = attach_view->image_state.get();
1688 if (!image_state) return HazardResult();
1689
John Zulauf355e49b2020-04-24 15:11:15 -06001690 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001691 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001692
1693 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001694 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1695 const auto merged_barrier = MergeBarriers(track_back.barriers);
1696 HazardResult hazard =
1697 track_back.context->DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
1698 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001699 if (!hazard.hazard) {
1700 // The Async hazard check is against the current context's async set.
John Zulaufa0a98292020-09-18 09:30:10 -06001701 hazard = DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001702 attach_view->normalized_subresource_range, kDetectAsync);
1703 }
John Zulaufa0a98292020-09-18 09:30:10 -06001704
John Zulauf355e49b2020-04-24 15:11:15 -06001705 return hazard;
1706}
1707
John Zulaufb02c1eb2020-10-06 16:33:36 -06001708void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
1709 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1710 const ResourceUsageTag &tag) {
1711 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001712 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001713 for (const auto &transition : transitions) {
1714 const auto prev_pass = transition.prev_pass;
1715 const auto attachment_view = attachment_views[transition.attachment];
1716 if (!attachment_view) continue;
1717 const auto *image = attachment_view->image_state.get();
1718 if (!image) continue;
1719 if (!SimpleBinding(*image)) continue;
1720
1721 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1722 assert(trackback);
1723
1724 // Import the attachments into the current context
1725 const auto *prev_context = trackback->context;
1726 assert(prev_context);
1727 const auto address_type = ImageAddressType(*image);
1728 auto &target_map = GetAccessStateMap(address_type);
1729 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
1730 prev_context->ResolveAccessRange(*image, attachment_view->normalized_subresource_range, barrier_action, address_type,
John Zulauf646cc292020-10-23 09:16:45 -06001731 &target_map, &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001732 }
1733
John Zulauf86356ca2020-10-19 11:46:41 -06001734 // If there were no transitions skip this global map walk
1735 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001736 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulauf86356ca2020-10-19 11:46:41 -06001737 ApplyGlobalBarriers(apply_pending_action);
1738 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001739}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001740
1741void CommandBufferAccessContext::ApplyBufferBarriers(const SyncEventState &sync_event, const SyncExecScope &dst,
1742 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
John Zulauf4a6105a2020-11-17 15:11:05 -07001743 const auto &scope_tag = sync_event.first_scope_tag;
1744 auto *access_context = GetCurrentAccessContext();
1745 const auto address_type = AccessAddressType::kLinear;
1746 for (uint32_t index = 0; index < barrier_count; index++) {
1747 auto barrier = barriers[index]; // barrier is a copy
1748 const auto *buffer = sync_state_->Get<BUFFER_STATE>(barrier.buffer);
1749 if (!buffer) continue;
1750 const auto base_address = ResourceBaseAddress(*buffer);
1751 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
1752 const ResourceAccessRange range = MakeRange(barrier) + base_address;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001753 const SyncBarrier sync_barrier(barrier, sync_event.scope, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001754 const ApplyBarrierFunctor<WaitEventBarrierOp> barrier_action({scope_tag, sync_barrier, false /* layout_transition */});
1755 EventSimpleRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), range);
1756 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barrier_action, &filtered_range_gen);
1757 }
1758}
1759
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001760void CommandBufferAccessContext::ApplyGlobalBarriers(SyncEventState &sync_event, const SyncExecScope &dst,
1761 uint32_t memory_barrier_count, const VkMemoryBarrier *pMemoryBarriers,
1762 const ResourceUsageTag &tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07001763 std::vector<WaitEventBarrierOp> barrier_ops;
1764 barrier_ops.reserve(std::min<uint32_t>(memory_barrier_count, 1));
1765 const auto &scope_tag = sync_event.first_scope_tag;
1766 auto *access_context = GetCurrentAccessContext();
1767 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
1768 const auto &barrier = pMemoryBarriers[barrier_index];
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001769 SyncBarrier sync_barrier(barrier, sync_event.scope, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001770 barrier_ops.emplace_back(scope_tag, sync_barrier, false);
1771 }
1772 if (0 == memory_barrier_count) {
1773 // If there are no global memory barriers, force an exec barrier
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001774 barrier_ops.emplace_back(scope_tag, SyncBarrier(sync_event.scope, dst), false);
John Zulauf4a6105a2020-11-17 15:11:05 -07001775 }
1776 ApplyBarrierOpsFunctor<WaitEventBarrierOp> barriers_functor(false /* don't resolve */, barrier_ops, tag);
1777 for (const auto address_type : kAddressTypes) {
1778 EventSimpleRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), kFullRange);
1779 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &filtered_range_gen);
1780 }
1781
1782 // Apply the global barrier to the event itself (for race condition tracking)
1783 // 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 -07001784 sync_event.barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1785 sync_event.barriers |= dst.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07001786}
1787
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001788void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
1789 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
John Zulauf4a6105a2020-11-17 15:11:05 -07001790 for (auto &event_pair : event_state_) {
1791 assert(event_pair.second); // Shouldn't be storing empty
1792 auto &sync_event = *event_pair.second;
1793 // 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 -07001794 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
1795 sync_event.barriers |= dst.exec_scope;
1796 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
John Zulauf4a6105a2020-11-17 15:11:05 -07001797 }
1798 }
1799}
1800
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001801void CommandBufferAccessContext::ApplyImageBarriers(const SyncEventState &sync_event, const SyncExecScope &dst,
1802 uint32_t barrier_count, const VkImageMemoryBarrier *barriers,
1803 const ResourceUsageTag &tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07001804 const auto &scope_tag = sync_event.first_scope_tag;
1805 auto *access_context = GetCurrentAccessContext();
1806 for (uint32_t index = 0; index < barrier_count; index++) {
1807 const auto &barrier = barriers[index];
1808 const auto *image = sync_state_->Get<IMAGE_STATE>(barrier.image);
1809 if (!image) continue;
1810 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
1811 bool layout_transition = barrier.oldLayout != barrier.newLayout;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001812 const SyncBarrier sync_barrier(barrier, sync_event.scope, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001813 const ApplyBarrierFunctor<WaitEventBarrierOp> barrier_action({scope_tag, sync_barrier, layout_transition});
1814 const auto base_address = ResourceBaseAddress(*image);
1815 subresource_adapter::ImageRangeGenerator range_gen(*image->fragment_encoder.get(), subresource_range, {0, 0, 0},
1816 image->createInfo.extent, base_address);
1817 const auto address_type = AccessContext::ImageAddressType(*image);
1818 EventImageRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), range_gen);
1819 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barrier_action, &filtered_range_gen);
1820 }
1821}
John Zulaufb02c1eb2020-10-06 16:33:36 -06001822
John Zulauf355e49b2020-04-24 15:11:15 -06001823// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1824bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1825
1826 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001827 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001828 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1829 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06001830
John Zulauf86356ca2020-10-19 11:46:41 -06001831 assert(pRenderPassBegin);
1832 if (nullptr == pRenderPassBegin) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06001833
John Zulauf86356ca2020-10-19 11:46:41 -06001834 const uint32_t subpass = 0;
John Zulauf355e49b2020-04-24 15:11:15 -06001835
John Zulauf86356ca2020-10-19 11:46:41 -06001836 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
1837 // hasn't happened yet)
1838 const std::vector<AccessContext> empty_context_vector;
1839 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1840 const_cast<AccessContext *>(&cb_access_context_));
John Zulauf355e49b2020-04-24 15:11:15 -06001841
John Zulauf86356ca2020-10-19 11:46:41 -06001842 // Create a view list
1843 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1844 assert(fb_state);
1845 if (nullptr == fb_state) return skip;
1846 // NOTE: Must not use COMMAND_BUFFER_STATE variant of this as RecordCmdBeginRenderPass hasn't run and thus
1847 // the activeRenderPass.* fields haven't been set.
1848 const auto views = sync_state_->GetAttachmentViews(*pRenderPassBegin, *fb_state);
1849
1850 // Validate transitions
1851 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
1852
1853 // Validate load operations if there were no layout transition hazards
1854 if (!skip) {
1855 temp_context.RecordLayoutTransitions(rp_state, subpass, views, kCurrentCommandTag);
1856 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001857 }
John Zulauf86356ca2020-10-19 11:46:41 -06001858
John Zulauf355e49b2020-04-24 15:11:15 -06001859 return skip;
1860}
1861
locke-lunarg61870c22020-06-09 14:51:50 -06001862bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1863 const char *func_name) const {
1864 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001865 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001866 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001867 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1868 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001869 return skip;
1870 }
1871
1872 using DescriptorClass = cvdescriptorset::DescriptorClass;
1873 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1874 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1875 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1876 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1877
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001878 for (const auto &stage_state : pipe->stage_state) {
1879 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1880 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001881 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001882 }
locke-lunarg61870c22020-06-09 14:51:50 -06001883 for (const auto &set_binding : stage_state.descriptor_uses) {
1884 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1885 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1886 set_binding.first.second);
1887 const auto descriptor_type = binding_it.GetType();
1888 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1889 auto array_idx = 0;
1890
1891 if (binding_it.IsVariableDescriptorCount()) {
1892 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1893 }
1894 SyncStageAccessIndex sync_index =
1895 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1896
1897 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1898 uint32_t index = i - index_range.start;
1899 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1900 switch (descriptor->GetClass()) {
1901 case DescriptorClass::ImageSampler:
1902 case DescriptorClass::Image: {
1903 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001904 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001905 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001906 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1907 img_view_state = image_sampler_descriptor->GetImageViewState();
1908 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001909 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001910 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1911 img_view_state = image_descriptor->GetImageViewState();
1912 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001913 }
1914 if (!img_view_state) continue;
1915 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1916 VkExtent3D extent = {};
1917 VkOffset3D offset = {};
1918 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1919 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1920 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1921 } else {
1922 extent = img_state->createInfo.extent;
1923 }
John Zulauf361fb532020-07-22 10:45:39 -06001924 HazardResult hazard;
1925 const auto &subresource_range = img_view_state->normalized_subresource_range;
1926 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1927 // Input attachments are subject to raster ordering rules
1928 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1929 kAttachmentRasterOrder, offset, extent);
1930 } else {
1931 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1932 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001933 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001934 skip |= sync_state_->LogError(
1935 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001936 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1937 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001938 func_name, string_SyncHazard(hazard.hazard),
1939 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1940 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001941 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001942 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1943 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1944 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001945 }
1946 break;
1947 }
1948 case DescriptorClass::TexelBuffer: {
1949 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1950 if (!buf_view_state) continue;
1951 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001952 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001953 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001954 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001955 skip |= sync_state_->LogError(
1956 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001957 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1958 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001959 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1960 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001961 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001962 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1963 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1964 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001965 }
1966 break;
1967 }
1968 case DescriptorClass::GeneralBuffer: {
1969 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1970 auto buf_state = buffer_descriptor->GetBufferState();
1971 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001972 const ResourceAccessRange range =
1973 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001974 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001975 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001976 skip |= sync_state_->LogError(
1977 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001978 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1979 func_name, string_SyncHazard(hazard.hazard),
1980 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001981 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001982 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001983 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1984 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1985 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001986 }
1987 break;
1988 }
1989 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1990 default:
1991 break;
1992 }
1993 }
1994 }
1995 }
1996 return skip;
1997}
1998
1999void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
2000 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002001 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06002002 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002003 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
2004 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06002005 return;
2006 }
2007
2008 using DescriptorClass = cvdescriptorset::DescriptorClass;
2009 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
2010 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
2011 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
2012 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
2013
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002014 for (const auto &stage_state : pipe->stage_state) {
2015 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
2016 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06002017 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002018 }
locke-lunarg61870c22020-06-09 14:51:50 -06002019 for (const auto &set_binding : stage_state.descriptor_uses) {
2020 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
2021 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
2022 set_binding.first.second);
2023 const auto descriptor_type = binding_it.GetType();
2024 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2025 auto array_idx = 0;
2026
2027 if (binding_it.IsVariableDescriptorCount()) {
2028 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2029 }
2030 SyncStageAccessIndex sync_index =
2031 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
2032
2033 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2034 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2035 switch (descriptor->GetClass()) {
2036 case DescriptorClass::ImageSampler:
2037 case DescriptorClass::Image: {
2038 const IMAGE_VIEW_STATE *img_view_state = nullptr;
2039 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
2040 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
2041 } else {
2042 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
2043 }
2044 if (!img_view_state) continue;
2045 const IMAGE_STATE *img_state = img_view_state->image_state.get();
2046 VkExtent3D extent = {};
2047 VkOffset3D offset = {};
2048 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
2049 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
2050 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
2051 } else {
2052 extent = img_state->createInfo.extent;
2053 }
2054 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
2055 offset, extent, tag);
2056 break;
2057 }
2058 case DescriptorClass::TexelBuffer: {
2059 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
2060 if (!buf_view_state) continue;
2061 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002062 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06002063 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
2064 break;
2065 }
2066 case DescriptorClass::GeneralBuffer: {
2067 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
2068 auto buf_state = buffer_descriptor->GetBufferState();
2069 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002070 const ResourceAccessRange range =
2071 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06002072 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
2073 break;
2074 }
2075 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2076 default:
2077 break;
2078 }
2079 }
2080 }
2081 }
2082}
2083
2084bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2085 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002086 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2087 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002088 return skip;
2089 }
2090
2091 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2092 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002093 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002094
2095 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002096 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002097 if (binding_description.binding < binding_buffers_size) {
2098 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002099 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002100
locke-lunarg1ae57d62020-11-18 10:49:19 -07002101 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002102 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2103 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06002104 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
2105 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002106 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002107 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 -06002108 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002109 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002110 }
2111 }
2112 }
2113 return skip;
2114}
2115
2116void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002117 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2118 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002119 return;
2120 }
2121 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2122 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002123 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002124
2125 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002126 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002127 if (binding_description.binding < binding_buffers_size) {
2128 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002129 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002130
locke-lunarg1ae57d62020-11-18 10:49:19 -07002131 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002132 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2133 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06002134 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
2135 }
2136 }
2137}
2138
2139bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2140 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002141 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002142 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002143 }
locke-lunarg61870c22020-06-09 14:51:50 -06002144
locke-lunarg1ae57d62020-11-18 10:49:19 -07002145 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002146 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002147 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2148 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06002149 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
2150 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002151 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002152 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 -06002153 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002154 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002155 }
2156
2157 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2158 // We will detect more accurate range in the future.
2159 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2160 return skip;
2161}
2162
2163void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002164 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 -06002165
locke-lunarg1ae57d62020-11-18 10:49:19 -07002166 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002167 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002168 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2169 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06002170 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
2171
2172 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2173 // We will detect more accurate range in the future.
2174 RecordDrawVertex(UINT32_MAX, 0, tag);
2175}
2176
2177bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002178 bool skip = false;
2179 if (!current_renderpass_context_) return skip;
2180 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
2181 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
2182 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002183}
2184
2185void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002186 if (current_renderpass_context_) {
locke-lunarg7077d502020-06-18 21:37:26 -06002187 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
2188 tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002189 }
locke-lunarg61870c22020-06-09 14:51:50 -06002190}
2191
John Zulauf355e49b2020-04-24 15:11:15 -06002192bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002193 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06002194 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06002195 skip |=
2196 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002197
2198 return skip;
2199}
2200
2201bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
2202 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06002203 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002204 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06002205 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06002206 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
2207 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002208
2209 return skip;
2210}
2211
2212void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
2213 assert(sync_state_);
2214 if (!cb_state_) return;
2215
2216 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06002217 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06002218 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06002219 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002220 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002221}
2222
John Zulauf355e49b2020-04-24 15:11:15 -06002223void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002224 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06002225 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002226 current_context_ = &current_renderpass_context_->CurrentContext();
2227}
2228
John Zulauf355e49b2020-04-24 15:11:15 -06002229void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002230 assert(current_renderpass_context_);
2231 if (!current_renderpass_context_) return;
2232
John Zulauf1a224292020-06-30 14:52:13 -06002233 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002234 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002235 current_renderpass_context_ = nullptr;
2236}
2237
John Zulauf49beb112020-11-04 16:06:31 -07002238bool CommandBufferAccessContext::ValidateSetEvent(VkCommandBuffer commandBuffer, VkEvent event,
2239 VkPipelineStageFlags stageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002240 // I'll put this here just in case we need to pass this in for future extension support
2241 const auto cmd = CMD_SETEVENT;
2242 bool skip = false;
2243 const auto *sync_event = GetEventState(event);
2244 if (!sync_event) return false; // Core, Lifetimes, or Param check needs to catch invalid events.
2245
2246 const char *const reset_set =
2247 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
2248 "hazards.";
2249 const char *const wait =
2250 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
2251
2252 const auto exec_scope = WithEarlierPipelineStages(ExpandPipelineStages(GetQueueFlags(), stageMask));
2253 if (!sync_event->HasBarrier(stageMask, exec_scope)) {
2254 const char *vuid = nullptr;
2255 const char *message = nullptr;
2256 switch (sync_event->last_command) {
2257 case CMD_RESETEVENT:
2258 // Needs a barrier between reset and set
2259 vuid = "SYNC-vkCmdSetEvent-missingbarrier-reset";
2260 message = reset_set;
2261 break;
2262 case CMD_SETEVENT:
2263 // Needs a barrier between set and set
2264 vuid = "SYNC-vkCmdSetEvent-missingbarrier-set";
2265 message = reset_set;
2266 break;
2267 case CMD_WAITEVENTS:
2268 // Needs a barrier or is in second execution scope
2269 vuid = "SYNC-vkCmdSetEvent-missingbarrier-wait";
2270 message = wait;
2271 break;
2272 default:
2273 // The only other valid last command that wasn't one.
2274 assert(sync_event->last_command == CMD_NONE);
2275 break;
2276 }
2277 if (vuid) {
2278 assert(nullptr != message);
2279 const char *const cmd_name = CommandTypeString(cmd);
2280 skip |= sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2281 cmd_name, CommandTypeString(sync_event->last_command));
2282 }
2283 }
2284
2285 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002286}
2287
John Zulauf4a6105a2020-11-17 15:11:05 -07002288void CommandBufferAccessContext::RecordSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask,
2289 const ResourceUsageTag &tag) {
2290 auto *sync_event = GetEventState(event);
2291 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
2292
2293 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
2294 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
2295 // any issues caused by naive scope setting here.
2296
2297 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
2298 // Given:
2299 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
2300 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002301 auto scope = SyncExecScope::MakeSrc(GetQueueFlags(), stageMask);
John Zulauf4a6105a2020-11-17 15:11:05 -07002302
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002303 if (!sync_event->HasBarrier(stageMask, scope.exec_scope)) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002304 sync_event->unsynchronized_set = sync_event->last_command;
2305 sync_event->ResetFirstScope();
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002306 } else if (sync_event->scope.exec_scope == 0) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002307 // We only set the scope if there isn't one
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002308 sync_event->scope = scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002309
2310 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
2311 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002312 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002313 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
2314 }
2315 };
2316 GetCurrentAccessContext()->ForAll(set_scope);
2317 sync_event->unsynchronized_set = CMD_NONE;
2318 sync_event->first_scope_tag = tag;
2319 }
2320 sync_event->last_command = CMD_SETEVENT;
2321 sync_event->barriers = 0U;
2322}
John Zulauf49beb112020-11-04 16:06:31 -07002323
2324bool CommandBufferAccessContext::ValidateResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
2325 VkPipelineStageFlags stageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002326 // I'll put this here just in case we need to pass this in for future extension support
2327 const auto cmd = CMD_RESETEVENT;
2328
2329 bool skip = false;
2330 // TODO: EVENTS:
2331 // What is it we need to check... that we've had a reset since a set? Set/Set seems ill formed...
2332 const auto *sync_event = GetEventState(event);
2333 if (!sync_event) return false; // Core, Lifetimes, or Param check needs to catch invalid events.
2334
2335 const char *const set_wait =
2336 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
2337 "hazards.";
2338 const char *message = set_wait; // Only one message this call.
2339 const auto exec_scope = WithEarlierPipelineStages(ExpandPipelineStages(GetQueueFlags(), stageMask));
2340 if (!sync_event->HasBarrier(stageMask, exec_scope)) {
2341 const char *vuid = nullptr;
2342 switch (sync_event->last_command) {
2343 case CMD_SETEVENT:
2344 // Needs a barrier between set and reset
2345 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
2346 break;
2347 case CMD_WAITEVENTS: {
2348 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
2349 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
2350 break;
2351 }
2352 default:
2353 // The only other valid last command that wasn't one.
2354 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT));
2355 break;
2356 }
2357 if (vuid) {
2358 const char *const cmd_name = CommandTypeString(cmd);
2359 skip |= sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2360 cmd_name, CommandTypeString(sync_event->last_command));
2361 }
2362 }
2363 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002364}
2365
John Zulauf4a6105a2020-11-17 15:11:05 -07002366void CommandBufferAccessContext::RecordResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
2367 const auto cmd = CMD_RESETEVENT;
2368 auto *sync_event = GetEventState(event);
2369 if (!sync_event) return;
John Zulauf49beb112020-11-04 16:06:31 -07002370
John Zulauf4a6105a2020-11-17 15:11:05 -07002371 // Clear out the first sync scope, any races vs. wait or set are reported, so we'll keep the bookkeeping simple assuming
2372 // the safe case
2373 for (const auto address_type : kAddressTypes) {
2374 sync_event->first_scope[static_cast<size_t>(address_type)].clear();
2375 }
2376
2377 // Update the event state
2378 sync_event->last_command = cmd;
2379 sync_event->unsynchronized_set = CMD_NONE;
2380 sync_event->ResetFirstScope();
2381 sync_event->barriers = 0U;
2382}
2383
2384bool CommandBufferAccessContext::ValidateWaitEvents(uint32_t eventCount, const VkEvent *pEvents, VkPipelineStageFlags srcStageMask,
2385 VkPipelineStageFlags dstStageMask, uint32_t memoryBarrierCount,
2386 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
John Zulauf49beb112020-11-04 16:06:31 -07002387 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2388 uint32_t imageMemoryBarrierCount,
2389 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002390 const auto cmd = CMD_WAITEVENTS;
2391 const char *const ignored = "Wait operation is ignored for this event.";
2392 bool skip = false;
2393
2394 if (srcStageMask & VK_PIPELINE_STAGE_HOST_BIT) {
2395 const char *const cmd_name = CommandTypeString(cmd);
2396 const char *const vuid = "SYNC-vkCmdWaitEvents-hostevent-unsupported";
John Zulauffe757512020-12-18 12:17:47 -07002397 skip = sync_state_->LogInfo(cb_state_->commandBuffer, vuid,
2398 "%s, srcStageMask includes %s, unsupported by synchronization validaton.", cmd_name,
2399 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT), ignored);
John Zulauf4a6105a2020-11-17 15:11:05 -07002400 }
2401
2402 VkPipelineStageFlags event_stage_masks = 0U;
John Zulauffe757512020-12-18 12:17:47 -07002403 bool events_not_found = false;
John Zulauf4a6105a2020-11-17 15:11:05 -07002404 for (uint32_t event_index = 0; event_index < eventCount; event_index++) {
2405 const auto event = pEvents[event_index];
2406 const auto *sync_event = GetEventState(event);
John Zulauffe757512020-12-18 12:17:47 -07002407 if (!sync_event) {
2408 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
2409 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives.
2410
2411 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
2412 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002413
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002414 event_stage_masks |= sync_event->scope.mask_param;
John Zulauf4a6105a2020-11-17 15:11:05 -07002415 const auto ignore_reason = sync_event->IsIgnoredByWait(srcStageMask);
2416 if (ignore_reason) {
2417 switch (ignore_reason) {
2418 case SyncEventState::ResetWaitRace: {
2419 const char *const cmd_name = CommandTypeString(cmd);
2420 const char *const vuid = "SYNC-vkCmdWaitEvents-missingbarrier-reset";
2421 const char *const message =
2422 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
2423 skip |=
2424 sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2425 cmd_name, CommandTypeString(sync_event->last_command), ignored);
2426 break;
2427 }
2428 case SyncEventState::SetRace: {
2429 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for this
2430 // event
2431 const char *const cmd_name = CommandTypeString(cmd);
2432 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
2433 const char *const message =
2434 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, % %s";
2435 const char *const reason = "First synchronization scope is undefined.";
2436 skip |=
2437 sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2438 CommandTypeString(sync_event->last_command), reason, ignored);
2439 break;
2440 }
2441 case SyncEventState::MissingStageBits: {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002442 const VkPipelineStageFlags missing_bits = sync_event->scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07002443 // Issue error message that event waited for is not in wait events scope
2444 const char *const cmd_name = CommandTypeString(cmd);
2445 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
2446 const char *const message =
2447 "%s: %s stageMask 0x%" PRIx32 " includes bits not present in srcStageMask 0x%" PRIx32
2448 ". Bits missing from srcStageMask %s. %s";
2449 skip |= sync_state_->LogError(
2450 event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002451 sync_event->scope.mask_param, srcStageMask, string_VkPipelineStageFlags(missing_bits).c_str(), ignored);
John Zulauf4a6105a2020-11-17 15:11:05 -07002452 break;
2453 }
2454 default:
2455 assert(ignore_reason == SyncEventState::NotIgnored);
2456 }
2457 } else if (imageMemoryBarrierCount) {
2458 const auto *context = GetCurrentAccessContext();
2459 assert(context);
2460 for (uint32_t barrier_index = 0; barrier_index < imageMemoryBarrierCount; barrier_index++) {
2461 const auto &barrier = pImageMemoryBarriers[barrier_index];
2462 if (barrier.oldLayout == barrier.newLayout) continue;
2463 const auto *image_state = sync_state_->Get<IMAGE_STATE>(barrier.image);
2464 if (!image_state) continue;
2465 auto subresource_range = NormalizeSubresourceRange(image_state->createInfo, barrier.subresourceRange);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002466 const auto src_access_scope = SyncStageAccess::AccessScope(sync_event->scope.valid_accesses, barrier.srcAccessMask);
John Zulauf4a6105a2020-11-17 15:11:05 -07002467 const auto hazard =
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002468 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
2469 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
John Zulauf4a6105a2020-11-17 15:11:05 -07002470 if (hazard.hazard) {
2471 const char *const cmd_name = CommandTypeString(cmd);
2472 skip |= sync_state_->LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
2473 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", cmd_name,
2474 string_SyncHazard(hazard.hazard), barrier_index,
2475 sync_state_->report_data->FormatHandle(barrier.image).c_str(),
2476 string_UsageTag(hazard).c_str());
2477 break;
2478 }
2479 }
2480 }
2481 }
2482
2483 // Note that we can't check for HOST in pEvents as we don't track that set event type
2484 const auto extra_stage_bits = (srcStageMask & ~VK_PIPELINE_STAGE_HOST_BIT) & ~event_stage_masks;
2485 if (extra_stage_bits) {
2486 // Issue error message that event waited for is not in wait events scope
2487 const char *const cmd_name = CommandTypeString(cmd);
2488 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
2489 const char *const message =
John Zulauffe757512020-12-18 12:17:47 -07002490 "%s: srcStageMask 0x%" PRIx32 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
2491 if (events_not_found) {
2492 skip |= sync_state_->LogInfo(cb_state_->commandBuffer, vuid, message, cmd_name, srcStageMask,
2493 string_VkPipelineStageFlags(extra_stage_bits).c_str(),
2494 " vkCmdSetEvent may be in previously submitted command buffer.");
2495 } else {
2496 skip |= sync_state_->LogError(cb_state_->commandBuffer, vuid, message, cmd_name, srcStageMask,
2497 string_VkPipelineStageFlags(extra_stage_bits).c_str(), "");
2498 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002499 }
2500 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002501}
2502
2503void CommandBufferAccessContext::RecordWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
2504 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
2505 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2506 uint32_t bufferMemoryBarrierCount,
2507 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2508 uint32_t imageMemoryBarrierCount,
John Zulauf4a6105a2020-11-17 15:11:05 -07002509 const VkImageMemoryBarrier *pImageMemoryBarriers, const ResourceUsageTag &tag) {
2510 auto *access_context = GetCurrentAccessContext();
2511 assert(access_context);
2512 if (!access_context) return;
2513
2514 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
2515 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
2516 // 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 -07002517 access_context->ResolvePreviousAccesses();
2518
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002519 auto dst = SyncExecScope::MakeDst(GetQueueFlags(), dstStageMask);
John Zulauf4a6105a2020-11-17 15:11:05 -07002520 for (uint32_t event_index = 0; event_index < eventCount; event_index++) {
2521 const auto event = pEvents[event_index];
2522 auto *sync_event = GetEventState(event);
2523 if (!sync_event) continue;
2524
2525 sync_event->last_command = CMD_WAITEVENTS;
2526
2527 if (!sync_event->IsIgnoredByWait(srcStageMask)) {
2528 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
2529 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
2530 // of the barriers is maintained.
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002531 ApplyBufferBarriers(*sync_event, dst, bufferMemoryBarrierCount, pBufferMemoryBarriers);
2532 ApplyImageBarriers(*sync_event, dst, imageMemoryBarrierCount, pImageMemoryBarriers, tag);
2533 ApplyGlobalBarriers(*sync_event, dst, memoryBarrierCount, pMemoryBarriers, tag);
John Zulauf4a6105a2020-11-17 15:11:05 -07002534 } else {
2535 // We ignored this wait, so we don't have any effective synchronization barriers for it.
2536 sync_event->barriers = 0U;
2537 }
2538 }
2539
2540 // Apply the pending barriers
2541 ResolvePendingBarrierFunctor apply_pending_action(tag);
2542 access_context->ApplyGlobalBarriers(apply_pending_action);
2543}
2544
2545void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2546 // Erase is okay with the key not being
2547 event_state_.erase(event);
2548}
2549
2550SyncEventState *CommandBufferAccessContext::GetEventState(VkEvent event) {
2551 auto &event_up = event_state_[event];
2552 if (!event_up) {
2553 auto event_atate = sync_state_->GetShared<EVENT_STATE>(event);
2554 event_up.reset(new SyncEventState(event_atate));
2555 }
2556 return event_up.get();
2557}
2558
2559const SyncEventState *CommandBufferAccessContext::GetEventState(VkEvent event) const {
2560 auto event_it = event_state_.find(event);
2561 if (event_it == event_state_.cend()) {
2562 return nullptr;
2563 }
2564 return event_it->second.get();
2565}
John Zulauf49beb112020-11-04 16:06:31 -07002566
locke-lunarg61870c22020-06-09 14:51:50 -06002567bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
2568 const VkRect2D &render_area, const char *func_name) const {
2569 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002570 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2571 if (!pipe ||
2572 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002573 return skip;
2574 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002575 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002576 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2577 VkExtent3D extent = CastTo3D(render_area.extent);
2578 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06002579
John Zulauf1a224292020-06-30 14:52:13 -06002580 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002581 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002582 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2583 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002584 if (location >= subpass.colorAttachmentCount ||
2585 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002586 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002587 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002588 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002589 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
2590 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06002591 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002592 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002593 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002594 func_name, string_SyncHazard(hazard.hazard),
2595 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2596 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002597 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002598 }
2599 }
2600 }
locke-lunarg37047832020-06-12 13:44:45 -06002601
2602 // PHASE1 TODO: Add layout based read/vs. write selection.
2603 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002604 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002605 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002606 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002607 bool depth_write = false, stencil_write = false;
2608
2609 // PHASE1 TODO: These validation should be in core_checks.
2610 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002611 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2612 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002613 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2614 depth_write = true;
2615 }
2616 // PHASE1 TODO: It needs to check if stencil is writable.
2617 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2618 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2619 // PHASE1 TODO: These validation should be in core_checks.
2620 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002621 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002622 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2623 stencil_write = true;
2624 }
2625
2626 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2627 if (depth_write) {
2628 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002629 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2630 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002631 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002632 skip |= sync_state.LogError(
2633 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002634 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002635 func_name, string_SyncHazard(hazard.hazard),
2636 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2637 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002638 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002639 }
2640 }
2641 if (stencil_write) {
2642 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002643 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2644 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002645 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002646 skip |= sync_state.LogError(
2647 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002648 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002649 func_name, string_SyncHazard(hazard.hazard),
2650 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2651 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002652 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002653 }
locke-lunarg61870c22020-06-09 14:51:50 -06002654 }
2655 }
2656 return skip;
2657}
2658
locke-lunarg96dc9632020-06-10 17:22:18 -06002659void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
2660 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002661 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2662 if (!pipe ||
2663 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002664 return;
2665 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002666 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002667 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2668 VkExtent3D extent = CastTo3D(render_area.extent);
2669 VkOffset3D offset = CastTo3D(render_area.offset);
2670
John Zulauf1a224292020-06-30 14:52:13 -06002671 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002672 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002673 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2674 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002675 if (location >= subpass.colorAttachmentCount ||
2676 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002677 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002678 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002679 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002680 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
2681 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002682 }
2683 }
locke-lunarg37047832020-06-12 13:44:45 -06002684
2685 // PHASE1 TODO: Add layout based read/vs. write selection.
2686 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002687 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002688 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002689 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002690 bool depth_write = false, stencil_write = false;
2691
2692 // PHASE1 TODO: These validation should be in core_checks.
2693 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002694 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2695 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002696 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2697 depth_write = true;
2698 }
2699 // PHASE1 TODO: It needs to check if stencil is writable.
2700 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2701 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2702 // PHASE1 TODO: These validation should be in core_checks.
2703 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002704 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002705 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2706 stencil_write = true;
2707 }
2708
2709 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2710 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002711 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2712 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002713 }
2714 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002715 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2716 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002717 }
locke-lunarg61870c22020-06-09 14:51:50 -06002718 }
2719}
2720
John Zulauf1507ee42020-05-18 11:33:09 -06002721bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
2722 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002723 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002724 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06002725 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2726 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002727 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2728 func_name);
2729
John Zulauf355e49b2020-04-24 15:11:15 -06002730 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002731 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06002732 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002733 if (!skip) {
2734 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2735 // on a copy of the (empty) next context.
2736 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2737 AccessContext temp_context(next_context);
2738 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
2739 skip |= temp_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2740 }
John Zulauf7635de32020-05-29 17:14:15 -06002741 return skip;
2742}
2743bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
2744 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002745 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002746 bool skip = false;
2747 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2748 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002749 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2750 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002751 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002752 return skip;
2753}
2754
John Zulauf7635de32020-05-29 17:14:15 -06002755AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2756 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2757}
2758
2759bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2760 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002761 bool skip = false;
2762
John Zulauf7635de32020-05-29 17:14:15 -06002763 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2764 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2765 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2766 // to apply and only copy then, if this proves a hot spot.
2767 std::unique_ptr<AccessContext> proxy_for_current;
2768
John Zulauf355e49b2020-04-24 15:11:15 -06002769 // Validate the "finalLayout" transitions to external
2770 // Get them from where there we're hidding in the extra entry.
2771 const auto &final_transitions = rp_state_->subpass_transitions.back();
2772 for (const auto &transition : final_transitions) {
2773 const auto &attach_view = attachment_views_[transition.attachment];
2774 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2775 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002776 auto *context = trackback.context;
2777
2778 if (transition.prev_pass == current_subpass_) {
2779 if (!proxy_for_current) {
2780 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2781 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2782 }
2783 context = proxy_for_current.get();
2784 }
2785
John Zulaufa0a98292020-09-18 09:30:10 -06002786 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2787 const auto merged_barrier = MergeBarriers(trackback.barriers);
2788 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2789 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2790 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002791 if (hazard.hazard) {
2792 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2793 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002794 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002795 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002796 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002797 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002798 }
2799 }
2800 return skip;
2801}
2802
2803void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2804 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002805 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002806}
2807
John Zulauf1507ee42020-05-18 11:33:09 -06002808void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2809 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2810 auto &subpass_context = subpass_contexts_[current_subpass_];
2811 VkExtent3D extent = CastTo3D(render_area.extent);
2812 VkOffset3D offset = CastTo3D(render_area.offset);
2813
2814 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2815 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2816 if (attachment_views_[i] == nullptr) continue; // UNUSED
2817 const auto &view = *attachment_views_[i];
2818 const IMAGE_STATE *image = view.image_state.get();
2819 if (image == nullptr) continue;
2820
2821 const auto &ci = attachment_ci[i];
2822 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002823 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002824 const bool is_color = !(has_depth || has_stencil);
2825
2826 if (is_color) {
2827 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2828 extent, tag);
2829 } else {
2830 auto update_range = view.normalized_subresource_range;
2831 if (has_depth) {
2832 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2833 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2834 }
2835 if (has_stencil) {
2836 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2837 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2838 tag);
2839 }
2840 }
2841 }
2842 }
2843}
2844
John Zulauf355e49b2020-04-24 15:11:15 -06002845void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002846 const AccessContext *external_context, VkQueueFlags queue_flags,
2847 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002848 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002849 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002850 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2851 // Add this for all subpasses here so that they exsist during next subpass validation
2852 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002853 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002854 }
2855 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2856
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002857 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002858 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002859 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002860}
John Zulauf1507ee42020-05-18 11:33:09 -06002861
2862void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002863 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2864 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002865 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002866
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002867 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2868 // subpass, so their tag needs to be different from the layout and load operations below.
Jeremy Gebben4bb73502020-12-14 11:17:50 -07002869 ResourceUsageTag next_tag = tag.NextSubCommand();
John Zulauf355e49b2020-04-24 15:11:15 -06002870 current_subpass_++;
2871 assert(current_subpass_ < subpass_contexts_.size());
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002872 subpass_contexts_[current_subpass_].SetStartTag(next_tag);
2873 RecordLayoutTransitions(next_tag);
2874 RecordLoadOperations(render_area, next_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002875}
2876
John Zulauf1a224292020-06-30 14:52:13 -06002877void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2878 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002879 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002880 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002881 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002882
John Zulauf355e49b2020-04-24 15:11:15 -06002883 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002884 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002885
2886 // Add the "finalLayout" transitions to external
2887 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002888 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2889 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2890 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002891 const auto &final_transitions = rp_state_->subpass_transitions.back();
2892 for (const auto &transition : final_transitions) {
2893 const auto &attachment = attachment_views_[transition.attachment];
2894 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002895 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf1e331ec2020-12-04 18:29:38 -07002896 std::vector<PipelineBarrierOp> barrier_ops;
2897 barrier_ops.reserve(last_trackback.barriers.size());
2898 for (const auto &barrier : last_trackback.barriers) {
2899 barrier_ops.emplace_back(barrier, true);
2900 }
2901 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, barrier_ops, tag);
2902 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002903 }
2904}
2905
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002906SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags mask_param) {
2907 SyncExecScope result;
2908 result.mask_param = mask_param;
2909 result.expanded_mask = ExpandPipelineStages(queue_flags, mask_param);
2910 result.exec_scope = WithEarlierPipelineStages(result.expanded_mask);
2911 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2912 return result;
2913}
2914
2915SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags mask_param) {
2916 SyncExecScope result;
2917 result.mask_param = mask_param;
2918 result.expanded_mask = ExpandPipelineStages(queue_flags, mask_param);
2919 result.exec_scope = WithLaterPipelineStages(result.expanded_mask);
2920 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2921 return result;
2922}
2923
2924SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
2925 src_exec_scope = src.exec_scope;
2926 src_access_scope = 0;
2927 dst_exec_scope = dst.exec_scope;
2928 dst_access_scope = 0;
2929}
2930
2931template <typename Barrier>
2932SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
2933 src_exec_scope = src.exec_scope;
2934 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2935 dst_exec_scope = dst.exec_scope;
2936 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2937}
2938
2939SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
2940 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
2941 src_exec_scope = src.exec_scope;
2942 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2943
2944 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
2945 dst_exec_scope = dst.exec_scope;
2946 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002947}
2948
John Zulaufb02c1eb2020-10-06 16:33:36 -06002949// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2950void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2951 for (const auto &barrier : barriers) {
2952 ApplyBarrier(barrier, layout_transition);
2953 }
2954}
2955
John Zulauf89311b42020-09-29 16:28:47 -06002956// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2957// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2958// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002959void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2960 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002961 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002962 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002963 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002964 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002965 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002966 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002967}
John Zulauf9cb530d2019-09-30 14:14:10 -06002968HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2969 HazardResult hazard;
2970 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002971 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002972 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002973 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002974 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002975 }
2976 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002977 // Write operation:
2978 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2979 // If reads exists -- test only against them because either:
2980 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2981 // * 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
2982 // the current write happens after the reads, so just test the write against the reades
2983 // Otherwise test against last_write
2984 //
2985 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002986 if (last_reads.size()) {
2987 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002988 if (IsReadHazard(usage_stage, read_access)) {
2989 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2990 break;
2991 }
2992 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002993 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002994 // Write-After-Write check -- if we have a previous write to test against
2995 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002996 }
2997 }
2998 return hazard;
2999}
3000
John Zulauf69133422020-05-20 14:55:53 -06003001HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
3002 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
3003 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06003004 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06003005 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003006 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
3007 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06003008 if (IsRead(usage_bit)) {
3009 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
3010 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
3011 if (is_raw_hazard) {
3012 // NOTE: we know last_write is non-zero
3013 // See if the ordering rules save us from the simple RAW check above
3014 // First check to see if the current usage is covered by the ordering rules
3015 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
3016 const bool usage_is_ordered =
3017 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
3018 if (usage_is_ordered) {
3019 // Now see of the most recent write (or a subsequent read) are ordered
3020 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
3021 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06003022 }
3023 }
John Zulauf4285ee92020-09-23 10:20:52 -06003024 if (is_raw_hazard) {
3025 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
3026 }
John Zulauf361fb532020-07-22 10:45:39 -06003027 } else {
3028 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003029 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07003030 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06003031 // Look for any WAR hazards outside the ordered set of stages
John Zulauf4285ee92020-09-23 10:20:52 -06003032 VkPipelineStageFlags ordered_stages = 0;
3033 if (usage_write_is_ordered) {
3034 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
3035 ordered_stages = GetOrderedStages(ordering);
3036 }
3037 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
3038 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003039 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06003040 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
3041 if (IsReadHazard(usage_stage, read_access)) {
3042 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3043 break;
3044 }
John Zulaufd14743a2020-07-03 09:42:39 -06003045 }
3046 }
John Zulauf4285ee92020-09-23 10:20:52 -06003047 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003048 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003049 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06003050 }
John Zulauf69133422020-05-20 14:55:53 -06003051 }
3052 }
3053 return hazard;
3054}
3055
John Zulauf2f952d22020-02-10 11:34:51 -07003056// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003057HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07003058 HazardResult hazard;
3059 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003060 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
3061 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
3062 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07003063 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003064 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06003065 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07003066 }
3067 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003068 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06003069 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07003070 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003071 // 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 -07003072 for (const auto &read_access : last_reads) {
3073 if (read_access.tag.index >= start_tag.index) {
3074 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003075 break;
3076 }
3077 }
John Zulauf2f952d22020-02-10 11:34:51 -07003078 }
3079 }
3080 return hazard;
3081}
3082
John Zulauf36bcf6a2020-02-03 15:12:52 -07003083HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003084 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07003085 // Only supporting image layout transitions for now
3086 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3087 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06003088 // only test for WAW if there no intervening read operations.
3089 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07003090 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06003091 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07003092 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003093 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06003094 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07003095 break;
3096 }
3097 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003098 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3099 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3100 }
3101
3102 return hazard;
3103}
3104
3105HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
3106 const SyncStageAccessFlags &src_access_scope,
3107 const ResourceUsageTag &event_tag) const {
3108 // Only supporting image layout transitions for now
3109 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3110 HazardResult hazard;
3111 // only test for WAW if there no intervening read operations.
3112 // See DetectHazard(SyncStagetAccessIndex) above for more details.
3113
John Zulaufab7756b2020-12-29 16:10:16 -07003114 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003115 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
3116 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07003117 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003118 if (read_access.tag.IsBefore(event_tag)) {
3119 // The read is in the events first synchronization scope, so we use a barrier hazard check
3120 // If the read stage is not in the src sync scope
3121 // *AND* not execution chained with an existing sync barrier (that's the or)
3122 // then the barrier access is unsafe (R/W after R)
3123 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3124 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3125 break;
3126 }
3127 } else {
3128 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3129 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3130 }
3131 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003132 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003133 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
3134 if (write_tag.IsBefore(event_tag)) {
3135 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3136 // So do a normal barrier hazard check
3137 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3138 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3139 }
3140 } else {
3141 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003142 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3143 }
John Zulaufd14743a2020-07-03 09:42:39 -06003144 }
John Zulauf361fb532020-07-22 10:45:39 -06003145
John Zulauf0cb5be22020-01-23 12:18:22 -07003146 return hazard;
3147}
3148
John Zulauf5f13a792020-03-10 07:31:21 -06003149// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3150// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3151// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3152void ResourceAccessState::Resolve(const ResourceAccessState &other) {
3153 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003154 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3155 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003156 *this = other;
3157 } else if (!other.write_tag.IsBefore(write_tag)) {
3158 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
3159 // dependency chaining logic or any stage expansion)
3160 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003161 pending_write_barriers |= other.pending_write_barriers;
3162 pending_layout_transition |= other.pending_layout_transition;
3163 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003164
John Zulaufd14743a2020-07-03 09:42:39 -06003165 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07003166 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06003167 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07003168 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003169 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003170 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003171 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003172 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3173 // but we should wait on profiling data for that.
3174 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003175 auto &my_read = last_reads[my_read_index];
3176 if (other_read.stage == my_read.stage) {
3177 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003178 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003179 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003180 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003181 my_read.pending_dep_chain = other_read.pending_dep_chain;
3182 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3183 // May require tracking more than one access per stage.
3184 my_read.barriers = other_read.barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003185 if (my_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
3186 // Since I'm overwriting the fragement stage read, also update the input attachment info
3187 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003188 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003189 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003190 } else if (other_read.tag.IsBefore(my_read.tag)) {
3191 // The read tags match so merge the barriers
3192 my_read.barriers |= other_read.barriers;
3193 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003194 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003195
John Zulauf5f13a792020-03-10 07:31:21 -06003196 break;
3197 }
3198 }
3199 } else {
3200 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07003201 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06003202 last_read_stages |= other_read.stage;
John Zulauf4285ee92020-09-23 10:20:52 -06003203 if (other_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06003204 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003205 }
John Zulauf5f13a792020-03-10 07:31:21 -06003206 }
3207 }
John Zulauf361fb532020-07-22 10:45:39 -06003208 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003209 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3210 // ignore it.
John Zulauf5f13a792020-03-10 07:31:21 -06003211}
3212
John Zulauf9cb530d2019-09-30 14:14:10 -06003213void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
3214 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3215 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003216 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003217 // Mulitple outstanding reads may be of interest and do dependency chains independently
3218 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3219 const auto usage_stage = PipelineStageBit(usage_index);
3220 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003221 for (auto &read_access : last_reads) {
3222 if (read_access.stage == usage_stage) {
3223 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003224 break;
3225 }
3226 }
3227 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07003228 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003229 last_read_stages |= usage_stage;
3230 }
John Zulauf4285ee92020-09-23 10:20:52 -06003231
3232 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
3233 if (usage_stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06003234 // TODO Revisit re: multiple reads for a given stage
3235 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003236 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003237 } else {
3238 // Assume write
3239 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003240 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003241 }
3242}
John Zulauf5f13a792020-03-10 07:31:21 -06003243
John Zulauf89311b42020-09-29 16:28:47 -06003244// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3245// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3246// We can overwrite them as *this* write is now after them.
3247//
3248// 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 -07003249void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003250 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06003251 last_read_stages = 0;
3252 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003253 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003254
3255 write_barriers = 0;
3256 write_dependency_chain = 0;
3257 write_tag = tag;
3258 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003259}
3260
John Zulauf89311b42020-09-29 16:28:47 -06003261// Apply the memory barrier without updating the existing barriers. The execution barrier
3262// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3263// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3264// replace the current write barriers or add to them, so accumulate to pending as well.
3265void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3266 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3267 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003268 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3269 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3270 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3271 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulauf4a6105a2020-11-17 15:11:05 -07003272 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003273 pending_write_barriers |= barrier.dst_access_scope;
3274 pending_write_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003275 }
John Zulauf89311b42020-09-29 16:28:47 -06003276 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3277 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003278
John Zulauf89311b42020-09-29 16:28:47 -06003279 if (!pending_layout_transition) {
3280 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3281 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003282 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06003283 // 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 -07003284 if (barrier.src_exec_scope & (read_access.stage | read_access.barriers)) {
3285 read_access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003286 }
3287 }
John Zulaufa0a98292020-09-18 09:30:10 -06003288 }
John Zulaufa0a98292020-09-18 09:30:10 -06003289}
3290
John Zulauf4a6105a2020-11-17 15:11:05 -07003291// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3292// changes the "chaining" state, but to keep barriers independent. See discussion above.
3293void ResourceAccessState::ApplyBarrier(const ResourceUsageTag &scope_tag, const SyncBarrier &barrier, bool layout_transition) {
3294 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3295 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3296 // in order to know if it's in the excecution scope
3297 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3298 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3299 // errors w.r.t. "most recent" accesses.
3300 if (layout_transition || ((write_tag.IsBefore(scope_tag)) && (barrier.src_access_scope & last_write).any())) {
3301 pending_write_barriers |= barrier.dst_access_scope;
3302 pending_write_dep_chain |= barrier.dst_exec_scope;
3303 }
3304 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3305 pending_layout_transition |= layout_transition;
3306
3307 if (!pending_layout_transition) {
3308 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3309 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003310 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003311 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3312 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3313 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3314 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3315 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3316 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3317 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulaufab7756b2020-12-29 16:10:16 -07003318 if (read_access.tag.IsBefore(scope_tag) && (barrier.src_exec_scope & (read_access.stage | read_access.barriers))) {
3319 read_access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003320 }
3321 }
3322 }
3323}
John Zulauf89311b42020-09-29 16:28:47 -06003324void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
3325 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06003326 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
3327 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
3328 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003329 }
John Zulauf89311b42020-09-29 16:28:47 -06003330
3331 // Apply the accumulate execution barriers (and thus update chaining information)
3332 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003333 for (auto &read_access : last_reads) {
3334 read_access.barriers |= read_access.pending_dep_chain;
3335 read_execution_barriers |= read_access.barriers;
3336 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003337 }
3338
3339 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3340 write_dependency_chain |= pending_write_dep_chain;
3341 write_barriers |= pending_write_barriers;
3342 pending_write_dep_chain = 0;
3343 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003344}
3345
John Zulauf59e25072020-07-17 10:55:21 -06003346// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003347VkPipelineStageFlags ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
John Zulauf59e25072020-07-17 10:55:21 -06003348 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003349
John Zulaufab7756b2020-12-29 16:10:16 -07003350 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003351 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003352 barriers = read_access.barriers;
3353 break;
John Zulauf59e25072020-07-17 10:55:21 -06003354 }
3355 }
John Zulauf4285ee92020-09-23 10:20:52 -06003356
John Zulauf59e25072020-07-17 10:55:21 -06003357 return barriers;
3358}
3359
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003360inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003361 assert(IsRead(usage));
3362 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3363 // * the previous reads are not hazards, and thus last_write must be visible and available to
3364 // any reads that happen after.
3365 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3366 // 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 -07003367 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003368}
3369
John Zulauf4285ee92020-09-23 10:20:52 -06003370VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const SyncOrderingBarrier &ordering) const {
3371 // Whether the stage are in the ordering scope only matters if the current write is ordered
3372 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
3373 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003374 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003375 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003376 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
3377 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
3378 }
3379
3380 return ordered_stages;
3381}
3382
John Zulaufd1f85d42020-04-15 12:23:15 -06003383void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003384 auto *access_context = GetAccessContextNoInsert(command_buffer);
3385 if (access_context) {
3386 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003387 }
3388}
3389
John Zulaufd1f85d42020-04-15 12:23:15 -06003390void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3391 auto access_found = cb_access_state.find(command_buffer);
3392 if (access_found != cb_access_state.end()) {
3393 access_found->second->Reset();
3394 cb_access_state.erase(access_found);
3395 }
3396}
3397
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003398void SyncValidator::ApplyGlobalBarriers(AccessContext *context, const SyncExecScope &src, const SyncExecScope &dst,
3399 uint32_t memory_barrier_count, const VkMemoryBarrier *pMemoryBarriers,
3400 const ResourceUsageTag &tag) {
John Zulauf1e331ec2020-12-04 18:29:38 -07003401 std::vector<PipelineBarrierOp> barrier_ops;
3402 barrier_ops.reserve(std::min<uint32_t>(1, memory_barrier_count));
John Zulauf89311b42020-09-29 16:28:47 -06003403 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
3404 const auto &barrier = pMemoryBarriers[barrier_index];
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003405 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf1e331ec2020-12-04 18:29:38 -07003406 barrier_ops.emplace_back(sync_barrier, false);
John Zulauf89311b42020-09-29 16:28:47 -06003407 }
3408 if (0 == memory_barrier_count) {
3409 // If there are no global memory barriers, force an exec barrier
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003410 barrier_ops.emplace_back(SyncBarrier(src, dst), false);
John Zulauf89311b42020-09-29 16:28:47 -06003411 }
John Zulauf1e331ec2020-12-04 18:29:38 -07003412 ApplyBarrierOpsFunctor<PipelineBarrierOp> barriers_functor(true /* resolve */, barrier_ops, tag);
John Zulauf540266b2020-04-06 18:54:53 -06003413 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06003414}
3415
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003416void SyncValidator::ApplyBufferBarriers(AccessContext *context, const SyncExecScope &src, const SyncExecScope &dst,
3417 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003418 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003419 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06003420 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
3421 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06003422 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
3423 const ResourceAccessRange range = MakeRange(barrier);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003424 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07003425 const ApplyBarrierFunctor<PipelineBarrierOp> update_action({sync_barrier, false /* layout_transition */});
John Zulauf89311b42020-09-29 16:28:47 -06003426 context->UpdateResourceAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06003427 }
3428}
3429
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003430void SyncValidator::ApplyImageBarriers(AccessContext *context, const SyncExecScope &src, const SyncExecScope &dst,
3431 uint32_t barrier_count, const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07003432 for (uint32_t index = 0; index < barrier_count; index++) {
3433 const auto &barrier = barriers[index];
3434 const auto *image = Get<IMAGE_STATE>(barrier.image);
3435 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06003436 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06003437 bool layout_transition = barrier.oldLayout != barrier.newLayout;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003438 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07003439 const ApplyBarrierFunctor<PipelineBarrierOp> barrier_action({sync_barrier, layout_transition});
John Zulauf89311b42020-09-29 16:28:47 -06003440 context->UpdateResourceAccess(*image, subresource_range, barrier_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06003441 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003442}
3443
3444bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3445 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3446 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003447 const auto *cb_context = GetAccessContext(commandBuffer);
3448 assert(cb_context);
3449 if (!cb_context) return skip;
3450 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003451
John Zulauf3d84f1b2020-03-09 13:33:25 -06003452 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003453 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003454 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003455
3456 for (uint32_t region = 0; region < regionCount; region++) {
3457 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003458 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003459 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003460 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003461 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003462 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003463 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003464 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003465 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003466 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003467 }
John Zulauf16adfc92020-04-08 10:28:33 -06003468 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003469 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06003470 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003471 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003472 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003473 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003474 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003475 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003476 }
3477 }
3478 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003479 }
3480 return skip;
3481}
3482
3483void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3484 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003485 auto *cb_context = GetAccessContext(commandBuffer);
3486 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003487 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003488 auto *context = cb_context->GetCurrentAccessContext();
3489
John Zulauf9cb530d2019-09-30 14:14:10 -06003490 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003491 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003492
3493 for (uint32_t region = 0; region < regionCount; region++) {
3494 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003495 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003496 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003497 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003498 }
John Zulauf16adfc92020-04-08 10:28:33 -06003499 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003500 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003501 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003502 }
3503 }
3504}
3505
John Zulauf4a6105a2020-11-17 15:11:05 -07003506void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3507 // Clear out events from the command buffer contexts
3508 for (auto &cb_context : cb_access_state) {
3509 cb_context.second->RecordDestroyEvent(event);
3510 }
3511}
3512
Jeff Leger178b1e52020-10-05 12:22:23 -04003513bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3514 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3515 bool skip = false;
3516 const auto *cb_context = GetAccessContext(commandBuffer);
3517 assert(cb_context);
3518 if (!cb_context) return skip;
3519 const auto *context = cb_context->GetCurrentAccessContext();
3520
3521 // If we have no previous accesses, we have no hazards
3522 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3523 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3524
3525 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3526 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3527 if (src_buffer) {
3528 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
3529 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
3530 if (hazard.hazard) {
3531 // TODO -- add tag information to log msg when useful.
3532 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3533 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3534 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
3535 region, string_UsageTag(hazard).c_str());
3536 }
3537 }
3538 if (dst_buffer && !skip) {
3539 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
3540 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
3541 if (hazard.hazard) {
3542 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3543 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3544 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
3545 region, string_UsageTag(hazard).c_str());
3546 }
3547 }
3548 if (skip) break;
3549 }
3550 return skip;
3551}
3552
3553void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3554 auto *cb_context = GetAccessContext(commandBuffer);
3555 assert(cb_context);
3556 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3557 auto *context = cb_context->GetCurrentAccessContext();
3558
3559 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3560 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3561
3562 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3563 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3564 if (src_buffer) {
3565 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
3566 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
3567 }
3568 if (dst_buffer) {
3569 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
3570 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
3571 }
3572 }
3573}
3574
John Zulauf5c5e88d2019-12-26 11:22:02 -07003575bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3576 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3577 const VkImageCopy *pRegions) const {
3578 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003579 const auto *cb_access_context = GetAccessContext(commandBuffer);
3580 assert(cb_access_context);
3581 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003582
John Zulauf3d84f1b2020-03-09 13:33:25 -06003583 const auto *context = cb_access_context->GetCurrentAccessContext();
3584 assert(context);
3585 if (!context) return skip;
3586
3587 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3588 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003589 for (uint32_t region = 0; region < regionCount; region++) {
3590 const auto &copy_region = pRegions[region];
3591 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003592 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003593 copy_region.srcOffset, copy_region.extent);
3594 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003595 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003596 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003597 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003598 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003599 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003600 }
3601
3602 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003603 VkExtent3D dst_copy_extent =
3604 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06003605 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003606 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003607 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003608 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003609 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003610 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003611 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003612 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003613 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003614 }
3615 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003616
John Zulauf5c5e88d2019-12-26 11:22:02 -07003617 return skip;
3618}
3619
3620void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3621 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3622 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003623 auto *cb_access_context = GetAccessContext(commandBuffer);
3624 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003625 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003626 auto *context = cb_access_context->GetCurrentAccessContext();
3627 assert(context);
3628
John Zulauf5c5e88d2019-12-26 11:22:02 -07003629 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003630 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003631
3632 for (uint32_t region = 0; region < regionCount; region++) {
3633 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003634 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003635 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
3636 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003637 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003638 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003639 VkExtent3D dst_copy_extent =
3640 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06003641 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
3642 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003643 }
3644 }
3645}
3646
Jeff Leger178b1e52020-10-05 12:22:23 -04003647bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3648 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3649 bool skip = false;
3650 const auto *cb_access_context = GetAccessContext(commandBuffer);
3651 assert(cb_access_context);
3652 if (!cb_access_context) return skip;
3653
3654 const auto *context = cb_access_context->GetCurrentAccessContext();
3655 assert(context);
3656 if (!context) return skip;
3657
3658 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3659 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3660 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3661 const auto &copy_region = pCopyImageInfo->pRegions[region];
3662 if (src_image) {
3663 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
3664 copy_region.srcOffset, copy_region.extent);
3665 if (hazard.hazard) {
3666 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3667 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3668 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
3669 region, string_UsageTag(hazard).c_str());
3670 }
3671 }
3672
3673 if (dst_image) {
3674 VkExtent3D dst_copy_extent =
3675 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
3676 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
3677 copy_region.dstOffset, dst_copy_extent);
3678 if (hazard.hazard) {
3679 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3680 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3681 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
3682 region, string_UsageTag(hazard).c_str());
3683 }
3684 if (skip) break;
3685 }
3686 }
3687
3688 return skip;
3689}
3690
3691void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3692 auto *cb_access_context = GetAccessContext(commandBuffer);
3693 assert(cb_access_context);
3694 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3695 auto *context = cb_access_context->GetCurrentAccessContext();
3696 assert(context);
3697
3698 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3699 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3700
3701 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3702 const auto &copy_region = pCopyImageInfo->pRegions[region];
3703 if (src_image) {
3704 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
3705 copy_region.extent, tag);
3706 }
3707 if (dst_image) {
3708 VkExtent3D dst_copy_extent =
3709 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
3710 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
3711 dst_copy_extent, tag);
3712 }
3713 }
3714}
3715
John Zulauf9cb530d2019-09-30 14:14:10 -06003716bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3717 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3718 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3719 uint32_t bufferMemoryBarrierCount,
3720 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3721 uint32_t imageMemoryBarrierCount,
3722 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3723 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003724 const auto *cb_access_context = GetAccessContext(commandBuffer);
3725 assert(cb_access_context);
3726 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003727
John Zulauf3d84f1b2020-03-09 13:33:25 -06003728 const auto *context = cb_access_context->GetCurrentAccessContext();
3729 assert(context);
3730 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003731
John Zulauf3d84f1b2020-03-09 13:33:25 -06003732 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003733 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3734 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07003735 // Validate Image Layout transitions
3736 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
3737 const auto &barrier = pImageMemoryBarriers[index];
3738 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
3739 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
3740 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06003741 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07003742 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003743 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003744 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003745 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003746 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003747 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07003748 }
3749 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003750
3751 return skip;
3752}
3753
3754void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3755 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3756 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3757 uint32_t bufferMemoryBarrierCount,
3758 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3759 uint32_t imageMemoryBarrierCount,
3760 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003761 auto *cb_access_context = GetAccessContext(commandBuffer);
3762 assert(cb_access_context);
3763 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06003764 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003765 auto access_context = cb_access_context->GetCurrentAccessContext();
3766 assert(access_context);
3767 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003768
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003769 auto src = SyncExecScope::MakeSrc(cb_access_context->GetQueueFlags(), srcStageMask);
3770 auto dst = SyncExecScope::MakeDst(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf89311b42020-09-29 16:28:47 -06003771
3772 // These two apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
3773 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
3774 // of the barriers is maintained.
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003775 ApplyBufferBarriers(access_context, src, dst, bufferMemoryBarrierCount, pBufferMemoryBarriers);
3776 ApplyImageBarriers(access_context, src, dst, imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003777
John Zulauf89311b42020-09-29 16:28:47 -06003778 // Apply the global barriers last as is it walks all memory, it can also clean up the "pending" state without requiring an
3779 // additional pass, updating the dependency chains *last* as it goes along.
3780 // This is needed to guarantee order independence of the three lists.
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003781 ApplyGlobalBarriers(access_context, src, dst, memoryBarrierCount, pMemoryBarriers, tag);
John Zulauf4a6105a2020-11-17 15:11:05 -07003782
Jeremy Gebben9893daf2021-01-04 10:40:50 -07003783 cb_access_context->ApplyGlobalBarriersToEvents(src, dst);
John Zulauf9cb530d2019-09-30 14:14:10 -06003784}
3785
3786void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3787 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3788 // The state tracker sets up the device state
3789 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3790
John Zulauf5f13a792020-03-10 07:31:21 -06003791 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3792 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003793 // TODO: Find a good way to do this hooklessly.
3794 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3795 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3796 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3797
John Zulaufd1f85d42020-04-15 12:23:15 -06003798 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3799 sync_device_state->ResetCommandBufferCallback(command_buffer);
3800 });
3801 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3802 sync_device_state->FreeCommandBufferCallback(command_buffer);
3803 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003804}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003805
John Zulauf355e49b2020-04-24 15:11:15 -06003806bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003807 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003808 bool skip = false;
3809 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
3810 auto cb_context = GetAccessContext(commandBuffer);
3811
3812 if (rp_state && cb_context) {
3813 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
3814 }
3815
3816 return skip;
3817}
3818
3819bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3820 VkSubpassContents contents) const {
3821 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003822 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003823 subpass_begin_info.contents = contents;
3824 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
3825 return skip;
3826}
3827
3828bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003829 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003830 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3831 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
3832 return skip;
3833}
3834
3835bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3836 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003837 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003838 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3839 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3840 return skip;
3841}
3842
John Zulauf3d84f1b2020-03-09 13:33:25 -06003843void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3844 VkResult result) {
3845 // The state tracker sets up the command buffer state
3846 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3847
3848 // Create/initialize the structure that trackers accesses at the command buffer scope.
3849 auto cb_access_context = GetAccessContext(commandBuffer);
3850 assert(cb_access_context);
3851 cb_access_context->Reset();
3852}
3853
3854void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003855 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003856 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003857 if (cb_context) {
3858 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003859 }
3860}
3861
3862void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3863 VkSubpassContents contents) {
3864 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003865 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003866 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003867 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003868}
3869
3870void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3871 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3872 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003873 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003874}
3875
3876void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3877 const VkRenderPassBeginInfo *pRenderPassBegin,
3878 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3879 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003880 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3881}
3882
Mike Schuchardt2df08912020-12-15 16:28:09 -08003883bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3884 const VkSubpassEndInfo *pSubpassEndInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003885 bool skip = false;
3886
3887 auto cb_context = GetAccessContext(commandBuffer);
3888 assert(cb_context);
3889 auto cb_state = cb_context->GetCommandBufferState();
3890 if (!cb_state) return skip;
3891
3892 auto rp_state = cb_state->activeRenderPass;
3893 if (!rp_state) return skip;
3894
3895 skip |= cb_context->ValidateNextSubpass(func_name);
3896
3897 return skip;
3898}
3899
3900bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3901 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003902 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003903 subpass_begin_info.contents = contents;
3904 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3905 return skip;
3906}
3907
Mike Schuchardt2df08912020-12-15 16:28:09 -08003908bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3909 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003910 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3911 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3912 return skip;
3913}
3914
3915bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3916 const VkSubpassEndInfo *pSubpassEndInfo) const {
3917 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3918 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3919 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003920}
3921
3922void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003923 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003924 auto cb_context = GetAccessContext(commandBuffer);
3925 assert(cb_context);
3926 auto cb_state = cb_context->GetCommandBufferState();
3927 if (!cb_state) return;
3928
3929 auto rp_state = cb_state->activeRenderPass;
3930 if (!rp_state) return;
3931
John Zulauf355e49b2020-04-24 15:11:15 -06003932 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003933}
3934
3935void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3936 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003937 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003938 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003939 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003940}
3941
3942void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3943 const VkSubpassEndInfo *pSubpassEndInfo) {
3944 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003945 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003946}
3947
3948void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3949 const VkSubpassEndInfo *pSubpassEndInfo) {
3950 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003951 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003952}
3953
Mike Schuchardt2df08912020-12-15 16:28:09 -08003954bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003955 const char *func_name) const {
3956 bool skip = false;
3957
3958 auto cb_context = GetAccessContext(commandBuffer);
3959 assert(cb_context);
3960 auto cb_state = cb_context->GetCommandBufferState();
3961 if (!cb_state) return skip;
3962
3963 auto rp_state = cb_state->activeRenderPass;
3964 if (!rp_state) return skip;
3965
3966 skip |= cb_context->ValidateEndRenderpass(func_name);
3967 return skip;
3968}
3969
3970bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3971 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
3972 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
3973 return skip;
3974}
3975
Mike Schuchardt2df08912020-12-15 16:28:09 -08003976bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003977 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
3978 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
3979 return skip;
3980}
3981
3982bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003983 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003984 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
3985 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
3986 return skip;
3987}
3988
3989void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3990 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003991 // Resolve the all subpass contexts to the command buffer contexts
3992 auto cb_context = GetAccessContext(commandBuffer);
3993 assert(cb_context);
3994 auto cb_state = cb_context->GetCommandBufferState();
3995 if (!cb_state) return;
3996
locke-lunargaecf2152020-05-12 17:15:41 -06003997 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06003998 if (!rp_state) return;
3999
John Zulauf355e49b2020-04-24 15:11:15 -06004000 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06004001}
John Zulauf3d84f1b2020-03-09 13:33:25 -06004002
John Zulauf33fc1d52020-07-17 11:01:10 -06004003// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
4004// updates to a resource which do not conflict at the byte level.
4005// TODO: Revisit this rule to see if it needs to be tighter or looser
4006// TODO: Add programatic control over suppression heuristics
4007bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
4008 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
4009}
4010
John Zulauf3d84f1b2020-03-09 13:33:25 -06004011void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004012 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06004013 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004014}
4015
4016void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06004017 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06004018 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004019}
4020
4021void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06004022 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06004023 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004024}
locke-lunarga19c71d2020-03-02 18:17:04 -07004025
Jeff Leger178b1e52020-10-05 12:22:23 -04004026template <typename BufferImageCopyRegionType>
4027bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4028 VkImageLayout dstImageLayout, uint32_t regionCount,
4029 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004030 bool skip = false;
4031 const auto *cb_access_context = GetAccessContext(commandBuffer);
4032 assert(cb_access_context);
4033 if (!cb_access_context) return skip;
4034
Jeff Leger178b1e52020-10-05 12:22:23 -04004035 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4036 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
4037
locke-lunarga19c71d2020-03-02 18:17:04 -07004038 const auto *context = cb_access_context->GetCurrentAccessContext();
4039 assert(context);
4040 if (!context) return skip;
4041
4042 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07004043 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4044
4045 for (uint32_t region = 0; region < regionCount; region++) {
4046 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06004047 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004048 ResourceAccessRange src_range =
4049 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004050 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07004051 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06004052 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06004053 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004054 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004055 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004056 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004057 }
4058 }
4059 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004060 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004061 copy_region.imageOffset, copy_region.imageExtent);
4062 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004063 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004064 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004065 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004066 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004067 }
4068 if (skip) break;
4069 }
4070 if (skip) break;
4071 }
4072 return skip;
4073}
4074
Jeff Leger178b1e52020-10-05 12:22:23 -04004075bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4076 VkImageLayout dstImageLayout, uint32_t regionCount,
4077 const VkBufferImageCopy *pRegions) const {
4078 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
4079 COPY_COMMAND_VERSION_1);
4080}
4081
4082bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4083 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
4084 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4085 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4086 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4087}
4088
4089template <typename BufferImageCopyRegionType>
4090void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4091 VkImageLayout dstImageLayout, uint32_t regionCount,
4092 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004093 auto *cb_access_context = GetAccessContext(commandBuffer);
4094 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004095
4096 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4097 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
4098
4099 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004100 auto *context = cb_access_context->GetCurrentAccessContext();
4101 assert(context);
4102
4103 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06004104 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004105
4106 for (uint32_t region = 0; region < regionCount; region++) {
4107 const auto &copy_region = pRegions[region];
4108 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004109 ResourceAccessRange src_range =
4110 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004111 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004112 }
4113 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004114 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06004115 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004116 }
4117 }
4118}
4119
Jeff Leger178b1e52020-10-05 12:22:23 -04004120void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4121 VkImageLayout dstImageLayout, uint32_t regionCount,
4122 const VkBufferImageCopy *pRegions) {
4123 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
4124 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4125}
4126
4127void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4128 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4129 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4130 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4131 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4132 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4133}
4134
4135template <typename BufferImageCopyRegionType>
4136bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4137 VkBuffer dstBuffer, uint32_t regionCount,
4138 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004139 bool skip = false;
4140 const auto *cb_access_context = GetAccessContext(commandBuffer);
4141 assert(cb_access_context);
4142 if (!cb_access_context) return skip;
4143
Jeff Leger178b1e52020-10-05 12:22:23 -04004144 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4145 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
4146
locke-lunarga19c71d2020-03-02 18:17:04 -07004147 const auto *context = cb_access_context->GetCurrentAccessContext();
4148 assert(context);
4149 if (!context) return skip;
4150
4151 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4152 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4153 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
4154 for (uint32_t region = 0; region < regionCount; region++) {
4155 const auto &copy_region = pRegions[region];
4156 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004157 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004158 copy_region.imageOffset, copy_region.imageExtent);
4159 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004160 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004161 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004162 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004163 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004164 }
4165 }
4166 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06004167 ResourceAccessRange dst_range =
4168 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004169 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07004170 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004171 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004172 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004173 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004174 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004175 }
4176 }
4177 if (skip) break;
4178 }
4179 return skip;
4180}
4181
Jeff Leger178b1e52020-10-05 12:22:23 -04004182bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4183 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4184 const VkBufferImageCopy *pRegions) const {
4185 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
4186 COPY_COMMAND_VERSION_1);
4187}
4188
4189bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4190 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4191 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4192 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4193 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4194}
4195
4196template <typename BufferImageCopyRegionType>
4197void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4198 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
4199 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004200 auto *cb_access_context = GetAccessContext(commandBuffer);
4201 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004202
4203 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4204 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
4205
4206 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004207 auto *context = cb_access_context->GetCurrentAccessContext();
4208 assert(context);
4209
4210 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004211 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4212 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 -06004213 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004214
4215 for (uint32_t region = 0; region < regionCount; region++) {
4216 const auto &copy_region = pRegions[region];
4217 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004218 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06004219 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004220 }
4221 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004222 ResourceAccessRange dst_range =
4223 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004224 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004225 }
4226 }
4227}
4228
Jeff Leger178b1e52020-10-05 12:22:23 -04004229void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4230 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4231 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
4232 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4233}
4234
4235void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4236 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4237 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4238 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4239 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4240 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4241}
4242
4243template <typename RegionType>
4244bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4245 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4246 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004247 bool skip = false;
4248 const auto *cb_access_context = GetAccessContext(commandBuffer);
4249 assert(cb_access_context);
4250 if (!cb_access_context) return skip;
4251
4252 const auto *context = cb_access_context->GetCurrentAccessContext();
4253 assert(context);
4254 if (!context) return skip;
4255
4256 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4257 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4258
4259 for (uint32_t region = 0; region < regionCount; region++) {
4260 const auto &blit_region = pRegions[region];
4261 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004262 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4263 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4264 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4265 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4266 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4267 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
4268 auto hazard =
4269 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004270 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004271 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004272 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004273 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004274 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004275 }
4276 }
4277
4278 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004279 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4280 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4281 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4282 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4283 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4284 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
4285 auto hazard =
4286 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004287 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004288 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004289 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004290 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004291 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004292 }
4293 if (skip) break;
4294 }
4295 }
4296
4297 return skip;
4298}
4299
Jeff Leger178b1e52020-10-05 12:22:23 -04004300bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4301 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4302 const VkImageBlit *pRegions, VkFilter filter) const {
4303 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4304 "vkCmdBlitImage");
4305}
4306
4307bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4308 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4309 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4310 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4311 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4312}
4313
4314template <typename RegionType>
4315void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4316 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4317 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004318 auto *cb_access_context = GetAccessContext(commandBuffer);
4319 assert(cb_access_context);
4320 auto *context = cb_access_context->GetCurrentAccessContext();
4321 assert(context);
4322
4323 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004324 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004325
4326 for (uint32_t region = 0; region < regionCount; region++) {
4327 const auto &blit_region = pRegions[region];
4328 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004329 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4330 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4331 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4332 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4333 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4334 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
4335 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004336 }
4337 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004338 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4339 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4340 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4341 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4342 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4343 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
4344 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004345 }
4346 }
4347}
locke-lunarg36ba2592020-04-03 09:42:04 -06004348
Jeff Leger178b1e52020-10-05 12:22:23 -04004349void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4350 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4351 const VkImageBlit *pRegions, VkFilter filter) {
4352 auto *cb_access_context = GetAccessContext(commandBuffer);
4353 assert(cb_access_context);
4354 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4355 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4356 pRegions, filter);
4357 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4358}
4359
4360void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4361 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4362 auto *cb_access_context = GetAccessContext(commandBuffer);
4363 assert(cb_access_context);
4364 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4365 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4366 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4367 pBlitImageInfo->filter, tag);
4368}
4369
locke-lunarg61870c22020-06-09 14:51:50 -06004370bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
4371 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
4372 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004373 bool skip = false;
4374 if (drawCount == 0) return skip;
4375
4376 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4377 VkDeviceSize size = struct_size;
4378 if (drawCount == 1 || stride == size) {
4379 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004380 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004381 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4382 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004383 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004384 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004385 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06004386 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004387 }
4388 } else {
4389 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004390 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004391 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4392 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004393 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004394 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4395 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
4396 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004397 break;
4398 }
4399 }
4400 }
4401 return skip;
4402}
4403
locke-lunarg61870c22020-06-09 14:51:50 -06004404void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
4405 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4406 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06004407 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4408 VkDeviceSize size = struct_size;
4409 if (drawCount == 1 || stride == size) {
4410 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004411 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004412 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
4413 } else {
4414 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004415 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004416 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
4417 }
4418 }
4419}
4420
locke-lunarg61870c22020-06-09 14:51:50 -06004421bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
4422 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004423 bool skip = false;
4424
4425 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004426 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004427 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4428 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004429 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004430 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004431 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06004432 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004433 }
4434 return skip;
4435}
4436
locke-lunarg61870c22020-06-09 14:51:50 -06004437void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06004438 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004439 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004440 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
4441}
4442
locke-lunarg36ba2592020-04-03 09:42:04 -06004443bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004444 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004445 const auto *cb_access_context = GetAccessContext(commandBuffer);
4446 assert(cb_access_context);
4447 if (!cb_access_context) return skip;
4448
locke-lunarg61870c22020-06-09 14:51:50 -06004449 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004450 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004451}
4452
4453void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004454 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004455 auto *cb_access_context = GetAccessContext(commandBuffer);
4456 assert(cb_access_context);
4457 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004458
locke-lunarg61870c22020-06-09 14:51:50 -06004459 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004460}
locke-lunarge1a67022020-04-29 00:15:36 -06004461
4462bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004463 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004464 const auto *cb_access_context = GetAccessContext(commandBuffer);
4465 assert(cb_access_context);
4466 if (!cb_access_context) return skip;
4467
4468 const auto *context = cb_access_context->GetCurrentAccessContext();
4469 assert(context);
4470 if (!context) return skip;
4471
locke-lunarg61870c22020-06-09 14:51:50 -06004472 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
4473 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
4474 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004475 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004476}
4477
4478void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004479 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004480 auto *cb_access_context = GetAccessContext(commandBuffer);
4481 assert(cb_access_context);
4482 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4483 auto *context = cb_access_context->GetCurrentAccessContext();
4484 assert(context);
4485
locke-lunarg61870c22020-06-09 14:51:50 -06004486 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4487 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004488}
4489
4490bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4491 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004492 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004493 const auto *cb_access_context = GetAccessContext(commandBuffer);
4494 assert(cb_access_context);
4495 if (!cb_access_context) return skip;
4496
locke-lunarg61870c22020-06-09 14:51:50 -06004497 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4498 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4499 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004500 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004501}
4502
4503void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4504 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004505 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004506 auto *cb_access_context = GetAccessContext(commandBuffer);
4507 assert(cb_access_context);
4508 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004509
locke-lunarg61870c22020-06-09 14:51:50 -06004510 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4511 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4512 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004513}
4514
4515bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4516 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004517 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004518 const auto *cb_access_context = GetAccessContext(commandBuffer);
4519 assert(cb_access_context);
4520 if (!cb_access_context) return skip;
4521
locke-lunarg61870c22020-06-09 14:51:50 -06004522 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4523 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4524 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004525 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004526}
4527
4528void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4529 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004530 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004531 auto *cb_access_context = GetAccessContext(commandBuffer);
4532 assert(cb_access_context);
4533 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004534
locke-lunarg61870c22020-06-09 14:51:50 -06004535 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4536 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4537 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004538}
4539
4540bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4541 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004542 bool skip = false;
4543 if (drawCount == 0) return skip;
4544
locke-lunargff255f92020-05-13 18:53:52 -06004545 const auto *cb_access_context = GetAccessContext(commandBuffer);
4546 assert(cb_access_context);
4547 if (!cb_access_context) return skip;
4548
4549 const auto *context = cb_access_context->GetCurrentAccessContext();
4550 assert(context);
4551 if (!context) return skip;
4552
locke-lunarg61870c22020-06-09 14:51:50 -06004553 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4554 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
4555 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
4556 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004557
4558 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4559 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4560 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004561 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004562 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004563}
4564
4565void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4566 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004567 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004568 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004569 auto *cb_access_context = GetAccessContext(commandBuffer);
4570 assert(cb_access_context);
4571 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4572 auto *context = cb_access_context->GetCurrentAccessContext();
4573 assert(context);
4574
locke-lunarg61870c22020-06-09 14:51:50 -06004575 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4576 cb_access_context->RecordDrawSubpassAttachment(tag);
4577 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004578
4579 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4580 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4581 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004582 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004583}
4584
4585bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4586 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004587 bool skip = false;
4588 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004589 const auto *cb_access_context = GetAccessContext(commandBuffer);
4590 assert(cb_access_context);
4591 if (!cb_access_context) return skip;
4592
4593 const auto *context = cb_access_context->GetCurrentAccessContext();
4594 assert(context);
4595 if (!context) return skip;
4596
locke-lunarg61870c22020-06-09 14:51:50 -06004597 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4598 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
4599 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
4600 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004601
4602 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4603 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4604 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004605 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004606 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004607}
4608
4609void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4610 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004611 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004612 auto *cb_access_context = GetAccessContext(commandBuffer);
4613 assert(cb_access_context);
4614 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4615 auto *context = cb_access_context->GetCurrentAccessContext();
4616 assert(context);
4617
locke-lunarg61870c22020-06-09 14:51:50 -06004618 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4619 cb_access_context->RecordDrawSubpassAttachment(tag);
4620 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004621
4622 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4623 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4624 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004625 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004626}
4627
4628bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4629 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4630 uint32_t stride, const char *function) const {
4631 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004632 const auto *cb_access_context = GetAccessContext(commandBuffer);
4633 assert(cb_access_context);
4634 if (!cb_access_context) return skip;
4635
4636 const auto *context = cb_access_context->GetCurrentAccessContext();
4637 assert(context);
4638 if (!context) return skip;
4639
locke-lunarg61870c22020-06-09 14:51:50 -06004640 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4641 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
4642 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
4643 function);
4644 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004645
4646 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4647 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4648 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004649 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004650 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004651}
4652
4653bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4654 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4655 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004656 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4657 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004658}
4659
4660void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4661 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4662 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004663 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4664 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004665 auto *cb_access_context = GetAccessContext(commandBuffer);
4666 assert(cb_access_context);
4667 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
4668 auto *context = cb_access_context->GetCurrentAccessContext();
4669 assert(context);
4670
locke-lunarg61870c22020-06-09 14:51:50 -06004671 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4672 cb_access_context->RecordDrawSubpassAttachment(tag);
4673 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4674 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004675
4676 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4677 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4678 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004679 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004680}
4681
4682bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4683 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4684 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004685 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4686 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004687}
4688
4689void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4690 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4691 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004692 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4693 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004694 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004695}
4696
4697bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(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 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004702}
4703
4704void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(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::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4708 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004709 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4710}
4711
4712bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4713 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4714 uint32_t stride, const char *function) const {
4715 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004716 const auto *cb_access_context = GetAccessContext(commandBuffer);
4717 assert(cb_access_context);
4718 if (!cb_access_context) return skip;
4719
4720 const auto *context = cb_access_context->GetCurrentAccessContext();
4721 assert(context);
4722 if (!context) return skip;
4723
locke-lunarg61870c22020-06-09 14:51:50 -06004724 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4725 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
4726 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
4727 stride, function);
4728 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004729
4730 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4731 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4732 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004733 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004734 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004735}
4736
4737bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4738 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4739 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004740 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4741 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004742}
4743
4744void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4745 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4746 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004747 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4748 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004749 auto *cb_access_context = GetAccessContext(commandBuffer);
4750 assert(cb_access_context);
4751 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4752 auto *context = cb_access_context->GetCurrentAccessContext();
4753 assert(context);
4754
locke-lunarg61870c22020-06-09 14:51:50 -06004755 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4756 cb_access_context->RecordDrawSubpassAttachment(tag);
4757 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4758 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004759
4760 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4761 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004762 // We will update the index and vertex buffer in SubmitQueue in the future.
4763 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004764}
4765
4766bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4767 VkDeviceSize offset, VkBuffer countBuffer,
4768 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4769 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004770 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4771 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004772}
4773
4774void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4775 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4776 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004777 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4778 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004779 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4780}
4781
4782bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4783 VkDeviceSize offset, VkBuffer countBuffer,
4784 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4785 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004786 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4787 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004788}
4789
4790void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4791 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4792 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004793 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4794 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004795 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4796}
4797
4798bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4799 const VkClearColorValue *pColor, uint32_t rangeCount,
4800 const VkImageSubresourceRange *pRanges) const {
4801 bool skip = false;
4802 const auto *cb_access_context = GetAccessContext(commandBuffer);
4803 assert(cb_access_context);
4804 if (!cb_access_context) return skip;
4805
4806 const auto *context = cb_access_context->GetCurrentAccessContext();
4807 assert(context);
4808 if (!context) return skip;
4809
4810 const auto *image_state = Get<IMAGE_STATE>(image);
4811
4812 for (uint32_t index = 0; index < rangeCount; index++) {
4813 const auto &range = pRanges[index];
4814 if (image_state) {
4815 auto hazard =
4816 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4817 if (hazard.hazard) {
4818 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004819 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004820 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004821 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004822 }
4823 }
4824 }
4825 return skip;
4826}
4827
4828void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4829 const VkClearColorValue *pColor, uint32_t rangeCount,
4830 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004831 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004832 auto *cb_access_context = GetAccessContext(commandBuffer);
4833 assert(cb_access_context);
4834 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4835 auto *context = cb_access_context->GetCurrentAccessContext();
4836 assert(context);
4837
4838 const auto *image_state = Get<IMAGE_STATE>(image);
4839
4840 for (uint32_t index = 0; index < rangeCount; index++) {
4841 const auto &range = pRanges[index];
4842 if (image_state) {
4843 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4844 tag);
4845 }
4846 }
4847}
4848
4849bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4850 VkImageLayout imageLayout,
4851 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4852 const VkImageSubresourceRange *pRanges) const {
4853 bool skip = false;
4854 const auto *cb_access_context = GetAccessContext(commandBuffer);
4855 assert(cb_access_context);
4856 if (!cb_access_context) return skip;
4857
4858 const auto *context = cb_access_context->GetCurrentAccessContext();
4859 assert(context);
4860 if (!context) return skip;
4861
4862 const auto *image_state = Get<IMAGE_STATE>(image);
4863
4864 for (uint32_t index = 0; index < rangeCount; index++) {
4865 const auto &range = pRanges[index];
4866 if (image_state) {
4867 auto hazard =
4868 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4869 if (hazard.hazard) {
4870 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004871 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004872 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004873 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004874 }
4875 }
4876 }
4877 return skip;
4878}
4879
4880void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4881 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4882 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004883 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004884 auto *cb_access_context = GetAccessContext(commandBuffer);
4885 assert(cb_access_context);
4886 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4887 auto *context = cb_access_context->GetCurrentAccessContext();
4888 assert(context);
4889
4890 const auto *image_state = Get<IMAGE_STATE>(image);
4891
4892 for (uint32_t index = 0; index < rangeCount; index++) {
4893 const auto &range = pRanges[index];
4894 if (image_state) {
4895 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4896 tag);
4897 }
4898 }
4899}
4900
4901bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4902 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4903 VkDeviceSize dstOffset, VkDeviceSize stride,
4904 VkQueryResultFlags flags) const {
4905 bool skip = false;
4906 const auto *cb_access_context = GetAccessContext(commandBuffer);
4907 assert(cb_access_context);
4908 if (!cb_access_context) return skip;
4909
4910 const auto *context = cb_access_context->GetCurrentAccessContext();
4911 assert(context);
4912 if (!context) return skip;
4913
4914 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4915
4916 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004917 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004918 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4919 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004920 skip |=
4921 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4922 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4923 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004924 }
4925 }
locke-lunargff255f92020-05-13 18:53:52 -06004926
4927 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004928 return skip;
4929}
4930
4931void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4932 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4933 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004934 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4935 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004936 auto *cb_access_context = GetAccessContext(commandBuffer);
4937 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004938 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004939 auto *context = cb_access_context->GetCurrentAccessContext();
4940 assert(context);
4941
4942 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4943
4944 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004945 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004946 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4947 }
locke-lunargff255f92020-05-13 18:53:52 -06004948
4949 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004950}
4951
4952bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4953 VkDeviceSize size, uint32_t data) const {
4954 bool skip = false;
4955 const auto *cb_access_context = GetAccessContext(commandBuffer);
4956 assert(cb_access_context);
4957 if (!cb_access_context) return skip;
4958
4959 const auto *context = cb_access_context->GetCurrentAccessContext();
4960 assert(context);
4961 if (!context) return skip;
4962
4963 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4964
4965 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004966 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004967 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4968 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004969 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004970 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004971 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004972 }
4973 }
4974 return skip;
4975}
4976
4977void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4978 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004979 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004980 auto *cb_access_context = GetAccessContext(commandBuffer);
4981 assert(cb_access_context);
4982 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4983 auto *context = cb_access_context->GetCurrentAccessContext();
4984 assert(context);
4985
4986 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4987
4988 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004989 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004990 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4991 }
4992}
4993
4994bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4995 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4996 const VkImageResolve *pRegions) const {
4997 bool skip = false;
4998 const auto *cb_access_context = GetAccessContext(commandBuffer);
4999 assert(cb_access_context);
5000 if (!cb_access_context) return skip;
5001
5002 const auto *context = cb_access_context->GetCurrentAccessContext();
5003 assert(context);
5004 if (!context) return skip;
5005
5006 const auto *src_image = Get<IMAGE_STATE>(srcImage);
5007 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
5008
5009 for (uint32_t region = 0; region < regionCount; region++) {
5010 const auto &resolve_region = pRegions[region];
5011 if (src_image) {
5012 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5013 resolve_region.srcOffset, resolve_region.extent);
5014 if (hazard.hazard) {
5015 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005016 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005017 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06005018 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005019 }
5020 }
5021
5022 if (dst_image) {
5023 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5024 resolve_region.dstOffset, resolve_region.extent);
5025 if (hazard.hazard) {
5026 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005027 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005028 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06005029 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005030 }
5031 if (skip) break;
5032 }
5033 }
5034
5035 return skip;
5036}
5037
5038void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5039 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5040 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005041 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
5042 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06005043 auto *cb_access_context = GetAccessContext(commandBuffer);
5044 assert(cb_access_context);
5045 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
5046 auto *context = cb_access_context->GetCurrentAccessContext();
5047 assert(context);
5048
5049 auto *src_image = Get<IMAGE_STATE>(srcImage);
5050 auto *dst_image = Get<IMAGE_STATE>(dstImage);
5051
5052 for (uint32_t region = 0; region < regionCount; region++) {
5053 const auto &resolve_region = pRegions[region];
5054 if (src_image) {
5055 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5056 resolve_region.srcOffset, resolve_region.extent, tag);
5057 }
5058 if (dst_image) {
5059 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5060 resolve_region.dstOffset, resolve_region.extent, tag);
5061 }
5062 }
5063}
5064
Jeff Leger178b1e52020-10-05 12:22:23 -04005065bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5066 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
5067 bool skip = false;
5068 const auto *cb_access_context = GetAccessContext(commandBuffer);
5069 assert(cb_access_context);
5070 if (!cb_access_context) return skip;
5071
5072 const auto *context = cb_access_context->GetCurrentAccessContext();
5073 assert(context);
5074 if (!context) return skip;
5075
5076 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5077 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5078
5079 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5080 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5081 if (src_image) {
5082 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5083 resolve_region.srcOffset, resolve_region.extent);
5084 if (hazard.hazard) {
5085 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
5086 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
5087 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
5088 region, string_UsageTag(hazard).c_str());
5089 }
5090 }
5091
5092 if (dst_image) {
5093 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5094 resolve_region.dstOffset, resolve_region.extent);
5095 if (hazard.hazard) {
5096 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
5097 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
5098 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
5099 region, string_UsageTag(hazard).c_str());
5100 }
5101 if (skip) break;
5102 }
5103 }
5104
5105 return skip;
5106}
5107
5108void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5109 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5110 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5111 auto *cb_access_context = GetAccessContext(commandBuffer);
5112 assert(cb_access_context);
5113 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
5114 auto *context = cb_access_context->GetCurrentAccessContext();
5115 assert(context);
5116
5117 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5118 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5119
5120 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5121 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5122 if (src_image) {
5123 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5124 resolve_region.srcOffset, resolve_region.extent, tag);
5125 }
5126 if (dst_image) {
5127 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5128 resolve_region.dstOffset, resolve_region.extent, tag);
5129 }
5130 }
5131}
5132
locke-lunarge1a67022020-04-29 00:15:36 -06005133bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5134 VkDeviceSize dataSize, const void *pData) const {
5135 bool skip = false;
5136 const auto *cb_access_context = GetAccessContext(commandBuffer);
5137 assert(cb_access_context);
5138 if (!cb_access_context) return skip;
5139
5140 const auto *context = cb_access_context->GetCurrentAccessContext();
5141 assert(context);
5142 if (!context) return skip;
5143
5144 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5145
5146 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005147 // VK_WHOLE_SIZE not allowed
5148 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06005149 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
5150 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005151 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005152 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06005153 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005154 }
5155 }
5156 return skip;
5157}
5158
5159void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5160 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005161 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005162 auto *cb_access_context = GetAccessContext(commandBuffer);
5163 assert(cb_access_context);
5164 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5165 auto *context = cb_access_context->GetCurrentAccessContext();
5166 assert(context);
5167
5168 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5169
5170 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005171 // VK_WHOLE_SIZE not allowed
5172 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06005173 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
5174 }
5175}
locke-lunargff255f92020-05-13 18:53:52 -06005176
5177bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5178 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5179 bool skip = false;
5180 const auto *cb_access_context = GetAccessContext(commandBuffer);
5181 assert(cb_access_context);
5182 if (!cb_access_context) return skip;
5183
5184 const auto *context = cb_access_context->GetCurrentAccessContext();
5185 assert(context);
5186 if (!context) return skip;
5187
5188 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5189
5190 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005191 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06005192 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
5193 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005194 skip |=
5195 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5196 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
5197 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005198 }
5199 }
5200 return skip;
5201}
5202
5203void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5204 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005205 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005206 auto *cb_access_context = GetAccessContext(commandBuffer);
5207 assert(cb_access_context);
5208 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5209 auto *context = cb_access_context->GetCurrentAccessContext();
5210 assert(context);
5211
5212 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5213
5214 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005215 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06005216 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
5217 }
5218}
John Zulauf49beb112020-11-04 16:06:31 -07005219
5220bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5221 bool skip = false;
5222 const auto *cb_context = GetAccessContext(commandBuffer);
5223 assert(cb_context);
5224 if (!cb_context) return skip;
5225
5226 return cb_context->ValidateSetEvent(commandBuffer, event, stageMask);
5227}
5228
5229void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5230 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5231 auto *cb_context = GetAccessContext(commandBuffer);
5232 assert(cb_context);
5233 if (!cb_context) return;
John Zulauf4a6105a2020-11-17 15:11:05 -07005234 const auto tag = cb_context->NextCommandTag(CMD_SETEVENT);
5235 cb_context->RecordSetEvent(commandBuffer, event, stageMask, tag);
John Zulauf49beb112020-11-04 16:06:31 -07005236}
5237
5238bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5239 VkPipelineStageFlags stageMask) const {
5240 bool skip = false;
5241 const auto *cb_context = GetAccessContext(commandBuffer);
5242 assert(cb_context);
5243 if (!cb_context) return skip;
5244
5245 return cb_context->ValidateResetEvent(commandBuffer, event, stageMask);
5246}
5247
5248void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5249 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5250 auto *cb_context = GetAccessContext(commandBuffer);
5251 assert(cb_context);
5252 if (!cb_context) return;
5253
5254 cb_context->RecordResetEvent(commandBuffer, event, stageMask);
5255}
5256
5257bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5258 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5259 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5260 uint32_t bufferMemoryBarrierCount,
5261 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5262 uint32_t imageMemoryBarrierCount,
5263 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5264 bool skip = false;
5265 const auto *cb_context = GetAccessContext(commandBuffer);
5266 assert(cb_context);
5267 if (!cb_context) return skip;
5268
John Zulauf4a6105a2020-11-17 15:11:05 -07005269 return cb_context->ValidateWaitEvents(eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount, pMemoryBarriers,
5270 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
John Zulauf49beb112020-11-04 16:06:31 -07005271 pImageMemoryBarriers);
5272}
5273
5274void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5275 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5276 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5277 uint32_t bufferMemoryBarrierCount,
5278 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5279 uint32_t imageMemoryBarrierCount,
5280 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5281 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5282 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5283 imageMemoryBarrierCount, pImageMemoryBarriers);
5284
5285 auto *cb_context = GetAccessContext(commandBuffer);
5286 assert(cb_context);
5287 if (!cb_context) return;
5288
John Zulauf4a6105a2020-11-17 15:11:05 -07005289 const auto tag = cb_context->NextCommandTag(CMD_WAITEVENTS);
John Zulauf49beb112020-11-04 16:06:31 -07005290 cb_context->RecordWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5291 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
John Zulauf4a6105a2020-11-17 15:11:05 -07005292 pImageMemoryBarriers, tag);
5293}
5294
5295void SyncEventState::ResetFirstScope() {
5296 for (const auto address_type : kAddressTypes) {
5297 first_scope[static_cast<size_t>(address_type)].clear();
5298 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005299 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07005300}
5301
5302// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
5303SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(VkPipelineStageFlags srcStageMask) const {
5304 IgnoreReason reason = NotIgnored;
5305
5306 if (last_command == CMD_RESETEVENT && !HasBarrier(0U, 0U)) {
5307 reason = ResetWaitRace;
5308 } else if (unsynchronized_set) {
5309 reason = SetRace;
5310 } else {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005311 const VkPipelineStageFlags missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005312 if (missing_bits) reason = MissingStageBits;
5313 }
5314
5315 return reason;
5316}
5317
5318bool SyncEventState::HasBarrier(VkPipelineStageFlags stageMask, VkPipelineStageFlags exec_scope_arg) const {
5319 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5320 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5321 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005322}