blob: 9c8017876d31368e3b9a54cb3d7552006e7bdbc1 [file] [log] [blame]
locke-lunarg8ec19162020-06-16 18:48:34 -06001/* Copyright (c) 2019-2020 The Khronos Group Inc.
2 * Copyright (c) 2019-2020 Valve Corporation
3 * Copyright (c) 2019-2020 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>
18 */
19
20#include <limits>
21#include <vector>
locke-lunarg296a3c92020-03-25 01:04:29 -060022#include <memory>
23#include <bitset>
John Zulauf9cb530d2019-09-30 14:14:10 -060024#include "synchronization_validation.h"
25
John Zulauf43cc7462020-12-03 12:33:12 -070026const static std::array<AccessAddressType, static_cast<size_t>(AccessAddressType::kTypeCount)> kAddressTypes = {
27 AccessAddressType::kLinear, AccessAddressType::kIdealized};
28
John Zulauf9cb530d2019-09-30 14:14:10 -060029static const char *string_SyncHazardVUID(SyncHazard hazard) {
30 switch (hazard) {
31 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070032 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060033 break;
34 case SyncHazard::READ_AFTER_WRITE:
35 return "SYNC-HAZARD-READ_AFTER_WRITE";
36 break;
37 case SyncHazard::WRITE_AFTER_READ:
38 return "SYNC-HAZARD-WRITE_AFTER_READ";
39 break;
40 case SyncHazard::WRITE_AFTER_WRITE:
41 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
42 break;
John Zulauf2f952d22020-02-10 11:34:51 -070043 case SyncHazard::READ_RACING_WRITE:
44 return "SYNC-HAZARD-READ-RACING-WRITE";
45 break;
46 case SyncHazard::WRITE_RACING_WRITE:
47 return "SYNC-HAZARD-WRITE-RACING-WRITE";
48 break;
49 case SyncHazard::WRITE_RACING_READ:
50 return "SYNC-HAZARD-WRITE-RACING-READ";
51 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060052 default:
53 assert(0);
54 }
55 return "SYNC-HAZARD-INVALID";
56}
57
John Zulauf59e25072020-07-17 10:55:21 -060058static bool IsHazardVsRead(SyncHazard hazard) {
59 switch (hazard) {
60 case SyncHazard::NONE:
61 return false;
62 break;
63 case SyncHazard::READ_AFTER_WRITE:
64 return false;
65 break;
66 case SyncHazard::WRITE_AFTER_READ:
67 return true;
68 break;
69 case SyncHazard::WRITE_AFTER_WRITE:
70 return false;
71 break;
72 case SyncHazard::READ_RACING_WRITE:
73 return false;
74 break;
75 case SyncHazard::WRITE_RACING_WRITE:
76 return false;
77 break;
78 case SyncHazard::WRITE_RACING_READ:
79 return true;
80 break;
81 default:
82 assert(0);
83 }
84 return false;
85}
86
John Zulauf9cb530d2019-09-30 14:14:10 -060087static const char *string_SyncHazard(SyncHazard hazard) {
88 switch (hazard) {
89 case SyncHazard::NONE:
90 return "NONR";
91 break;
92 case SyncHazard::READ_AFTER_WRITE:
93 return "READ_AFTER_WRITE";
94 break;
95 case SyncHazard::WRITE_AFTER_READ:
96 return "WRITE_AFTER_READ";
97 break;
98 case SyncHazard::WRITE_AFTER_WRITE:
99 return "WRITE_AFTER_WRITE";
100 break;
John Zulauf2f952d22020-02-10 11:34:51 -0700101 case SyncHazard::READ_RACING_WRITE:
102 return "READ_RACING_WRITE";
103 break;
104 case SyncHazard::WRITE_RACING_WRITE:
105 return "WRITE_RACING_WRITE";
106 break;
107 case SyncHazard::WRITE_RACING_READ:
108 return "WRITE_RACING_READ";
109 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600110 default:
111 assert(0);
112 }
113 return "INVALID HAZARD";
114}
115
John Zulauf37ceaed2020-07-03 16:18:15 -0600116static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
117 // Return the info for the first bit found
118 const SyncStageAccessInfoType *info = nullptr;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700119 for (size_t i = 0; i < flags.size(); i++) {
120 if (flags.test(i)) {
121 info = &syncStageAccessInfoByStageAccessIndex[i];
122 break;
John Zulauf37ceaed2020-07-03 16:18:15 -0600123 }
124 }
125 return info;
126}
127
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700128static std::string string_SyncStageAccessFlags(const SyncStageAccessFlags &flags, const char *sep = "|") {
John Zulauf59e25072020-07-17 10:55:21 -0600129 std::string out_str;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700130 if (flags.none()) {
John Zulauf389c34b2020-07-28 11:19:35 -0600131 out_str = "0";
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700132 } else {
133 for (size_t i = 0; i < syncStageAccessInfoByStageAccessIndex.size(); i++) {
134 const auto &info = syncStageAccessInfoByStageAccessIndex[i];
135 if ((flags & info.stage_access_bit).any()) {
136 if (!out_str.empty()) {
137 out_str.append(sep);
138 }
139 out_str.append(info.name);
John Zulauf59e25072020-07-17 10:55:21 -0600140 }
John Zulauf59e25072020-07-17 10:55:21 -0600141 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700142 if (out_str.length() == 0) {
143 out_str.append("Unhandled SyncStageAccess");
144 }
John Zulauf59e25072020-07-17 10:55:21 -0600145 }
146 return out_str;
147}
148
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700149static std::string string_UsageTag(const ResourceUsageTag &tag) {
150 std::stringstream out;
151
Jeremy Gebben4bb73502020-12-14 11:17:50 -0700152 out << "command: " << CommandTypeString(tag.GetCommand());
153 out << ", seq_no: " << tag.GetSeqNum() << ", reset_no: " << tag.GetResetNum();
154 if (tag.GetSubCommand() != 0) {
155 out << ", subcmd: " << tag.GetSubCommand();
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700156 }
157 return out.str();
158}
159
John Zulauf37ceaed2020-07-03 16:18:15 -0600160static std::string string_UsageTag(const HazardResult &hazard) {
161 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600162 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
163 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600164 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600165 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
166 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf59e25072020-07-17 10:55:21 -0600167 out << "(usage: " << usage_info.name << ", prior_usage: " << stage_access_name;
168 if (IsHazardVsRead(hazard.hazard)) {
169 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
170 out << ", read_barriers: " << string_VkPipelineStageFlags(barriers);
171 } else {
172 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
173 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
174 }
175
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700176 out << ", " << string_UsageTag(tag) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600177 return out.str();
178}
179
John Zulaufd14743a2020-07-03 09:42:39 -0600180// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
181// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
182// also reflects this special case for read hazard detection (using access instead of exec scope)
John Zulaufb027cdb2020-05-21 14:25:22 -0600183static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700184static const SyncStageAccessFlags kColorAttachmentAccessScope =
185 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
186 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
187 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
188 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600189static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
190 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700191static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
192 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
193 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
194 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600195
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700196static const SyncOrderingBarrier kColorAttachmentRasterOrder = {kColorAttachmentExecScope, kColorAttachmentAccessScope};
197static const SyncOrderingBarrier kDepthStencilAttachmentRasterOrder = {kDepthStencilAttachmentExecScope,
198 kDepthStencilAttachmentAccessScope};
199static const SyncOrderingBarrier kAttachmentRasterOrder = {kDepthStencilAttachmentExecScope | kColorAttachmentExecScope,
200 kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope};
John Zulauf7635de32020-05-29 17:14:15 -0600201// 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 -0600202static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600203
John Zulaufb02c1eb2020-10-06 16:33:36 -0600204static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) {
205 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
206}
207
208static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
209
locke-lunarg3c038002020-04-30 23:08:08 -0600210inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
211 if (size == VK_WHOLE_SIZE) {
212 return (whole_size - offset);
213 }
214 return size;
215}
216
John Zulauf3e86bf02020-09-12 10:47:57 -0600217static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
218 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
219}
220
John Zulauf16adfc92020-04-08 10:28:33 -0600221template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600222static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600223 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
224}
225
John Zulauf355e49b2020-04-24 15:11:15 -0600226static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600227
John Zulauf3e86bf02020-09-12 10:47:57 -0600228static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
229 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
230}
231
232static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
233 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
234}
235
John Zulauf4a6105a2020-11-17 15:11:05 -0700236// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
237//
John Zulauf10f1f522020-12-18 12:00:35 -0700238// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
239//
John Zulauf4a6105a2020-11-17 15:11:05 -0700240// Usage:
241// Constructor() -- initializes the generator to point to the begin of the space declared.
242// * -- the current range of the generator empty signfies end
243// ++ -- advance to the next non-empty range (or end)
244
245// A wrapper for a single range with the same semantics as the actual generators below
246template <typename KeyType>
247class SingleRangeGenerator {
248 public:
249 SingleRangeGenerator(const KeyType &range) : current_(range) {}
250 KeyType &operator*() const { return *current_; }
251 KeyType *operator->() const { return &*current_; }
252 SingleRangeGenerator &operator++() {
253 current_ = KeyType(); // just one real range
254 return *this;
255 }
256
257 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
258
259 private:
260 SingleRangeGenerator() = default;
261 const KeyType range_;
262 KeyType current_;
263};
264
265// Generate the ranges that are the intersection of range and the entries in the FilterMap
266template <typename FilterMap, typename KeyType = typename FilterMap::key_type>
267class FilteredRangeGenerator {
268 public:
269 FilteredRangeGenerator(const FilterMap &filter, const KeyType &range)
270 : range_(range), filter_(&filter), filter_pos_(), current_() {
271 SeekBegin();
272 }
273 const KeyType &operator*() const { return current_; }
274 const KeyType *operator->() const { return &current_; }
275 FilteredRangeGenerator &operator++() {
276 ++filter_pos_;
277 UpdateCurrent();
278 return *this;
279 }
280
281 bool operator==(const FilteredRangeGenerator &other) const { return current_ == other.current_; }
282
283 private:
284 FilteredRangeGenerator() = default;
285 void UpdateCurrent() {
286 if (filter_pos_ != filter_->cend()) {
287 current_ = range_ & filter_pos_->first;
288 } else {
289 current_ = KeyType();
290 }
291 }
292 void SeekBegin() {
293 filter_pos_ = filter_->lower_bound(range_);
294 UpdateCurrent();
295 }
296 const KeyType range_;
297 const FilterMap *filter_;
298 typename FilterMap::const_iterator filter_pos_;
299 KeyType current_;
300};
301using EventSimpleRangeGenerator = FilteredRangeGenerator<SyncEventState::ScopeMap>;
302
303// Templated to allow for different Range generators or map sources...
304
305// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulauf4a6105a2020-11-17 15:11:05 -0700306template <typename FilterMap, typename RangeGen, typename KeyType = typename FilterMap::key_type>
307class FilteredGeneratorGenerator {
308 public:
309 FilteredGeneratorGenerator(const FilterMap &filter, RangeGen &gen) : filter_(&filter), gen_(&gen), filter_pos_(), current_() {
310 SeekBegin();
311 }
312 const KeyType &operator*() const { return current_; }
313 const KeyType *operator->() const { return &current_; }
314 FilteredGeneratorGenerator &operator++() {
315 KeyType gen_range = GenRange();
316 KeyType filter_range = FilterRange();
317 current_ = KeyType();
318 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
319 if (gen_range.end > filter_range.end) {
320 // if the generated range is beyond the filter_range, advance the filter range
321 filter_range = AdvanceFilter();
322 } else {
323 gen_range = AdvanceGen();
324 }
325 current_ = gen_range & filter_range;
326 }
327 return *this;
328 }
329
330 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
331
332 private:
333 KeyType AdvanceFilter() {
334 ++filter_pos_;
335 auto filter_range = FilterRange();
336 if (filter_range.valid()) {
337 FastForwardGen(filter_range);
338 }
339 return filter_range;
340 }
341 KeyType AdvanceGen() {
342 ++(*gen_);
343 auto gen_range = GenRange();
344 if (gen_range.valid()) {
345 FastForwardFilter(gen_range);
346 }
347 return gen_range;
348 }
349
350 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
351 KeyType GenRange() const { return *(*gen_); }
352
353 KeyType FastForwardFilter(const KeyType &range) {
354 auto filter_range = FilterRange();
355 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700356 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700357 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
358 if (retry_count < kRetryLimit) {
359 ++filter_pos_;
360 filter_range = FilterRange();
361 retry_count++;
362 } else {
363 // Okay we've tried walking, do a seek.
364 filter_pos_ = filter_->lower_bound(range);
365 break;
366 }
367 }
368 return FilterRange();
369 }
370
371 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
372 // faster.
373 KeyType FastForwardGen(const KeyType &range) {
374 auto gen_range = GenRange();
375 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
376 ++(*gen_);
377 gen_range = GenRange();
378 }
379 return gen_range;
380 }
381
382 void SeekBegin() {
383 auto gen_range = GenRange();
384 if (gen_range.empty()) {
385 current_ = KeyType();
386 filter_pos_ = filter_->cend();
387 } else {
388 filter_pos_ = filter_->lower_bound(gen_range);
389 current_ = gen_range & FilterRange();
390 }
391 }
392
393 FilteredGeneratorGenerator() = default;
394 const FilterMap *filter_;
395 RangeGen *const gen_;
396 typename FilterMap::const_iterator filter_pos_;
397 KeyType current_;
398};
399
400using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
401
John Zulauf0cb5be22020-01-23 12:18:22 -0700402// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
403VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
404 VkPipelineStageFlags expanded = stage_mask;
405 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
406 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
407 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
408 if (all_commands.first & queue_flags) {
409 expanded |= all_commands.second;
410 }
411 }
412 }
413 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
414 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
415 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
416 }
417 return expanded;
418}
419
John Zulauf36bcf6a2020-02-03 15:12:52 -0700420VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
Jeremy Gebben91c36902020-11-09 08:17:08 -0700421 const std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
John Zulauf36bcf6a2020-02-03 15:12:52 -0700422 VkPipelineStageFlags unscanned = stage_mask;
423 VkPipelineStageFlags related = 0;
Jonah Ryan-Davis185189c2020-07-14 10:28:52 -0400424 for (const auto &entry : map) {
425 const auto &stage = entry.first;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700426 if (stage & unscanned) {
427 related = related | entry.second;
428 unscanned = unscanned & ~stage;
429 if (!unscanned) break;
430 }
431 }
432 return related;
433}
434
435VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
436 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
437}
438
439VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
440 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
441}
442
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700443static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700444
John Zulauf3e86bf02020-09-12 10:47:57 -0600445ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
446 VkDeviceSize stride) {
447 VkDeviceSize range_start = offset + first_index * stride;
448 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600449 if (count == UINT32_MAX) {
450 range_size = buf_whole_size - range_start;
451 } else {
452 range_size = count * stride;
453 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600454 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600455}
456
locke-lunarg654e3692020-06-04 17:19:15 -0600457SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
458 VkShaderStageFlagBits stage_flag) {
459 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
460 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
461 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
462 }
463 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
464 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
465 assert(0);
466 }
467 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
468 return stage_access->second.uniform_read;
469 }
470
471 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
472 // Because if write hazard happens, read hazard might or might not happen.
473 // But if write hazard doesn't happen, read hazard is impossible to happen.
474 if (descriptor_data.is_writable) {
475 return stage_access->second.shader_write;
476 }
477 return stage_access->second.shader_read;
478}
479
locke-lunarg37047832020-06-12 13:44:45 -0600480bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
481 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
482 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
483 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
484 ? true
485 : false;
486}
487
488bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
489 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
490 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
491 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
492 ? true
493 : false;
494}
495
John Zulauf355e49b2020-04-24 15:11:15 -0600496// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600497template <typename Action>
498static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
499 Action &action) {
500 // At this point the "apply over range" logic only supports a single memory binding
501 if (!SimpleBinding(image_state)) return;
502 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600503 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700504 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
505 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600506 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700507 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600508 }
509}
510
John Zulauf7635de32020-05-29 17:14:15 -0600511// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
512// Used by both validation and record operations
513//
514// The signature for Action() reflect the needs of both uses.
515template <typename Action>
516void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
517 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
518 VkExtent3D extent = CastTo3D(render_area.extent);
519 VkOffset3D offset = CastTo3D(render_area.offset);
520 const auto &rp_ci = rp_state.createInfo;
521 const auto *attachment_ci = rp_ci.pAttachments;
522 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
523
524 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
525 const auto *color_attachments = subpass_ci.pColorAttachments;
526 const auto *color_resolve = subpass_ci.pResolveAttachments;
527 if (color_resolve && color_attachments) {
528 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
529 const auto &color_attach = color_attachments[i].attachment;
530 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
531 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
532 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
533 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
534 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
535 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
536 }
537 }
538 }
539
540 // Depth stencil resolve only if the extension is present
541 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
542 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
543 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
544 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
545 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
546 const auto src_ci = attachment_ci[src_at];
547 // The formats are required to match so we can pick either
548 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
549 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
550 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
551 VkImageAspectFlags aspect_mask = 0u;
552
553 // Figure out which aspects are actually touched during resolve operations
554 const char *aspect_string = nullptr;
555 if (resolve_depth && resolve_stencil) {
556 // Validate all aspects together
557 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
558 aspect_string = "depth/stencil";
559 } else if (resolve_depth) {
560 // Validate depth only
561 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
562 aspect_string = "depth";
563 } else if (resolve_stencil) {
564 // Validate all stencil only
565 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
566 aspect_string = "stencil";
567 }
568
569 if (aspect_mask) {
570 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
Jeremy Gebbenec5cd382020-11-16 15:53:45 -0700571 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kAttachmentRasterOrder, offset, extent,
John Zulauf7635de32020-05-29 17:14:15 -0600572 aspect_mask);
573 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
574 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
575 }
576 }
577}
578
579// Action for validating resolve operations
580class ValidateResolveAction {
581 public:
582 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
583 const char *func_name)
584 : render_pass_(render_pass),
585 subpass_(subpass),
586 context_(context),
587 sync_state_(sync_state),
588 func_name_(func_name),
589 skip_(false) {}
590 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
591 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
592 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
593 HazardResult hazard;
594 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
595 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600596 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
597 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600598 " to resolve attachment %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -0600599 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
John Zulauf37ceaed2020-07-03 16:18:15 -0600600 src_at, dst_at, string_UsageTag(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600601 }
602 }
603 // Providing a mechanism for the constructing caller to get the result of the validation
604 bool GetSkip() const { return skip_; }
605
606 private:
607 VkRenderPass render_pass_;
608 const uint32_t subpass_;
609 const AccessContext &context_;
610 const SyncValidator &sync_state_;
611 const char *func_name_;
612 bool skip_;
613};
614
615// Update action for resolve operations
616class UpdateStateResolveAction {
617 public:
618 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
619 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
620 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
621 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
622 // Ignores validation only arguments...
623 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
624 }
625
626 private:
627 AccessContext &context_;
628 const ResourceUsageTag &tag_;
629};
630
John Zulauf59e25072020-07-17 10:55:21 -0600631void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700632 const SyncStageAccessFlags &prior_, const ResourceUsageTag &tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600633 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
634 usage_index = usage_index_;
635 hazard = hazard_;
636 prior_access = prior_;
637 tag = tag_;
638}
639
John Zulauf540266b2020-04-06 18:54:53 -0600640AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
641 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600642 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600643 Reset();
644 const auto &subpass_dep = dependencies[subpass];
645 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600646 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600647 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600648 const auto prev_pass = prev_dep.first->pass;
649 const auto &prev_barriers = prev_dep.second;
650 assert(prev_dep.second.size());
651 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
652 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700653 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600654
655 async_.reserve(subpass_dep.async.size());
656 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700657 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600658 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600659 if (subpass_dep.barrier_from_external.size()) {
660 src_external_ = TrackBack(external_context, queue_flags, subpass_dep.barrier_from_external);
John Zulaufe5da6e52020-03-18 15:32:18 -0600661 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600662 if (subpass_dep.barrier_to_external.size()) {
663 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600664 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700665}
666
John Zulauf5f13a792020-03-10 07:31:21 -0600667template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700668HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600669 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600670 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600671 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600672
673 HazardResult hazard;
674 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
675 hazard = detector.Detect(prev);
676 }
677 return hazard;
678}
679
John Zulauf4a6105a2020-11-17 15:11:05 -0700680template <typename Action>
681void AccessContext::ForAll(Action &&action) {
682 for (const auto address_type : kAddressTypes) {
683 auto &accesses = GetAccessStateMap(address_type);
684 for (const auto &access : accesses) {
685 action(address_type, access);
686 }
687 }
688}
689
John Zulauf3d84f1b2020-03-09 13:33:25 -0600690// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
691// the DAG of the contexts (for example subpasses)
692template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700693HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600694 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600695 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600696
John Zulauf1a224292020-06-30 14:52:13 -0600697 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600698 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
699 // so we'll check these first
700 for (const auto &async_context : async_) {
701 hazard = async_context->DetectAsyncHazard(type, detector, range);
702 if (hazard.hazard) return hazard;
703 }
John Zulauf5f13a792020-03-10 07:31:21 -0600704 }
705
John Zulauf1a224292020-06-30 14:52:13 -0600706 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600707
John Zulauf69133422020-05-20 14:55:53 -0600708 const auto &accesses = GetAccessStateMap(type);
709 const auto from = accesses.lower_bound(range);
710 const auto to = accesses.upper_bound(range);
711 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600712
John Zulauf69133422020-05-20 14:55:53 -0600713 for (auto pos = from; pos != to; ++pos) {
714 // Cover any leading gap, or gap between entries
715 if (detect_prev) {
716 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
717 // Cover any leading gap, or gap between entries
718 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600719 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600720 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600721 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600722 if (hazard.hazard) return hazard;
723 }
John Zulauf69133422020-05-20 14:55:53 -0600724 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
725 gap.begin = pos->first.end;
726 }
727
728 hazard = detector.Detect(pos);
729 if (hazard.hazard) return hazard;
730 }
731
732 if (detect_prev) {
733 // Detect in the trailing empty as needed
734 gap.end = range.end;
735 if (gap.non_empty()) {
736 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600737 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600738 }
739
740 return hazard;
741}
742
743// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
744template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700745HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
746 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600747 auto &accesses = GetAccessStateMap(type);
748 const auto from = accesses.lower_bound(range);
749 const auto to = accesses.upper_bound(range);
750
John Zulauf3d84f1b2020-03-09 13:33:25 -0600751 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600752 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700753 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600754 }
John Zulauf16adfc92020-04-08 10:28:33 -0600755
John Zulauf3d84f1b2020-03-09 13:33:25 -0600756 return hazard;
757}
758
John Zulaufb02c1eb2020-10-06 16:33:36 -0600759struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700760 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600761 void operator()(ResourceAccessState *access) const {
762 assert(access);
763 access->ApplyBarriers(barriers, true);
764 }
765 const std::vector<SyncBarrier> &barriers;
766};
767
768struct ApplyTrackbackBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700769 explicit ApplyTrackbackBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600770 void operator()(ResourceAccessState *access) const {
771 assert(access);
772 assert(!access->HasPendingState());
773 access->ApplyBarriers(barriers, false);
774 access->ApplyPendingBarriers(kCurrentCommandTag);
775 }
776 const std::vector<SyncBarrier> &barriers;
777};
778
779// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
780// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
781// *different* map from dest.
782// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
783// range [first, last)
784template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600785static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
786 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600787 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600788 auto at = entry;
789 for (auto pos = first; pos != last; ++pos) {
790 // Every member of the input iterator range must fit within the remaining portion of entry
791 assert(at->first.includes(pos->first));
792 assert(at != dest->end());
793 // Trim up at to the same size as the entry to resolve
794 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600795 auto access = pos->second; // intentional copy
796 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600797 at->second.Resolve(access);
798 ++at; // Go to the remaining unused section of entry
799 }
800}
801
John Zulaufa0a98292020-09-18 09:30:10 -0600802static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
803 SyncBarrier merged = {};
804 for (const auto &barrier : barriers) {
805 merged.Merge(barrier);
806 }
807 return merged;
808}
809
John Zulaufb02c1eb2020-10-06 16:33:36 -0600810template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700811void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600812 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
813 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600814 if (!range.non_empty()) return;
815
John Zulauf355e49b2020-04-24 15:11:15 -0600816 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
817 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600818 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600819 if (current->pos_B->valid) {
820 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600821 auto access = src_pos->second; // intentional copy
822 barrier_action(&access);
823
John Zulauf16adfc92020-04-08 10:28:33 -0600824 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600825 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
826 trimmed->second.Resolve(access);
827 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600828 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600829 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600830 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600831 }
John Zulauf16adfc92020-04-08 10:28:33 -0600832 } else {
833 // we have to descend to fill this gap
834 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600835 if (current->pos_A->valid) {
836 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
837 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600838 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600839 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -0600840 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600841 // There isn't anything in dest in current)range, so we can accumulate directly into it.
842 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600843 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
844 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
845 barrier_action(&pos->second);
John Zulauf355e49b2020-04-24 15:11:15 -0600846 }
847 }
848 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
849 // iterator of the outer while.
850
851 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
852 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
853 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600854 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
855 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600856 current.seek(seek_to);
857 } else if (!current->pos_A->valid && infill_state) {
858 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
859 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
860 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600861 }
John Zulauf5f13a792020-03-10 07:31:21 -0600862 }
John Zulauf16adfc92020-04-08 10:28:33 -0600863 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600864 }
John Zulauf1a224292020-06-30 14:52:13 -0600865
866 // Infill if range goes passed both the current and resolve map prior contents
867 if (recur_to_infill && (current->range.end < range.end)) {
868 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
869 ResourceAccessRangeMap gap_map;
870 const auto the_end = resolve_map->end();
871 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
872 for (auto &access : gap_map) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600873 barrier_action(&access.second);
John Zulauf1a224292020-06-30 14:52:13 -0600874 resolve_map->insert(the_end, access);
875 }
876 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600877}
878
John Zulauf43cc7462020-12-03 12:33:12 -0700879void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
880 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600881 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600882 if (range.non_empty() && infill_state) {
883 descent_map->insert(std::make_pair(range, *infill_state));
884 }
885 } else {
886 // Look for something to fill the gap further along.
887 for (const auto &prev_dep : prev_) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600888 const ApplyTrackbackBarriersAction barrier_action(prev_dep.barriers);
889 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600890 }
891
John Zulaufe5da6e52020-03-18 15:32:18 -0600892 if (src_external_.context) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600893 const ApplyTrackbackBarriersAction barrier_action(src_external_.barriers);
894 src_external_.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600895 }
896 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600897}
898
John Zulauf4a6105a2020-11-17 15:11:05 -0700899// Non-lazy import of all accesses, WaitEvents needs this.
900void AccessContext::ResolvePreviousAccesses() {
901 ResourceAccessState default_state;
902 for (const auto address_type : kAddressTypes) {
903 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
904 }
905}
906
John Zulauf43cc7462020-12-03 12:33:12 -0700907AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
908 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -0600909}
910
John Zulauf1507ee42020-05-18 11:33:09 -0600911static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
912 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
913 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
914 return stage_access;
915}
916static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
917 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
918 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
919 return stage_access;
920}
921
John Zulauf7635de32020-05-29 17:14:15 -0600922// Caller must manage returned pointer
923static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
924 uint32_t subpass, const VkRect2D &render_area,
925 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
926 auto *proxy = new AccessContext(context);
927 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600928 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600929 return proxy;
930}
931
John Zulaufb02c1eb2020-10-06 16:33:36 -0600932template <typename BarrierAction>
John Zulauf52446eb2020-10-22 16:40:08 -0600933class ResolveAccessRangeFunctor {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600934 public:
John Zulauf43cc7462020-12-03 12:33:12 -0700935 ResolveAccessRangeFunctor(const AccessContext &context, AccessAddressType address_type, ResourceAccessRangeMap *descent_map,
936 const ResourceAccessState *infill_state, BarrierAction &barrier_action)
John Zulauf52446eb2020-10-22 16:40:08 -0600937 : context_(context),
938 address_type_(address_type),
939 descent_map_(descent_map),
940 infill_state_(infill_state),
941 barrier_action_(barrier_action) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600942 ResolveAccessRangeFunctor() = delete;
943 void operator()(const ResourceAccessRange &range) const {
944 context_.ResolveAccessRange(address_type_, range, barrier_action_, descent_map_, infill_state_);
945 }
946
947 private:
John Zulauf52446eb2020-10-22 16:40:08 -0600948 const AccessContext &context_;
John Zulauf43cc7462020-12-03 12:33:12 -0700949 const AccessAddressType address_type_;
John Zulauf52446eb2020-10-22 16:40:08 -0600950 ResourceAccessRangeMap *const descent_map_;
951 const ResourceAccessState *infill_state_;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600952 BarrierAction &barrier_action_;
953};
954
John Zulaufb02c1eb2020-10-06 16:33:36 -0600955template <typename BarrierAction>
956void AccessContext::ResolveAccessRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -0700957 BarrierAction &barrier_action, AccessAddressType address_type,
958 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600959 const ResolveAccessRangeFunctor<BarrierAction> action(*this, address_type, descent_map, infill_state, barrier_action);
960 ApplyOverImageRange(image_state, subresource_range, action);
John Zulauf62f10592020-04-03 12:20:02 -0600961}
962
John Zulauf7635de32020-05-29 17:14:15 -0600963// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600964bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600965 const VkRect2D &render_area, uint32_t subpass,
966 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
967 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600968 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600969 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
970 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
971 // those affects have not been recorded yet.
972 //
973 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
974 // to apply and only copy then, if this proves a hot spot.
975 std::unique_ptr<AccessContext> proxy_for_prev;
976 TrackBack proxy_track_back;
977
John Zulauf355e49b2020-04-24 15:11:15 -0600978 const auto &transitions = rp_state.subpass_transitions[subpass];
979 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600980 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
981
982 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
983 if (prev_needs_proxy) {
984 if (!proxy_for_prev) {
985 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
986 render_area, attachment_views));
987 proxy_track_back = *track_back;
988 proxy_track_back.context = proxy_for_prev.get();
989 }
990 track_back = &proxy_track_back;
991 }
992 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600993 if (hazard.hazard) {
John Zulauf389c34b2020-07-28 11:19:35 -0600994 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
995 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
996 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
997 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
998 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
999 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001000 }
1001 }
1002 return skip;
1003}
1004
John Zulauf1507ee42020-05-18 11:33:09 -06001005bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001006 const VkRect2D &render_area, uint32_t subpass,
1007 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1008 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001009 bool skip = false;
1010 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1011 VkExtent3D extent = CastTo3D(render_area.extent);
1012 VkOffset3D offset = CastTo3D(render_area.offset);
John Zulaufa0a98292020-09-18 09:30:10 -06001013
John Zulauf1507ee42020-05-18 11:33:09 -06001014 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1015 if (subpass == rp_state.attachment_first_subpass[i]) {
1016 if (attachment_views[i] == nullptr) continue;
1017 const IMAGE_VIEW_STATE &view = *attachment_views[i];
1018 const IMAGE_STATE *image = view.image_state.get();
1019 if (image == nullptr) continue;
1020 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001021
1022 // Need check in the following way
1023 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1024 // vs. transition
1025 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1026 // for each aspect loaded.
1027
1028 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001029 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001030 const bool is_color = !(has_depth || has_stencil);
1031
1032 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001033 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001034
John Zulaufaff20662020-06-01 14:07:58 -06001035 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001036 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001037
John Zulaufb02c1eb2020-10-06 16:33:36 -06001038 auto hazard_range = view.normalized_subresource_range;
1039 bool checked_stencil = false;
1040 if (is_color) {
John Zulauf859089b2020-10-29 17:37:03 -06001041 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, kColorAttachmentRasterOrder, offset,
1042 extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001043 aspect = "color";
1044 } else {
1045 if (has_depth) {
1046 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf859089b2020-10-29 17:37:03 -06001047 hazard = DetectHazard(*image, load_index, hazard_range, kDepthStencilAttachmentRasterOrder, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001048 aspect = "depth";
1049 }
1050 if (!hazard.hazard && has_stencil) {
1051 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf859089b2020-10-29 17:37:03 -06001052 hazard =
1053 DetectHazard(*image, stencil_load_index, hazard_range, kDepthStencilAttachmentRasterOrder, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001054 aspect = "stencil";
1055 checked_stencil = true;
1056 }
1057 }
1058
1059 if (hazard.hazard) {
1060 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
1061 if (hazard.tag == kCurrentCommandTag) {
1062 // Hazard vs. ILT
1063 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1064 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1065 " aspect %s during load with loadOp %s.",
1066 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1067 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06001068 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1069 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001070 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001071 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -06001072 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001073 }
1074 }
1075 }
1076 }
1077 return skip;
1078}
1079
John Zulaufaff20662020-06-01 14:07:58 -06001080// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1081// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1082// store is part of the same Next/End operation.
1083// The latter is handled in layout transistion validation directly
1084bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
1085 const VkRect2D &render_area, uint32_t subpass,
1086 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1087 const char *func_name) const {
1088 bool skip = false;
1089 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1090 VkExtent3D extent = CastTo3D(render_area.extent);
1091 VkOffset3D offset = CastTo3D(render_area.offset);
1092
1093 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1094 if (subpass == rp_state.attachment_last_subpass[i]) {
1095 if (attachment_views[i] == nullptr) continue;
1096 const IMAGE_VIEW_STATE &view = *attachment_views[i];
1097 const IMAGE_STATE *image = view.image_state.get();
1098 if (image == nullptr) continue;
1099 const auto &ci = attachment_ci[i];
1100
1101 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1102 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1103 // sake, we treat DONT_CARE as writing.
1104 const bool has_depth = FormatHasDepth(ci.format);
1105 const bool has_stencil = FormatHasStencil(ci.format);
1106 const bool is_color = !(has_depth || has_stencil);
1107 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1108 if (!has_stencil && !store_op_stores) continue;
1109
1110 HazardResult hazard;
1111 const char *aspect = nullptr;
1112 bool checked_stencil = false;
1113 if (is_color) {
1114 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1115 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
1116 aspect = "color";
1117 } else {
1118 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1119 auto hazard_range = view.normalized_subresource_range;
1120 if (has_depth && store_op_stores) {
1121 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1122 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
1123 kAttachmentRasterOrder, offset, extent);
1124 aspect = "depth";
1125 }
1126 if (!hazard.hazard && has_stencil && stencil_op_stores) {
1127 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1128 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
1129 kAttachmentRasterOrder, offset, extent);
1130 aspect = "stencil";
1131 checked_stencil = true;
1132 }
1133 }
1134
1135 if (hazard.hazard) {
1136 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1137 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -06001138 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1139 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001140 " %s aspect during store with %s %s. Access info %s",
John Zulauf1dae9192020-06-16 15:46:44 -06001141 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -06001142 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001143 }
1144 }
1145 }
1146 return skip;
1147}
1148
John Zulaufb027cdb2020-05-21 14:25:22 -06001149bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
1150 const VkRect2D &render_area,
1151 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
1152 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -06001153 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
1154 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
1155 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001156}
1157
John Zulauf3d84f1b2020-03-09 13:33:25 -06001158class HazardDetector {
1159 SyncStageAccessIndex usage_index_;
1160
1161 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001162 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001163 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1164 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001165 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001166 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001167};
1168
John Zulauf69133422020-05-20 14:55:53 -06001169class HazardDetectorWithOrdering {
1170 const SyncStageAccessIndex usage_index_;
1171 const SyncOrderingBarrier &ordering_;
1172
1173 public:
1174 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1175 return pos->second.DetectHazard(usage_index_, ordering_);
1176 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001177 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1178 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001179 }
1180 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
1181 : usage_index_(usage), ordering_(ordering) {}
1182};
1183
John Zulauf16adfc92020-04-08 10:28:33 -06001184HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001185 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001186 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001187 const auto base_address = ResourceBaseAddress(buffer);
1188 HazardDetector detector(usage_index);
1189 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001190}
1191
John Zulauf69133422020-05-20 14:55:53 -06001192template <typename Detector>
1193HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1194 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1195 const VkExtent3D &extent, DetectOptions options) const {
1196 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001197 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001198 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1199 base_address);
1200 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001201 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001202 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001203 if (hazard.hazard) return hazard;
1204 }
1205 return HazardResult();
1206}
1207
John Zulauf540266b2020-04-06 18:54:53 -06001208HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1209 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1210 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001211 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1212 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -06001213 return DetectHazard(image, current_usage, subresource_range, offset, extent);
1214}
1215
1216HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1217 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1218 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001219 HazardDetector detector(current_usage);
1220 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1221}
1222
1223HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1224 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
1225 const VkOffset3D &offset, const VkExtent3D &extent) const {
1226 HazardDetectorWithOrdering detector(current_usage, ordering);
1227 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001228}
1229
John Zulaufb027cdb2020-05-21 14:25:22 -06001230// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1231// should have reported the issue regarding an invalid attachment entry
1232HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
1233 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
1234 VkImageAspectFlags aspect_mask) const {
1235 if (view != nullptr) {
1236 const IMAGE_STATE *image = view->image_state.get();
1237 if (image != nullptr) {
1238 auto *detect_range = &view->normalized_subresource_range;
1239 VkImageSubresourceRange masked_range;
1240 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1241 masked_range = view->normalized_subresource_range;
1242 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1243 detect_range = &masked_range;
1244 }
1245
1246 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1247 if (detect_range->aspectMask) {
1248 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
1249 }
1250 }
1251 }
1252 return HazardResult();
1253}
John Zulauf43cc7462020-12-03 12:33:12 -07001254
John Zulauf3d84f1b2020-03-09 13:33:25 -06001255class BarrierHazardDetector {
1256 public:
1257 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1258 SyncStageAccessFlags src_access_scope)
1259 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1260
John Zulauf5f13a792020-03-10 07:31:21 -06001261 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1262 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001263 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001264 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001265 // 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 -07001266 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001267 }
1268
1269 private:
1270 SyncStageAccessIndex usage_index_;
1271 VkPipelineStageFlags src_exec_scope_;
1272 SyncStageAccessFlags src_access_scope_;
1273};
1274
John Zulauf4a6105a2020-11-17 15:11:05 -07001275class EventBarrierHazardDetector {
1276 public:
1277 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1278 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
1279 const ResourceUsageTag &scope_tag)
1280 : usage_index_(usage_index),
1281 src_exec_scope_(src_exec_scope),
1282 src_access_scope_(src_access_scope),
1283 event_scope_(event_scope),
1284 scope_pos_(event_scope.cbegin()),
1285 scope_end_(event_scope.cend()),
1286 scope_tag_(scope_tag) {}
1287
1288 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1289 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1290 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1291 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1292 if (scope_pos_ == scope_end_) return HazardResult();
1293 if (!scope_pos_->first.intersects(pos->first)) {
1294 event_scope_.lower_bound(pos->first);
1295 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1296 }
1297
1298 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1299 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1300 }
1301 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1302 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1303 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1304 }
1305
1306 private:
1307 SyncStageAccessIndex usage_index_;
1308 VkPipelineStageFlags src_exec_scope_;
1309 SyncStageAccessFlags src_access_scope_;
1310 const SyncEventState::ScopeMap &event_scope_;
1311 SyncEventState::ScopeMap::const_iterator scope_pos_;
1312 SyncEventState::ScopeMap::const_iterator scope_end_;
1313 const ResourceUsageTag &scope_tag_;
1314};
1315
1316HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1317 const SyncStageAccessFlags &src_access_scope,
1318 const VkImageSubresourceRange &subresource_range,
1319 const SyncEventState &sync_event, DetectOptions options) const {
1320 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1321 // first access scope map to use, and there's no easy way to plumb it in below.
1322 const auto address_type = ImageAddressType(image);
1323 const auto &event_scope = sync_event.FirstScope(address_type);
1324
1325 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1326 event_scope, sync_event.first_scope_tag);
1327 VkOffset3D zero_offset = {0, 0, 0};
1328 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
1329}
1330
John Zulauf16adfc92020-04-08 10:28:33 -06001331HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001332 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001333 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001334 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001335 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1336 VkOffset3D zero_offset = {0, 0, 0};
1337 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001338}
1339
John Zulauf355e49b2020-04-24 15:11:15 -06001340HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001341 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001342 const VkImageMemoryBarrier &barrier) const {
1343 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1344 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1345 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1346}
1347
John Zulauf9cb530d2019-09-30 14:14:10 -06001348template <typename Flags, typename Map>
1349SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1350 SyncStageAccessFlags scope = 0;
1351 for (const auto &bit_scope : map) {
1352 if (flag_mask < bit_scope.first) break;
1353
1354 if (flag_mask & bit_scope.first) {
1355 scope |= bit_scope.second;
1356 }
1357 }
1358 return scope;
1359}
1360
1361SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1362 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1363}
1364
1365SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1366 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1367}
1368
1369// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1370SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001371 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1372 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1373 // 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 -06001374 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1375}
1376
1377template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001378void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001379 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1380 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001381 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001382 auto pos = accesses->lower_bound(range);
1383 if (pos == accesses->end() || !pos->first.intersects(range)) {
1384 // The range is empty, fill it with a default value.
1385 pos = action.Infill(accesses, pos, range);
1386 } else if (range.begin < pos->first.begin) {
1387 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001388 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001389 } else if (pos->first.begin < range.begin) {
1390 // Trim the beginning if needed
1391 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1392 ++pos;
1393 }
1394
1395 const auto the_end = accesses->end();
1396 while ((pos != the_end) && pos->first.intersects(range)) {
1397 if (pos->first.end > range.end) {
1398 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1399 }
1400
1401 pos = action(accesses, pos);
1402 if (pos == the_end) break;
1403
1404 auto next = pos;
1405 ++next;
1406 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1407 // Need to infill if next is disjoint
1408 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001409 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001410 next = action.Infill(accesses, next, new_range);
1411 }
1412 pos = next;
1413 }
1414}
John Zulauf4a6105a2020-11-17 15:11:05 -07001415template <typename Action, typename RangeGen>
1416void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1417 assert(range_gen_arg);
1418 auto &range_gen = *range_gen_arg; // Non-const references must be * by style requirement but deref-ing * iterator is a pain
1419 for (; range_gen->non_empty(); ++range_gen) {
1420 UpdateMemoryAccessState(accesses, *range_gen, action);
1421 }
1422}
John Zulauf9cb530d2019-09-30 14:14:10 -06001423
1424struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001425 using Iterator = ResourceAccessRangeMap::iterator;
1426 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001427 // this is only called on gaps, and never returns a gap.
1428 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001429 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001430 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001431 }
John Zulauf5f13a792020-03-10 07:31:21 -06001432
John Zulauf5c5e88d2019-12-26 11:22:02 -07001433 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001434 auto &access_state = pos->second;
1435 access_state.Update(usage, tag);
1436 return pos;
1437 }
1438
John Zulauf43cc7462020-12-03 12:33:12 -07001439 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001440 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001441 : type(type_), context(context_), usage(usage_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001442 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001443 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001444 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001445 const ResourceUsageTag &tag;
1446};
1447
John Zulauf4a6105a2020-11-17 15:11:05 -07001448// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001449struct PipelineBarrierOp {
1450 SyncBarrier barrier;
1451 bool layout_transition;
1452 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1453 : barrier(barrier_), layout_transition(layout_transition_) {}
1454 PipelineBarrierOp() = default;
1455 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1456};
John Zulauf4a6105a2020-11-17 15:11:05 -07001457// The barrier operation for wait events
1458struct WaitEventBarrierOp {
1459 const ResourceUsageTag *scope_tag;
1460 SyncBarrier barrier;
1461 bool layout_transition;
1462 WaitEventBarrierOp(const ResourceUsageTag &scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1463 : scope_tag(&scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
1464 WaitEventBarrierOp() = default;
1465 void operator()(ResourceAccessState *access_state) const {
1466 assert(scope_tag); // Not valid to have a non-scope op executed, default construct included for std::vector support
1467 access_state->ApplyBarrier(*scope_tag, barrier, layout_transition);
1468 }
1469};
John Zulauf1e331ec2020-12-04 18:29:38 -07001470
John Zulauf4a6105a2020-11-17 15:11:05 -07001471// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1472// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1473// of a collection is known/present.
John Zulauf1e331ec2020-12-04 18:29:38 -07001474template <typename BarrierOp>
John Zulauf89311b42020-09-29 16:28:47 -06001475class ApplyBarrierOpsFunctor {
1476 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001477 using Iterator = ResourceAccessRangeMap::iterator;
1478 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001479
John Zulauf5c5e88d2019-12-26 11:22:02 -07001480 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001481 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001482 for (const auto &op : barrier_ops_) {
1483 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001484 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001485
John Zulauf89311b42020-09-29 16:28:47 -06001486 if (resolve_) {
1487 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1488 // another walk
1489 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001490 }
1491 return pos;
1492 }
1493
John Zulauf89311b42020-09-29 16:28:47 -06001494 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulauf1e331ec2020-12-04 18:29:38 -07001495 ApplyBarrierOpsFunctor(bool resolve, const std::vector<BarrierOp> &barrier_ops, const ResourceUsageTag &tag)
1496 : resolve_(resolve), barrier_ops_(barrier_ops), tag_(tag) {}
John Zulauf89311b42020-09-29 16:28:47 -06001497
1498 private:
1499 bool resolve_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001500 const std::vector<BarrierOp> &barrier_ops_;
1501 const ResourceUsageTag &tag_;
1502};
1503
John Zulauf4a6105a2020-11-17 15:11:05 -07001504// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1505// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1506template <typename BarrierOp>
1507class ApplyBarrierFunctor {
1508 public:
1509 using Iterator = ResourceAccessRangeMap::iterator;
1510 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1511
1512 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1513 auto &access_state = pos->second;
1514 barrier_op_(&access_state);
1515 return pos;
1516 }
1517
1518 ApplyBarrierFunctor(const BarrierOp &barrier_op) : barrier_op_(barrier_op) {}
1519
1520 private:
1521 const BarrierOp barrier_op_;
1522};
1523
John Zulauf1e331ec2020-12-04 18:29:38 -07001524// This functor resolves the pendinging state.
1525class ResolvePendingBarrierFunctor {
1526 public:
1527 using Iterator = ResourceAccessRangeMap::iterator;
1528 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1529
1530 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1531 auto &access_state = pos->second;
1532 access_state.ApplyPendingBarriers(tag_);
1533 return pos;
1534 }
1535
1536 ResolvePendingBarrierFunctor(const ResourceUsageTag &tag) : tag_(tag) {}
1537
1538 private:
John Zulauf89311b42020-09-29 16:28:47 -06001539 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001540};
1541
John Zulauf43cc7462020-12-03 12:33:12 -07001542void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -06001543 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001544 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1545 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001546}
1547
John Zulauf16adfc92020-04-08 10:28:33 -06001548void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001549 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001550 if (!SimpleBinding(buffer)) return;
1551 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf43cc7462020-12-03 12:33:12 -07001552 UpdateAccessState(AccessAddressType::kLinear, current_usage, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001553}
John Zulauf355e49b2020-04-24 15:11:15 -06001554
John Zulauf540266b2020-04-06 18:54:53 -06001555void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001556 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001557 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001558 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001559 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001560 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1561 base_address);
1562 const auto address_type = ImageAddressType(image);
John Zulauf16adfc92020-04-08 10:28:33 -06001563 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001564 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001565 UpdateMemoryAccessState(&GetAccessStateMap(address_type), *range_gen, action);
John Zulauf5f13a792020-03-10 07:31:21 -06001566 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001567}
John Zulauf7635de32020-05-29 17:14:15 -06001568void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1569 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1570 if (view != nullptr) {
1571 const IMAGE_STATE *image = view->image_state.get();
1572 if (image != nullptr) {
1573 auto *update_range = &view->normalized_subresource_range;
1574 VkImageSubresourceRange masked_range;
1575 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1576 masked_range = view->normalized_subresource_range;
1577 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1578 update_range = &masked_range;
1579 }
1580 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1581 }
1582 }
1583}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001584
John Zulauf355e49b2020-04-24 15:11:15 -06001585void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1586 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1587 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001588 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1589 subresource.layerCount};
1590 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1591}
1592
John Zulauf540266b2020-04-06 18:54:53 -06001593template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001594void AccessContext::UpdateResourceAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001595 if (!SimpleBinding(buffer)) return;
1596 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf43cc7462020-12-03 12:33:12 -07001597 UpdateMemoryAccessState(&GetAccessStateMap(AccessAddressType::kLinear), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001598}
1599
1600template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001601void AccessContext::UpdateResourceAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1602 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001603 if (!SimpleBinding(image)) return;
1604 const auto address_type = ImageAddressType(image);
1605 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001606
John Zulauf16adfc92020-04-08 10:28:33 -06001607 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001608 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
1609 image.createInfo.extent, base_address);
1610
John Zulauf540266b2020-04-06 18:54:53 -06001611 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001612 UpdateMemoryAccessState(accesses, *range_gen, action);
John Zulauf540266b2020-04-06 18:54:53 -06001613 }
1614}
1615
John Zulauf7635de32020-05-29 17:14:15 -06001616void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1617 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1618 const ResourceUsageTag &tag) {
1619 UpdateStateResolveAction update(*this, tag);
1620 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1621}
1622
John Zulaufaff20662020-06-01 14:07:58 -06001623void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1624 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1625 const ResourceUsageTag &tag) {
1626 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1627 VkExtent3D extent = CastTo3D(render_area.extent);
1628 VkOffset3D offset = CastTo3D(render_area.offset);
1629
1630 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1631 if (rp_state.attachment_last_subpass[i] == subpass) {
1632 if (attachment_views[i] == nullptr) continue; // UNUSED
1633 const auto &view = *attachment_views[i];
1634 const IMAGE_STATE *image = view.image_state.get();
1635 if (image == nullptr) continue;
1636
1637 const auto &ci = attachment_ci[i];
1638 const bool has_depth = FormatHasDepth(ci.format);
1639 const bool has_stencil = FormatHasStencil(ci.format);
1640 const bool is_color = !(has_depth || has_stencil);
1641 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1642
1643 if (is_color && store_op_stores) {
1644 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1645 offset, extent, tag);
1646 } else {
1647 auto update_range = view.normalized_subresource_range;
1648 if (has_depth && store_op_stores) {
1649 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1650 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1651 tag);
1652 }
1653 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1654 if (has_stencil && stencil_op_stores) {
1655 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1656 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1657 tag);
1658 }
1659 }
1660 }
1661 }
1662}
1663
John Zulauf540266b2020-04-06 18:54:53 -06001664template <typename Action>
1665void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1666 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001667 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001668 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001669 }
1670}
1671
1672void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001673 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1674 auto &context = contexts[subpass_index];
John Zulaufb02c1eb2020-10-06 16:33:36 -06001675 ApplyTrackbackBarriersAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001676 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001677 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001678 }
1679 }
1680}
1681
John Zulauf355e49b2020-04-24 15:11:15 -06001682// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001683HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001684 if (!attach_view) return HazardResult();
1685 const auto image_state = attach_view->image_state.get();
1686 if (!image_state) return HazardResult();
1687
John Zulauf355e49b2020-04-24 15:11:15 -06001688 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001689 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001690
1691 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001692 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1693 const auto merged_barrier = MergeBarriers(track_back.barriers);
1694 HazardResult hazard =
1695 track_back.context->DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
1696 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001697 if (!hazard.hazard) {
1698 // The Async hazard check is against the current context's async set.
John Zulaufa0a98292020-09-18 09:30:10 -06001699 hazard = DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001700 attach_view->normalized_subresource_range, kDetectAsync);
1701 }
John Zulaufa0a98292020-09-18 09:30:10 -06001702
John Zulauf355e49b2020-04-24 15:11:15 -06001703 return hazard;
1704}
1705
John Zulaufb02c1eb2020-10-06 16:33:36 -06001706void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
1707 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1708 const ResourceUsageTag &tag) {
1709 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001710 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001711 for (const auto &transition : transitions) {
1712 const auto prev_pass = transition.prev_pass;
1713 const auto attachment_view = attachment_views[transition.attachment];
1714 if (!attachment_view) continue;
1715 const auto *image = attachment_view->image_state.get();
1716 if (!image) continue;
1717 if (!SimpleBinding(*image)) continue;
1718
1719 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1720 assert(trackback);
1721
1722 // Import the attachments into the current context
1723 const auto *prev_context = trackback->context;
1724 assert(prev_context);
1725 const auto address_type = ImageAddressType(*image);
1726 auto &target_map = GetAccessStateMap(address_type);
1727 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
1728 prev_context->ResolveAccessRange(*image, attachment_view->normalized_subresource_range, barrier_action, address_type,
John Zulauf646cc292020-10-23 09:16:45 -06001729 &target_map, &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001730 }
1731
John Zulauf86356ca2020-10-19 11:46:41 -06001732 // If there were no transitions skip this global map walk
1733 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001734 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulauf86356ca2020-10-19 11:46:41 -06001735 ApplyGlobalBarriers(apply_pending_action);
1736 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001737}
John Zulauf4a6105a2020-11-17 15:11:05 -07001738void CommandBufferAccessContext::ApplyBufferBarriers(const SyncEventState &sync_event, VkPipelineStageFlags dst_exec_scope,
1739 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
1740 const VkBufferMemoryBarrier *barriers) {
1741 const auto &scope_tag = sync_event.first_scope_tag;
1742 auto *access_context = GetCurrentAccessContext();
1743 const auto address_type = AccessAddressType::kLinear;
1744 for (uint32_t index = 0; index < barrier_count; index++) {
1745 auto barrier = barriers[index]; // barrier is a copy
1746 const auto *buffer = sync_state_->Get<BUFFER_STATE>(barrier.buffer);
1747 if (!buffer) continue;
1748 const auto base_address = ResourceBaseAddress(*buffer);
1749 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
1750 const ResourceAccessRange range = MakeRange(barrier) + base_address;
1751 const auto src_access_scope = SyncStageAccess::AccessScope(sync_event.stage_accesses, barrier.srcAccessMask);
1752 const auto dst_access_scope = SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1753 const SyncBarrier sync_barrier(sync_event.exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1754 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
1760void CommandBufferAccessContext::ApplyGlobalBarriers(SyncEventState &sync_event, VkPipelineStageFlags dstStageMask,
1761 VkPipelineStageFlags dst_exec_scope,
1762 const SyncStageAccessFlags &dst_stage_accesses, uint32_t memory_barrier_count,
1763 const VkMemoryBarrier *pMemoryBarriers, const ResourceUsageTag &tag) {
1764 std::vector<WaitEventBarrierOp> barrier_ops;
1765 barrier_ops.reserve(std::min<uint32_t>(memory_barrier_count, 1));
1766 const auto &scope_tag = sync_event.first_scope_tag;
1767 auto *access_context = GetCurrentAccessContext();
1768 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
1769 const auto &barrier = pMemoryBarriers[barrier_index];
1770 SyncBarrier sync_barrier(sync_event.exec_scope,
1771 SyncStageAccess::AccessScope(sync_event.stage_accesses, barrier.srcAccessMask), dst_exec_scope,
1772 SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
1773 barrier_ops.emplace_back(scope_tag, sync_barrier, false);
1774 }
1775 if (0 == memory_barrier_count) {
1776 // If there are no global memory barriers, force an exec barrier
1777 barrier_ops.emplace_back(scope_tag, SyncBarrier(sync_event.exec_scope, 0, dst_exec_scope, 0), false);
1778 }
1779 ApplyBarrierOpsFunctor<WaitEventBarrierOp> barriers_functor(false /* don't resolve */, barrier_ops, tag);
1780 for (const auto address_type : kAddressTypes) {
1781 EventSimpleRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), kFullRange);
1782 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &filtered_range_gen);
1783 }
1784
1785 // Apply the global barrier to the event itself (for race condition tracking)
1786 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
1787 sync_event.barriers = dstStageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1788 sync_event.barriers |= dst_exec_scope;
1789}
1790
1791void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags src_exec_scope,
1792 VkPipelineStageFlags dstStageMask,
1793 VkPipelineStageFlags dst_exec_scope) {
1794 const bool all_commands_bit = 0 != (srcStageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
1795 for (auto &event_pair : event_state_) {
1796 assert(event_pair.second); // Shouldn't be storing empty
1797 auto &sync_event = *event_pair.second;
1798 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
1799 if ((sync_event.barriers & src_exec_scope) || all_commands_bit) {
1800 sync_event.barriers |= dst_exec_scope;
1801 sync_event.barriers |= dstStageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1802 }
1803 }
1804}
1805
1806void CommandBufferAccessContext::ApplyImageBarriers(const SyncEventState &sync_event, VkPipelineStageFlags dst_exec_scope,
1807 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
1808 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
1809 const auto &scope_tag = sync_event.first_scope_tag;
1810 auto *access_context = GetCurrentAccessContext();
1811 for (uint32_t index = 0; index < barrier_count; index++) {
1812 const auto &barrier = barriers[index];
1813 const auto *image = sync_state_->Get<IMAGE_STATE>(barrier.image);
1814 if (!image) continue;
1815 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
1816 bool layout_transition = barrier.oldLayout != barrier.newLayout;
1817 const auto src_access_scope = SyncStageAccess::AccessScope(sync_event.stage_accesses, barrier.srcAccessMask);
1818 const auto dst_access_scope = SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1819 const SyncBarrier sync_barrier(sync_event.exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1820 const ApplyBarrierFunctor<WaitEventBarrierOp> barrier_action({scope_tag, sync_barrier, layout_transition});
1821 const auto base_address = ResourceBaseAddress(*image);
1822 subresource_adapter::ImageRangeGenerator range_gen(*image->fragment_encoder.get(), subresource_range, {0, 0, 0},
1823 image->createInfo.extent, base_address);
1824 const auto address_type = AccessContext::ImageAddressType(*image);
1825 EventImageRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), range_gen);
1826 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barrier_action, &filtered_range_gen);
1827 }
1828}
John Zulaufb02c1eb2020-10-06 16:33:36 -06001829
John Zulauf355e49b2020-04-24 15:11:15 -06001830// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1831bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1832
1833 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001834 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001835 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1836 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06001837
John Zulauf86356ca2020-10-19 11:46:41 -06001838 assert(pRenderPassBegin);
1839 if (nullptr == pRenderPassBegin) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06001840
John Zulauf86356ca2020-10-19 11:46:41 -06001841 const uint32_t subpass = 0;
John Zulauf355e49b2020-04-24 15:11:15 -06001842
John Zulauf86356ca2020-10-19 11:46:41 -06001843 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
1844 // hasn't happened yet)
1845 const std::vector<AccessContext> empty_context_vector;
1846 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1847 const_cast<AccessContext *>(&cb_access_context_));
John Zulauf355e49b2020-04-24 15:11:15 -06001848
John Zulauf86356ca2020-10-19 11:46:41 -06001849 // Create a view list
1850 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1851 assert(fb_state);
1852 if (nullptr == fb_state) return skip;
1853 // NOTE: Must not use COMMAND_BUFFER_STATE variant of this as RecordCmdBeginRenderPass hasn't run and thus
1854 // the activeRenderPass.* fields haven't been set.
1855 const auto views = sync_state_->GetAttachmentViews(*pRenderPassBegin, *fb_state);
1856
1857 // Validate transitions
1858 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
1859
1860 // Validate load operations if there were no layout transition hazards
1861 if (!skip) {
1862 temp_context.RecordLayoutTransitions(rp_state, subpass, views, kCurrentCommandTag);
1863 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001864 }
John Zulauf86356ca2020-10-19 11:46:41 -06001865
John Zulauf355e49b2020-04-24 15:11:15 -06001866 return skip;
1867}
1868
locke-lunarg61870c22020-06-09 14:51:50 -06001869bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1870 const char *func_name) const {
1871 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001872 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001873 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001874 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1875 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001876 return skip;
1877 }
1878
1879 using DescriptorClass = cvdescriptorset::DescriptorClass;
1880 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1881 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1882 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1883 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1884
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001885 for (const auto &stage_state : pipe->stage_state) {
1886 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1887 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001888 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001889 }
locke-lunarg61870c22020-06-09 14:51:50 -06001890 for (const auto &set_binding : stage_state.descriptor_uses) {
1891 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1892 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1893 set_binding.first.second);
1894 const auto descriptor_type = binding_it.GetType();
1895 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1896 auto array_idx = 0;
1897
1898 if (binding_it.IsVariableDescriptorCount()) {
1899 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1900 }
1901 SyncStageAccessIndex sync_index =
1902 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1903
1904 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1905 uint32_t index = i - index_range.start;
1906 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1907 switch (descriptor->GetClass()) {
1908 case DescriptorClass::ImageSampler:
1909 case DescriptorClass::Image: {
1910 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001911 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001912 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001913 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1914 img_view_state = image_sampler_descriptor->GetImageViewState();
1915 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001916 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001917 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1918 img_view_state = image_descriptor->GetImageViewState();
1919 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001920 }
1921 if (!img_view_state) continue;
1922 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1923 VkExtent3D extent = {};
1924 VkOffset3D offset = {};
1925 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1926 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1927 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1928 } else {
1929 extent = img_state->createInfo.extent;
1930 }
John Zulauf361fb532020-07-22 10:45:39 -06001931 HazardResult hazard;
1932 const auto &subresource_range = img_view_state->normalized_subresource_range;
1933 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1934 // Input attachments are subject to raster ordering rules
1935 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1936 kAttachmentRasterOrder, offset, extent);
1937 } else {
1938 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1939 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001940 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001941 skip |= sync_state_->LogError(
1942 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001943 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1944 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001945 func_name, string_SyncHazard(hazard.hazard),
1946 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1947 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001948 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001949 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1950 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1951 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001952 }
1953 break;
1954 }
1955 case DescriptorClass::TexelBuffer: {
1956 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1957 if (!buf_view_state) continue;
1958 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001959 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001960 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001961 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001962 skip |= sync_state_->LogError(
1963 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001964 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1965 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001966 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1967 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001968 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001969 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1970 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1971 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001972 }
1973 break;
1974 }
1975 case DescriptorClass::GeneralBuffer: {
1976 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1977 auto buf_state = buffer_descriptor->GetBufferState();
1978 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001979 const ResourceAccessRange range =
1980 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001981 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001982 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001983 skip |= sync_state_->LogError(
1984 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001985 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1986 func_name, string_SyncHazard(hazard.hazard),
1987 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001988 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001989 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001990 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1991 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1992 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001993 }
1994 break;
1995 }
1996 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1997 default:
1998 break;
1999 }
2000 }
2001 }
2002 }
2003 return skip;
2004}
2005
2006void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
2007 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002008 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06002009 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002010 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
2011 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06002012 return;
2013 }
2014
2015 using DescriptorClass = cvdescriptorset::DescriptorClass;
2016 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
2017 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
2018 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
2019 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
2020
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002021 for (const auto &stage_state : pipe->stage_state) {
2022 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
2023 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06002024 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002025 }
locke-lunarg61870c22020-06-09 14:51:50 -06002026 for (const auto &set_binding : stage_state.descriptor_uses) {
2027 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
2028 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
2029 set_binding.first.second);
2030 const auto descriptor_type = binding_it.GetType();
2031 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2032 auto array_idx = 0;
2033
2034 if (binding_it.IsVariableDescriptorCount()) {
2035 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2036 }
2037 SyncStageAccessIndex sync_index =
2038 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
2039
2040 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2041 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2042 switch (descriptor->GetClass()) {
2043 case DescriptorClass::ImageSampler:
2044 case DescriptorClass::Image: {
2045 const IMAGE_VIEW_STATE *img_view_state = nullptr;
2046 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
2047 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
2048 } else {
2049 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
2050 }
2051 if (!img_view_state) continue;
2052 const IMAGE_STATE *img_state = img_view_state->image_state.get();
2053 VkExtent3D extent = {};
2054 VkOffset3D offset = {};
2055 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
2056 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
2057 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
2058 } else {
2059 extent = img_state->createInfo.extent;
2060 }
2061 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
2062 offset, extent, tag);
2063 break;
2064 }
2065 case DescriptorClass::TexelBuffer: {
2066 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
2067 if (!buf_view_state) continue;
2068 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002069 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06002070 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
2071 break;
2072 }
2073 case DescriptorClass::GeneralBuffer: {
2074 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
2075 auto buf_state = buffer_descriptor->GetBufferState();
2076 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002077 const ResourceAccessRange range =
2078 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06002079 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
2080 break;
2081 }
2082 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2083 default:
2084 break;
2085 }
2086 }
2087 }
2088 }
2089}
2090
2091bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2092 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002093 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2094 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002095 return skip;
2096 }
2097
2098 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2099 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002100 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002101
2102 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002103 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002104 if (binding_description.binding < binding_buffers_size) {
2105 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002106 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002107
locke-lunarg1ae57d62020-11-18 10:49:19 -07002108 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002109 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2110 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06002111 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
2112 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002113 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002114 buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002115 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002116 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002117 }
2118 }
2119 }
2120 return skip;
2121}
2122
2123void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002124 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2125 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002126 return;
2127 }
2128 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2129 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002130 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002131
2132 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002133 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002134 if (binding_description.binding < binding_buffers_size) {
2135 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002136 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002137
locke-lunarg1ae57d62020-11-18 10:49:19 -07002138 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002139 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2140 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06002141 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
2142 }
2143 }
2144}
2145
2146bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2147 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002148 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002149 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002150 }
locke-lunarg61870c22020-06-09 14:51:50 -06002151
locke-lunarg1ae57d62020-11-18 10:49:19 -07002152 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002153 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002154 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2155 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06002156 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
2157 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002158 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002159 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 -06002160 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002161 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002162 }
2163
2164 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2165 // We will detect more accurate range in the future.
2166 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2167 return skip;
2168}
2169
2170void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002171 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 -06002172
locke-lunarg1ae57d62020-11-18 10:49:19 -07002173 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002174 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002175 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2176 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06002177 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
2178
2179 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2180 // We will detect more accurate range in the future.
2181 RecordDrawVertex(UINT32_MAX, 0, tag);
2182}
2183
2184bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002185 bool skip = false;
2186 if (!current_renderpass_context_) return skip;
2187 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
2188 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
2189 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002190}
2191
2192void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002193 if (current_renderpass_context_) {
locke-lunarg7077d502020-06-18 21:37:26 -06002194 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
2195 tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002196 }
locke-lunarg61870c22020-06-09 14:51:50 -06002197}
2198
John Zulauf355e49b2020-04-24 15:11:15 -06002199bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002200 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06002201 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06002202 skip |=
2203 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002204
2205 return skip;
2206}
2207
2208bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
2209 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06002210 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002211 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06002212 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06002213 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
2214 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002215
2216 return skip;
2217}
2218
2219void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
2220 assert(sync_state_);
2221 if (!cb_state_) return;
2222
2223 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06002224 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06002225 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06002226 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002227 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002228}
2229
John Zulauf355e49b2020-04-24 15:11:15 -06002230void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002231 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06002232 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002233 current_context_ = &current_renderpass_context_->CurrentContext();
2234}
2235
John Zulauf355e49b2020-04-24 15:11:15 -06002236void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002237 assert(current_renderpass_context_);
2238 if (!current_renderpass_context_) return;
2239
John Zulauf1a224292020-06-30 14:52:13 -06002240 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002241 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002242 current_renderpass_context_ = nullptr;
2243}
2244
John Zulauf49beb112020-11-04 16:06:31 -07002245bool CommandBufferAccessContext::ValidateSetEvent(VkCommandBuffer commandBuffer, VkEvent event,
2246 VkPipelineStageFlags stageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002247 // I'll put this here just in case we need to pass this in for future extension support
2248 const auto cmd = CMD_SETEVENT;
2249 bool skip = false;
2250 const auto *sync_event = GetEventState(event);
2251 if (!sync_event) return false; // Core, Lifetimes, or Param check needs to catch invalid events.
2252
2253 const char *const reset_set =
2254 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
2255 "hazards.";
2256 const char *const wait =
2257 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
2258
2259 const auto exec_scope = WithEarlierPipelineStages(ExpandPipelineStages(GetQueueFlags(), stageMask));
2260 if (!sync_event->HasBarrier(stageMask, exec_scope)) {
2261 const char *vuid = nullptr;
2262 const char *message = nullptr;
2263 switch (sync_event->last_command) {
2264 case CMD_RESETEVENT:
2265 // Needs a barrier between reset and set
2266 vuid = "SYNC-vkCmdSetEvent-missingbarrier-reset";
2267 message = reset_set;
2268 break;
2269 case CMD_SETEVENT:
2270 // Needs a barrier between set and set
2271 vuid = "SYNC-vkCmdSetEvent-missingbarrier-set";
2272 message = reset_set;
2273 break;
2274 case CMD_WAITEVENTS:
2275 // Needs a barrier or is in second execution scope
2276 vuid = "SYNC-vkCmdSetEvent-missingbarrier-wait";
2277 message = wait;
2278 break;
2279 default:
2280 // The only other valid last command that wasn't one.
2281 assert(sync_event->last_command == CMD_NONE);
2282 break;
2283 }
2284 if (vuid) {
2285 assert(nullptr != message);
2286 const char *const cmd_name = CommandTypeString(cmd);
2287 skip |= sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2288 cmd_name, CommandTypeString(sync_event->last_command));
2289 }
2290 }
2291
2292 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002293}
2294
John Zulauf4a6105a2020-11-17 15:11:05 -07002295void CommandBufferAccessContext::RecordSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask,
2296 const ResourceUsageTag &tag) {
2297 auto *sync_event = GetEventState(event);
2298 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
2299
2300 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
2301 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
2302 // any issues caused by naive scope setting here.
2303
2304 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
2305 // Given:
2306 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
2307 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
2308
2309 const auto stage_mask = ExpandPipelineStages(GetQueueFlags(), stageMask);
2310 const auto exec_scope = WithEarlierPipelineStages(stage_mask);
2311
2312 sync_event->stage_mask_param = stageMask;
2313
2314 if (!sync_event->HasBarrier(stageMask, exec_scope)) {
2315 sync_event->unsynchronized_set = sync_event->last_command;
2316 sync_event->ResetFirstScope();
2317 } else if (sync_event->exec_scope == 0) {
2318 // We only set the scope if there isn't one
2319 sync_event->stage_mask = stage_mask;
2320 sync_event->exec_scope = exec_scope;
2321 sync_event->stage_accesses = SyncStageAccess::AccessScopeByStage(sync_event->stage_mask);
2322
2323 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
2324 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
2325 if (access.second.InSourceScopeOrChain(sync_event->exec_scope, sync_event->stage_accesses)) {
2326 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
2327 }
2328 };
2329 GetCurrentAccessContext()->ForAll(set_scope);
2330 sync_event->unsynchronized_set = CMD_NONE;
2331 sync_event->first_scope_tag = tag;
2332 }
2333 sync_event->last_command = CMD_SETEVENT;
2334 sync_event->barriers = 0U;
2335}
John Zulauf49beb112020-11-04 16:06:31 -07002336
2337bool CommandBufferAccessContext::ValidateResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
2338 VkPipelineStageFlags stageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002339 // I'll put this here just in case we need to pass this in for future extension support
2340 const auto cmd = CMD_RESETEVENT;
2341
2342 bool skip = false;
2343 // TODO: EVENTS:
2344 // What is it we need to check... that we've had a reset since a set? Set/Set seems ill formed...
2345 const auto *sync_event = GetEventState(event);
2346 if (!sync_event) return false; // Core, Lifetimes, or Param check needs to catch invalid events.
2347
2348 const char *const set_wait =
2349 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
2350 "hazards.";
2351 const char *message = set_wait; // Only one message this call.
2352 const auto exec_scope = WithEarlierPipelineStages(ExpandPipelineStages(GetQueueFlags(), stageMask));
2353 if (!sync_event->HasBarrier(stageMask, exec_scope)) {
2354 const char *vuid = nullptr;
2355 switch (sync_event->last_command) {
2356 case CMD_SETEVENT:
2357 // Needs a barrier between set and reset
2358 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
2359 break;
2360 case CMD_WAITEVENTS: {
2361 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
2362 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
2363 break;
2364 }
2365 default:
2366 // The only other valid last command that wasn't one.
2367 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT));
2368 break;
2369 }
2370 if (vuid) {
2371 const char *const cmd_name = CommandTypeString(cmd);
2372 skip |= sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2373 cmd_name, CommandTypeString(sync_event->last_command));
2374 }
2375 }
2376 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002377}
2378
John Zulauf4a6105a2020-11-17 15:11:05 -07002379void CommandBufferAccessContext::RecordResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
2380 const auto cmd = CMD_RESETEVENT;
2381 auto *sync_event = GetEventState(event);
2382 if (!sync_event) return;
John Zulauf49beb112020-11-04 16:06:31 -07002383
John Zulauf4a6105a2020-11-17 15:11:05 -07002384 // Clear out the first sync scope, any races vs. wait or set are reported, so we'll keep the bookkeeping simple assuming
2385 // the safe case
2386 for (const auto address_type : kAddressTypes) {
2387 sync_event->first_scope[static_cast<size_t>(address_type)].clear();
2388 }
2389
2390 // Update the event state
2391 sync_event->last_command = cmd;
2392 sync_event->unsynchronized_set = CMD_NONE;
2393 sync_event->ResetFirstScope();
2394 sync_event->barriers = 0U;
2395}
2396
2397bool CommandBufferAccessContext::ValidateWaitEvents(uint32_t eventCount, const VkEvent *pEvents, VkPipelineStageFlags srcStageMask,
2398 VkPipelineStageFlags dstStageMask, uint32_t memoryBarrierCount,
2399 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
John Zulauf49beb112020-11-04 16:06:31 -07002400 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2401 uint32_t imageMemoryBarrierCount,
2402 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002403 const auto cmd = CMD_WAITEVENTS;
2404 const char *const ignored = "Wait operation is ignored for this event.";
2405 bool skip = false;
2406
2407 if (srcStageMask & VK_PIPELINE_STAGE_HOST_BIT) {
2408 const char *const cmd_name = CommandTypeString(cmd);
2409 const char *const vuid = "SYNC-vkCmdWaitEvents-hostevent-unsupported";
John Zulauffe757512020-12-18 12:17:47 -07002410 skip = sync_state_->LogInfo(cb_state_->commandBuffer, vuid,
2411 "%s, srcStageMask includes %s, unsupported by synchronization validaton.", cmd_name,
2412 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT), ignored);
John Zulauf4a6105a2020-11-17 15:11:05 -07002413 }
2414
2415 VkPipelineStageFlags event_stage_masks = 0U;
John Zulauffe757512020-12-18 12:17:47 -07002416 bool events_not_found = false;
John Zulauf4a6105a2020-11-17 15:11:05 -07002417 for (uint32_t event_index = 0; event_index < eventCount; event_index++) {
2418 const auto event = pEvents[event_index];
2419 const auto *sync_event = GetEventState(event);
John Zulauffe757512020-12-18 12:17:47 -07002420 if (!sync_event) {
2421 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
2422 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives.
2423
2424 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
2425 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002426
2427 event_stage_masks |= sync_event->stage_mask_param;
2428 const auto ignore_reason = sync_event->IsIgnoredByWait(srcStageMask);
2429 if (ignore_reason) {
2430 switch (ignore_reason) {
2431 case SyncEventState::ResetWaitRace: {
2432 const char *const cmd_name = CommandTypeString(cmd);
2433 const char *const vuid = "SYNC-vkCmdWaitEvents-missingbarrier-reset";
2434 const char *const message =
2435 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
2436 skip |=
2437 sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2438 cmd_name, CommandTypeString(sync_event->last_command), ignored);
2439 break;
2440 }
2441 case SyncEventState::SetRace: {
2442 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for this
2443 // event
2444 const char *const cmd_name = CommandTypeString(cmd);
2445 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
2446 const char *const message =
2447 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, % %s";
2448 const char *const reason = "First synchronization scope is undefined.";
2449 skip |=
2450 sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2451 CommandTypeString(sync_event->last_command), reason, ignored);
2452 break;
2453 }
2454 case SyncEventState::MissingStageBits: {
2455 const VkPipelineStageFlags missing_bits = sync_event->stage_mask_param & ~srcStageMask;
2456 // Issue error message that event waited for is not in wait events scope
2457 const char *const cmd_name = CommandTypeString(cmd);
2458 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
2459 const char *const message =
2460 "%s: %s stageMask 0x%" PRIx32 " includes bits not present in srcStageMask 0x%" PRIx32
2461 ". Bits missing from srcStageMask %s. %s";
2462 skip |= sync_state_->LogError(
2463 event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2464 sync_event->stage_mask_param, srcStageMask, string_VkPipelineStageFlags(missing_bits).c_str(), ignored);
2465 break;
2466 }
2467 default:
2468 assert(ignore_reason == SyncEventState::NotIgnored);
2469 }
2470 } else if (imageMemoryBarrierCount) {
2471 const auto *context = GetCurrentAccessContext();
2472 assert(context);
2473 for (uint32_t barrier_index = 0; barrier_index < imageMemoryBarrierCount; barrier_index++) {
2474 const auto &barrier = pImageMemoryBarriers[barrier_index];
2475 if (barrier.oldLayout == barrier.newLayout) continue;
2476 const auto *image_state = sync_state_->Get<IMAGE_STATE>(barrier.image);
2477 if (!image_state) continue;
2478 auto subresource_range = NormalizeSubresourceRange(image_state->createInfo, barrier.subresourceRange);
2479 const auto src_access_scope = SyncStageAccess::AccessScope(sync_event->stage_accesses, barrier.srcAccessMask);
2480 const auto hazard =
2481 context->DetectImageBarrierHazard(*image_state, sync_event->exec_scope, src_access_scope, subresource_range,
2482 *sync_event, AccessContext::DetectOptions::kDetectAll);
2483 if (hazard.hazard) {
2484 const char *const cmd_name = CommandTypeString(cmd);
2485 skip |= sync_state_->LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
2486 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", cmd_name,
2487 string_SyncHazard(hazard.hazard), barrier_index,
2488 sync_state_->report_data->FormatHandle(barrier.image).c_str(),
2489 string_UsageTag(hazard).c_str());
2490 break;
2491 }
2492 }
2493 }
2494 }
2495
2496 // Note that we can't check for HOST in pEvents as we don't track that set event type
2497 const auto extra_stage_bits = (srcStageMask & ~VK_PIPELINE_STAGE_HOST_BIT) & ~event_stage_masks;
2498 if (extra_stage_bits) {
2499 // Issue error message that event waited for is not in wait events scope
2500 const char *const cmd_name = CommandTypeString(cmd);
2501 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
2502 const char *const message =
John Zulauffe757512020-12-18 12:17:47 -07002503 "%s: srcStageMask 0x%" PRIx32 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
2504 if (events_not_found) {
2505 skip |= sync_state_->LogInfo(cb_state_->commandBuffer, vuid, message, cmd_name, srcStageMask,
2506 string_VkPipelineStageFlags(extra_stage_bits).c_str(),
2507 " vkCmdSetEvent may be in previously submitted command buffer.");
2508 } else {
2509 skip |= sync_state_->LogError(cb_state_->commandBuffer, vuid, message, cmd_name, srcStageMask,
2510 string_VkPipelineStageFlags(extra_stage_bits).c_str(), "");
2511 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002512 }
2513 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002514}
2515
2516void CommandBufferAccessContext::RecordWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
2517 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
2518 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2519 uint32_t bufferMemoryBarrierCount,
2520 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2521 uint32_t imageMemoryBarrierCount,
John Zulauf4a6105a2020-11-17 15:11:05 -07002522 const VkImageMemoryBarrier *pImageMemoryBarriers, const ResourceUsageTag &tag) {
2523 auto *access_context = GetCurrentAccessContext();
2524 assert(access_context);
2525 if (!access_context) return;
2526
2527 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
2528 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
2529 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
2530 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here
2531 access_context->ResolvePreviousAccesses();
2532
2533 const auto dst_stage_mask = ExpandPipelineStages(GetQueueFlags(), dstStageMask);
2534 auto dst_stage_accesses = SyncStageAccess::AccessScopeByStage(dst_stage_mask);
2535 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2536 for (uint32_t event_index = 0; event_index < eventCount; event_index++) {
2537 const auto event = pEvents[event_index];
2538 auto *sync_event = GetEventState(event);
2539 if (!sync_event) continue;
2540
2541 sync_event->last_command = CMD_WAITEVENTS;
2542
2543 if (!sync_event->IsIgnoredByWait(srcStageMask)) {
2544 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
2545 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
2546 // of the barriers is maintained.
2547 ApplyBufferBarriers(*sync_event, dst_exec_scope, dst_stage_accesses, bufferMemoryBarrierCount, pBufferMemoryBarriers);
2548 ApplyImageBarriers(*sync_event, dst_exec_scope, dst_stage_accesses, imageMemoryBarrierCount, pImageMemoryBarriers, tag);
2549 ApplyGlobalBarriers(*sync_event, dstStageMask, dst_exec_scope, dst_stage_accesses, memoryBarrierCount, pMemoryBarriers,
2550 tag);
2551 } else {
2552 // We ignored this wait, so we don't have any effective synchronization barriers for it.
2553 sync_event->barriers = 0U;
2554 }
2555 }
2556
2557 // Apply the pending barriers
2558 ResolvePendingBarrierFunctor apply_pending_action(tag);
2559 access_context->ApplyGlobalBarriers(apply_pending_action);
2560}
2561
2562void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2563 // Erase is okay with the key not being
2564 event_state_.erase(event);
2565}
2566
2567SyncEventState *CommandBufferAccessContext::GetEventState(VkEvent event) {
2568 auto &event_up = event_state_[event];
2569 if (!event_up) {
2570 auto event_atate = sync_state_->GetShared<EVENT_STATE>(event);
2571 event_up.reset(new SyncEventState(event_atate));
2572 }
2573 return event_up.get();
2574}
2575
2576const SyncEventState *CommandBufferAccessContext::GetEventState(VkEvent event) const {
2577 auto event_it = event_state_.find(event);
2578 if (event_it == event_state_.cend()) {
2579 return nullptr;
2580 }
2581 return event_it->second.get();
2582}
John Zulauf49beb112020-11-04 16:06:31 -07002583
locke-lunarg61870c22020-06-09 14:51:50 -06002584bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
2585 const VkRect2D &render_area, const char *func_name) const {
2586 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002587 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2588 if (!pipe ||
2589 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002590 return skip;
2591 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002592 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002593 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2594 VkExtent3D extent = CastTo3D(render_area.extent);
2595 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06002596
John Zulauf1a224292020-06-30 14:52:13 -06002597 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002598 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002599 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2600 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002601 if (location >= subpass.colorAttachmentCount ||
2602 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002603 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002604 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002605 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002606 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
2607 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06002608 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002609 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002610 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002611 func_name, string_SyncHazard(hazard.hazard),
2612 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2613 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002614 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002615 }
2616 }
2617 }
locke-lunarg37047832020-06-12 13:44:45 -06002618
2619 // PHASE1 TODO: Add layout based read/vs. write selection.
2620 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002621 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002622 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002623 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002624 bool depth_write = false, stencil_write = false;
2625
2626 // PHASE1 TODO: These validation should be in core_checks.
2627 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002628 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2629 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002630 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2631 depth_write = true;
2632 }
2633 // PHASE1 TODO: It needs to check if stencil is writable.
2634 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2635 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2636 // PHASE1 TODO: These validation should be in core_checks.
2637 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002638 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002639 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2640 stencil_write = true;
2641 }
2642
2643 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2644 if (depth_write) {
2645 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002646 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2647 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002648 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002649 skip |= sync_state.LogError(
2650 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002651 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002652 func_name, string_SyncHazard(hazard.hazard),
2653 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2654 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002655 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002656 }
2657 }
2658 if (stencil_write) {
2659 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002660 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2661 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002662 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002663 skip |= sync_state.LogError(
2664 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002665 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002666 func_name, string_SyncHazard(hazard.hazard),
2667 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2668 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002669 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002670 }
locke-lunarg61870c22020-06-09 14:51:50 -06002671 }
2672 }
2673 return skip;
2674}
2675
locke-lunarg96dc9632020-06-10 17:22:18 -06002676void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
2677 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002678 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2679 if (!pipe ||
2680 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002681 return;
2682 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002683 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002684 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2685 VkExtent3D extent = CastTo3D(render_area.extent);
2686 VkOffset3D offset = CastTo3D(render_area.offset);
2687
John Zulauf1a224292020-06-30 14:52:13 -06002688 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002689 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002690 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2691 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002692 if (location >= subpass.colorAttachmentCount ||
2693 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002694 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002695 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002696 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002697 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
2698 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002699 }
2700 }
locke-lunarg37047832020-06-12 13:44:45 -06002701
2702 // PHASE1 TODO: Add layout based read/vs. write selection.
2703 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002704 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002705 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002706 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002707 bool depth_write = false, stencil_write = false;
2708
2709 // PHASE1 TODO: These validation should be in core_checks.
2710 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002711 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2712 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002713 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2714 depth_write = true;
2715 }
2716 // PHASE1 TODO: It needs to check if stencil is writable.
2717 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2718 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2719 // PHASE1 TODO: These validation should be in core_checks.
2720 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002721 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002722 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2723 stencil_write = true;
2724 }
2725
2726 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2727 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002728 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2729 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002730 }
2731 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002732 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2733 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002734 }
locke-lunarg61870c22020-06-09 14:51:50 -06002735 }
2736}
2737
John Zulauf1507ee42020-05-18 11:33:09 -06002738bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
2739 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002740 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002741 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06002742 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2743 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002744 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2745 func_name);
2746
John Zulauf355e49b2020-04-24 15:11:15 -06002747 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002748 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06002749 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002750 if (!skip) {
2751 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2752 // on a copy of the (empty) next context.
2753 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2754 AccessContext temp_context(next_context);
2755 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
2756 skip |= temp_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2757 }
John Zulauf7635de32020-05-29 17:14:15 -06002758 return skip;
2759}
2760bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
2761 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002762 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002763 bool skip = false;
2764 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2765 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002766 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2767 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002768 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002769 return skip;
2770}
2771
John Zulauf7635de32020-05-29 17:14:15 -06002772AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2773 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2774}
2775
2776bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2777 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002778 bool skip = false;
2779
John Zulauf7635de32020-05-29 17:14:15 -06002780 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2781 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2782 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2783 // to apply and only copy then, if this proves a hot spot.
2784 std::unique_ptr<AccessContext> proxy_for_current;
2785
John Zulauf355e49b2020-04-24 15:11:15 -06002786 // Validate the "finalLayout" transitions to external
2787 // Get them from where there we're hidding in the extra entry.
2788 const auto &final_transitions = rp_state_->subpass_transitions.back();
2789 for (const auto &transition : final_transitions) {
2790 const auto &attach_view = attachment_views_[transition.attachment];
2791 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2792 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002793 auto *context = trackback.context;
2794
2795 if (transition.prev_pass == current_subpass_) {
2796 if (!proxy_for_current) {
2797 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2798 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2799 }
2800 context = proxy_for_current.get();
2801 }
2802
John Zulaufa0a98292020-09-18 09:30:10 -06002803 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2804 const auto merged_barrier = MergeBarriers(trackback.barriers);
2805 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2806 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2807 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002808 if (hazard.hazard) {
2809 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2810 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002811 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002812 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002813 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002814 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002815 }
2816 }
2817 return skip;
2818}
2819
2820void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2821 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002822 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002823}
2824
John Zulauf1507ee42020-05-18 11:33:09 -06002825void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2826 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2827 auto &subpass_context = subpass_contexts_[current_subpass_];
2828 VkExtent3D extent = CastTo3D(render_area.extent);
2829 VkOffset3D offset = CastTo3D(render_area.offset);
2830
2831 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2832 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2833 if (attachment_views_[i] == nullptr) continue; // UNUSED
2834 const auto &view = *attachment_views_[i];
2835 const IMAGE_STATE *image = view.image_state.get();
2836 if (image == nullptr) continue;
2837
2838 const auto &ci = attachment_ci[i];
2839 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002840 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002841 const bool is_color = !(has_depth || has_stencil);
2842
2843 if (is_color) {
2844 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2845 extent, tag);
2846 } else {
2847 auto update_range = view.normalized_subresource_range;
2848 if (has_depth) {
2849 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2850 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2851 }
2852 if (has_stencil) {
2853 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2854 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2855 tag);
2856 }
2857 }
2858 }
2859 }
2860}
2861
John Zulauf355e49b2020-04-24 15:11:15 -06002862void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002863 const AccessContext *external_context, VkQueueFlags queue_flags,
2864 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002865 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002866 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002867 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2868 // Add this for all subpasses here so that they exsist during next subpass validation
2869 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002870 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002871 }
2872 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2873
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002874 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002875 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002876 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002877}
John Zulauf1507ee42020-05-18 11:33:09 -06002878
2879void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002880 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2881 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002882 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002883
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002884 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2885 // subpass, so their tag needs to be different from the layout and load operations below.
Jeremy Gebben4bb73502020-12-14 11:17:50 -07002886 ResourceUsageTag next_tag = tag.NextSubCommand();
John Zulauf355e49b2020-04-24 15:11:15 -06002887 current_subpass_++;
2888 assert(current_subpass_ < subpass_contexts_.size());
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002889 subpass_contexts_[current_subpass_].SetStartTag(next_tag);
2890 RecordLayoutTransitions(next_tag);
2891 RecordLoadOperations(render_area, next_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002892}
2893
John Zulauf1a224292020-06-30 14:52:13 -06002894void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2895 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002896 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002897 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002898 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002899
John Zulauf355e49b2020-04-24 15:11:15 -06002900 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002901 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002902
2903 // Add the "finalLayout" transitions to external
2904 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002905 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2906 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2907 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002908 const auto &final_transitions = rp_state_->subpass_transitions.back();
2909 for (const auto &transition : final_transitions) {
2910 const auto &attachment = attachment_views_[transition.attachment];
2911 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002912 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf1e331ec2020-12-04 18:29:38 -07002913 std::vector<PipelineBarrierOp> barrier_ops;
2914 barrier_ops.reserve(last_trackback.barriers.size());
2915 for (const auto &barrier : last_trackback.barriers) {
2916 barrier_ops.emplace_back(barrier, true);
2917 }
2918 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, barrier_ops, tag);
2919 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002920 }
2921}
2922
John Zulauf3d84f1b2020-03-09 13:33:25 -06002923SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2924 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2925 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2926 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2927 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2928 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2929 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2930}
2931
John Zulaufb02c1eb2020-10-06 16:33:36 -06002932// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2933void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2934 for (const auto &barrier : barriers) {
2935 ApplyBarrier(barrier, layout_transition);
2936 }
2937}
2938
John Zulauf89311b42020-09-29 16:28:47 -06002939// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2940// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2941// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002942void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2943 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002944 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002945 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002946 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002947 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002948 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002949 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002950}
John Zulauf9cb530d2019-09-30 14:14:10 -06002951HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2952 HazardResult hazard;
2953 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002954 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002955 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002956 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002957 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002958 }
2959 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002960 // Write operation:
2961 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2962 // If reads exists -- test only against them because either:
2963 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2964 // * 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
2965 // the current write happens after the reads, so just test the write against the reades
2966 // Otherwise test against last_write
2967 //
2968 // Look for casus belli for WAR
2969 if (last_read_count) {
2970 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2971 const auto &read_access = last_reads[read_index];
2972 if (IsReadHazard(usage_stage, read_access)) {
2973 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2974 break;
2975 }
2976 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002977 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002978 // Write-After-Write check -- if we have a previous write to test against
2979 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002980 }
2981 }
2982 return hazard;
2983}
2984
John Zulauf69133422020-05-20 14:55:53 -06002985HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2986 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2987 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002988 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002989 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002990 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2991 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002992 if (IsRead(usage_bit)) {
2993 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2994 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2995 if (is_raw_hazard) {
2996 // NOTE: we know last_write is non-zero
2997 // See if the ordering rules save us from the simple RAW check above
2998 // First check to see if the current usage is covered by the ordering rules
2999 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
3000 const bool usage_is_ordered =
3001 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
3002 if (usage_is_ordered) {
3003 // Now see of the most recent write (or a subsequent read) are ordered
3004 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
3005 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06003006 }
3007 }
John Zulauf4285ee92020-09-23 10:20:52 -06003008 if (is_raw_hazard) {
3009 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
3010 }
John Zulauf361fb532020-07-22 10:45:39 -06003011 } else {
3012 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003013 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulauf361fb532020-07-22 10:45:39 -06003014 if (last_read_count) {
John Zulauf361fb532020-07-22 10:45:39 -06003015 // Look for any WAR hazards outside the ordered set of stages
John Zulauf4285ee92020-09-23 10:20:52 -06003016 VkPipelineStageFlags ordered_stages = 0;
3017 if (usage_write_is_ordered) {
3018 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
3019 ordered_stages = GetOrderedStages(ordering);
3020 }
3021 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
3022 if ((ordered_stages & last_read_stages) != last_read_stages) {
3023 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
3024 const auto &read_access = last_reads[read_index];
3025 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
3026 if (IsReadHazard(usage_stage, read_access)) {
3027 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3028 break;
3029 }
John Zulaufd14743a2020-07-03 09:42:39 -06003030 }
3031 }
John Zulauf4285ee92020-09-23 10:20:52 -06003032 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003033 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003034 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06003035 }
John Zulauf69133422020-05-20 14:55:53 -06003036 }
3037 }
3038 return hazard;
3039}
3040
John Zulauf2f952d22020-02-10 11:34:51 -07003041// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003042HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07003043 HazardResult hazard;
3044 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003045 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
3046 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
3047 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07003048 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003049 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06003050 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07003051 }
3052 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003053 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06003054 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07003055 } else if (last_read_count > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003056 // Any reads during the other subpass will conflict with this write, so we need to check them all.
3057 for (uint32_t i = 0; i < last_read_count; i++) {
3058 if (last_reads[i].tag.index >= start_tag.index) {
3059 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[i].access, last_reads[i].tag);
3060 break;
3061 }
3062 }
John Zulauf2f952d22020-02-10 11:34:51 -07003063 }
3064 }
3065 return hazard;
3066}
3067
John Zulauf36bcf6a2020-02-03 15:12:52 -07003068HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003069 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07003070 // Only supporting image layout transitions for now
3071 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3072 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06003073 // only test for WAW if there no intervening read operations.
3074 // See DetectHazard(SyncStagetAccessIndex) above for more details.
3075 if (last_read_count) {
John Zulauf355e49b2020-04-24 15:11:15 -06003076 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07003077 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07003078 const auto &read_access = last_reads[read_index];
John Zulauf4a6105a2020-11-17 15:11:05 -07003079 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06003080 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07003081 break;
3082 }
3083 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003084 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3085 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3086 }
3087
3088 return hazard;
3089}
3090
3091HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
3092 const SyncStageAccessFlags &src_access_scope,
3093 const ResourceUsageTag &event_tag) const {
3094 // Only supporting image layout transitions for now
3095 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3096 HazardResult hazard;
3097 // only test for WAW if there no intervening read operations.
3098 // See DetectHazard(SyncStagetAccessIndex) above for more details.
3099
3100 if (last_read_count) {
3101 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
3102 // first scope, or they are a hazard.
3103 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
3104 const auto &read_access = last_reads[read_index];
3105 if (read_access.tag.IsBefore(event_tag)) {
3106 // The read is in the events first synchronization scope, so we use a barrier hazard check
3107 // If the read stage is not in the src sync scope
3108 // *AND* not execution chained with an existing sync barrier (that's the or)
3109 // then the barrier access is unsafe (R/W after R)
3110 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3111 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3112 break;
3113 }
3114 } else {
3115 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3116 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3117 }
3118 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003119 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003120 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
3121 if (write_tag.IsBefore(event_tag)) {
3122 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3123 // So do a normal barrier hazard check
3124 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3125 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3126 }
3127 } else {
3128 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003129 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3130 }
John Zulaufd14743a2020-07-03 09:42:39 -06003131 }
John Zulauf361fb532020-07-22 10:45:39 -06003132
John Zulauf0cb5be22020-01-23 12:18:22 -07003133 return hazard;
3134}
3135
John Zulauf5f13a792020-03-10 07:31:21 -06003136// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3137// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3138// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3139void ResourceAccessState::Resolve(const ResourceAccessState &other) {
3140 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003141 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3142 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003143 *this = other;
3144 } else if (!other.write_tag.IsBefore(write_tag)) {
3145 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
3146 // dependency chaining logic or any stage expansion)
3147 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003148 pending_write_barriers |= other.pending_write_barriers;
3149 pending_layout_transition |= other.pending_layout_transition;
3150 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003151
John Zulaufd14743a2020-07-03 09:42:39 -06003152 // Merge the read states
John Zulauf4285ee92020-09-23 10:20:52 -06003153 const auto pre_merge_count = last_read_count;
3154 const auto pre_merge_stages = last_read_stages;
John Zulauf5f13a792020-03-10 07:31:21 -06003155 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
3156 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003157 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003158 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003159 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3160 // but we should wait on profiling data for that.
3161 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003162 auto &my_read = last_reads[my_read_index];
3163 if (other_read.stage == my_read.stage) {
3164 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003165 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003166 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003167 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003168 my_read.pending_dep_chain = other_read.pending_dep_chain;
3169 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3170 // May require tracking more than one access per stage.
3171 my_read.barriers = other_read.barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003172 if (my_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
3173 // Since I'm overwriting the fragement stage read, also update the input attachment info
3174 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003175 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003176 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003177 } else if (other_read.tag.IsBefore(my_read.tag)) {
3178 // The read tags match so merge the barriers
3179 my_read.barriers |= other_read.barriers;
3180 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003181 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003182
John Zulauf5f13a792020-03-10 07:31:21 -06003183 break;
3184 }
3185 }
3186 } else {
3187 // The other read stage doesn't exist in this, so add it.
3188 last_reads[last_read_count] = other_read;
3189 last_read_count++;
3190 last_read_stages |= other_read.stage;
John Zulauf4285ee92020-09-23 10:20:52 -06003191 if (other_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06003192 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003193 }
John Zulauf5f13a792020-03-10 07:31:21 -06003194 }
3195 }
John Zulauf361fb532020-07-22 10:45:39 -06003196 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003197 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3198 // ignore it.
John Zulauf5f13a792020-03-10 07:31:21 -06003199}
3200
John Zulauf9cb530d2019-09-30 14:14:10 -06003201void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
3202 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3203 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003204 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003205 // Mulitple outstanding reads may be of interest and do dependency chains independently
3206 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3207 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003208 uint32_t update_index = kStageCount;
John Zulauf9cb530d2019-09-30 14:14:10 -06003209 if (usage_stage & last_read_stages) {
3210 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf4285ee92020-09-23 10:20:52 -06003211 if (last_reads[read_index].stage == usage_stage) {
3212 update_index = read_index;
John Zulauf9cb530d2019-09-30 14:14:10 -06003213 break;
3214 }
3215 }
John Zulauf4285ee92020-09-23 10:20:52 -06003216 assert(update_index < last_read_count);
John Zulauf9cb530d2019-09-30 14:14:10 -06003217 } else {
John Zulauf9cb530d2019-09-30 14:14:10 -06003218 assert(last_read_count < last_reads.size());
John Zulauf4285ee92020-09-23 10:20:52 -06003219 update_index = last_read_count++;
John Zulauf9cb530d2019-09-30 14:14:10 -06003220 last_read_stages |= usage_stage;
3221 }
John Zulauf4285ee92020-09-23 10:20:52 -06003222 last_reads[update_index].Set(usage_stage, usage_bit, 0, tag);
3223
3224 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
3225 if (usage_stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06003226 // TODO Revisit re: multiple reads for a given stage
3227 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003228 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003229 } else {
3230 // Assume write
3231 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003232 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003233 }
3234}
John Zulauf5f13a792020-03-10 07:31:21 -06003235
John Zulauf89311b42020-09-29 16:28:47 -06003236// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3237// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3238// We can overwrite them as *this* write is now after them.
3239//
3240// 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 -07003241void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulauf89311b42020-09-29 16:28:47 -06003242 last_read_count = 0;
3243 last_read_stages = 0;
3244 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003245 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003246
3247 write_barriers = 0;
3248 write_dependency_chain = 0;
3249 write_tag = tag;
3250 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003251}
3252
John Zulauf89311b42020-09-29 16:28:47 -06003253// Apply the memory barrier without updating the existing barriers. The execution barrier
3254// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3255// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3256// replace the current write barriers or add to them, so accumulate to pending as well.
3257void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3258 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3259 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003260 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3261 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3262 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3263 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulauf4a6105a2020-11-17 15:11:05 -07003264 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003265 pending_write_barriers |= barrier.dst_access_scope;
3266 pending_write_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003267 }
John Zulauf89311b42020-09-29 16:28:47 -06003268 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3269 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003270
John Zulauf89311b42020-09-29 16:28:47 -06003271 if (!pending_layout_transition) {
3272 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3273 // don't need to be tracked as we're just going to zero them.
John Zulaufa0a98292020-09-18 09:30:10 -06003274 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf89311b42020-09-29 16:28:47 -06003275 ReadState &access = last_reads[read_index];
3276 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
3277 if (barrier.src_exec_scope & (access.stage | access.barriers)) {
3278 access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003279 }
3280 }
John Zulaufa0a98292020-09-18 09:30:10 -06003281 }
John Zulaufa0a98292020-09-18 09:30:10 -06003282}
3283
John Zulauf4a6105a2020-11-17 15:11:05 -07003284// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3285// changes the "chaining" state, but to keep barriers independent. See discussion above.
3286void ResourceAccessState::ApplyBarrier(const ResourceUsageTag &scope_tag, const SyncBarrier &barrier, bool layout_transition) {
3287 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3288 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3289 // in order to know if it's in the excecution scope
3290 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3291 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3292 // errors w.r.t. "most recent" accesses.
3293 if (layout_transition || ((write_tag.IsBefore(scope_tag)) && (barrier.src_access_scope & last_write).any())) {
3294 pending_write_barriers |= barrier.dst_access_scope;
3295 pending_write_dep_chain |= barrier.dst_exec_scope;
3296 }
3297 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3298 pending_layout_transition |= layout_transition;
3299
3300 if (!pending_layout_transition) {
3301 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3302 // don't need to be tracked as we're just going to zero them.
3303 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
3304 ReadState &access = last_reads[read_index];
3305 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3306 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3307 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3308 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3309 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3310 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3311 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
3312 if (access.tag.IsBefore(scope_tag) && (barrier.src_exec_scope & (access.stage | access.barriers))) {
3313 access.pending_dep_chain |= barrier.dst_exec_scope;
3314 }
3315 }
3316 }
3317}
John Zulauf89311b42020-09-29 16:28:47 -06003318void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
3319 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06003320 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
3321 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
3322 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003323 }
John Zulauf89311b42020-09-29 16:28:47 -06003324
3325 // Apply the accumulate execution barriers (and thus update chaining information)
3326 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
3327 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
3328 ReadState &access = last_reads[read_index];
3329 access.barriers |= access.pending_dep_chain;
3330 read_execution_barriers |= access.barriers;
3331 access.pending_dep_chain = 0;
3332 }
3333
3334 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3335 write_dependency_chain |= pending_write_dep_chain;
3336 write_barriers |= pending_write_barriers;
3337 pending_write_dep_chain = 0;
3338 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003339}
3340
John Zulauf59e25072020-07-17 10:55:21 -06003341// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003342VkPipelineStageFlags ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
John Zulauf59e25072020-07-17 10:55:21 -06003343 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003344
3345 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
3346 const auto &read_access = last_reads[read_index];
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003347 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003348 barriers = read_access.barriers;
3349 break;
John Zulauf59e25072020-07-17 10:55:21 -06003350 }
3351 }
John Zulauf4285ee92020-09-23 10:20:52 -06003352
John Zulauf59e25072020-07-17 10:55:21 -06003353 return barriers;
3354}
3355
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003356inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003357 assert(IsRead(usage));
3358 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3359 // * the previous reads are not hazards, and thus last_write must be visible and available to
3360 // any reads that happen after.
3361 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3362 // 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 -07003363 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003364}
3365
John Zulauf4285ee92020-09-23 10:20:52 -06003366VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const SyncOrderingBarrier &ordering) const {
3367 // Whether the stage are in the ordering scope only matters if the current write is ordered
3368 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
3369 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003370 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003371 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003372 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
3373 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
3374 }
3375
3376 return ordered_stages;
3377}
3378
3379inline ResourceAccessState::ReadState *ResourceAccessState::GetReadStateForStage(VkPipelineStageFlagBits stage,
3380 uint32_t search_limit) {
3381 ReadState *read_state = nullptr;
3382 search_limit = std::min(search_limit, last_read_count);
3383 for (uint32_t i = 0; i < search_limit; i++) {
3384 if (last_reads[i].stage == stage) {
3385 read_state = &last_reads[i];
3386 break;
3387 }
3388 }
3389 return read_state;
3390}
3391
John Zulaufd1f85d42020-04-15 12:23:15 -06003392void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003393 auto *access_context = GetAccessContextNoInsert(command_buffer);
3394 if (access_context) {
3395 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003396 }
3397}
3398
John Zulaufd1f85d42020-04-15 12:23:15 -06003399void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3400 auto access_found = cb_access_state.find(command_buffer);
3401 if (access_found != cb_access_state.end()) {
3402 access_found->second->Reset();
3403 cb_access_state.erase(access_found);
3404 }
3405}
3406
John Zulauf89311b42020-09-29 16:28:47 -06003407void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
3408 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags src_access_scope,
3409 SyncStageAccessFlags dst_access_scope, uint32_t memory_barrier_count,
3410 const VkMemoryBarrier *pMemoryBarriers, const ResourceUsageTag &tag) {
John Zulauf1e331ec2020-12-04 18:29:38 -07003411 std::vector<PipelineBarrierOp> barrier_ops;
3412 barrier_ops.reserve(std::min<uint32_t>(1, memory_barrier_count));
John Zulauf89311b42020-09-29 16:28:47 -06003413 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
3414 const auto &barrier = pMemoryBarriers[barrier_index];
3415 SyncBarrier sync_barrier(src_exec_scope, SyncStageAccess::AccessScope(src_access_scope, barrier.srcAccessMask),
3416 dst_exec_scope, SyncStageAccess::AccessScope(dst_access_scope, barrier.dstAccessMask));
John Zulauf1e331ec2020-12-04 18:29:38 -07003417 barrier_ops.emplace_back(sync_barrier, false);
John Zulauf89311b42020-09-29 16:28:47 -06003418 }
3419 if (0 == memory_barrier_count) {
3420 // If there are no global memory barriers, force an exec barrier
John Zulauf1e331ec2020-12-04 18:29:38 -07003421 barrier_ops.emplace_back(SyncBarrier(src_exec_scope, 0, dst_exec_scope, 0), false);
John Zulauf89311b42020-09-29 16:28:47 -06003422 }
John Zulauf1e331ec2020-12-04 18:29:38 -07003423 ApplyBarrierOpsFunctor<PipelineBarrierOp> barriers_functor(true /* resolve */, barrier_ops, tag);
John Zulauf540266b2020-04-06 18:54:53 -06003424 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06003425}
3426
John Zulauf540266b2020-04-06 18:54:53 -06003427void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003428 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
3429 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06003430 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003431 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003432 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06003433 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
3434 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06003435 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
3436 const ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06003437 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
3438 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06003439 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf4a6105a2020-11-17 15:11:05 -07003440 const ApplyBarrierFunctor<PipelineBarrierOp> update_action({sync_barrier, false /* layout_transition */});
John Zulauf89311b42020-09-29 16:28:47 -06003441 context->UpdateResourceAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06003442 }
3443}
3444
John Zulauf540266b2020-04-06 18:54:53 -06003445void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003446 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
3447 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06003448 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07003449 for (uint32_t index = 0; index < barrier_count; index++) {
3450 const auto &barrier = barriers[index];
3451 const auto *image = Get<IMAGE_STATE>(barrier.image);
3452 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06003453 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06003454 bool layout_transition = barrier.oldLayout != barrier.newLayout;
3455 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
3456 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06003457 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf4a6105a2020-11-17 15:11:05 -07003458 const ApplyBarrierFunctor<PipelineBarrierOp> barrier_action({sync_barrier, layout_transition});
John Zulauf89311b42020-09-29 16:28:47 -06003459 context->UpdateResourceAccess(*image, subresource_range, barrier_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06003460 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003461}
3462
3463bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3464 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3465 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003466 const auto *cb_context = GetAccessContext(commandBuffer);
3467 assert(cb_context);
3468 if (!cb_context) return skip;
3469 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003470
John Zulauf3d84f1b2020-03-09 13:33:25 -06003471 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003472 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003473 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003474
3475 for (uint32_t region = 0; region < regionCount; region++) {
3476 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003477 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003478 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003479 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003480 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003481 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003482 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003483 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003484 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003485 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003486 }
John Zulauf16adfc92020-04-08 10:28:33 -06003487 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003488 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06003489 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003490 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003491 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003492 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003493 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003494 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003495 }
3496 }
3497 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003498 }
3499 return skip;
3500}
3501
3502void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3503 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003504 auto *cb_context = GetAccessContext(commandBuffer);
3505 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003506 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003507 auto *context = cb_context->GetCurrentAccessContext();
3508
John Zulauf9cb530d2019-09-30 14:14:10 -06003509 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003510 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003511
3512 for (uint32_t region = 0; region < regionCount; region++) {
3513 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003514 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003515 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003516 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003517 }
John Zulauf16adfc92020-04-08 10:28:33 -06003518 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003519 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003520 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003521 }
3522 }
3523}
3524
John Zulauf4a6105a2020-11-17 15:11:05 -07003525void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3526 // Clear out events from the command buffer contexts
3527 for (auto &cb_context : cb_access_state) {
3528 cb_context.second->RecordDestroyEvent(event);
3529 }
3530}
3531
Jeff Leger178b1e52020-10-05 12:22:23 -04003532bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3533 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3534 bool skip = false;
3535 const auto *cb_context = GetAccessContext(commandBuffer);
3536 assert(cb_context);
3537 if (!cb_context) return skip;
3538 const auto *context = cb_context->GetCurrentAccessContext();
3539
3540 // If we have no previous accesses, we have no hazards
3541 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3542 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3543
3544 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3545 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3546 if (src_buffer) {
3547 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
3548 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
3549 if (hazard.hazard) {
3550 // TODO -- add tag information to log msg when useful.
3551 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3552 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3553 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
3554 region, string_UsageTag(hazard).c_str());
3555 }
3556 }
3557 if (dst_buffer && !skip) {
3558 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
3559 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
3560 if (hazard.hazard) {
3561 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3562 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3563 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
3564 region, string_UsageTag(hazard).c_str());
3565 }
3566 }
3567 if (skip) break;
3568 }
3569 return skip;
3570}
3571
3572void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3573 auto *cb_context = GetAccessContext(commandBuffer);
3574 assert(cb_context);
3575 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3576 auto *context = cb_context->GetCurrentAccessContext();
3577
3578 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3579 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3580
3581 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3582 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3583 if (src_buffer) {
3584 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
3585 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
3586 }
3587 if (dst_buffer) {
3588 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
3589 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
3590 }
3591 }
3592}
3593
John Zulauf5c5e88d2019-12-26 11:22:02 -07003594bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3595 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3596 const VkImageCopy *pRegions) const {
3597 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003598 const auto *cb_access_context = GetAccessContext(commandBuffer);
3599 assert(cb_access_context);
3600 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003601
John Zulauf3d84f1b2020-03-09 13:33:25 -06003602 const auto *context = cb_access_context->GetCurrentAccessContext();
3603 assert(context);
3604 if (!context) return skip;
3605
3606 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3607 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003608 for (uint32_t region = 0; region < regionCount; region++) {
3609 const auto &copy_region = pRegions[region];
3610 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003611 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003612 copy_region.srcOffset, copy_region.extent);
3613 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003614 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003615 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003616 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003617 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003618 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003619 }
3620
3621 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003622 VkExtent3D dst_copy_extent =
3623 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06003624 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003625 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003626 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003627 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003628 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003629 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003630 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003631 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003632 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003633 }
3634 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003635
John Zulauf5c5e88d2019-12-26 11:22:02 -07003636 return skip;
3637}
3638
3639void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3640 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3641 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003642 auto *cb_access_context = GetAccessContext(commandBuffer);
3643 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003644 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003645 auto *context = cb_access_context->GetCurrentAccessContext();
3646 assert(context);
3647
John Zulauf5c5e88d2019-12-26 11:22:02 -07003648 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003649 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003650
3651 for (uint32_t region = 0; region < regionCount; region++) {
3652 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003653 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003654 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
3655 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003656 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003657 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003658 VkExtent3D dst_copy_extent =
3659 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06003660 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
3661 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003662 }
3663 }
3664}
3665
Jeff Leger178b1e52020-10-05 12:22:23 -04003666bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3667 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3668 bool skip = false;
3669 const auto *cb_access_context = GetAccessContext(commandBuffer);
3670 assert(cb_access_context);
3671 if (!cb_access_context) return skip;
3672
3673 const auto *context = cb_access_context->GetCurrentAccessContext();
3674 assert(context);
3675 if (!context) return skip;
3676
3677 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3678 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3679 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3680 const auto &copy_region = pCopyImageInfo->pRegions[region];
3681 if (src_image) {
3682 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
3683 copy_region.srcOffset, copy_region.extent);
3684 if (hazard.hazard) {
3685 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3686 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3687 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
3688 region, string_UsageTag(hazard).c_str());
3689 }
3690 }
3691
3692 if (dst_image) {
3693 VkExtent3D dst_copy_extent =
3694 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
3695 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
3696 copy_region.dstOffset, dst_copy_extent);
3697 if (hazard.hazard) {
3698 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3699 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3700 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
3701 region, string_UsageTag(hazard).c_str());
3702 }
3703 if (skip) break;
3704 }
3705 }
3706
3707 return skip;
3708}
3709
3710void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3711 auto *cb_access_context = GetAccessContext(commandBuffer);
3712 assert(cb_access_context);
3713 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3714 auto *context = cb_access_context->GetCurrentAccessContext();
3715 assert(context);
3716
3717 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3718 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3719
3720 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3721 const auto &copy_region = pCopyImageInfo->pRegions[region];
3722 if (src_image) {
3723 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
3724 copy_region.extent, tag);
3725 }
3726 if (dst_image) {
3727 VkExtent3D dst_copy_extent =
3728 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
3729 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
3730 dst_copy_extent, tag);
3731 }
3732 }
3733}
3734
John Zulauf9cb530d2019-09-30 14:14:10 -06003735bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3736 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3737 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3738 uint32_t bufferMemoryBarrierCount,
3739 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3740 uint32_t imageMemoryBarrierCount,
3741 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3742 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003743 const auto *cb_access_context = GetAccessContext(commandBuffer);
3744 assert(cb_access_context);
3745 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003746
John Zulauf3d84f1b2020-03-09 13:33:25 -06003747 const auto *context = cb_access_context->GetCurrentAccessContext();
3748 assert(context);
3749 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003750
John Zulauf3d84f1b2020-03-09 13:33:25 -06003751 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003752 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3753 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07003754 // Validate Image Layout transitions
3755 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
3756 const auto &barrier = pImageMemoryBarriers[index];
3757 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
3758 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
3759 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06003760 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07003761 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003762 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003763 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003764 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003765 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003766 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07003767 }
3768 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003769
3770 return skip;
3771}
3772
3773void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3774 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3775 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3776 uint32_t bufferMemoryBarrierCount,
3777 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3778 uint32_t imageMemoryBarrierCount,
3779 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003780 auto *cb_access_context = GetAccessContext(commandBuffer);
3781 assert(cb_access_context);
3782 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06003783 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003784 auto access_context = cb_access_context->GetCurrentAccessContext();
3785 assert(access_context);
3786 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003787
John Zulauf3d84f1b2020-03-09 13:33:25 -06003788 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003789 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003790 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003791 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
3792 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3793 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf89311b42020-09-29 16:28:47 -06003794
3795 // These two apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
3796 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
3797 // of the barriers is maintained.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003798 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
3799 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06003800 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06003801 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003802
John Zulauf89311b42020-09-29 16:28:47 -06003803 // Apply the global barriers last as is it walks all memory, it can also clean up the "pending" state without requiring an
3804 // additional pass, updating the dependency chains *last* as it goes along.
3805 // This is needed to guarantee order independence of the three lists.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003806 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf89311b42020-09-29 16:28:47 -06003807 pMemoryBarriers, tag);
John Zulauf4a6105a2020-11-17 15:11:05 -07003808
3809 // Need to pass the unexpanded stage masks as the ALL_COMMANDS bit is removed during expansion.
3810 cb_access_context->ApplyGlobalBarriersToEvents(srcStageMask, src_exec_scope, dstStageMask, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06003811}
3812
3813void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3814 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3815 // The state tracker sets up the device state
3816 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3817
John Zulauf5f13a792020-03-10 07:31:21 -06003818 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3819 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003820 // TODO: Find a good way to do this hooklessly.
3821 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3822 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3823 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3824
John Zulaufd1f85d42020-04-15 12:23:15 -06003825 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3826 sync_device_state->ResetCommandBufferCallback(command_buffer);
3827 });
3828 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3829 sync_device_state->FreeCommandBufferCallback(command_buffer);
3830 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003831}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003832
John Zulauf355e49b2020-04-24 15:11:15 -06003833bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003834 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003835 bool skip = false;
3836 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
3837 auto cb_context = GetAccessContext(commandBuffer);
3838
3839 if (rp_state && cb_context) {
3840 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
3841 }
3842
3843 return skip;
3844}
3845
3846bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3847 VkSubpassContents contents) const {
3848 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3849 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3850 subpass_begin_info.contents = contents;
3851 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
3852 return skip;
3853}
3854
3855bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003856 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003857 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3858 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
3859 return skip;
3860}
3861
3862bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3863 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003864 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003865 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3866 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3867 return skip;
3868}
3869
John Zulauf3d84f1b2020-03-09 13:33:25 -06003870void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3871 VkResult result) {
3872 // The state tracker sets up the command buffer state
3873 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3874
3875 // Create/initialize the structure that trackers accesses at the command buffer scope.
3876 auto cb_access_context = GetAccessContext(commandBuffer);
3877 assert(cb_access_context);
3878 cb_access_context->Reset();
3879}
3880
3881void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003882 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003883 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003884 if (cb_context) {
3885 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003886 }
3887}
3888
3889void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3890 VkSubpassContents contents) {
3891 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3892 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3893 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003894 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003895}
3896
3897void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3898 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3899 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003900 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003901}
3902
3903void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3904 const VkRenderPassBeginInfo *pRenderPassBegin,
3905 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3906 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003907 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3908}
3909
Mike Schuchardt2df08912020-12-15 16:28:09 -08003910bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3911 const VkSubpassEndInfo *pSubpassEndInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003912 bool skip = false;
3913
3914 auto cb_context = GetAccessContext(commandBuffer);
3915 assert(cb_context);
3916 auto cb_state = cb_context->GetCommandBufferState();
3917 if (!cb_state) return skip;
3918
3919 auto rp_state = cb_state->activeRenderPass;
3920 if (!rp_state) return skip;
3921
3922 skip |= cb_context->ValidateNextSubpass(func_name);
3923
3924 return skip;
3925}
3926
3927bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3928 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
3929 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3930 subpass_begin_info.contents = contents;
3931 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3932 return skip;
3933}
3934
Mike Schuchardt2df08912020-12-15 16:28:09 -08003935bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3936 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003937 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3938 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3939 return skip;
3940}
3941
3942bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3943 const VkSubpassEndInfo *pSubpassEndInfo) const {
3944 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3945 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3946 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003947}
3948
3949void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003950 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003951 auto cb_context = GetAccessContext(commandBuffer);
3952 assert(cb_context);
3953 auto cb_state = cb_context->GetCommandBufferState();
3954 if (!cb_state) return;
3955
3956 auto rp_state = cb_state->activeRenderPass;
3957 if (!rp_state) return;
3958
John Zulauf355e49b2020-04-24 15:11:15 -06003959 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003960}
3961
3962void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3963 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
3964 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3965 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003966 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003967}
3968
3969void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3970 const VkSubpassEndInfo *pSubpassEndInfo) {
3971 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003972 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003973}
3974
3975void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3976 const VkSubpassEndInfo *pSubpassEndInfo) {
3977 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003978 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003979}
3980
Mike Schuchardt2df08912020-12-15 16:28:09 -08003981bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003982 const char *func_name) const {
3983 bool skip = false;
3984
3985 auto cb_context = GetAccessContext(commandBuffer);
3986 assert(cb_context);
3987 auto cb_state = cb_context->GetCommandBufferState();
3988 if (!cb_state) return skip;
3989
3990 auto rp_state = cb_state->activeRenderPass;
3991 if (!rp_state) return skip;
3992
3993 skip |= cb_context->ValidateEndRenderpass(func_name);
3994 return skip;
3995}
3996
3997bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3998 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
3999 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
4000 return skip;
4001}
4002
Mike Schuchardt2df08912020-12-15 16:28:09 -08004003bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06004004 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
4005 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
4006 return skip;
4007}
4008
4009bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08004010 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06004011 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
4012 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
4013 return skip;
4014}
4015
4016void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
4017 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06004018 // Resolve the all subpass contexts to the command buffer contexts
4019 auto cb_context = GetAccessContext(commandBuffer);
4020 assert(cb_context);
4021 auto cb_state = cb_context->GetCommandBufferState();
4022 if (!cb_state) return;
4023
locke-lunargaecf2152020-05-12 17:15:41 -06004024 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06004025 if (!rp_state) return;
4026
John Zulauf355e49b2020-04-24 15:11:15 -06004027 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06004028}
John Zulauf3d84f1b2020-03-09 13:33:25 -06004029
John Zulauf33fc1d52020-07-17 11:01:10 -06004030// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
4031// updates to a resource which do not conflict at the byte level.
4032// TODO: Revisit this rule to see if it needs to be tighter or looser
4033// TODO: Add programatic control over suppression heuristics
4034bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
4035 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
4036}
4037
John Zulauf3d84f1b2020-03-09 13:33:25 -06004038void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004039 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06004040 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004041}
4042
4043void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06004044 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06004045 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004046}
4047
4048void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06004049 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06004050 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004051}
locke-lunarga19c71d2020-03-02 18:17:04 -07004052
Jeff Leger178b1e52020-10-05 12:22:23 -04004053template <typename BufferImageCopyRegionType>
4054bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4055 VkImageLayout dstImageLayout, uint32_t regionCount,
4056 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004057 bool skip = false;
4058 const auto *cb_access_context = GetAccessContext(commandBuffer);
4059 assert(cb_access_context);
4060 if (!cb_access_context) return skip;
4061
Jeff Leger178b1e52020-10-05 12:22:23 -04004062 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4063 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
4064
locke-lunarga19c71d2020-03-02 18:17:04 -07004065 const auto *context = cb_access_context->GetCurrentAccessContext();
4066 assert(context);
4067 if (!context) return skip;
4068
4069 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07004070 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4071
4072 for (uint32_t region = 0; region < regionCount; region++) {
4073 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06004074 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004075 ResourceAccessRange src_range =
4076 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004077 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07004078 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06004079 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06004080 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004081 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004082 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004083 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004084 }
4085 }
4086 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004087 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004088 copy_region.imageOffset, copy_region.imageExtent);
4089 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004090 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004091 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004092 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004093 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004094 }
4095 if (skip) break;
4096 }
4097 if (skip) break;
4098 }
4099 return skip;
4100}
4101
Jeff Leger178b1e52020-10-05 12:22:23 -04004102bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4103 VkImageLayout dstImageLayout, uint32_t regionCount,
4104 const VkBufferImageCopy *pRegions) const {
4105 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
4106 COPY_COMMAND_VERSION_1);
4107}
4108
4109bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4110 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
4111 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4112 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4113 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4114}
4115
4116template <typename BufferImageCopyRegionType>
4117void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4118 VkImageLayout dstImageLayout, uint32_t regionCount,
4119 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004120 auto *cb_access_context = GetAccessContext(commandBuffer);
4121 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004122
4123 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4124 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
4125
4126 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004127 auto *context = cb_access_context->GetCurrentAccessContext();
4128 assert(context);
4129
4130 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06004131 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004132
4133 for (uint32_t region = 0; region < regionCount; region++) {
4134 const auto &copy_region = pRegions[region];
4135 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004136 ResourceAccessRange src_range =
4137 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004138 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004139 }
4140 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004141 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06004142 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004143 }
4144 }
4145}
4146
Jeff Leger178b1e52020-10-05 12:22:23 -04004147void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4148 VkImageLayout dstImageLayout, uint32_t regionCount,
4149 const VkBufferImageCopy *pRegions) {
4150 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
4151 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4152}
4153
4154void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4155 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4156 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4157 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4158 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4159 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4160}
4161
4162template <typename BufferImageCopyRegionType>
4163bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4164 VkBuffer dstBuffer, uint32_t regionCount,
4165 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004166 bool skip = false;
4167 const auto *cb_access_context = GetAccessContext(commandBuffer);
4168 assert(cb_access_context);
4169 if (!cb_access_context) return skip;
4170
Jeff Leger178b1e52020-10-05 12:22:23 -04004171 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4172 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
4173
locke-lunarga19c71d2020-03-02 18:17:04 -07004174 const auto *context = cb_access_context->GetCurrentAccessContext();
4175 assert(context);
4176 if (!context) return skip;
4177
4178 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4179 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4180 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
4181 for (uint32_t region = 0; region < regionCount; region++) {
4182 const auto &copy_region = pRegions[region];
4183 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004184 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004185 copy_region.imageOffset, copy_region.imageExtent);
4186 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004187 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004188 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004189 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004190 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004191 }
4192 }
4193 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06004194 ResourceAccessRange dst_range =
4195 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004196 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07004197 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004198 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004199 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004200 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004201 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004202 }
4203 }
4204 if (skip) break;
4205 }
4206 return skip;
4207}
4208
Jeff Leger178b1e52020-10-05 12:22:23 -04004209bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4210 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4211 const VkBufferImageCopy *pRegions) const {
4212 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
4213 COPY_COMMAND_VERSION_1);
4214}
4215
4216bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4217 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4218 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4219 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4220 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4221}
4222
4223template <typename BufferImageCopyRegionType>
4224void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4225 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
4226 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004227 auto *cb_access_context = GetAccessContext(commandBuffer);
4228 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004229
4230 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4231 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
4232
4233 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004234 auto *context = cb_access_context->GetCurrentAccessContext();
4235 assert(context);
4236
4237 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004238 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4239 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 -06004240 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004241
4242 for (uint32_t region = 0; region < regionCount; region++) {
4243 const auto &copy_region = pRegions[region];
4244 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004245 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06004246 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004247 }
4248 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004249 ResourceAccessRange dst_range =
4250 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004251 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004252 }
4253 }
4254}
4255
Jeff Leger178b1e52020-10-05 12:22:23 -04004256void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4257 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4258 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
4259 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4260}
4261
4262void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4263 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4264 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4265 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4266 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4267 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4268}
4269
4270template <typename RegionType>
4271bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4272 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4273 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004274 bool skip = false;
4275 const auto *cb_access_context = GetAccessContext(commandBuffer);
4276 assert(cb_access_context);
4277 if (!cb_access_context) return skip;
4278
4279 const auto *context = cb_access_context->GetCurrentAccessContext();
4280 assert(context);
4281 if (!context) return skip;
4282
4283 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4284 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4285
4286 for (uint32_t region = 0; region < regionCount; region++) {
4287 const auto &blit_region = pRegions[region];
4288 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004289 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4290 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4291 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4292 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4293 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4294 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
4295 auto hazard =
4296 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004297 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004298 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004299 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004300 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004301 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004302 }
4303 }
4304
4305 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004306 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4307 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4308 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4309 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4310 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4311 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
4312 auto hazard =
4313 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004314 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004315 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004316 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004317 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004318 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004319 }
4320 if (skip) break;
4321 }
4322 }
4323
4324 return skip;
4325}
4326
Jeff Leger178b1e52020-10-05 12:22:23 -04004327bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4328 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4329 const VkImageBlit *pRegions, VkFilter filter) const {
4330 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4331 "vkCmdBlitImage");
4332}
4333
4334bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4335 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4336 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4337 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4338 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4339}
4340
4341template <typename RegionType>
4342void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4343 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4344 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004345 auto *cb_access_context = GetAccessContext(commandBuffer);
4346 assert(cb_access_context);
4347 auto *context = cb_access_context->GetCurrentAccessContext();
4348 assert(context);
4349
4350 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004351 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004352
4353 for (uint32_t region = 0; region < regionCount; region++) {
4354 const auto &blit_region = pRegions[region];
4355 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004356 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4357 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4358 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4359 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4360 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4361 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
4362 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004363 }
4364 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004365 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4366 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4367 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4368 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4369 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4370 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
4371 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004372 }
4373 }
4374}
locke-lunarg36ba2592020-04-03 09:42:04 -06004375
Jeff Leger178b1e52020-10-05 12:22:23 -04004376void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4377 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4378 const VkImageBlit *pRegions, VkFilter filter) {
4379 auto *cb_access_context = GetAccessContext(commandBuffer);
4380 assert(cb_access_context);
4381 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4382 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4383 pRegions, filter);
4384 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4385}
4386
4387void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4388 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4389 auto *cb_access_context = GetAccessContext(commandBuffer);
4390 assert(cb_access_context);
4391 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4392 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4393 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4394 pBlitImageInfo->filter, tag);
4395}
4396
locke-lunarg61870c22020-06-09 14:51:50 -06004397bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
4398 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
4399 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004400 bool skip = false;
4401 if (drawCount == 0) return skip;
4402
4403 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4404 VkDeviceSize size = struct_size;
4405 if (drawCount == 1 || stride == size) {
4406 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004407 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004408 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4409 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004410 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004411 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004412 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06004413 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004414 }
4415 } else {
4416 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004417 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004418 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4419 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004420 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004421 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4422 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
4423 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004424 break;
4425 }
4426 }
4427 }
4428 return skip;
4429}
4430
locke-lunarg61870c22020-06-09 14:51:50 -06004431void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
4432 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4433 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06004434 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4435 VkDeviceSize size = struct_size;
4436 if (drawCount == 1 || stride == size) {
4437 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004438 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004439 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
4440 } else {
4441 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004442 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004443 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
4444 }
4445 }
4446}
4447
locke-lunarg61870c22020-06-09 14:51:50 -06004448bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
4449 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004450 bool skip = false;
4451
4452 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004453 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004454 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4455 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004456 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004457 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004458 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06004459 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004460 }
4461 return skip;
4462}
4463
locke-lunarg61870c22020-06-09 14:51:50 -06004464void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06004465 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004466 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004467 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
4468}
4469
locke-lunarg36ba2592020-04-03 09:42:04 -06004470bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004471 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004472 const auto *cb_access_context = GetAccessContext(commandBuffer);
4473 assert(cb_access_context);
4474 if (!cb_access_context) return skip;
4475
locke-lunarg61870c22020-06-09 14:51:50 -06004476 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004477 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004478}
4479
4480void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004481 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004482 auto *cb_access_context = GetAccessContext(commandBuffer);
4483 assert(cb_access_context);
4484 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004485
locke-lunarg61870c22020-06-09 14:51:50 -06004486 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004487}
locke-lunarge1a67022020-04-29 00:15:36 -06004488
4489bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004490 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004491 const auto *cb_access_context = GetAccessContext(commandBuffer);
4492 assert(cb_access_context);
4493 if (!cb_access_context) return skip;
4494
4495 const auto *context = cb_access_context->GetCurrentAccessContext();
4496 assert(context);
4497 if (!context) return skip;
4498
locke-lunarg61870c22020-06-09 14:51:50 -06004499 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
4500 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
4501 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004502 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004503}
4504
4505void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004506 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004507 auto *cb_access_context = GetAccessContext(commandBuffer);
4508 assert(cb_access_context);
4509 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4510 auto *context = cb_access_context->GetCurrentAccessContext();
4511 assert(context);
4512
locke-lunarg61870c22020-06-09 14:51:50 -06004513 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4514 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004515}
4516
4517bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4518 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004519 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004520 const auto *cb_access_context = GetAccessContext(commandBuffer);
4521 assert(cb_access_context);
4522 if (!cb_access_context) return skip;
4523
locke-lunarg61870c22020-06-09 14:51:50 -06004524 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4525 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4526 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004527 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004528}
4529
4530void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4531 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004532 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004533 auto *cb_access_context = GetAccessContext(commandBuffer);
4534 assert(cb_access_context);
4535 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004536
locke-lunarg61870c22020-06-09 14:51:50 -06004537 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4538 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4539 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004540}
4541
4542bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4543 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004544 bool skip = false;
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
locke-lunarg61870c22020-06-09 14:51:50 -06004549 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4550 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4551 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004552 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004553}
4554
4555void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4556 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004557 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004558 auto *cb_access_context = GetAccessContext(commandBuffer);
4559 assert(cb_access_context);
4560 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004561
locke-lunarg61870c22020-06-09 14:51:50 -06004562 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4563 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4564 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004565}
4566
4567bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4568 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004569 bool skip = false;
4570 if (drawCount == 0) return skip;
4571
locke-lunargff255f92020-05-13 18:53:52 -06004572 const auto *cb_access_context = GetAccessContext(commandBuffer);
4573 assert(cb_access_context);
4574 if (!cb_access_context) return skip;
4575
4576 const auto *context = cb_access_context->GetCurrentAccessContext();
4577 assert(context);
4578 if (!context) return skip;
4579
locke-lunarg61870c22020-06-09 14:51:50 -06004580 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4581 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
4582 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
4583 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004584
4585 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4586 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4587 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004588 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004589 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004590}
4591
4592void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4593 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004594 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004595 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004596 auto *cb_access_context = GetAccessContext(commandBuffer);
4597 assert(cb_access_context);
4598 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4599 auto *context = cb_access_context->GetCurrentAccessContext();
4600 assert(context);
4601
locke-lunarg61870c22020-06-09 14:51:50 -06004602 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4603 cb_access_context->RecordDrawSubpassAttachment(tag);
4604 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004605
4606 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4607 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4608 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004609 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004610}
4611
4612bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4613 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004614 bool skip = false;
4615 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004616 const auto *cb_access_context = GetAccessContext(commandBuffer);
4617 assert(cb_access_context);
4618 if (!cb_access_context) return skip;
4619
4620 const auto *context = cb_access_context->GetCurrentAccessContext();
4621 assert(context);
4622 if (!context) return skip;
4623
locke-lunarg61870c22020-06-09 14:51:50 -06004624 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4625 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
4626 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
4627 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004628
4629 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4630 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4631 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004632 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004633 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004634}
4635
4636void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4637 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004638 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004639 auto *cb_access_context = GetAccessContext(commandBuffer);
4640 assert(cb_access_context);
4641 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4642 auto *context = cb_access_context->GetCurrentAccessContext();
4643 assert(context);
4644
locke-lunarg61870c22020-06-09 14:51:50 -06004645 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4646 cb_access_context->RecordDrawSubpassAttachment(tag);
4647 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004648
4649 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4650 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4651 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004652 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004653}
4654
4655bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4656 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4657 uint32_t stride, const char *function) const {
4658 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004659 const auto *cb_access_context = GetAccessContext(commandBuffer);
4660 assert(cb_access_context);
4661 if (!cb_access_context) return skip;
4662
4663 const auto *context = cb_access_context->GetCurrentAccessContext();
4664 assert(context);
4665 if (!context) return skip;
4666
locke-lunarg61870c22020-06-09 14:51:50 -06004667 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4668 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
4669 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
4670 function);
4671 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004672
4673 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4674 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4675 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004676 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004677 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004678}
4679
4680bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4681 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4682 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004683 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4684 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004685}
4686
4687void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4688 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4689 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004690 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4691 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004692 auto *cb_access_context = GetAccessContext(commandBuffer);
4693 assert(cb_access_context);
4694 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
4695 auto *context = cb_access_context->GetCurrentAccessContext();
4696 assert(context);
4697
locke-lunarg61870c22020-06-09 14:51:50 -06004698 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4699 cb_access_context->RecordDrawSubpassAttachment(tag);
4700 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4701 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004702
4703 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4704 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4705 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004706 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004707}
4708
4709bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4710 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4711 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004712 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4713 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004714}
4715
4716void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4717 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4718 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004719 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4720 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004721 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004722}
4723
4724bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4725 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4726 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004727 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4728 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004729}
4730
4731void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4732 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4733 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004734 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4735 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004736 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4737}
4738
4739bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4740 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4741 uint32_t stride, const char *function) const {
4742 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004743 const auto *cb_access_context = GetAccessContext(commandBuffer);
4744 assert(cb_access_context);
4745 if (!cb_access_context) return skip;
4746
4747 const auto *context = cb_access_context->GetCurrentAccessContext();
4748 assert(context);
4749 if (!context) return skip;
4750
locke-lunarg61870c22020-06-09 14:51:50 -06004751 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4752 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
4753 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
4754 stride, function);
4755 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004756
4757 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4758 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4759 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004760 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004761 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004762}
4763
4764bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4765 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4766 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004767 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4768 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004769}
4770
4771void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4772 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4773 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004774 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4775 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004776 auto *cb_access_context = GetAccessContext(commandBuffer);
4777 assert(cb_access_context);
4778 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4779 auto *context = cb_access_context->GetCurrentAccessContext();
4780 assert(context);
4781
locke-lunarg61870c22020-06-09 14:51:50 -06004782 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4783 cb_access_context->RecordDrawSubpassAttachment(tag);
4784 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4785 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004786
4787 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4788 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004789 // We will update the index and vertex buffer in SubmitQueue in the future.
4790 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004791}
4792
4793bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4794 VkDeviceSize offset, VkBuffer countBuffer,
4795 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4796 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004797 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4798 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004799}
4800
4801void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4802 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4803 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004804 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4805 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004806 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4807}
4808
4809bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4810 VkDeviceSize offset, VkBuffer countBuffer,
4811 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4812 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004813 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4814 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004815}
4816
4817void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4818 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4819 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004820 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4821 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004822 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4823}
4824
4825bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4826 const VkClearColorValue *pColor, uint32_t rangeCount,
4827 const VkImageSubresourceRange *pRanges) const {
4828 bool skip = false;
4829 const auto *cb_access_context = GetAccessContext(commandBuffer);
4830 assert(cb_access_context);
4831 if (!cb_access_context) return skip;
4832
4833 const auto *context = cb_access_context->GetCurrentAccessContext();
4834 assert(context);
4835 if (!context) return skip;
4836
4837 const auto *image_state = Get<IMAGE_STATE>(image);
4838
4839 for (uint32_t index = 0; index < rangeCount; index++) {
4840 const auto &range = pRanges[index];
4841 if (image_state) {
4842 auto hazard =
4843 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4844 if (hazard.hazard) {
4845 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004846 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004847 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004848 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004849 }
4850 }
4851 }
4852 return skip;
4853}
4854
4855void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4856 const VkClearColorValue *pColor, uint32_t rangeCount,
4857 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004858 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004859 auto *cb_access_context = GetAccessContext(commandBuffer);
4860 assert(cb_access_context);
4861 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4862 auto *context = cb_access_context->GetCurrentAccessContext();
4863 assert(context);
4864
4865 const auto *image_state = Get<IMAGE_STATE>(image);
4866
4867 for (uint32_t index = 0; index < rangeCount; index++) {
4868 const auto &range = pRanges[index];
4869 if (image_state) {
4870 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4871 tag);
4872 }
4873 }
4874}
4875
4876bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4877 VkImageLayout imageLayout,
4878 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4879 const VkImageSubresourceRange *pRanges) const {
4880 bool skip = false;
4881 const auto *cb_access_context = GetAccessContext(commandBuffer);
4882 assert(cb_access_context);
4883 if (!cb_access_context) return skip;
4884
4885 const auto *context = cb_access_context->GetCurrentAccessContext();
4886 assert(context);
4887 if (!context) return skip;
4888
4889 const auto *image_state = Get<IMAGE_STATE>(image);
4890
4891 for (uint32_t index = 0; index < rangeCount; index++) {
4892 const auto &range = pRanges[index];
4893 if (image_state) {
4894 auto hazard =
4895 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4896 if (hazard.hazard) {
4897 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004898 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004899 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004900 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004901 }
4902 }
4903 }
4904 return skip;
4905}
4906
4907void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4908 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4909 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004910 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004911 auto *cb_access_context = GetAccessContext(commandBuffer);
4912 assert(cb_access_context);
4913 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4914 auto *context = cb_access_context->GetCurrentAccessContext();
4915 assert(context);
4916
4917 const auto *image_state = Get<IMAGE_STATE>(image);
4918
4919 for (uint32_t index = 0; index < rangeCount; index++) {
4920 const auto &range = pRanges[index];
4921 if (image_state) {
4922 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4923 tag);
4924 }
4925 }
4926}
4927
4928bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4929 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4930 VkDeviceSize dstOffset, VkDeviceSize stride,
4931 VkQueryResultFlags flags) const {
4932 bool skip = false;
4933 const auto *cb_access_context = GetAccessContext(commandBuffer);
4934 assert(cb_access_context);
4935 if (!cb_access_context) return skip;
4936
4937 const auto *context = cb_access_context->GetCurrentAccessContext();
4938 assert(context);
4939 if (!context) return skip;
4940
4941 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4942
4943 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004944 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004945 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4946 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004947 skip |=
4948 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4949 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4950 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004951 }
4952 }
locke-lunargff255f92020-05-13 18:53:52 -06004953
4954 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004955 return skip;
4956}
4957
4958void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4959 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4960 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004961 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4962 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004963 auto *cb_access_context = GetAccessContext(commandBuffer);
4964 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004965 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004966 auto *context = cb_access_context->GetCurrentAccessContext();
4967 assert(context);
4968
4969 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4970
4971 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004972 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004973 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4974 }
locke-lunargff255f92020-05-13 18:53:52 -06004975
4976 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004977}
4978
4979bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4980 VkDeviceSize size, uint32_t data) const {
4981 bool skip = false;
4982 const auto *cb_access_context = GetAccessContext(commandBuffer);
4983 assert(cb_access_context);
4984 if (!cb_access_context) return skip;
4985
4986 const auto *context = cb_access_context->GetCurrentAccessContext();
4987 assert(context);
4988 if (!context) return skip;
4989
4990 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4991
4992 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004993 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004994 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4995 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004996 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004997 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004998 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004999 }
5000 }
5001 return skip;
5002}
5003
5004void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5005 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005006 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06005007 auto *cb_access_context = GetAccessContext(commandBuffer);
5008 assert(cb_access_context);
5009 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
5010 auto *context = cb_access_context->GetCurrentAccessContext();
5011 assert(context);
5012
5013 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5014
5015 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005016 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06005017 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
5018 }
5019}
5020
5021bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5022 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5023 const VkImageResolve *pRegions) const {
5024 bool skip = false;
5025 const auto *cb_access_context = GetAccessContext(commandBuffer);
5026 assert(cb_access_context);
5027 if (!cb_access_context) return skip;
5028
5029 const auto *context = cb_access_context->GetCurrentAccessContext();
5030 assert(context);
5031 if (!context) return skip;
5032
5033 const auto *src_image = Get<IMAGE_STATE>(srcImage);
5034 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
5035
5036 for (uint32_t region = 0; region < regionCount; region++) {
5037 const auto &resolve_region = pRegions[region];
5038 if (src_image) {
5039 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5040 resolve_region.srcOffset, resolve_region.extent);
5041 if (hazard.hazard) {
5042 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005043 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005044 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06005045 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005046 }
5047 }
5048
5049 if (dst_image) {
5050 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5051 resolve_region.dstOffset, resolve_region.extent);
5052 if (hazard.hazard) {
5053 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005054 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005055 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06005056 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005057 }
5058 if (skip) break;
5059 }
5060 }
5061
5062 return skip;
5063}
5064
5065void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5066 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5067 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005068 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
5069 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06005070 auto *cb_access_context = GetAccessContext(commandBuffer);
5071 assert(cb_access_context);
5072 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
5073 auto *context = cb_access_context->GetCurrentAccessContext();
5074 assert(context);
5075
5076 auto *src_image = Get<IMAGE_STATE>(srcImage);
5077 auto *dst_image = Get<IMAGE_STATE>(dstImage);
5078
5079 for (uint32_t region = 0; region < regionCount; region++) {
5080 const auto &resolve_region = pRegions[region];
5081 if (src_image) {
5082 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5083 resolve_region.srcOffset, resolve_region.extent, tag);
5084 }
5085 if (dst_image) {
5086 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5087 resolve_region.dstOffset, resolve_region.extent, tag);
5088 }
5089 }
5090}
5091
Jeff Leger178b1e52020-10-05 12:22:23 -04005092bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5093 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
5094 bool skip = false;
5095 const auto *cb_access_context = GetAccessContext(commandBuffer);
5096 assert(cb_access_context);
5097 if (!cb_access_context) return skip;
5098
5099 const auto *context = cb_access_context->GetCurrentAccessContext();
5100 assert(context);
5101 if (!context) return skip;
5102
5103 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5104 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5105
5106 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5107 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5108 if (src_image) {
5109 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5110 resolve_region.srcOffset, resolve_region.extent);
5111 if (hazard.hazard) {
5112 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
5113 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
5114 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
5115 region, string_UsageTag(hazard).c_str());
5116 }
5117 }
5118
5119 if (dst_image) {
5120 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5121 resolve_region.dstOffset, resolve_region.extent);
5122 if (hazard.hazard) {
5123 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
5124 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
5125 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
5126 region, string_UsageTag(hazard).c_str());
5127 }
5128 if (skip) break;
5129 }
5130 }
5131
5132 return skip;
5133}
5134
5135void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5136 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5137 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5138 auto *cb_access_context = GetAccessContext(commandBuffer);
5139 assert(cb_access_context);
5140 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
5141 auto *context = cb_access_context->GetCurrentAccessContext();
5142 assert(context);
5143
5144 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5145 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5146
5147 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5148 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5149 if (src_image) {
5150 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5151 resolve_region.srcOffset, resolve_region.extent, tag);
5152 }
5153 if (dst_image) {
5154 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5155 resolve_region.dstOffset, resolve_region.extent, tag);
5156 }
5157 }
5158}
5159
locke-lunarge1a67022020-04-29 00:15:36 -06005160bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5161 VkDeviceSize dataSize, const void *pData) const {
5162 bool skip = false;
5163 const auto *cb_access_context = GetAccessContext(commandBuffer);
5164 assert(cb_access_context);
5165 if (!cb_access_context) return skip;
5166
5167 const auto *context = cb_access_context->GetCurrentAccessContext();
5168 assert(context);
5169 if (!context) return skip;
5170
5171 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5172
5173 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005174 // VK_WHOLE_SIZE not allowed
5175 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06005176 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
5177 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005178 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005179 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06005180 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005181 }
5182 }
5183 return skip;
5184}
5185
5186void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5187 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005188 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005189 auto *cb_access_context = GetAccessContext(commandBuffer);
5190 assert(cb_access_context);
5191 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5192 auto *context = cb_access_context->GetCurrentAccessContext();
5193 assert(context);
5194
5195 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5196
5197 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005198 // VK_WHOLE_SIZE not allowed
5199 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06005200 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
5201 }
5202}
locke-lunargff255f92020-05-13 18:53:52 -06005203
5204bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5205 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5206 bool skip = false;
5207 const auto *cb_access_context = GetAccessContext(commandBuffer);
5208 assert(cb_access_context);
5209 if (!cb_access_context) return skip;
5210
5211 const auto *context = cb_access_context->GetCurrentAccessContext();
5212 assert(context);
5213 if (!context) return skip;
5214
5215 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5216
5217 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005218 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06005219 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
5220 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005221 skip |=
5222 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5223 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
5224 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005225 }
5226 }
5227 return skip;
5228}
5229
5230void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5231 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005232 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005233 auto *cb_access_context = GetAccessContext(commandBuffer);
5234 assert(cb_access_context);
5235 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5236 auto *context = cb_access_context->GetCurrentAccessContext();
5237 assert(context);
5238
5239 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5240
5241 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005242 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06005243 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
5244 }
5245}
John Zulauf49beb112020-11-04 16:06:31 -07005246
5247bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5248 bool skip = false;
5249 const auto *cb_context = GetAccessContext(commandBuffer);
5250 assert(cb_context);
5251 if (!cb_context) return skip;
5252
5253 return cb_context->ValidateSetEvent(commandBuffer, event, stageMask);
5254}
5255
5256void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5257 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5258 auto *cb_context = GetAccessContext(commandBuffer);
5259 assert(cb_context);
5260 if (!cb_context) return;
John Zulauf4a6105a2020-11-17 15:11:05 -07005261 const auto tag = cb_context->NextCommandTag(CMD_SETEVENT);
5262 cb_context->RecordSetEvent(commandBuffer, event, stageMask, tag);
John Zulauf49beb112020-11-04 16:06:31 -07005263}
5264
5265bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5266 VkPipelineStageFlags stageMask) const {
5267 bool skip = false;
5268 const auto *cb_context = GetAccessContext(commandBuffer);
5269 assert(cb_context);
5270 if (!cb_context) return skip;
5271
5272 return cb_context->ValidateResetEvent(commandBuffer, event, stageMask);
5273}
5274
5275void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5276 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5277 auto *cb_context = GetAccessContext(commandBuffer);
5278 assert(cb_context);
5279 if (!cb_context) return;
5280
5281 cb_context->RecordResetEvent(commandBuffer, event, stageMask);
5282}
5283
5284bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5285 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5286 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5287 uint32_t bufferMemoryBarrierCount,
5288 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5289 uint32_t imageMemoryBarrierCount,
5290 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5291 bool skip = false;
5292 const auto *cb_context = GetAccessContext(commandBuffer);
5293 assert(cb_context);
5294 if (!cb_context) return skip;
5295
John Zulauf4a6105a2020-11-17 15:11:05 -07005296 return cb_context->ValidateWaitEvents(eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount, pMemoryBarriers,
5297 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
John Zulauf49beb112020-11-04 16:06:31 -07005298 pImageMemoryBarriers);
5299}
5300
5301void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5302 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5303 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5304 uint32_t bufferMemoryBarrierCount,
5305 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5306 uint32_t imageMemoryBarrierCount,
5307 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5308 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5309 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5310 imageMemoryBarrierCount, pImageMemoryBarriers);
5311
5312 auto *cb_context = GetAccessContext(commandBuffer);
5313 assert(cb_context);
5314 if (!cb_context) return;
5315
John Zulauf4a6105a2020-11-17 15:11:05 -07005316 const auto tag = cb_context->NextCommandTag(CMD_WAITEVENTS);
John Zulauf49beb112020-11-04 16:06:31 -07005317 cb_context->RecordWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5318 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
John Zulauf4a6105a2020-11-17 15:11:05 -07005319 pImageMemoryBarriers, tag);
5320}
5321
5322void SyncEventState::ResetFirstScope() {
5323 for (const auto address_type : kAddressTypes) {
5324 first_scope[static_cast<size_t>(address_type)].clear();
5325 }
5326 stage_mask = 0U;
5327 exec_scope = 0U;
5328 stage_accesses.reset();
5329}
5330
5331// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
5332SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(VkPipelineStageFlags srcStageMask) const {
5333 IgnoreReason reason = NotIgnored;
5334
5335 if (last_command == CMD_RESETEVENT && !HasBarrier(0U, 0U)) {
5336 reason = ResetWaitRace;
5337 } else if (unsynchronized_set) {
5338 reason = SetRace;
5339 } else {
5340 const VkPipelineStageFlags missing_bits = stage_mask_param & ~srcStageMask;
5341 if (missing_bits) reason = MissingStageBits;
5342 }
5343
5344 return reason;
5345}
5346
5347bool SyncEventState::HasBarrier(VkPipelineStageFlags stageMask, VkPipelineStageFlags exec_scope_arg) const {
5348 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5349 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5350 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005351}