blob: 2151af8c2a753f801044014d39acf949783c2525 [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
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700541 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600542 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],
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700571 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kAttachmentRasterOrder, offset, extent, aspect_mask);
John Zulauf7635de32020-05-29 17:14:15 -0600572 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
573 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
574 }
575 }
576}
577
578// Action for validating resolve operations
579class ValidateResolveAction {
580 public:
581 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
582 const char *func_name)
583 : render_pass_(render_pass),
584 subpass_(subpass),
585 context_(context),
586 sync_state_(sync_state),
587 func_name_(func_name),
588 skip_(false) {}
589 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
590 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
591 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
592 HazardResult hazard;
593 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
594 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600595 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
596 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600597 " to resolve attachment %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -0600598 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
John Zulauf37ceaed2020-07-03 16:18:15 -0600599 src_at, dst_at, string_UsageTag(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600600 }
601 }
602 // Providing a mechanism for the constructing caller to get the result of the validation
603 bool GetSkip() const { return skip_; }
604
605 private:
606 VkRenderPass render_pass_;
607 const uint32_t subpass_;
608 const AccessContext &context_;
609 const SyncValidator &sync_state_;
610 const char *func_name_;
611 bool skip_;
612};
613
614// Update action for resolve operations
615class UpdateStateResolveAction {
616 public:
617 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
618 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
619 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
620 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
621 // Ignores validation only arguments...
622 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
623 }
624
625 private:
626 AccessContext &context_;
627 const ResourceUsageTag &tag_;
628};
629
John Zulauf59e25072020-07-17 10:55:21 -0600630void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700631 const SyncStageAccessFlags &prior_, const ResourceUsageTag &tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600632 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
633 usage_index = usage_index_;
634 hazard = hazard_;
635 prior_access = prior_;
636 tag = tag_;
637}
638
John Zulauf540266b2020-04-06 18:54:53 -0600639AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
640 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600641 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600642 Reset();
643 const auto &subpass_dep = dependencies[subpass];
644 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600645 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600646 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600647 const auto prev_pass = prev_dep.first->pass;
648 const auto &prev_barriers = prev_dep.second;
649 assert(prev_dep.second.size());
650 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
651 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700652 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600653
654 async_.reserve(subpass_dep.async.size());
655 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700656 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600657 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600658 if (subpass_dep.barrier_from_external.size()) {
659 src_external_ = TrackBack(external_context, queue_flags, subpass_dep.barrier_from_external);
John Zulaufe5da6e52020-03-18 15:32:18 -0600660 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600661 if (subpass_dep.barrier_to_external.size()) {
662 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600663 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700664}
665
John Zulauf5f13a792020-03-10 07:31:21 -0600666template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700667HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600668 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600669 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600670 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600671
672 HazardResult hazard;
673 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
674 hazard = detector.Detect(prev);
675 }
676 return hazard;
677}
678
John Zulauf4a6105a2020-11-17 15:11:05 -0700679template <typename Action>
680void AccessContext::ForAll(Action &&action) {
681 for (const auto address_type : kAddressTypes) {
682 auto &accesses = GetAccessStateMap(address_type);
683 for (const auto &access : accesses) {
684 action(address_type, access);
685 }
686 }
687}
688
John Zulauf3d84f1b2020-03-09 13:33:25 -0600689// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
690// the DAG of the contexts (for example subpasses)
691template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700692HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600693 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600694 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600695
John Zulauf1a224292020-06-30 14:52:13 -0600696 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600697 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
698 // so we'll check these first
699 for (const auto &async_context : async_) {
700 hazard = async_context->DetectAsyncHazard(type, detector, range);
701 if (hazard.hazard) return hazard;
702 }
John Zulauf5f13a792020-03-10 07:31:21 -0600703 }
704
John Zulauf1a224292020-06-30 14:52:13 -0600705 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600706
John Zulauf69133422020-05-20 14:55:53 -0600707 const auto &accesses = GetAccessStateMap(type);
708 const auto from = accesses.lower_bound(range);
709 const auto to = accesses.upper_bound(range);
710 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600711
John Zulauf69133422020-05-20 14:55:53 -0600712 for (auto pos = from; pos != to; ++pos) {
713 // Cover any leading gap, or gap between entries
714 if (detect_prev) {
715 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
716 // Cover any leading gap, or gap between entries
717 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600718 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600719 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600720 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600721 if (hazard.hazard) return hazard;
722 }
John Zulauf69133422020-05-20 14:55:53 -0600723 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
724 gap.begin = pos->first.end;
725 }
726
727 hazard = detector.Detect(pos);
728 if (hazard.hazard) return hazard;
729 }
730
731 if (detect_prev) {
732 // Detect in the trailing empty as needed
733 gap.end = range.end;
734 if (gap.non_empty()) {
735 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600736 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600737 }
738
739 return hazard;
740}
741
742// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
743template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700744HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
745 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600746 auto &accesses = GetAccessStateMap(type);
747 const auto from = accesses.lower_bound(range);
748 const auto to = accesses.upper_bound(range);
749
John Zulauf3d84f1b2020-03-09 13:33:25 -0600750 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600751 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700752 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600753 }
John Zulauf16adfc92020-04-08 10:28:33 -0600754
John Zulauf3d84f1b2020-03-09 13:33:25 -0600755 return hazard;
756}
757
John Zulaufb02c1eb2020-10-06 16:33:36 -0600758struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700759 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600760 void operator()(ResourceAccessState *access) const {
761 assert(access);
762 access->ApplyBarriers(barriers, true);
763 }
764 const std::vector<SyncBarrier> &barriers;
765};
766
767struct ApplyTrackbackBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700768 explicit ApplyTrackbackBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600769 void operator()(ResourceAccessState *access) const {
770 assert(access);
771 assert(!access->HasPendingState());
772 access->ApplyBarriers(barriers, false);
773 access->ApplyPendingBarriers(kCurrentCommandTag);
774 }
775 const std::vector<SyncBarrier> &barriers;
776};
777
778// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
779// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
780// *different* map from dest.
781// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
782// range [first, last)
783template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600784static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
785 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600786 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600787 auto at = entry;
788 for (auto pos = first; pos != last; ++pos) {
789 // Every member of the input iterator range must fit within the remaining portion of entry
790 assert(at->first.includes(pos->first));
791 assert(at != dest->end());
792 // Trim up at to the same size as the entry to resolve
793 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600794 auto access = pos->second; // intentional copy
795 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600796 at->second.Resolve(access);
797 ++at; // Go to the remaining unused section of entry
798 }
799}
800
John Zulaufa0a98292020-09-18 09:30:10 -0600801static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
802 SyncBarrier merged = {};
803 for (const auto &barrier : barriers) {
804 merged.Merge(barrier);
805 }
806 return merged;
807}
808
John Zulaufb02c1eb2020-10-06 16:33:36 -0600809template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700810void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600811 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
812 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600813 if (!range.non_empty()) return;
814
John Zulauf355e49b2020-04-24 15:11:15 -0600815 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
816 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600817 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600818 if (current->pos_B->valid) {
819 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600820 auto access = src_pos->second; // intentional copy
821 barrier_action(&access);
822
John Zulauf16adfc92020-04-08 10:28:33 -0600823 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600824 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
825 trimmed->second.Resolve(access);
826 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600827 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600828 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600829 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600830 }
John Zulauf16adfc92020-04-08 10:28:33 -0600831 } else {
832 // we have to descend to fill this gap
833 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600834 if (current->pos_A->valid) {
835 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
836 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600837 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600838 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -0600839 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600840 // There isn't anything in dest in current)range, so we can accumulate directly into it.
841 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600842 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
843 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
844 barrier_action(&pos->second);
John Zulauf355e49b2020-04-24 15:11:15 -0600845 }
846 }
847 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
848 // iterator of the outer while.
849
850 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
851 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
852 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600853 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
854 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600855 current.seek(seek_to);
856 } else if (!current->pos_A->valid && infill_state) {
857 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
858 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
859 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600860 }
John Zulauf5f13a792020-03-10 07:31:21 -0600861 }
John Zulauf16adfc92020-04-08 10:28:33 -0600862 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600863 }
John Zulauf1a224292020-06-30 14:52:13 -0600864
865 // Infill if range goes passed both the current and resolve map prior contents
866 if (recur_to_infill && (current->range.end < range.end)) {
867 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
868 ResourceAccessRangeMap gap_map;
869 const auto the_end = resolve_map->end();
870 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
871 for (auto &access : gap_map) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600872 barrier_action(&access.second);
John Zulauf1a224292020-06-30 14:52:13 -0600873 resolve_map->insert(the_end, access);
874 }
875 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600876}
877
John Zulauf43cc7462020-12-03 12:33:12 -0700878void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
879 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600880 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600881 if (range.non_empty() && infill_state) {
882 descent_map->insert(std::make_pair(range, *infill_state));
883 }
884 } else {
885 // Look for something to fill the gap further along.
886 for (const auto &prev_dep : prev_) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600887 const ApplyTrackbackBarriersAction barrier_action(prev_dep.barriers);
888 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600889 }
890
John Zulaufe5da6e52020-03-18 15:32:18 -0600891 if (src_external_.context) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600892 const ApplyTrackbackBarriersAction barrier_action(src_external_.barriers);
893 src_external_.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600894 }
895 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600896}
897
John Zulauf4a6105a2020-11-17 15:11:05 -0700898// Non-lazy import of all accesses, WaitEvents needs this.
899void AccessContext::ResolvePreviousAccesses() {
900 ResourceAccessState default_state;
901 for (const auto address_type : kAddressTypes) {
902 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
903 }
904}
905
John Zulauf43cc7462020-12-03 12:33:12 -0700906AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
907 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -0600908}
909
John Zulauf1507ee42020-05-18 11:33:09 -0600910static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
911 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
912 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
913 return stage_access;
914}
915static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
916 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
917 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
918 return stage_access;
919}
920
John Zulauf7635de32020-05-29 17:14:15 -0600921// Caller must manage returned pointer
922static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
923 uint32_t subpass, const VkRect2D &render_area,
924 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
925 auto *proxy = new AccessContext(context);
926 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600927 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600928 return proxy;
929}
930
John Zulaufb02c1eb2020-10-06 16:33:36 -0600931template <typename BarrierAction>
John Zulauf52446eb2020-10-22 16:40:08 -0600932class ResolveAccessRangeFunctor {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600933 public:
John Zulauf43cc7462020-12-03 12:33:12 -0700934 ResolveAccessRangeFunctor(const AccessContext &context, AccessAddressType address_type, ResourceAccessRangeMap *descent_map,
935 const ResourceAccessState *infill_state, BarrierAction &barrier_action)
John Zulauf52446eb2020-10-22 16:40:08 -0600936 : context_(context),
937 address_type_(address_type),
938 descent_map_(descent_map),
939 infill_state_(infill_state),
940 barrier_action_(barrier_action) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600941 ResolveAccessRangeFunctor() = delete;
942 void operator()(const ResourceAccessRange &range) const {
943 context_.ResolveAccessRange(address_type_, range, barrier_action_, descent_map_, infill_state_);
944 }
945
946 private:
John Zulauf52446eb2020-10-22 16:40:08 -0600947 const AccessContext &context_;
John Zulauf43cc7462020-12-03 12:33:12 -0700948 const AccessAddressType address_type_;
John Zulauf52446eb2020-10-22 16:40:08 -0600949 ResourceAccessRangeMap *const descent_map_;
950 const ResourceAccessState *infill_state_;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600951 BarrierAction &barrier_action_;
952};
953
John Zulaufb02c1eb2020-10-06 16:33:36 -0600954template <typename BarrierAction>
955void AccessContext::ResolveAccessRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -0700956 BarrierAction &barrier_action, AccessAddressType address_type,
957 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600958 const ResolveAccessRangeFunctor<BarrierAction> action(*this, address_type, descent_map, infill_state, barrier_action);
959 ApplyOverImageRange(image_state, subresource_range, action);
John Zulauf62f10592020-04-03 12:20:02 -0600960}
961
John Zulauf7635de32020-05-29 17:14:15 -0600962// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600963bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600964 const VkRect2D &render_area, uint32_t subpass,
965 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
966 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600967 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600968 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
969 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
970 // those affects have not been recorded yet.
971 //
972 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
973 // to apply and only copy then, if this proves a hot spot.
974 std::unique_ptr<AccessContext> proxy_for_prev;
975 TrackBack proxy_track_back;
976
John Zulauf355e49b2020-04-24 15:11:15 -0600977 const auto &transitions = rp_state.subpass_transitions[subpass];
978 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600979 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
980
981 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
982 if (prev_needs_proxy) {
983 if (!proxy_for_prev) {
984 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
985 render_area, attachment_views));
986 proxy_track_back = *track_back;
987 proxy_track_back.context = proxy_for_prev.get();
988 }
989 track_back = &proxy_track_back;
990 }
991 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600992 if (hazard.hazard) {
John Zulauf389c34b2020-07-28 11:19:35 -0600993 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
994 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
995 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
996 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
997 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
998 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600999 }
1000 }
1001 return skip;
1002}
1003
John Zulauf1507ee42020-05-18 11:33:09 -06001004bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001005 const VkRect2D &render_area, uint32_t subpass,
1006 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1007 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001008 bool skip = false;
1009 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1010 VkExtent3D extent = CastTo3D(render_area.extent);
1011 VkOffset3D offset = CastTo3D(render_area.offset);
John Zulaufa0a98292020-09-18 09:30:10 -06001012
John Zulauf1507ee42020-05-18 11:33:09 -06001013 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1014 if (subpass == rp_state.attachment_first_subpass[i]) {
1015 if (attachment_views[i] == nullptr) continue;
1016 const IMAGE_VIEW_STATE &view = *attachment_views[i];
1017 const IMAGE_STATE *image = view.image_state.get();
1018 if (image == nullptr) continue;
1019 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001020
1021 // Need check in the following way
1022 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1023 // vs. transition
1024 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1025 // for each aspect loaded.
1026
1027 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001028 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001029 const bool is_color = !(has_depth || has_stencil);
1030
1031 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001032 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001033
John Zulaufaff20662020-06-01 14:07:58 -06001034 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001035 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001036
John Zulaufb02c1eb2020-10-06 16:33:36 -06001037 auto hazard_range = view.normalized_subresource_range;
1038 bool checked_stencil = false;
1039 if (is_color) {
John Zulauf859089b2020-10-29 17:37:03 -06001040 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, kColorAttachmentRasterOrder, offset,
1041 extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001042 aspect = "color";
1043 } else {
1044 if (has_depth) {
1045 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf859089b2020-10-29 17:37:03 -06001046 hazard = DetectHazard(*image, load_index, hazard_range, kDepthStencilAttachmentRasterOrder, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001047 aspect = "depth";
1048 }
1049 if (!hazard.hazard && has_stencil) {
1050 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf859089b2020-10-29 17:37:03 -06001051 hazard =
1052 DetectHazard(*image, stencil_load_index, hazard_range, kDepthStencilAttachmentRasterOrder, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001053 aspect = "stencil";
1054 checked_stencil = true;
1055 }
1056 }
1057
1058 if (hazard.hazard) {
1059 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
1060 if (hazard.tag == kCurrentCommandTag) {
1061 // Hazard vs. ILT
1062 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1063 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1064 " aspect %s during load with loadOp %s.",
1065 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1066 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06001067 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1068 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001069 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001070 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -06001071 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001072 }
1073 }
1074 }
1075 }
1076 return skip;
1077}
1078
John Zulaufaff20662020-06-01 14:07:58 -06001079// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1080// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1081// store is part of the same Next/End operation.
1082// The latter is handled in layout transistion validation directly
1083bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
1084 const VkRect2D &render_area, uint32_t subpass,
1085 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1086 const char *func_name) const {
1087 bool skip = false;
1088 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1089 VkExtent3D extent = CastTo3D(render_area.extent);
1090 VkOffset3D offset = CastTo3D(render_area.offset);
1091
1092 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1093 if (subpass == rp_state.attachment_last_subpass[i]) {
1094 if (attachment_views[i] == nullptr) continue;
1095 const IMAGE_VIEW_STATE &view = *attachment_views[i];
1096 const IMAGE_STATE *image = view.image_state.get();
1097 if (image == nullptr) continue;
1098 const auto &ci = attachment_ci[i];
1099
1100 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1101 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1102 // sake, we treat DONT_CARE as writing.
1103 const bool has_depth = FormatHasDepth(ci.format);
1104 const bool has_stencil = FormatHasStencil(ci.format);
1105 const bool is_color = !(has_depth || has_stencil);
1106 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1107 if (!has_stencil && !store_op_stores) continue;
1108
1109 HazardResult hazard;
1110 const char *aspect = nullptr;
1111 bool checked_stencil = false;
1112 if (is_color) {
1113 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1114 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
1115 aspect = "color";
1116 } else {
1117 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1118 auto hazard_range = view.normalized_subresource_range;
1119 if (has_depth && store_op_stores) {
1120 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1121 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
1122 kAttachmentRasterOrder, offset, extent);
1123 aspect = "depth";
1124 }
1125 if (!hazard.hazard && has_stencil && stencil_op_stores) {
1126 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1127 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
1128 kAttachmentRasterOrder, offset, extent);
1129 aspect = "stencil";
1130 checked_stencil = true;
1131 }
1132 }
1133
1134 if (hazard.hazard) {
1135 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1136 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -06001137 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1138 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001139 " %s aspect during store with %s %s. Access info %s",
John Zulauf1dae9192020-06-16 15:46:44 -06001140 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -06001141 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001142 }
1143 }
1144 }
1145 return skip;
1146}
1147
John Zulaufb027cdb2020-05-21 14:25:22 -06001148bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
1149 const VkRect2D &render_area,
1150 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
1151 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -06001152 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
1153 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
1154 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001155}
1156
John Zulauf3d84f1b2020-03-09 13:33:25 -06001157class HazardDetector {
1158 SyncStageAccessIndex usage_index_;
1159
1160 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001161 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001162 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1163 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001164 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001165 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001166};
1167
John Zulauf69133422020-05-20 14:55:53 -06001168class HazardDetectorWithOrdering {
1169 const SyncStageAccessIndex usage_index_;
1170 const SyncOrderingBarrier &ordering_;
1171
1172 public:
1173 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1174 return pos->second.DetectHazard(usage_index_, ordering_);
1175 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001176 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1177 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001178 }
1179 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
1180 : usage_index_(usage), ordering_(ordering) {}
1181};
1182
John Zulauf16adfc92020-04-08 10:28:33 -06001183HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001184 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001185 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001186 const auto base_address = ResourceBaseAddress(buffer);
1187 HazardDetector detector(usage_index);
1188 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001189}
1190
John Zulauf69133422020-05-20 14:55:53 -06001191template <typename Detector>
1192HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1193 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1194 const VkExtent3D &extent, DetectOptions options) const {
1195 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001196 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001197 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1198 base_address);
1199 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001200 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001201 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001202 if (hazard.hazard) return hazard;
1203 }
1204 return HazardResult();
1205}
1206
John Zulauf540266b2020-04-06 18:54:53 -06001207HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1208 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1209 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001210 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1211 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -06001212 return DetectHazard(image, current_usage, subresource_range, offset, extent);
1213}
1214
1215HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1216 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1217 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001218 HazardDetector detector(current_usage);
1219 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1220}
1221
1222HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1223 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
1224 const VkOffset3D &offset, const VkExtent3D &extent) const {
1225 HazardDetectorWithOrdering detector(current_usage, ordering);
1226 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001227}
1228
John Zulaufb027cdb2020-05-21 14:25:22 -06001229// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1230// should have reported the issue regarding an invalid attachment entry
1231HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
1232 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
1233 VkImageAspectFlags aspect_mask) const {
1234 if (view != nullptr) {
1235 const IMAGE_STATE *image = view->image_state.get();
1236 if (image != nullptr) {
1237 auto *detect_range = &view->normalized_subresource_range;
1238 VkImageSubresourceRange masked_range;
1239 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1240 masked_range = view->normalized_subresource_range;
1241 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1242 detect_range = &masked_range;
1243 }
1244
1245 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1246 if (detect_range->aspectMask) {
1247 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
1248 }
1249 }
1250 }
1251 return HazardResult();
1252}
John Zulauf43cc7462020-12-03 12:33:12 -07001253
John Zulauf3d84f1b2020-03-09 13:33:25 -06001254class BarrierHazardDetector {
1255 public:
1256 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1257 SyncStageAccessFlags src_access_scope)
1258 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1259
John Zulauf5f13a792020-03-10 07:31:21 -06001260 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1261 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001262 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001263 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001264 // 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 -07001265 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001266 }
1267
1268 private:
1269 SyncStageAccessIndex usage_index_;
1270 VkPipelineStageFlags src_exec_scope_;
1271 SyncStageAccessFlags src_access_scope_;
1272};
1273
John Zulauf4a6105a2020-11-17 15:11:05 -07001274class EventBarrierHazardDetector {
1275 public:
1276 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1277 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
1278 const ResourceUsageTag &scope_tag)
1279 : usage_index_(usage_index),
1280 src_exec_scope_(src_exec_scope),
1281 src_access_scope_(src_access_scope),
1282 event_scope_(event_scope),
1283 scope_pos_(event_scope.cbegin()),
1284 scope_end_(event_scope.cend()),
1285 scope_tag_(scope_tag) {}
1286
1287 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1288 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1289 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1290 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1291 if (scope_pos_ == scope_end_) return HazardResult();
1292 if (!scope_pos_->first.intersects(pos->first)) {
1293 event_scope_.lower_bound(pos->first);
1294 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1295 }
1296
1297 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1298 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1299 }
1300 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1301 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1302 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1303 }
1304
1305 private:
1306 SyncStageAccessIndex usage_index_;
1307 VkPipelineStageFlags src_exec_scope_;
1308 SyncStageAccessFlags src_access_scope_;
1309 const SyncEventState::ScopeMap &event_scope_;
1310 SyncEventState::ScopeMap::const_iterator scope_pos_;
1311 SyncEventState::ScopeMap::const_iterator scope_end_;
1312 const ResourceUsageTag &scope_tag_;
1313};
1314
1315HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1316 const SyncStageAccessFlags &src_access_scope,
1317 const VkImageSubresourceRange &subresource_range,
1318 const SyncEventState &sync_event, DetectOptions options) const {
1319 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1320 // first access scope map to use, and there's no easy way to plumb it in below.
1321 const auto address_type = ImageAddressType(image);
1322 const auto &event_scope = sync_event.FirstScope(address_type);
1323
1324 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1325 event_scope, sync_event.first_scope_tag);
1326 VkOffset3D zero_offset = {0, 0, 0};
1327 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
1328}
1329
John Zulauf16adfc92020-04-08 10:28:33 -06001330HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001331 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001332 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001333 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001334 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1335 VkOffset3D zero_offset = {0, 0, 0};
1336 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001337}
1338
John Zulauf355e49b2020-04-24 15:11:15 -06001339HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001340 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001341 const VkImageMemoryBarrier &barrier) const {
1342 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1343 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1344 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1345}
1346
John Zulauf9cb530d2019-09-30 14:14:10 -06001347template <typename Flags, typename Map>
1348SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1349 SyncStageAccessFlags scope = 0;
1350 for (const auto &bit_scope : map) {
1351 if (flag_mask < bit_scope.first) break;
1352
1353 if (flag_mask & bit_scope.first) {
1354 scope |= bit_scope.second;
1355 }
1356 }
1357 return scope;
1358}
1359
1360SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1361 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1362}
1363
1364SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1365 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1366}
1367
1368// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1369SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001370 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1371 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1372 // 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 -06001373 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1374}
1375
1376template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001377void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001378 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1379 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001380 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001381 auto pos = accesses->lower_bound(range);
1382 if (pos == accesses->end() || !pos->first.intersects(range)) {
1383 // The range is empty, fill it with a default value.
1384 pos = action.Infill(accesses, pos, range);
1385 } else if (range.begin < pos->first.begin) {
1386 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001387 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001388 } else if (pos->first.begin < range.begin) {
1389 // Trim the beginning if needed
1390 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1391 ++pos;
1392 }
1393
1394 const auto the_end = accesses->end();
1395 while ((pos != the_end) && pos->first.intersects(range)) {
1396 if (pos->first.end > range.end) {
1397 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1398 }
1399
1400 pos = action(accesses, pos);
1401 if (pos == the_end) break;
1402
1403 auto next = pos;
1404 ++next;
1405 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1406 // Need to infill if next is disjoint
1407 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001408 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001409 next = action.Infill(accesses, next, new_range);
1410 }
1411 pos = next;
1412 }
1413}
John Zulauf4a6105a2020-11-17 15:11:05 -07001414template <typename Action, typename RangeGen>
1415void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1416 assert(range_gen_arg);
1417 auto &range_gen = *range_gen_arg; // Non-const references must be * by style requirement but deref-ing * iterator is a pain
1418 for (; range_gen->non_empty(); ++range_gen) {
1419 UpdateMemoryAccessState(accesses, *range_gen, action);
1420 }
1421}
John Zulauf9cb530d2019-09-30 14:14:10 -06001422
1423struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001424 using Iterator = ResourceAccessRangeMap::iterator;
1425 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001426 // this is only called on gaps, and never returns a gap.
1427 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001428 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001429 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001430 }
John Zulauf5f13a792020-03-10 07:31:21 -06001431
John Zulauf5c5e88d2019-12-26 11:22:02 -07001432 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001433 auto &access_state = pos->second;
1434 access_state.Update(usage, tag);
1435 return pos;
1436 }
1437
John Zulauf43cc7462020-12-03 12:33:12 -07001438 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001439 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001440 : type(type_), context(context_), usage(usage_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001441 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001442 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001443 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001444 const ResourceUsageTag &tag;
1445};
1446
John Zulauf4a6105a2020-11-17 15:11:05 -07001447// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001448struct PipelineBarrierOp {
1449 SyncBarrier barrier;
1450 bool layout_transition;
1451 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1452 : barrier(barrier_), layout_transition(layout_transition_) {}
1453 PipelineBarrierOp() = default;
1454 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1455};
John Zulauf4a6105a2020-11-17 15:11:05 -07001456// The barrier operation for wait events
1457struct WaitEventBarrierOp {
1458 const ResourceUsageTag *scope_tag;
1459 SyncBarrier barrier;
1460 bool layout_transition;
1461 WaitEventBarrierOp(const ResourceUsageTag &scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1462 : scope_tag(&scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
1463 WaitEventBarrierOp() = default;
1464 void operator()(ResourceAccessState *access_state) const {
1465 assert(scope_tag); // Not valid to have a non-scope op executed, default construct included for std::vector support
1466 access_state->ApplyBarrier(*scope_tag, barrier, layout_transition);
1467 }
1468};
John Zulauf1e331ec2020-12-04 18:29:38 -07001469
John Zulauf4a6105a2020-11-17 15:11:05 -07001470// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1471// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1472// of a collection is known/present.
John Zulauf1e331ec2020-12-04 18:29:38 -07001473template <typename BarrierOp>
John Zulauf89311b42020-09-29 16:28:47 -06001474class ApplyBarrierOpsFunctor {
1475 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001476 using Iterator = ResourceAccessRangeMap::iterator;
1477 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001478
John Zulauf5c5e88d2019-12-26 11:22:02 -07001479 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001480 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001481 for (const auto &op : barrier_ops_) {
1482 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001483 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001484
John Zulauf89311b42020-09-29 16:28:47 -06001485 if (resolve_) {
1486 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1487 // another walk
1488 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001489 }
1490 return pos;
1491 }
1492
John Zulauf89311b42020-09-29 16:28:47 -06001493 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulauf1e331ec2020-12-04 18:29:38 -07001494 ApplyBarrierOpsFunctor(bool resolve, const std::vector<BarrierOp> &barrier_ops, const ResourceUsageTag &tag)
1495 : resolve_(resolve), barrier_ops_(barrier_ops), tag_(tag) {}
John Zulauf89311b42020-09-29 16:28:47 -06001496
1497 private:
1498 bool resolve_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001499 const std::vector<BarrierOp> &barrier_ops_;
1500 const ResourceUsageTag &tag_;
1501};
1502
John Zulauf4a6105a2020-11-17 15:11:05 -07001503// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1504// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1505template <typename BarrierOp>
1506class ApplyBarrierFunctor {
1507 public:
1508 using Iterator = ResourceAccessRangeMap::iterator;
1509 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1510
1511 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1512 auto &access_state = pos->second;
1513 barrier_op_(&access_state);
1514 return pos;
1515 }
1516
1517 ApplyBarrierFunctor(const BarrierOp &barrier_op) : barrier_op_(barrier_op) {}
1518
1519 private:
1520 const BarrierOp barrier_op_;
1521};
1522
John Zulauf1e331ec2020-12-04 18:29:38 -07001523// This functor resolves the pendinging state.
1524class ResolvePendingBarrierFunctor {
1525 public:
1526 using Iterator = ResourceAccessRangeMap::iterator;
1527 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1528
1529 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1530 auto &access_state = pos->second;
1531 access_state.ApplyPendingBarriers(tag_);
1532 return pos;
1533 }
1534
1535 ResolvePendingBarrierFunctor(const ResourceUsageTag &tag) : tag_(tag) {}
1536
1537 private:
John Zulauf89311b42020-09-29 16:28:47 -06001538 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001539};
1540
John Zulauf43cc7462020-12-03 12:33:12 -07001541void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -06001542 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001543 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1544 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001545}
1546
John Zulauf16adfc92020-04-08 10:28:33 -06001547void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001548 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001549 if (!SimpleBinding(buffer)) return;
1550 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf43cc7462020-12-03 12:33:12 -07001551 UpdateAccessState(AccessAddressType::kLinear, current_usage, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001552}
John Zulauf355e49b2020-04-24 15:11:15 -06001553
John Zulauf540266b2020-04-06 18:54:53 -06001554void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001555 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001556 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001557 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001558 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001559 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1560 base_address);
1561 const auto address_type = ImageAddressType(image);
John Zulauf16adfc92020-04-08 10:28:33 -06001562 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001563 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001564 UpdateMemoryAccessState(&GetAccessStateMap(address_type), *range_gen, action);
John Zulauf5f13a792020-03-10 07:31:21 -06001565 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001566}
John Zulauf7635de32020-05-29 17:14:15 -06001567void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1568 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1569 if (view != nullptr) {
1570 const IMAGE_STATE *image = view->image_state.get();
1571 if (image != nullptr) {
1572 auto *update_range = &view->normalized_subresource_range;
1573 VkImageSubresourceRange masked_range;
1574 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1575 masked_range = view->normalized_subresource_range;
1576 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1577 update_range = &masked_range;
1578 }
1579 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1580 }
1581 }
1582}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001583
John Zulauf355e49b2020-04-24 15:11:15 -06001584void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1585 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1586 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001587 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1588 subresource.layerCount};
1589 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1590}
1591
John Zulauf540266b2020-04-06 18:54:53 -06001592template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001593void AccessContext::UpdateResourceAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001594 if (!SimpleBinding(buffer)) return;
1595 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf43cc7462020-12-03 12:33:12 -07001596 UpdateMemoryAccessState(&GetAccessStateMap(AccessAddressType::kLinear), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001597}
1598
1599template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001600void AccessContext::UpdateResourceAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1601 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001602 if (!SimpleBinding(image)) return;
1603 const auto address_type = ImageAddressType(image);
1604 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001605
John Zulauf16adfc92020-04-08 10:28:33 -06001606 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001607 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
1608 image.createInfo.extent, base_address);
1609
John Zulauf540266b2020-04-06 18:54:53 -06001610 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001611 UpdateMemoryAccessState(accesses, *range_gen, action);
John Zulauf540266b2020-04-06 18:54:53 -06001612 }
1613}
1614
John Zulauf7635de32020-05-29 17:14:15 -06001615void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1616 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1617 const ResourceUsageTag &tag) {
1618 UpdateStateResolveAction update(*this, tag);
1619 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1620}
1621
John Zulaufaff20662020-06-01 14:07:58 -06001622void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1623 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1624 const ResourceUsageTag &tag) {
1625 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1626 VkExtent3D extent = CastTo3D(render_area.extent);
1627 VkOffset3D offset = CastTo3D(render_area.offset);
1628
1629 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1630 if (rp_state.attachment_last_subpass[i] == subpass) {
1631 if (attachment_views[i] == nullptr) continue; // UNUSED
1632 const auto &view = *attachment_views[i];
1633 const IMAGE_STATE *image = view.image_state.get();
1634 if (image == nullptr) continue;
1635
1636 const auto &ci = attachment_ci[i];
1637 const bool has_depth = FormatHasDepth(ci.format);
1638 const bool has_stencil = FormatHasStencil(ci.format);
1639 const bool is_color = !(has_depth || has_stencil);
1640 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1641
1642 if (is_color && store_op_stores) {
1643 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1644 offset, extent, tag);
1645 } else {
1646 auto update_range = view.normalized_subresource_range;
1647 if (has_depth && store_op_stores) {
1648 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1649 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1650 tag);
1651 }
1652 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1653 if (has_stencil && stencil_op_stores) {
1654 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1655 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1656 tag);
1657 }
1658 }
1659 }
1660 }
1661}
1662
John Zulauf540266b2020-04-06 18:54:53 -06001663template <typename Action>
1664void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1665 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001666 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001667 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001668 }
1669}
1670
1671void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001672 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1673 auto &context = contexts[subpass_index];
John Zulaufb02c1eb2020-10-06 16:33:36 -06001674 ApplyTrackbackBarriersAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001675 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001676 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001677 }
1678 }
1679}
1680
John Zulauf355e49b2020-04-24 15:11:15 -06001681// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001682HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001683 if (!attach_view) return HazardResult();
1684 const auto image_state = attach_view->image_state.get();
1685 if (!image_state) return HazardResult();
1686
John Zulauf355e49b2020-04-24 15:11:15 -06001687 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001688 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001689
1690 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001691 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1692 const auto merged_barrier = MergeBarriers(track_back.barriers);
1693 HazardResult hazard =
1694 track_back.context->DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
1695 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001696 if (!hazard.hazard) {
1697 // The Async hazard check is against the current context's async set.
John Zulaufa0a98292020-09-18 09:30:10 -06001698 hazard = DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001699 attach_view->normalized_subresource_range, kDetectAsync);
1700 }
John Zulaufa0a98292020-09-18 09:30:10 -06001701
John Zulauf355e49b2020-04-24 15:11:15 -06001702 return hazard;
1703}
1704
John Zulaufb02c1eb2020-10-06 16:33:36 -06001705void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
1706 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1707 const ResourceUsageTag &tag) {
1708 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001709 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001710 for (const auto &transition : transitions) {
1711 const auto prev_pass = transition.prev_pass;
1712 const auto attachment_view = attachment_views[transition.attachment];
1713 if (!attachment_view) continue;
1714 const auto *image = attachment_view->image_state.get();
1715 if (!image) continue;
1716 if (!SimpleBinding(*image)) continue;
1717
1718 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1719 assert(trackback);
1720
1721 // Import the attachments into the current context
1722 const auto *prev_context = trackback->context;
1723 assert(prev_context);
1724 const auto address_type = ImageAddressType(*image);
1725 auto &target_map = GetAccessStateMap(address_type);
1726 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
1727 prev_context->ResolveAccessRange(*image, attachment_view->normalized_subresource_range, barrier_action, address_type,
John Zulauf646cc292020-10-23 09:16:45 -06001728 &target_map, &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001729 }
1730
John Zulauf86356ca2020-10-19 11:46:41 -06001731 // If there were no transitions skip this global map walk
1732 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001733 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulauf86356ca2020-10-19 11:46:41 -06001734 ApplyGlobalBarriers(apply_pending_action);
1735 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001736}
John Zulauf4a6105a2020-11-17 15:11:05 -07001737void CommandBufferAccessContext::ApplyBufferBarriers(const SyncEventState &sync_event, VkPipelineStageFlags dst_exec_scope,
1738 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
1739 const VkBufferMemoryBarrier *barriers) {
1740 const auto &scope_tag = sync_event.first_scope_tag;
1741 auto *access_context = GetCurrentAccessContext();
1742 const auto address_type = AccessAddressType::kLinear;
1743 for (uint32_t index = 0; index < barrier_count; index++) {
1744 auto barrier = barriers[index]; // barrier is a copy
1745 const auto *buffer = sync_state_->Get<BUFFER_STATE>(barrier.buffer);
1746 if (!buffer) continue;
1747 const auto base_address = ResourceBaseAddress(*buffer);
1748 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
1749 const ResourceAccessRange range = MakeRange(barrier) + base_address;
1750 const auto src_access_scope = SyncStageAccess::AccessScope(sync_event.stage_accesses, barrier.srcAccessMask);
1751 const auto dst_access_scope = SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1752 const SyncBarrier sync_barrier(sync_event.exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1753 const ApplyBarrierFunctor<WaitEventBarrierOp> barrier_action({scope_tag, sync_barrier, false /* layout_transition */});
1754 EventSimpleRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), range);
1755 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barrier_action, &filtered_range_gen);
1756 }
1757}
1758
1759void CommandBufferAccessContext::ApplyGlobalBarriers(SyncEventState &sync_event, VkPipelineStageFlags dstStageMask,
1760 VkPipelineStageFlags dst_exec_scope,
1761 const SyncStageAccessFlags &dst_stage_accesses, uint32_t memory_barrier_count,
1762 const VkMemoryBarrier *pMemoryBarriers, const ResourceUsageTag &tag) {
1763 std::vector<WaitEventBarrierOp> barrier_ops;
1764 barrier_ops.reserve(std::min<uint32_t>(memory_barrier_count, 1));
1765 const auto &scope_tag = sync_event.first_scope_tag;
1766 auto *access_context = GetCurrentAccessContext();
1767 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
1768 const auto &barrier = pMemoryBarriers[barrier_index];
1769 SyncBarrier sync_barrier(sync_event.exec_scope,
1770 SyncStageAccess::AccessScope(sync_event.stage_accesses, barrier.srcAccessMask), dst_exec_scope,
1771 SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
1772 barrier_ops.emplace_back(scope_tag, sync_barrier, false);
1773 }
1774 if (0 == memory_barrier_count) {
1775 // If there are no global memory barriers, force an exec barrier
1776 barrier_ops.emplace_back(scope_tag, SyncBarrier(sync_event.exec_scope, 0, dst_exec_scope, 0), false);
1777 }
1778 ApplyBarrierOpsFunctor<WaitEventBarrierOp> barriers_functor(false /* don't resolve */, barrier_ops, tag);
1779 for (const auto address_type : kAddressTypes) {
1780 EventSimpleRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), kFullRange);
1781 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &filtered_range_gen);
1782 }
1783
1784 // Apply the global barrier to the event itself (for race condition tracking)
1785 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
1786 sync_event.barriers = dstStageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1787 sync_event.barriers |= dst_exec_scope;
1788}
1789
1790void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags src_exec_scope,
1791 VkPipelineStageFlags dstStageMask,
1792 VkPipelineStageFlags dst_exec_scope) {
1793 const bool all_commands_bit = 0 != (srcStageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
1794 for (auto &event_pair : event_state_) {
1795 assert(event_pair.second); // Shouldn't be storing empty
1796 auto &sync_event = *event_pair.second;
1797 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
1798 if ((sync_event.barriers & src_exec_scope) || all_commands_bit) {
1799 sync_event.barriers |= dst_exec_scope;
1800 sync_event.barriers |= dstStageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1801 }
1802 }
1803}
1804
1805void CommandBufferAccessContext::ApplyImageBarriers(const SyncEventState &sync_event, VkPipelineStageFlags dst_exec_scope,
1806 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
1807 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
1808 const auto &scope_tag = sync_event.first_scope_tag;
1809 auto *access_context = GetCurrentAccessContext();
1810 for (uint32_t index = 0; index < barrier_count; index++) {
1811 const auto &barrier = barriers[index];
1812 const auto *image = sync_state_->Get<IMAGE_STATE>(barrier.image);
1813 if (!image) continue;
1814 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
1815 bool layout_transition = barrier.oldLayout != barrier.newLayout;
1816 const auto src_access_scope = SyncStageAccess::AccessScope(sync_event.stage_accesses, barrier.srcAccessMask);
1817 const auto dst_access_scope = SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1818 const SyncBarrier sync_barrier(sync_event.exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1819 const ApplyBarrierFunctor<WaitEventBarrierOp> barrier_action({scope_tag, sync_barrier, layout_transition});
1820 const auto base_address = ResourceBaseAddress(*image);
1821 subresource_adapter::ImageRangeGenerator range_gen(*image->fragment_encoder.get(), subresource_range, {0, 0, 0},
1822 image->createInfo.extent, base_address);
1823 const auto address_type = AccessContext::ImageAddressType(*image);
1824 EventImageRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), range_gen);
1825 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barrier_action, &filtered_range_gen);
1826 }
1827}
John Zulaufb02c1eb2020-10-06 16:33:36 -06001828
John Zulauf355e49b2020-04-24 15:11:15 -06001829// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1830bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1831
1832 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001833 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001834 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1835 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06001836
John Zulauf86356ca2020-10-19 11:46:41 -06001837 assert(pRenderPassBegin);
1838 if (nullptr == pRenderPassBegin) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06001839
John Zulauf86356ca2020-10-19 11:46:41 -06001840 const uint32_t subpass = 0;
John Zulauf355e49b2020-04-24 15:11:15 -06001841
John Zulauf86356ca2020-10-19 11:46:41 -06001842 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
1843 // hasn't happened yet)
1844 const std::vector<AccessContext> empty_context_vector;
1845 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1846 const_cast<AccessContext *>(&cb_access_context_));
John Zulauf355e49b2020-04-24 15:11:15 -06001847
John Zulauf86356ca2020-10-19 11:46:41 -06001848 // Create a view list
1849 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1850 assert(fb_state);
1851 if (nullptr == fb_state) return skip;
1852 // NOTE: Must not use COMMAND_BUFFER_STATE variant of this as RecordCmdBeginRenderPass hasn't run and thus
1853 // the activeRenderPass.* fields haven't been set.
1854 const auto views = sync_state_->GetAttachmentViews(*pRenderPassBegin, *fb_state);
1855
1856 // Validate transitions
1857 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
1858
1859 // Validate load operations if there were no layout transition hazards
1860 if (!skip) {
1861 temp_context.RecordLayoutTransitions(rp_state, subpass, views, kCurrentCommandTag);
1862 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001863 }
John Zulauf86356ca2020-10-19 11:46:41 -06001864
John Zulauf355e49b2020-04-24 15:11:15 -06001865 return skip;
1866}
1867
locke-lunarg61870c22020-06-09 14:51:50 -06001868bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1869 const char *func_name) const {
1870 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001871 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001872 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001873 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1874 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001875 return skip;
1876 }
1877
1878 using DescriptorClass = cvdescriptorset::DescriptorClass;
1879 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1880 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1881 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1882 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1883
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001884 for (const auto &stage_state : pipe->stage_state) {
1885 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1886 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001887 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001888 }
locke-lunarg61870c22020-06-09 14:51:50 -06001889 for (const auto &set_binding : stage_state.descriptor_uses) {
1890 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1891 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1892 set_binding.first.second);
1893 const auto descriptor_type = binding_it.GetType();
1894 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1895 auto array_idx = 0;
1896
1897 if (binding_it.IsVariableDescriptorCount()) {
1898 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1899 }
1900 SyncStageAccessIndex sync_index =
1901 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1902
1903 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1904 uint32_t index = i - index_range.start;
1905 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1906 switch (descriptor->GetClass()) {
1907 case DescriptorClass::ImageSampler:
1908 case DescriptorClass::Image: {
1909 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001910 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001911 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001912 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1913 img_view_state = image_sampler_descriptor->GetImageViewState();
1914 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001915 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001916 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1917 img_view_state = image_descriptor->GetImageViewState();
1918 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001919 }
1920 if (!img_view_state) continue;
1921 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1922 VkExtent3D extent = {};
1923 VkOffset3D offset = {};
1924 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1925 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1926 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1927 } else {
1928 extent = img_state->createInfo.extent;
1929 }
John Zulauf361fb532020-07-22 10:45:39 -06001930 HazardResult hazard;
1931 const auto &subresource_range = img_view_state->normalized_subresource_range;
1932 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1933 // Input attachments are subject to raster ordering rules
1934 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1935 kAttachmentRasterOrder, offset, extent);
1936 } else {
1937 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1938 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001939 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001940 skip |= sync_state_->LogError(
1941 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001942 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1943 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001944 func_name, string_SyncHazard(hazard.hazard),
1945 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1946 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001947 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001948 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1949 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1950 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001951 }
1952 break;
1953 }
1954 case DescriptorClass::TexelBuffer: {
1955 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1956 if (!buf_view_state) continue;
1957 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001958 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001959 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001960 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001961 skip |= sync_state_->LogError(
1962 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001963 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1964 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001965 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1966 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001967 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001968 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1969 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1970 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001971 }
1972 break;
1973 }
1974 case DescriptorClass::GeneralBuffer: {
1975 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1976 auto buf_state = buffer_descriptor->GetBufferState();
1977 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001978 const ResourceAccessRange range =
1979 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001980 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001981 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001982 skip |= sync_state_->LogError(
1983 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001984 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1985 func_name, string_SyncHazard(hazard.hazard),
1986 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001987 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001988 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001989 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1990 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1991 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001992 }
1993 break;
1994 }
1995 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1996 default:
1997 break;
1998 }
1999 }
2000 }
2001 }
2002 return skip;
2003}
2004
2005void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
2006 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002007 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06002008 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002009 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
2010 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06002011 return;
2012 }
2013
2014 using DescriptorClass = cvdescriptorset::DescriptorClass;
2015 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
2016 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
2017 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
2018 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
2019
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002020 for (const auto &stage_state : pipe->stage_state) {
2021 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
2022 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06002023 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002024 }
locke-lunarg61870c22020-06-09 14:51:50 -06002025 for (const auto &set_binding : stage_state.descriptor_uses) {
2026 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
2027 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
2028 set_binding.first.second);
2029 const auto descriptor_type = binding_it.GetType();
2030 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2031 auto array_idx = 0;
2032
2033 if (binding_it.IsVariableDescriptorCount()) {
2034 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2035 }
2036 SyncStageAccessIndex sync_index =
2037 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
2038
2039 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2040 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2041 switch (descriptor->GetClass()) {
2042 case DescriptorClass::ImageSampler:
2043 case DescriptorClass::Image: {
2044 const IMAGE_VIEW_STATE *img_view_state = nullptr;
2045 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
2046 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
2047 } else {
2048 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
2049 }
2050 if (!img_view_state) continue;
2051 const IMAGE_STATE *img_state = img_view_state->image_state.get();
2052 VkExtent3D extent = {};
2053 VkOffset3D offset = {};
2054 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
2055 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
2056 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
2057 } else {
2058 extent = img_state->createInfo.extent;
2059 }
2060 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
2061 offset, extent, tag);
2062 break;
2063 }
2064 case DescriptorClass::TexelBuffer: {
2065 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
2066 if (!buf_view_state) continue;
2067 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002068 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06002069 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
2070 break;
2071 }
2072 case DescriptorClass::GeneralBuffer: {
2073 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
2074 auto buf_state = buffer_descriptor->GetBufferState();
2075 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002076 const ResourceAccessRange range =
2077 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06002078 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
2079 break;
2080 }
2081 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2082 default:
2083 break;
2084 }
2085 }
2086 }
2087 }
2088}
2089
2090bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2091 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002092 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2093 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002094 return skip;
2095 }
2096
2097 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2098 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002099 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002100
2101 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002102 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002103 if (binding_description.binding < binding_buffers_size) {
2104 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002105 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002106
locke-lunarg1ae57d62020-11-18 10:49:19 -07002107 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002108 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2109 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06002110 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
2111 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002112 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002113 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 -06002114 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002115 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002116 }
2117 }
2118 }
2119 return skip;
2120}
2121
2122void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002123 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2124 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002125 return;
2126 }
2127 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2128 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002129 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002130
2131 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002132 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002133 if (binding_description.binding < binding_buffers_size) {
2134 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002135 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002136
locke-lunarg1ae57d62020-11-18 10:49:19 -07002137 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002138 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2139 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06002140 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
2141 }
2142 }
2143}
2144
2145bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2146 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002147 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002148 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002149 }
locke-lunarg61870c22020-06-09 14:51:50 -06002150
locke-lunarg1ae57d62020-11-18 10:49:19 -07002151 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002152 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002153 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2154 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06002155 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
2156 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002157 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002158 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 -06002159 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002160 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002161 }
2162
2163 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2164 // We will detect more accurate range in the future.
2165 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2166 return skip;
2167}
2168
2169void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002170 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 -06002171
locke-lunarg1ae57d62020-11-18 10:49:19 -07002172 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002173 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002174 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2175 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06002176 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
2177
2178 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2179 // We will detect more accurate range in the future.
2180 RecordDrawVertex(UINT32_MAX, 0, tag);
2181}
2182
2183bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002184 bool skip = false;
2185 if (!current_renderpass_context_) return skip;
2186 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
2187 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
2188 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002189}
2190
2191void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002192 if (current_renderpass_context_) {
locke-lunarg7077d502020-06-18 21:37:26 -06002193 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
2194 tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002195 }
locke-lunarg61870c22020-06-09 14:51:50 -06002196}
2197
John Zulauf355e49b2020-04-24 15:11:15 -06002198bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002199 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06002200 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06002201 skip |=
2202 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002203
2204 return skip;
2205}
2206
2207bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
2208 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06002209 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002210 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06002211 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06002212 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
2213 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002214
2215 return skip;
2216}
2217
2218void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
2219 assert(sync_state_);
2220 if (!cb_state_) return;
2221
2222 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06002223 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06002224 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06002225 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002226 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002227}
2228
John Zulauf355e49b2020-04-24 15:11:15 -06002229void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002230 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06002231 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002232 current_context_ = &current_renderpass_context_->CurrentContext();
2233}
2234
John Zulauf355e49b2020-04-24 15:11:15 -06002235void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002236 assert(current_renderpass_context_);
2237 if (!current_renderpass_context_) return;
2238
John Zulauf1a224292020-06-30 14:52:13 -06002239 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002240 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002241 current_renderpass_context_ = nullptr;
2242}
2243
John Zulauf49beb112020-11-04 16:06:31 -07002244bool CommandBufferAccessContext::ValidateSetEvent(VkCommandBuffer commandBuffer, VkEvent event,
2245 VkPipelineStageFlags stageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002246 // I'll put this here just in case we need to pass this in for future extension support
2247 const auto cmd = CMD_SETEVENT;
2248 bool skip = false;
2249 const auto *sync_event = GetEventState(event);
2250 if (!sync_event) return false; // Core, Lifetimes, or Param check needs to catch invalid events.
2251
2252 const char *const reset_set =
2253 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
2254 "hazards.";
2255 const char *const wait =
2256 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
2257
2258 const auto exec_scope = WithEarlierPipelineStages(ExpandPipelineStages(GetQueueFlags(), stageMask));
2259 if (!sync_event->HasBarrier(stageMask, exec_scope)) {
2260 const char *vuid = nullptr;
2261 const char *message = nullptr;
2262 switch (sync_event->last_command) {
2263 case CMD_RESETEVENT:
2264 // Needs a barrier between reset and set
2265 vuid = "SYNC-vkCmdSetEvent-missingbarrier-reset";
2266 message = reset_set;
2267 break;
2268 case CMD_SETEVENT:
2269 // Needs a barrier between set and set
2270 vuid = "SYNC-vkCmdSetEvent-missingbarrier-set";
2271 message = reset_set;
2272 break;
2273 case CMD_WAITEVENTS:
2274 // Needs a barrier or is in second execution scope
2275 vuid = "SYNC-vkCmdSetEvent-missingbarrier-wait";
2276 message = wait;
2277 break;
2278 default:
2279 // The only other valid last command that wasn't one.
2280 assert(sync_event->last_command == CMD_NONE);
2281 break;
2282 }
2283 if (vuid) {
2284 assert(nullptr != message);
2285 const char *const cmd_name = CommandTypeString(cmd);
2286 skip |= sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2287 cmd_name, CommandTypeString(sync_event->last_command));
2288 }
2289 }
2290
2291 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002292}
2293
John Zulauf4a6105a2020-11-17 15:11:05 -07002294void CommandBufferAccessContext::RecordSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask,
2295 const ResourceUsageTag &tag) {
2296 auto *sync_event = GetEventState(event);
2297 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
2298
2299 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
2300 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
2301 // any issues caused by naive scope setting here.
2302
2303 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
2304 // Given:
2305 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
2306 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
2307
2308 const auto stage_mask = ExpandPipelineStages(GetQueueFlags(), stageMask);
2309 const auto exec_scope = WithEarlierPipelineStages(stage_mask);
2310
2311 sync_event->stage_mask_param = stageMask;
2312
2313 if (!sync_event->HasBarrier(stageMask, exec_scope)) {
2314 sync_event->unsynchronized_set = sync_event->last_command;
2315 sync_event->ResetFirstScope();
2316 } else if (sync_event->exec_scope == 0) {
2317 // We only set the scope if there isn't one
2318 sync_event->stage_mask = stage_mask;
2319 sync_event->exec_scope = exec_scope;
2320 sync_event->stage_accesses = SyncStageAccess::AccessScopeByStage(sync_event->stage_mask);
2321
2322 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
2323 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
2324 if (access.second.InSourceScopeOrChain(sync_event->exec_scope, sync_event->stage_accesses)) {
2325 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
2326 }
2327 };
2328 GetCurrentAccessContext()->ForAll(set_scope);
2329 sync_event->unsynchronized_set = CMD_NONE;
2330 sync_event->first_scope_tag = tag;
2331 }
2332 sync_event->last_command = CMD_SETEVENT;
2333 sync_event->barriers = 0U;
2334}
John Zulauf49beb112020-11-04 16:06:31 -07002335
2336bool CommandBufferAccessContext::ValidateResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
2337 VkPipelineStageFlags stageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002338 // I'll put this here just in case we need to pass this in for future extension support
2339 const auto cmd = CMD_RESETEVENT;
2340
2341 bool skip = false;
2342 // TODO: EVENTS:
2343 // What is it we need to check... that we've had a reset since a set? Set/Set seems ill formed...
2344 const auto *sync_event = GetEventState(event);
2345 if (!sync_event) return false; // Core, Lifetimes, or Param check needs to catch invalid events.
2346
2347 const char *const set_wait =
2348 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
2349 "hazards.";
2350 const char *message = set_wait; // Only one message this call.
2351 const auto exec_scope = WithEarlierPipelineStages(ExpandPipelineStages(GetQueueFlags(), stageMask));
2352 if (!sync_event->HasBarrier(stageMask, exec_scope)) {
2353 const char *vuid = nullptr;
2354 switch (sync_event->last_command) {
2355 case CMD_SETEVENT:
2356 // Needs a barrier between set and reset
2357 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
2358 break;
2359 case CMD_WAITEVENTS: {
2360 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
2361 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
2362 break;
2363 }
2364 default:
2365 // The only other valid last command that wasn't one.
2366 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT));
2367 break;
2368 }
2369 if (vuid) {
2370 const char *const cmd_name = CommandTypeString(cmd);
2371 skip |= sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2372 cmd_name, CommandTypeString(sync_event->last_command));
2373 }
2374 }
2375 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002376}
2377
John Zulauf4a6105a2020-11-17 15:11:05 -07002378void CommandBufferAccessContext::RecordResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
2379 const auto cmd = CMD_RESETEVENT;
2380 auto *sync_event = GetEventState(event);
2381 if (!sync_event) return;
John Zulauf49beb112020-11-04 16:06:31 -07002382
John Zulauf4a6105a2020-11-17 15:11:05 -07002383 // Clear out the first sync scope, any races vs. wait or set are reported, so we'll keep the bookkeeping simple assuming
2384 // the safe case
2385 for (const auto address_type : kAddressTypes) {
2386 sync_event->first_scope[static_cast<size_t>(address_type)].clear();
2387 }
2388
2389 // Update the event state
2390 sync_event->last_command = cmd;
2391 sync_event->unsynchronized_set = CMD_NONE;
2392 sync_event->ResetFirstScope();
2393 sync_event->barriers = 0U;
2394}
2395
2396bool CommandBufferAccessContext::ValidateWaitEvents(uint32_t eventCount, const VkEvent *pEvents, VkPipelineStageFlags srcStageMask,
2397 VkPipelineStageFlags dstStageMask, uint32_t memoryBarrierCount,
2398 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
John Zulauf49beb112020-11-04 16:06:31 -07002399 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2400 uint32_t imageMemoryBarrierCount,
2401 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002402 const auto cmd = CMD_WAITEVENTS;
2403 const char *const ignored = "Wait operation is ignored for this event.";
2404 bool skip = false;
2405
2406 if (srcStageMask & VK_PIPELINE_STAGE_HOST_BIT) {
2407 const char *const cmd_name = CommandTypeString(cmd);
2408 const char *const vuid = "SYNC-vkCmdWaitEvents-hostevent-unsupported";
John Zulauffe757512020-12-18 12:17:47 -07002409 skip = sync_state_->LogInfo(cb_state_->commandBuffer, vuid,
2410 "%s, srcStageMask includes %s, unsupported by synchronization validaton.", cmd_name,
2411 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT), ignored);
John Zulauf4a6105a2020-11-17 15:11:05 -07002412 }
2413
2414 VkPipelineStageFlags event_stage_masks = 0U;
John Zulauffe757512020-12-18 12:17:47 -07002415 bool events_not_found = false;
John Zulauf4a6105a2020-11-17 15:11:05 -07002416 for (uint32_t event_index = 0; event_index < eventCount; event_index++) {
2417 const auto event = pEvents[event_index];
2418 const auto *sync_event = GetEventState(event);
John Zulauffe757512020-12-18 12:17:47 -07002419 if (!sync_event) {
2420 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
2421 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives.
2422
2423 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
2424 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002425
2426 event_stage_masks |= sync_event->stage_mask_param;
2427 const auto ignore_reason = sync_event->IsIgnoredByWait(srcStageMask);
2428 if (ignore_reason) {
2429 switch (ignore_reason) {
2430 case SyncEventState::ResetWaitRace: {
2431 const char *const cmd_name = CommandTypeString(cmd);
2432 const char *const vuid = "SYNC-vkCmdWaitEvents-missingbarrier-reset";
2433 const char *const message =
2434 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
2435 skip |=
2436 sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2437 cmd_name, CommandTypeString(sync_event->last_command), ignored);
2438 break;
2439 }
2440 case SyncEventState::SetRace: {
2441 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for this
2442 // event
2443 const char *const cmd_name = CommandTypeString(cmd);
2444 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
2445 const char *const message =
2446 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, % %s";
2447 const char *const reason = "First synchronization scope is undefined.";
2448 skip |=
2449 sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2450 CommandTypeString(sync_event->last_command), reason, ignored);
2451 break;
2452 }
2453 case SyncEventState::MissingStageBits: {
2454 const VkPipelineStageFlags missing_bits = sync_event->stage_mask_param & ~srcStageMask;
2455 // Issue error message that event waited for is not in wait events scope
2456 const char *const cmd_name = CommandTypeString(cmd);
2457 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
2458 const char *const message =
2459 "%s: %s stageMask 0x%" PRIx32 " includes bits not present in srcStageMask 0x%" PRIx32
2460 ". Bits missing from srcStageMask %s. %s";
2461 skip |= sync_state_->LogError(
2462 event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2463 sync_event->stage_mask_param, srcStageMask, string_VkPipelineStageFlags(missing_bits).c_str(), ignored);
2464 break;
2465 }
2466 default:
2467 assert(ignore_reason == SyncEventState::NotIgnored);
2468 }
2469 } else if (imageMemoryBarrierCount) {
2470 const auto *context = GetCurrentAccessContext();
2471 assert(context);
2472 for (uint32_t barrier_index = 0; barrier_index < imageMemoryBarrierCount; barrier_index++) {
2473 const auto &barrier = pImageMemoryBarriers[barrier_index];
2474 if (barrier.oldLayout == barrier.newLayout) continue;
2475 const auto *image_state = sync_state_->Get<IMAGE_STATE>(barrier.image);
2476 if (!image_state) continue;
2477 auto subresource_range = NormalizeSubresourceRange(image_state->createInfo, barrier.subresourceRange);
2478 const auto src_access_scope = SyncStageAccess::AccessScope(sync_event->stage_accesses, barrier.srcAccessMask);
2479 const auto hazard =
2480 context->DetectImageBarrierHazard(*image_state, sync_event->exec_scope, src_access_scope, subresource_range,
2481 *sync_event, AccessContext::DetectOptions::kDetectAll);
2482 if (hazard.hazard) {
2483 const char *const cmd_name = CommandTypeString(cmd);
2484 skip |= sync_state_->LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
2485 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", cmd_name,
2486 string_SyncHazard(hazard.hazard), barrier_index,
2487 sync_state_->report_data->FormatHandle(barrier.image).c_str(),
2488 string_UsageTag(hazard).c_str());
2489 break;
2490 }
2491 }
2492 }
2493 }
2494
2495 // Note that we can't check for HOST in pEvents as we don't track that set event type
2496 const auto extra_stage_bits = (srcStageMask & ~VK_PIPELINE_STAGE_HOST_BIT) & ~event_stage_masks;
2497 if (extra_stage_bits) {
2498 // Issue error message that event waited for is not in wait events scope
2499 const char *const cmd_name = CommandTypeString(cmd);
2500 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
2501 const char *const message =
John Zulauffe757512020-12-18 12:17:47 -07002502 "%s: srcStageMask 0x%" PRIx32 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
2503 if (events_not_found) {
2504 skip |= sync_state_->LogInfo(cb_state_->commandBuffer, vuid, message, cmd_name, srcStageMask,
2505 string_VkPipelineStageFlags(extra_stage_bits).c_str(),
2506 " vkCmdSetEvent may be in previously submitted command buffer.");
2507 } else {
2508 skip |= sync_state_->LogError(cb_state_->commandBuffer, vuid, message, cmd_name, srcStageMask,
2509 string_VkPipelineStageFlags(extra_stage_bits).c_str(), "");
2510 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002511 }
2512 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002513}
2514
2515void CommandBufferAccessContext::RecordWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
2516 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
2517 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2518 uint32_t bufferMemoryBarrierCount,
2519 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2520 uint32_t imageMemoryBarrierCount,
John Zulauf4a6105a2020-11-17 15:11:05 -07002521 const VkImageMemoryBarrier *pImageMemoryBarriers, const ResourceUsageTag &tag) {
2522 auto *access_context = GetCurrentAccessContext();
2523 assert(access_context);
2524 if (!access_context) return;
2525
2526 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
2527 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
2528 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
2529 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here
2530 access_context->ResolvePreviousAccesses();
2531
2532 const auto dst_stage_mask = ExpandPipelineStages(GetQueueFlags(), dstStageMask);
2533 auto dst_stage_accesses = SyncStageAccess::AccessScopeByStage(dst_stage_mask);
2534 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2535 for (uint32_t event_index = 0; event_index < eventCount; event_index++) {
2536 const auto event = pEvents[event_index];
2537 auto *sync_event = GetEventState(event);
2538 if (!sync_event) continue;
2539
2540 sync_event->last_command = CMD_WAITEVENTS;
2541
2542 if (!sync_event->IsIgnoredByWait(srcStageMask)) {
2543 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
2544 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
2545 // of the barriers is maintained.
2546 ApplyBufferBarriers(*sync_event, dst_exec_scope, dst_stage_accesses, bufferMemoryBarrierCount, pBufferMemoryBarriers);
2547 ApplyImageBarriers(*sync_event, dst_exec_scope, dst_stage_accesses, imageMemoryBarrierCount, pImageMemoryBarriers, tag);
2548 ApplyGlobalBarriers(*sync_event, dstStageMask, dst_exec_scope, dst_stage_accesses, memoryBarrierCount, pMemoryBarriers,
2549 tag);
2550 } else {
2551 // We ignored this wait, so we don't have any effective synchronization barriers for it.
2552 sync_event->barriers = 0U;
2553 }
2554 }
2555
2556 // Apply the pending barriers
2557 ResolvePendingBarrierFunctor apply_pending_action(tag);
2558 access_context->ApplyGlobalBarriers(apply_pending_action);
2559}
2560
2561void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2562 // Erase is okay with the key not being
2563 event_state_.erase(event);
2564}
2565
2566SyncEventState *CommandBufferAccessContext::GetEventState(VkEvent event) {
2567 auto &event_up = event_state_[event];
2568 if (!event_up) {
2569 auto event_atate = sync_state_->GetShared<EVENT_STATE>(event);
2570 event_up.reset(new SyncEventState(event_atate));
2571 }
2572 return event_up.get();
2573}
2574
2575const SyncEventState *CommandBufferAccessContext::GetEventState(VkEvent event) const {
2576 auto event_it = event_state_.find(event);
2577 if (event_it == event_state_.cend()) {
2578 return nullptr;
2579 }
2580 return event_it->second.get();
2581}
John Zulauf49beb112020-11-04 16:06:31 -07002582
locke-lunarg61870c22020-06-09 14:51:50 -06002583bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
2584 const VkRect2D &render_area, const char *func_name) const {
2585 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002586 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2587 if (!pipe ||
2588 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002589 return skip;
2590 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002591 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002592 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2593 VkExtent3D extent = CastTo3D(render_area.extent);
2594 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06002595
John Zulauf1a224292020-06-30 14:52:13 -06002596 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002597 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002598 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2599 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002600 if (location >= subpass.colorAttachmentCount ||
2601 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002602 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002603 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002604 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002605 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
2606 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06002607 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002608 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002609 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002610 func_name, string_SyncHazard(hazard.hazard),
2611 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2612 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002613 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002614 }
2615 }
2616 }
locke-lunarg37047832020-06-12 13:44:45 -06002617
2618 // PHASE1 TODO: Add layout based read/vs. write selection.
2619 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002620 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002621 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002622 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002623 bool depth_write = false, stencil_write = false;
2624
2625 // PHASE1 TODO: These validation should be in core_checks.
2626 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002627 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2628 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002629 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2630 depth_write = true;
2631 }
2632 // PHASE1 TODO: It needs to check if stencil is writable.
2633 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2634 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2635 // PHASE1 TODO: These validation should be in core_checks.
2636 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002637 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002638 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2639 stencil_write = true;
2640 }
2641
2642 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2643 if (depth_write) {
2644 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002645 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2646 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002647 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002648 skip |= sync_state.LogError(
2649 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002650 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002651 func_name, string_SyncHazard(hazard.hazard),
2652 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2653 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002654 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002655 }
2656 }
2657 if (stencil_write) {
2658 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002659 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2660 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002661 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002662 skip |= sync_state.LogError(
2663 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002664 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002665 func_name, string_SyncHazard(hazard.hazard),
2666 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2667 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002668 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002669 }
locke-lunarg61870c22020-06-09 14:51:50 -06002670 }
2671 }
2672 return skip;
2673}
2674
locke-lunarg96dc9632020-06-10 17:22:18 -06002675void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
2676 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002677 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2678 if (!pipe ||
2679 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002680 return;
2681 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002682 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002683 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2684 VkExtent3D extent = CastTo3D(render_area.extent);
2685 VkOffset3D offset = CastTo3D(render_area.offset);
2686
John Zulauf1a224292020-06-30 14:52:13 -06002687 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002688 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002689 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2690 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002691 if (location >= subpass.colorAttachmentCount ||
2692 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002693 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002694 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002695 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002696 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
2697 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002698 }
2699 }
locke-lunarg37047832020-06-12 13:44:45 -06002700
2701 // PHASE1 TODO: Add layout based read/vs. write selection.
2702 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002703 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002704 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002705 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002706 bool depth_write = false, stencil_write = false;
2707
2708 // PHASE1 TODO: These validation should be in core_checks.
2709 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002710 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2711 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002712 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2713 depth_write = true;
2714 }
2715 // PHASE1 TODO: It needs to check if stencil is writable.
2716 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2717 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2718 // PHASE1 TODO: These validation should be in core_checks.
2719 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002720 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002721 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2722 stencil_write = true;
2723 }
2724
2725 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2726 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002727 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2728 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002729 }
2730 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002731 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2732 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002733 }
locke-lunarg61870c22020-06-09 14:51:50 -06002734 }
2735}
2736
John Zulauf1507ee42020-05-18 11:33:09 -06002737bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
2738 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002739 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002740 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06002741 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2742 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002743 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2744 func_name);
2745
John Zulauf355e49b2020-04-24 15:11:15 -06002746 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002747 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06002748 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002749 if (!skip) {
2750 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2751 // on a copy of the (empty) next context.
2752 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2753 AccessContext temp_context(next_context);
2754 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
2755 skip |= temp_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2756 }
John Zulauf7635de32020-05-29 17:14:15 -06002757 return skip;
2758}
2759bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
2760 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002761 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002762 bool skip = false;
2763 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2764 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002765 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2766 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002767 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002768 return skip;
2769}
2770
John Zulauf7635de32020-05-29 17:14:15 -06002771AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2772 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2773}
2774
2775bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2776 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002777 bool skip = false;
2778
John Zulauf7635de32020-05-29 17:14:15 -06002779 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2780 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2781 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2782 // to apply and only copy then, if this proves a hot spot.
2783 std::unique_ptr<AccessContext> proxy_for_current;
2784
John Zulauf355e49b2020-04-24 15:11:15 -06002785 // Validate the "finalLayout" transitions to external
2786 // Get them from where there we're hidding in the extra entry.
2787 const auto &final_transitions = rp_state_->subpass_transitions.back();
2788 for (const auto &transition : final_transitions) {
2789 const auto &attach_view = attachment_views_[transition.attachment];
2790 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2791 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002792 auto *context = trackback.context;
2793
2794 if (transition.prev_pass == current_subpass_) {
2795 if (!proxy_for_current) {
2796 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2797 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2798 }
2799 context = proxy_for_current.get();
2800 }
2801
John Zulaufa0a98292020-09-18 09:30:10 -06002802 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2803 const auto merged_barrier = MergeBarriers(trackback.barriers);
2804 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2805 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2806 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002807 if (hazard.hazard) {
2808 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2809 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002810 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002811 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002812 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002813 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002814 }
2815 }
2816 return skip;
2817}
2818
2819void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2820 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002821 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002822}
2823
John Zulauf1507ee42020-05-18 11:33:09 -06002824void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2825 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2826 auto &subpass_context = subpass_contexts_[current_subpass_];
2827 VkExtent3D extent = CastTo3D(render_area.extent);
2828 VkOffset3D offset = CastTo3D(render_area.offset);
2829
2830 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2831 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2832 if (attachment_views_[i] == nullptr) continue; // UNUSED
2833 const auto &view = *attachment_views_[i];
2834 const IMAGE_STATE *image = view.image_state.get();
2835 if (image == nullptr) continue;
2836
2837 const auto &ci = attachment_ci[i];
2838 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002839 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002840 const bool is_color = !(has_depth || has_stencil);
2841
2842 if (is_color) {
2843 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2844 extent, tag);
2845 } else {
2846 auto update_range = view.normalized_subresource_range;
2847 if (has_depth) {
2848 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2849 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2850 }
2851 if (has_stencil) {
2852 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2853 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2854 tag);
2855 }
2856 }
2857 }
2858 }
2859}
2860
John Zulauf355e49b2020-04-24 15:11:15 -06002861void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002862 const AccessContext *external_context, VkQueueFlags queue_flags,
2863 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002864 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002865 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002866 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2867 // Add this for all subpasses here so that they exsist during next subpass validation
2868 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002869 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002870 }
2871 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2872
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002873 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002874 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002875 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002876}
John Zulauf1507ee42020-05-18 11:33:09 -06002877
2878void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002879 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2880 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002881 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002882
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002883 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2884 // subpass, so their tag needs to be different from the layout and load operations below.
Jeremy Gebben4bb73502020-12-14 11:17:50 -07002885 ResourceUsageTag next_tag = tag.NextSubCommand();
John Zulauf355e49b2020-04-24 15:11:15 -06002886 current_subpass_++;
2887 assert(current_subpass_ < subpass_contexts_.size());
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002888 subpass_contexts_[current_subpass_].SetStartTag(next_tag);
2889 RecordLayoutTransitions(next_tag);
2890 RecordLoadOperations(render_area, next_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002891}
2892
John Zulauf1a224292020-06-30 14:52:13 -06002893void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2894 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002895 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002896 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002897 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002898
John Zulauf355e49b2020-04-24 15:11:15 -06002899 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002900 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002901
2902 // Add the "finalLayout" transitions to external
2903 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002904 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2905 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2906 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002907 const auto &final_transitions = rp_state_->subpass_transitions.back();
2908 for (const auto &transition : final_transitions) {
2909 const auto &attachment = attachment_views_[transition.attachment];
2910 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002911 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf1e331ec2020-12-04 18:29:38 -07002912 std::vector<PipelineBarrierOp> barrier_ops;
2913 barrier_ops.reserve(last_trackback.barriers.size());
2914 for (const auto &barrier : last_trackback.barriers) {
2915 barrier_ops.emplace_back(barrier, true);
2916 }
2917 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, barrier_ops, tag);
2918 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002919 }
2920}
2921
John Zulauf3d84f1b2020-03-09 13:33:25 -06002922SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2923 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2924 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2925 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2926 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2927 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2928 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2929}
2930
John Zulaufb02c1eb2020-10-06 16:33:36 -06002931// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2932void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2933 for (const auto &barrier : barriers) {
2934 ApplyBarrier(barrier, layout_transition);
2935 }
2936}
2937
John Zulauf89311b42020-09-29 16:28:47 -06002938// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2939// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2940// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002941void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2942 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002943 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002944 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002945 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002946 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002947 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002948 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002949}
John Zulauf9cb530d2019-09-30 14:14:10 -06002950HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2951 HazardResult hazard;
2952 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002953 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002954 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002955 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002956 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002957 }
2958 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002959 // Write operation:
2960 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2961 // If reads exists -- test only against them because either:
2962 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2963 // * 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
2964 // the current write happens after the reads, so just test the write against the reades
2965 // Otherwise test against last_write
2966 //
2967 // Look for casus belli for WAR
2968 if (last_read_count) {
2969 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2970 const auto &read_access = last_reads[read_index];
2971 if (IsReadHazard(usage_stage, read_access)) {
2972 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2973 break;
2974 }
2975 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002976 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002977 // Write-After-Write check -- if we have a previous write to test against
2978 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002979 }
2980 }
2981 return hazard;
2982}
2983
John Zulauf69133422020-05-20 14:55:53 -06002984HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2985 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2986 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002987 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002988 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002989 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2990 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002991 if (IsRead(usage_bit)) {
2992 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2993 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2994 if (is_raw_hazard) {
2995 // NOTE: we know last_write is non-zero
2996 // See if the ordering rules save us from the simple RAW check above
2997 // First check to see if the current usage is covered by the ordering rules
2998 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2999 const bool usage_is_ordered =
3000 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
3001 if (usage_is_ordered) {
3002 // Now see of the most recent write (or a subsequent read) are ordered
3003 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
3004 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06003005 }
3006 }
John Zulauf4285ee92020-09-23 10:20:52 -06003007 if (is_raw_hazard) {
3008 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
3009 }
John Zulauf361fb532020-07-22 10:45:39 -06003010 } else {
3011 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003012 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulauf361fb532020-07-22 10:45:39 -06003013 if (last_read_count) {
John Zulauf361fb532020-07-22 10:45:39 -06003014 // Look for any WAR hazards outside the ordered set of stages
John Zulauf4285ee92020-09-23 10:20:52 -06003015 VkPipelineStageFlags ordered_stages = 0;
3016 if (usage_write_is_ordered) {
3017 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
3018 ordered_stages = GetOrderedStages(ordering);
3019 }
3020 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
3021 if ((ordered_stages & last_read_stages) != last_read_stages) {
3022 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
3023 const auto &read_access = last_reads[read_index];
3024 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
3025 if (IsReadHazard(usage_stage, read_access)) {
3026 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3027 break;
3028 }
John Zulaufd14743a2020-07-03 09:42:39 -06003029 }
3030 }
John Zulauf4285ee92020-09-23 10:20:52 -06003031 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003032 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003033 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06003034 }
John Zulauf69133422020-05-20 14:55:53 -06003035 }
3036 }
3037 return hazard;
3038}
3039
John Zulauf2f952d22020-02-10 11:34:51 -07003040// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003041HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07003042 HazardResult hazard;
3043 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003044 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
3045 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
3046 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07003047 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003048 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06003049 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07003050 }
3051 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003052 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06003053 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07003054 } else if (last_read_count > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003055 // Any reads during the other subpass will conflict with this write, so we need to check them all.
3056 for (uint32_t i = 0; i < last_read_count; i++) {
3057 if (last_reads[i].tag.index >= start_tag.index) {
3058 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[i].access, last_reads[i].tag);
3059 break;
3060 }
3061 }
John Zulauf2f952d22020-02-10 11:34:51 -07003062 }
3063 }
3064 return hazard;
3065}
3066
John Zulauf36bcf6a2020-02-03 15:12:52 -07003067HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003068 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07003069 // Only supporting image layout transitions for now
3070 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3071 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06003072 // only test for WAW if there no intervening read operations.
3073 // See DetectHazard(SyncStagetAccessIndex) above for more details.
3074 if (last_read_count) {
John Zulauf355e49b2020-04-24 15:11:15 -06003075 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07003076 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07003077 const auto &read_access = last_reads[read_index];
John Zulauf4a6105a2020-11-17 15:11:05 -07003078 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06003079 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07003080 break;
3081 }
3082 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003083 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3084 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3085 }
3086
3087 return hazard;
3088}
3089
3090HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
3091 const SyncStageAccessFlags &src_access_scope,
3092 const ResourceUsageTag &event_tag) const {
3093 // Only supporting image layout transitions for now
3094 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3095 HazardResult hazard;
3096 // only test for WAW if there no intervening read operations.
3097 // See DetectHazard(SyncStagetAccessIndex) above for more details.
3098
3099 if (last_read_count) {
3100 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
3101 // first scope, or they are a hazard.
3102 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
3103 const auto &read_access = last_reads[read_index];
3104 if (read_access.tag.IsBefore(event_tag)) {
3105 // The read is in the events first synchronization scope, so we use a barrier hazard check
3106 // If the read stage is not in the src sync scope
3107 // *AND* not execution chained with an existing sync barrier (that's the or)
3108 // then the barrier access is unsafe (R/W after R)
3109 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3110 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3111 break;
3112 }
3113 } else {
3114 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3115 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3116 }
3117 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003118 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003119 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
3120 if (write_tag.IsBefore(event_tag)) {
3121 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3122 // So do a normal barrier hazard check
3123 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3124 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3125 }
3126 } else {
3127 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003128 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3129 }
John Zulaufd14743a2020-07-03 09:42:39 -06003130 }
John Zulauf361fb532020-07-22 10:45:39 -06003131
John Zulauf0cb5be22020-01-23 12:18:22 -07003132 return hazard;
3133}
3134
John Zulauf5f13a792020-03-10 07:31:21 -06003135// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3136// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3137// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3138void ResourceAccessState::Resolve(const ResourceAccessState &other) {
3139 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003140 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3141 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003142 *this = other;
3143 } else if (!other.write_tag.IsBefore(write_tag)) {
3144 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
3145 // dependency chaining logic or any stage expansion)
3146 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003147 pending_write_barriers |= other.pending_write_barriers;
3148 pending_layout_transition |= other.pending_layout_transition;
3149 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003150
John Zulaufd14743a2020-07-03 09:42:39 -06003151 // Merge the read states
John Zulauf4285ee92020-09-23 10:20:52 -06003152 const auto pre_merge_count = last_read_count;
3153 const auto pre_merge_stages = last_read_stages;
John Zulauf5f13a792020-03-10 07:31:21 -06003154 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
3155 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003156 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003157 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003158 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3159 // but we should wait on profiling data for that.
3160 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003161 auto &my_read = last_reads[my_read_index];
3162 if (other_read.stage == my_read.stage) {
3163 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003164 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003165 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003166 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003167 my_read.pending_dep_chain = other_read.pending_dep_chain;
3168 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3169 // May require tracking more than one access per stage.
3170 my_read.barriers = other_read.barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003171 if (my_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
3172 // Since I'm overwriting the fragement stage read, also update the input attachment info
3173 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003174 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003175 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003176 } else if (other_read.tag.IsBefore(my_read.tag)) {
3177 // The read tags match so merge the barriers
3178 my_read.barriers |= other_read.barriers;
3179 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003180 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003181
John Zulauf5f13a792020-03-10 07:31:21 -06003182 break;
3183 }
3184 }
3185 } else {
3186 // The other read stage doesn't exist in this, so add it.
3187 last_reads[last_read_count] = other_read;
3188 last_read_count++;
3189 last_read_stages |= other_read.stage;
John Zulauf4285ee92020-09-23 10:20:52 -06003190 if (other_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06003191 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003192 }
John Zulauf5f13a792020-03-10 07:31:21 -06003193 }
3194 }
John Zulauf361fb532020-07-22 10:45:39 -06003195 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003196 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3197 // ignore it.
John Zulauf5f13a792020-03-10 07:31:21 -06003198}
3199
John Zulauf9cb530d2019-09-30 14:14:10 -06003200void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
3201 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3202 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003203 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003204 // Mulitple outstanding reads may be of interest and do dependency chains independently
3205 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3206 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003207 uint32_t update_index = kStageCount;
John Zulauf9cb530d2019-09-30 14:14:10 -06003208 if (usage_stage & last_read_stages) {
3209 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf4285ee92020-09-23 10:20:52 -06003210 if (last_reads[read_index].stage == usage_stage) {
3211 update_index = read_index;
John Zulauf9cb530d2019-09-30 14:14:10 -06003212 break;
3213 }
3214 }
John Zulauf4285ee92020-09-23 10:20:52 -06003215 assert(update_index < last_read_count);
John Zulauf9cb530d2019-09-30 14:14:10 -06003216 } else {
John Zulauf9cb530d2019-09-30 14:14:10 -06003217 assert(last_read_count < last_reads.size());
John Zulauf4285ee92020-09-23 10:20:52 -06003218 update_index = last_read_count++;
John Zulauf9cb530d2019-09-30 14:14:10 -06003219 last_read_stages |= usage_stage;
3220 }
John Zulauf4285ee92020-09-23 10:20:52 -06003221 last_reads[update_index].Set(usage_stage, usage_bit, 0, tag);
3222
3223 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
3224 if (usage_stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06003225 // TODO Revisit re: multiple reads for a given stage
3226 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003227 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003228 } else {
3229 // Assume write
3230 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003231 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003232 }
3233}
John Zulauf5f13a792020-03-10 07:31:21 -06003234
John Zulauf89311b42020-09-29 16:28:47 -06003235// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3236// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3237// We can overwrite them as *this* write is now after them.
3238//
3239// 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 -07003240void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulauf89311b42020-09-29 16:28:47 -06003241 last_read_count = 0;
3242 last_read_stages = 0;
3243 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003244 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003245
3246 write_barriers = 0;
3247 write_dependency_chain = 0;
3248 write_tag = tag;
3249 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003250}
3251
John Zulauf89311b42020-09-29 16:28:47 -06003252// Apply the memory barrier without updating the existing barriers. The execution barrier
3253// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3254// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3255// replace the current write barriers or add to them, so accumulate to pending as well.
3256void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3257 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3258 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003259 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3260 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3261 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3262 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulauf4a6105a2020-11-17 15:11:05 -07003263 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003264 pending_write_barriers |= barrier.dst_access_scope;
3265 pending_write_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003266 }
John Zulauf89311b42020-09-29 16:28:47 -06003267 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3268 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003269
John Zulauf89311b42020-09-29 16:28:47 -06003270 if (!pending_layout_transition) {
3271 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3272 // don't need to be tracked as we're just going to zero them.
John Zulaufa0a98292020-09-18 09:30:10 -06003273 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf89311b42020-09-29 16:28:47 -06003274 ReadState &access = last_reads[read_index];
3275 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
3276 if (barrier.src_exec_scope & (access.stage | access.barriers)) {
3277 access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003278 }
3279 }
John Zulaufa0a98292020-09-18 09:30:10 -06003280 }
John Zulaufa0a98292020-09-18 09:30:10 -06003281}
3282
John Zulauf4a6105a2020-11-17 15:11:05 -07003283// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3284// changes the "chaining" state, but to keep barriers independent. See discussion above.
3285void ResourceAccessState::ApplyBarrier(const ResourceUsageTag &scope_tag, const SyncBarrier &barrier, bool layout_transition) {
3286 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3287 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3288 // in order to know if it's in the excecution scope
3289 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3290 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3291 // errors w.r.t. "most recent" accesses.
3292 if (layout_transition || ((write_tag.IsBefore(scope_tag)) && (barrier.src_access_scope & last_write).any())) {
3293 pending_write_barriers |= barrier.dst_access_scope;
3294 pending_write_dep_chain |= barrier.dst_exec_scope;
3295 }
3296 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3297 pending_layout_transition |= layout_transition;
3298
3299 if (!pending_layout_transition) {
3300 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3301 // don't need to be tracked as we're just going to zero them.
3302 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
3303 ReadState &access = last_reads[read_index];
3304 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3305 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3306 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3307 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3308 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3309 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3310 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
3311 if (access.tag.IsBefore(scope_tag) && (barrier.src_exec_scope & (access.stage | access.barriers))) {
3312 access.pending_dep_chain |= barrier.dst_exec_scope;
3313 }
3314 }
3315 }
3316}
John Zulauf89311b42020-09-29 16:28:47 -06003317void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
3318 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06003319 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
3320 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
3321 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003322 }
John Zulauf89311b42020-09-29 16:28:47 -06003323
3324 // Apply the accumulate execution barriers (and thus update chaining information)
3325 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
3326 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
3327 ReadState &access = last_reads[read_index];
3328 access.barriers |= access.pending_dep_chain;
3329 read_execution_barriers |= access.barriers;
3330 access.pending_dep_chain = 0;
3331 }
3332
3333 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3334 write_dependency_chain |= pending_write_dep_chain;
3335 write_barriers |= pending_write_barriers;
3336 pending_write_dep_chain = 0;
3337 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003338}
3339
John Zulauf59e25072020-07-17 10:55:21 -06003340// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003341VkPipelineStageFlags ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
John Zulauf59e25072020-07-17 10:55:21 -06003342 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003343
3344 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
3345 const auto &read_access = last_reads[read_index];
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003346 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003347 barriers = read_access.barriers;
3348 break;
John Zulauf59e25072020-07-17 10:55:21 -06003349 }
3350 }
John Zulauf4285ee92020-09-23 10:20:52 -06003351
John Zulauf59e25072020-07-17 10:55:21 -06003352 return barriers;
3353}
3354
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003355inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003356 assert(IsRead(usage));
3357 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3358 // * the previous reads are not hazards, and thus last_write must be visible and available to
3359 // any reads that happen after.
3360 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3361 // 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 -07003362 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003363}
3364
John Zulauf4285ee92020-09-23 10:20:52 -06003365VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const SyncOrderingBarrier &ordering) const {
3366 // Whether the stage are in the ordering scope only matters if the current write is ordered
3367 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
3368 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003369 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003370 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003371 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
3372 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
3373 }
3374
3375 return ordered_stages;
3376}
3377
3378inline ResourceAccessState::ReadState *ResourceAccessState::GetReadStateForStage(VkPipelineStageFlagBits stage,
3379 uint32_t search_limit) {
3380 ReadState *read_state = nullptr;
3381 search_limit = std::min(search_limit, last_read_count);
3382 for (uint32_t i = 0; i < search_limit; i++) {
3383 if (last_reads[i].stage == stage) {
3384 read_state = &last_reads[i];
3385 break;
3386 }
3387 }
3388 return read_state;
3389}
3390
John Zulaufd1f85d42020-04-15 12:23:15 -06003391void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003392 auto *access_context = GetAccessContextNoInsert(command_buffer);
3393 if (access_context) {
3394 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003395 }
3396}
3397
John Zulaufd1f85d42020-04-15 12:23:15 -06003398void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3399 auto access_found = cb_access_state.find(command_buffer);
3400 if (access_found != cb_access_state.end()) {
3401 access_found->second->Reset();
3402 cb_access_state.erase(access_found);
3403 }
3404}
3405
John Zulauf89311b42020-09-29 16:28:47 -06003406void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
3407 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags src_access_scope,
3408 SyncStageAccessFlags dst_access_scope, uint32_t memory_barrier_count,
3409 const VkMemoryBarrier *pMemoryBarriers, const ResourceUsageTag &tag) {
John Zulauf1e331ec2020-12-04 18:29:38 -07003410 std::vector<PipelineBarrierOp> barrier_ops;
3411 barrier_ops.reserve(std::min<uint32_t>(1, memory_barrier_count));
John Zulauf89311b42020-09-29 16:28:47 -06003412 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
3413 const auto &barrier = pMemoryBarriers[barrier_index];
3414 SyncBarrier sync_barrier(src_exec_scope, SyncStageAccess::AccessScope(src_access_scope, barrier.srcAccessMask),
3415 dst_exec_scope, SyncStageAccess::AccessScope(dst_access_scope, barrier.dstAccessMask));
John Zulauf1e331ec2020-12-04 18:29:38 -07003416 barrier_ops.emplace_back(sync_barrier, false);
John Zulauf89311b42020-09-29 16:28:47 -06003417 }
3418 if (0 == memory_barrier_count) {
3419 // If there are no global memory barriers, force an exec barrier
John Zulauf1e331ec2020-12-04 18:29:38 -07003420 barrier_ops.emplace_back(SyncBarrier(src_exec_scope, 0, dst_exec_scope, 0), false);
John Zulauf89311b42020-09-29 16:28:47 -06003421 }
John Zulauf1e331ec2020-12-04 18:29:38 -07003422 ApplyBarrierOpsFunctor<PipelineBarrierOp> barriers_functor(true /* resolve */, barrier_ops, tag);
John Zulauf540266b2020-04-06 18:54:53 -06003423 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06003424}
3425
John Zulauf540266b2020-04-06 18:54:53 -06003426void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003427 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
3428 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06003429 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003430 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003431 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06003432 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
3433 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06003434 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
3435 const ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06003436 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
3437 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06003438 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf4a6105a2020-11-17 15:11:05 -07003439 const ApplyBarrierFunctor<PipelineBarrierOp> update_action({sync_barrier, false /* layout_transition */});
John Zulauf89311b42020-09-29 16:28:47 -06003440 context->UpdateResourceAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06003441 }
3442}
3443
John Zulauf540266b2020-04-06 18:54:53 -06003444void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003445 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
3446 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06003447 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07003448 for (uint32_t index = 0; index < barrier_count; index++) {
3449 const auto &barrier = barriers[index];
3450 const auto *image = Get<IMAGE_STATE>(barrier.image);
3451 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06003452 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06003453 bool layout_transition = barrier.oldLayout != barrier.newLayout;
3454 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
3455 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06003456 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf4a6105a2020-11-17 15:11:05 -07003457 const ApplyBarrierFunctor<PipelineBarrierOp> barrier_action({sync_barrier, layout_transition});
John Zulauf89311b42020-09-29 16:28:47 -06003458 context->UpdateResourceAccess(*image, subresource_range, barrier_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06003459 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003460}
3461
3462bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3463 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3464 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003465 const auto *cb_context = GetAccessContext(commandBuffer);
3466 assert(cb_context);
3467 if (!cb_context) return skip;
3468 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003469
John Zulauf3d84f1b2020-03-09 13:33:25 -06003470 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003471 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003472 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003473
3474 for (uint32_t region = 0; region < regionCount; region++) {
3475 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003476 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003477 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003478 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003479 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003480 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003481 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003482 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003483 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003484 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003485 }
John Zulauf16adfc92020-04-08 10:28:33 -06003486 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003487 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06003488 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003489 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003490 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003491 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003492 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003493 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003494 }
3495 }
3496 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003497 }
3498 return skip;
3499}
3500
3501void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3502 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003503 auto *cb_context = GetAccessContext(commandBuffer);
3504 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003505 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003506 auto *context = cb_context->GetCurrentAccessContext();
3507
John Zulauf9cb530d2019-09-30 14:14:10 -06003508 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003509 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003510
3511 for (uint32_t region = 0; region < regionCount; region++) {
3512 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003513 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003514 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003515 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003516 }
John Zulauf16adfc92020-04-08 10:28:33 -06003517 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003518 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003519 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003520 }
3521 }
3522}
3523
John Zulauf4a6105a2020-11-17 15:11:05 -07003524void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3525 // Clear out events from the command buffer contexts
3526 for (auto &cb_context : cb_access_state) {
3527 cb_context.second->RecordDestroyEvent(event);
3528 }
3529}
3530
Jeff Leger178b1e52020-10-05 12:22:23 -04003531bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3532 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3533 bool skip = false;
3534 const auto *cb_context = GetAccessContext(commandBuffer);
3535 assert(cb_context);
3536 if (!cb_context) return skip;
3537 const auto *context = cb_context->GetCurrentAccessContext();
3538
3539 // If we have no previous accesses, we have no hazards
3540 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3541 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3542
3543 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3544 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3545 if (src_buffer) {
3546 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
3547 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
3548 if (hazard.hazard) {
3549 // TODO -- add tag information to log msg when useful.
3550 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3551 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3552 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
3553 region, string_UsageTag(hazard).c_str());
3554 }
3555 }
3556 if (dst_buffer && !skip) {
3557 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
3558 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
3559 if (hazard.hazard) {
3560 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3561 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3562 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
3563 region, string_UsageTag(hazard).c_str());
3564 }
3565 }
3566 if (skip) break;
3567 }
3568 return skip;
3569}
3570
3571void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3572 auto *cb_context = GetAccessContext(commandBuffer);
3573 assert(cb_context);
3574 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3575 auto *context = cb_context->GetCurrentAccessContext();
3576
3577 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3578 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3579
3580 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3581 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3582 if (src_buffer) {
3583 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
3584 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
3585 }
3586 if (dst_buffer) {
3587 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
3588 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
3589 }
3590 }
3591}
3592
John Zulauf5c5e88d2019-12-26 11:22:02 -07003593bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3594 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3595 const VkImageCopy *pRegions) const {
3596 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003597 const auto *cb_access_context = GetAccessContext(commandBuffer);
3598 assert(cb_access_context);
3599 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003600
John Zulauf3d84f1b2020-03-09 13:33:25 -06003601 const auto *context = cb_access_context->GetCurrentAccessContext();
3602 assert(context);
3603 if (!context) return skip;
3604
3605 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3606 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003607 for (uint32_t region = 0; region < regionCount; region++) {
3608 const auto &copy_region = pRegions[region];
3609 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003610 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003611 copy_region.srcOffset, copy_region.extent);
3612 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003613 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003614 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003615 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003616 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003617 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003618 }
3619
3620 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003621 VkExtent3D dst_copy_extent =
3622 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06003623 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003624 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003625 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003626 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003627 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003628 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003629 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003630 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003631 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003632 }
3633 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003634
John Zulauf5c5e88d2019-12-26 11:22:02 -07003635 return skip;
3636}
3637
3638void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3639 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3640 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003641 auto *cb_access_context = GetAccessContext(commandBuffer);
3642 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003643 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003644 auto *context = cb_access_context->GetCurrentAccessContext();
3645 assert(context);
3646
John Zulauf5c5e88d2019-12-26 11:22:02 -07003647 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003648 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003649
3650 for (uint32_t region = 0; region < regionCount; region++) {
3651 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003652 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003653 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
3654 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003655 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003656 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003657 VkExtent3D dst_copy_extent =
3658 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06003659 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
3660 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003661 }
3662 }
3663}
3664
Jeff Leger178b1e52020-10-05 12:22:23 -04003665bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3666 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3667 bool skip = false;
3668 const auto *cb_access_context = GetAccessContext(commandBuffer);
3669 assert(cb_access_context);
3670 if (!cb_access_context) return skip;
3671
3672 const auto *context = cb_access_context->GetCurrentAccessContext();
3673 assert(context);
3674 if (!context) return skip;
3675
3676 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3677 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3678 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3679 const auto &copy_region = pCopyImageInfo->pRegions[region];
3680 if (src_image) {
3681 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
3682 copy_region.srcOffset, copy_region.extent);
3683 if (hazard.hazard) {
3684 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3685 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3686 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
3687 region, string_UsageTag(hazard).c_str());
3688 }
3689 }
3690
3691 if (dst_image) {
3692 VkExtent3D dst_copy_extent =
3693 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
3694 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
3695 copy_region.dstOffset, dst_copy_extent);
3696 if (hazard.hazard) {
3697 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3698 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3699 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
3700 region, string_UsageTag(hazard).c_str());
3701 }
3702 if (skip) break;
3703 }
3704 }
3705
3706 return skip;
3707}
3708
3709void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3710 auto *cb_access_context = GetAccessContext(commandBuffer);
3711 assert(cb_access_context);
3712 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3713 auto *context = cb_access_context->GetCurrentAccessContext();
3714 assert(context);
3715
3716 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3717 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3718
3719 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3720 const auto &copy_region = pCopyImageInfo->pRegions[region];
3721 if (src_image) {
3722 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
3723 copy_region.extent, tag);
3724 }
3725 if (dst_image) {
3726 VkExtent3D dst_copy_extent =
3727 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
3728 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
3729 dst_copy_extent, tag);
3730 }
3731 }
3732}
3733
John Zulauf9cb530d2019-09-30 14:14:10 -06003734bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3735 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3736 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3737 uint32_t bufferMemoryBarrierCount,
3738 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3739 uint32_t imageMemoryBarrierCount,
3740 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3741 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003742 const auto *cb_access_context = GetAccessContext(commandBuffer);
3743 assert(cb_access_context);
3744 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003745
John Zulauf3d84f1b2020-03-09 13:33:25 -06003746 const auto *context = cb_access_context->GetCurrentAccessContext();
3747 assert(context);
3748 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003749
John Zulauf3d84f1b2020-03-09 13:33:25 -06003750 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003751 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3752 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07003753 // Validate Image Layout transitions
3754 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
3755 const auto &barrier = pImageMemoryBarriers[index];
3756 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
3757 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
3758 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06003759 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07003760 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003761 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003762 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003763 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003764 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003765 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07003766 }
3767 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003768
3769 return skip;
3770}
3771
3772void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3773 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3774 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3775 uint32_t bufferMemoryBarrierCount,
3776 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3777 uint32_t imageMemoryBarrierCount,
3778 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003779 auto *cb_access_context = GetAccessContext(commandBuffer);
3780 assert(cb_access_context);
3781 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06003782 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003783 auto access_context = cb_access_context->GetCurrentAccessContext();
3784 assert(access_context);
3785 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003786
John Zulauf3d84f1b2020-03-09 13:33:25 -06003787 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003788 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003789 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003790 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
3791 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3792 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf89311b42020-09-29 16:28:47 -06003793
3794 // These two apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
3795 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
3796 // of the barriers is maintained.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003797 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
3798 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06003799 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06003800 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003801
John Zulauf89311b42020-09-29 16:28:47 -06003802 // Apply the global barriers last as is it walks all memory, it can also clean up the "pending" state without requiring an
3803 // additional pass, updating the dependency chains *last* as it goes along.
3804 // This is needed to guarantee order independence of the three lists.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003805 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf89311b42020-09-29 16:28:47 -06003806 pMemoryBarriers, tag);
John Zulauf4a6105a2020-11-17 15:11:05 -07003807
3808 // Need to pass the unexpanded stage masks as the ALL_COMMANDS bit is removed during expansion.
3809 cb_access_context->ApplyGlobalBarriersToEvents(srcStageMask, src_exec_scope, dstStageMask, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06003810}
3811
3812void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3813 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3814 // The state tracker sets up the device state
3815 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3816
John Zulauf5f13a792020-03-10 07:31:21 -06003817 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3818 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003819 // TODO: Find a good way to do this hooklessly.
3820 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3821 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3822 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3823
John Zulaufd1f85d42020-04-15 12:23:15 -06003824 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3825 sync_device_state->ResetCommandBufferCallback(command_buffer);
3826 });
3827 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3828 sync_device_state->FreeCommandBufferCallback(command_buffer);
3829 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003830}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003831
John Zulauf355e49b2020-04-24 15:11:15 -06003832bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003833 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003834 bool skip = false;
3835 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
3836 auto cb_context = GetAccessContext(commandBuffer);
3837
3838 if (rp_state && cb_context) {
3839 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
3840 }
3841
3842 return skip;
3843}
3844
3845bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3846 VkSubpassContents contents) const {
3847 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3848 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3849 subpass_begin_info.contents = contents;
3850 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
3851 return skip;
3852}
3853
3854bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003855 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003856 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3857 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
3858 return skip;
3859}
3860
3861bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3862 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003863 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003864 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3865 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3866 return skip;
3867}
3868
John Zulauf3d84f1b2020-03-09 13:33:25 -06003869void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3870 VkResult result) {
3871 // The state tracker sets up the command buffer state
3872 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3873
3874 // Create/initialize the structure that trackers accesses at the command buffer scope.
3875 auto cb_access_context = GetAccessContext(commandBuffer);
3876 assert(cb_access_context);
3877 cb_access_context->Reset();
3878}
3879
3880void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003881 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003882 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003883 if (cb_context) {
3884 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003885 }
3886}
3887
3888void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3889 VkSubpassContents contents) {
3890 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3891 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3892 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003893 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003894}
3895
3896void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3897 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3898 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003899 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003900}
3901
3902void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3903 const VkRenderPassBeginInfo *pRenderPassBegin,
3904 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3905 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003906 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3907}
3908
Mike Schuchardt2df08912020-12-15 16:28:09 -08003909bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3910 const VkSubpassEndInfo *pSubpassEndInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003911 bool skip = false;
3912
3913 auto cb_context = GetAccessContext(commandBuffer);
3914 assert(cb_context);
3915 auto cb_state = cb_context->GetCommandBufferState();
3916 if (!cb_state) return skip;
3917
3918 auto rp_state = cb_state->activeRenderPass;
3919 if (!rp_state) return skip;
3920
3921 skip |= cb_context->ValidateNextSubpass(func_name);
3922
3923 return skip;
3924}
3925
3926bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3927 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
3928 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3929 subpass_begin_info.contents = contents;
3930 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3931 return skip;
3932}
3933
Mike Schuchardt2df08912020-12-15 16:28:09 -08003934bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3935 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003936 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3937 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3938 return skip;
3939}
3940
3941bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3942 const VkSubpassEndInfo *pSubpassEndInfo) const {
3943 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3944 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3945 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003946}
3947
3948void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003949 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003950 auto cb_context = GetAccessContext(commandBuffer);
3951 assert(cb_context);
3952 auto cb_state = cb_context->GetCommandBufferState();
3953 if (!cb_state) return;
3954
3955 auto rp_state = cb_state->activeRenderPass;
3956 if (!rp_state) return;
3957
John Zulauf355e49b2020-04-24 15:11:15 -06003958 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003959}
3960
3961void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3962 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
3963 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3964 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003965 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003966}
3967
3968void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3969 const VkSubpassEndInfo *pSubpassEndInfo) {
3970 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003971 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003972}
3973
3974void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3975 const VkSubpassEndInfo *pSubpassEndInfo) {
3976 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003977 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003978}
3979
Mike Schuchardt2df08912020-12-15 16:28:09 -08003980bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003981 const char *func_name) const {
3982 bool skip = false;
3983
3984 auto cb_context = GetAccessContext(commandBuffer);
3985 assert(cb_context);
3986 auto cb_state = cb_context->GetCommandBufferState();
3987 if (!cb_state) return skip;
3988
3989 auto rp_state = cb_state->activeRenderPass;
3990 if (!rp_state) return skip;
3991
3992 skip |= cb_context->ValidateEndRenderpass(func_name);
3993 return skip;
3994}
3995
3996bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3997 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
3998 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
3999 return skip;
4000}
4001
Mike Schuchardt2df08912020-12-15 16:28:09 -08004002bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06004003 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
4004 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
4005 return skip;
4006}
4007
4008bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08004009 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06004010 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
4011 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
4012 return skip;
4013}
4014
4015void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
4016 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06004017 // Resolve the all subpass contexts to the command buffer contexts
4018 auto cb_context = GetAccessContext(commandBuffer);
4019 assert(cb_context);
4020 auto cb_state = cb_context->GetCommandBufferState();
4021 if (!cb_state) return;
4022
locke-lunargaecf2152020-05-12 17:15:41 -06004023 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06004024 if (!rp_state) return;
4025
John Zulauf355e49b2020-04-24 15:11:15 -06004026 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06004027}
John Zulauf3d84f1b2020-03-09 13:33:25 -06004028
John Zulauf33fc1d52020-07-17 11:01:10 -06004029// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
4030// updates to a resource which do not conflict at the byte level.
4031// TODO: Revisit this rule to see if it needs to be tighter or looser
4032// TODO: Add programatic control over suppression heuristics
4033bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
4034 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
4035}
4036
John Zulauf3d84f1b2020-03-09 13:33:25 -06004037void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004038 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06004039 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004040}
4041
4042void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06004043 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06004044 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004045}
4046
4047void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06004048 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06004049 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004050}
locke-lunarga19c71d2020-03-02 18:17:04 -07004051
Jeff Leger178b1e52020-10-05 12:22:23 -04004052template <typename BufferImageCopyRegionType>
4053bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4054 VkImageLayout dstImageLayout, uint32_t regionCount,
4055 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004056 bool skip = false;
4057 const auto *cb_access_context = GetAccessContext(commandBuffer);
4058 assert(cb_access_context);
4059 if (!cb_access_context) return skip;
4060
Jeff Leger178b1e52020-10-05 12:22:23 -04004061 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4062 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
4063
locke-lunarga19c71d2020-03-02 18:17:04 -07004064 const auto *context = cb_access_context->GetCurrentAccessContext();
4065 assert(context);
4066 if (!context) return skip;
4067
4068 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07004069 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4070
4071 for (uint32_t region = 0; region < regionCount; region++) {
4072 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06004073 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004074 ResourceAccessRange src_range =
4075 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004076 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07004077 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06004078 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06004079 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004080 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004081 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004082 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004083 }
4084 }
4085 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004086 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004087 copy_region.imageOffset, copy_region.imageExtent);
4088 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004089 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004090 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004091 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004092 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004093 }
4094 if (skip) break;
4095 }
4096 if (skip) break;
4097 }
4098 return skip;
4099}
4100
Jeff Leger178b1e52020-10-05 12:22:23 -04004101bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4102 VkImageLayout dstImageLayout, uint32_t regionCount,
4103 const VkBufferImageCopy *pRegions) const {
4104 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
4105 COPY_COMMAND_VERSION_1);
4106}
4107
4108bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4109 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
4110 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4111 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4112 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4113}
4114
4115template <typename BufferImageCopyRegionType>
4116void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4117 VkImageLayout dstImageLayout, uint32_t regionCount,
4118 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004119 auto *cb_access_context = GetAccessContext(commandBuffer);
4120 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004121
4122 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4123 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
4124
4125 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004126 auto *context = cb_access_context->GetCurrentAccessContext();
4127 assert(context);
4128
4129 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06004130 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004131
4132 for (uint32_t region = 0; region < regionCount; region++) {
4133 const auto &copy_region = pRegions[region];
4134 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004135 ResourceAccessRange src_range =
4136 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004137 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004138 }
4139 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004140 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06004141 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004142 }
4143 }
4144}
4145
Jeff Leger178b1e52020-10-05 12:22:23 -04004146void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4147 VkImageLayout dstImageLayout, uint32_t regionCount,
4148 const VkBufferImageCopy *pRegions) {
4149 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
4150 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4151}
4152
4153void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4154 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4155 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4156 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4157 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4158 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4159}
4160
4161template <typename BufferImageCopyRegionType>
4162bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4163 VkBuffer dstBuffer, uint32_t regionCount,
4164 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004165 bool skip = false;
4166 const auto *cb_access_context = GetAccessContext(commandBuffer);
4167 assert(cb_access_context);
4168 if (!cb_access_context) return skip;
4169
Jeff Leger178b1e52020-10-05 12:22:23 -04004170 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4171 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
4172
locke-lunarga19c71d2020-03-02 18:17:04 -07004173 const auto *context = cb_access_context->GetCurrentAccessContext();
4174 assert(context);
4175 if (!context) return skip;
4176
4177 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4178 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4179 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
4180 for (uint32_t region = 0; region < regionCount; region++) {
4181 const auto &copy_region = pRegions[region];
4182 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004183 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004184 copy_region.imageOffset, copy_region.imageExtent);
4185 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004186 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004187 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004188 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004189 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004190 }
4191 }
4192 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06004193 ResourceAccessRange dst_range =
4194 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004195 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07004196 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004197 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004198 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004199 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004200 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004201 }
4202 }
4203 if (skip) break;
4204 }
4205 return skip;
4206}
4207
Jeff Leger178b1e52020-10-05 12:22:23 -04004208bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4209 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4210 const VkBufferImageCopy *pRegions) const {
4211 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
4212 COPY_COMMAND_VERSION_1);
4213}
4214
4215bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4216 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4217 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4218 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4219 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4220}
4221
4222template <typename BufferImageCopyRegionType>
4223void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4224 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
4225 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004226 auto *cb_access_context = GetAccessContext(commandBuffer);
4227 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004228
4229 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4230 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
4231
4232 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004233 auto *context = cb_access_context->GetCurrentAccessContext();
4234 assert(context);
4235
4236 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004237 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4238 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 -06004239 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004240
4241 for (uint32_t region = 0; region < regionCount; region++) {
4242 const auto &copy_region = pRegions[region];
4243 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004244 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06004245 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004246 }
4247 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004248 ResourceAccessRange dst_range =
4249 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004250 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004251 }
4252 }
4253}
4254
Jeff Leger178b1e52020-10-05 12:22:23 -04004255void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4256 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4257 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
4258 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4259}
4260
4261void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4262 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4263 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4264 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4265 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4266 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4267}
4268
4269template <typename RegionType>
4270bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4271 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4272 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004273 bool skip = false;
4274 const auto *cb_access_context = GetAccessContext(commandBuffer);
4275 assert(cb_access_context);
4276 if (!cb_access_context) return skip;
4277
4278 const auto *context = cb_access_context->GetCurrentAccessContext();
4279 assert(context);
4280 if (!context) return skip;
4281
4282 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4283 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4284
4285 for (uint32_t region = 0; region < regionCount; region++) {
4286 const auto &blit_region = pRegions[region];
4287 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004288 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4289 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4290 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4291 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4292 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4293 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
4294 auto hazard =
4295 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004296 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004297 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004298 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004299 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004300 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004301 }
4302 }
4303
4304 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004305 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4306 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4307 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4308 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4309 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4310 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
4311 auto hazard =
4312 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004313 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004314 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004315 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004316 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004317 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004318 }
4319 if (skip) break;
4320 }
4321 }
4322
4323 return skip;
4324}
4325
Jeff Leger178b1e52020-10-05 12:22:23 -04004326bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4327 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4328 const VkImageBlit *pRegions, VkFilter filter) const {
4329 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4330 "vkCmdBlitImage");
4331}
4332
4333bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4334 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4335 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4336 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4337 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4338}
4339
4340template <typename RegionType>
4341void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4342 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4343 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004344 auto *cb_access_context = GetAccessContext(commandBuffer);
4345 assert(cb_access_context);
4346 auto *context = cb_access_context->GetCurrentAccessContext();
4347 assert(context);
4348
4349 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004350 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004351
4352 for (uint32_t region = 0; region < regionCount; region++) {
4353 const auto &blit_region = pRegions[region];
4354 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004355 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4356 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4357 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4358 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4359 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4360 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
4361 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004362 }
4363 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004364 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4365 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4366 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4367 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4368 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4369 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
4370 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004371 }
4372 }
4373}
locke-lunarg36ba2592020-04-03 09:42:04 -06004374
Jeff Leger178b1e52020-10-05 12:22:23 -04004375void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4376 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4377 const VkImageBlit *pRegions, VkFilter filter) {
4378 auto *cb_access_context = GetAccessContext(commandBuffer);
4379 assert(cb_access_context);
4380 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4381 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4382 pRegions, filter);
4383 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4384}
4385
4386void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4387 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4388 auto *cb_access_context = GetAccessContext(commandBuffer);
4389 assert(cb_access_context);
4390 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4391 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4392 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4393 pBlitImageInfo->filter, tag);
4394}
4395
locke-lunarg61870c22020-06-09 14:51:50 -06004396bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
4397 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
4398 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004399 bool skip = false;
4400 if (drawCount == 0) return skip;
4401
4402 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4403 VkDeviceSize size = struct_size;
4404 if (drawCount == 1 || stride == size) {
4405 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004406 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004407 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4408 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004409 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004410 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004411 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06004412 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004413 }
4414 } else {
4415 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004416 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004417 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4418 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004419 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004420 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4421 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
4422 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004423 break;
4424 }
4425 }
4426 }
4427 return skip;
4428}
4429
locke-lunarg61870c22020-06-09 14:51:50 -06004430void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
4431 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4432 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06004433 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4434 VkDeviceSize size = struct_size;
4435 if (drawCount == 1 || stride == size) {
4436 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004437 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004438 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
4439 } else {
4440 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004441 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004442 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
4443 }
4444 }
4445}
4446
locke-lunarg61870c22020-06-09 14:51:50 -06004447bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
4448 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004449 bool skip = false;
4450
4451 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004452 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004453 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4454 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004455 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004456 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004457 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06004458 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004459 }
4460 return skip;
4461}
4462
locke-lunarg61870c22020-06-09 14:51:50 -06004463void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06004464 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004465 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004466 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
4467}
4468
locke-lunarg36ba2592020-04-03 09:42:04 -06004469bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004470 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004471 const auto *cb_access_context = GetAccessContext(commandBuffer);
4472 assert(cb_access_context);
4473 if (!cb_access_context) return skip;
4474
locke-lunarg61870c22020-06-09 14:51:50 -06004475 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004476 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004477}
4478
4479void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004480 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004481 auto *cb_access_context = GetAccessContext(commandBuffer);
4482 assert(cb_access_context);
4483 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004484
locke-lunarg61870c22020-06-09 14:51:50 -06004485 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004486}
locke-lunarge1a67022020-04-29 00:15:36 -06004487
4488bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004489 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004490 const auto *cb_access_context = GetAccessContext(commandBuffer);
4491 assert(cb_access_context);
4492 if (!cb_access_context) return skip;
4493
4494 const auto *context = cb_access_context->GetCurrentAccessContext();
4495 assert(context);
4496 if (!context) return skip;
4497
locke-lunarg61870c22020-06-09 14:51:50 -06004498 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
4499 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
4500 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004501 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004502}
4503
4504void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004505 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004506 auto *cb_access_context = GetAccessContext(commandBuffer);
4507 assert(cb_access_context);
4508 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4509 auto *context = cb_access_context->GetCurrentAccessContext();
4510 assert(context);
4511
locke-lunarg61870c22020-06-09 14:51:50 -06004512 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4513 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004514}
4515
4516bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4517 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004518 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004519 const auto *cb_access_context = GetAccessContext(commandBuffer);
4520 assert(cb_access_context);
4521 if (!cb_access_context) return skip;
4522
locke-lunarg61870c22020-06-09 14:51:50 -06004523 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4524 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4525 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004526 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004527}
4528
4529void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4530 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004531 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004532 auto *cb_access_context = GetAccessContext(commandBuffer);
4533 assert(cb_access_context);
4534 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004535
locke-lunarg61870c22020-06-09 14:51:50 -06004536 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4537 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4538 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004539}
4540
4541bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4542 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004543 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004544 const auto *cb_access_context = GetAccessContext(commandBuffer);
4545 assert(cb_access_context);
4546 if (!cb_access_context) return skip;
4547
locke-lunarg61870c22020-06-09 14:51:50 -06004548 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4549 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4550 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004551 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004552}
4553
4554void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4555 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004556 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004557 auto *cb_access_context = GetAccessContext(commandBuffer);
4558 assert(cb_access_context);
4559 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004560
locke-lunarg61870c22020-06-09 14:51:50 -06004561 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4562 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4563 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004564}
4565
4566bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4567 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004568 bool skip = false;
4569 if (drawCount == 0) return skip;
4570
locke-lunargff255f92020-05-13 18:53:52 -06004571 const auto *cb_access_context = GetAccessContext(commandBuffer);
4572 assert(cb_access_context);
4573 if (!cb_access_context) return skip;
4574
4575 const auto *context = cb_access_context->GetCurrentAccessContext();
4576 assert(context);
4577 if (!context) return skip;
4578
locke-lunarg61870c22020-06-09 14:51:50 -06004579 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4580 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
4581 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
4582 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004583
4584 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4585 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4586 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004587 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004588 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004589}
4590
4591void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4592 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004593 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004594 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004595 auto *cb_access_context = GetAccessContext(commandBuffer);
4596 assert(cb_access_context);
4597 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4598 auto *context = cb_access_context->GetCurrentAccessContext();
4599 assert(context);
4600
locke-lunarg61870c22020-06-09 14:51:50 -06004601 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4602 cb_access_context->RecordDrawSubpassAttachment(tag);
4603 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004604
4605 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4606 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4607 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004608 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004609}
4610
4611bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4612 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004613 bool skip = false;
4614 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004615 const auto *cb_access_context = GetAccessContext(commandBuffer);
4616 assert(cb_access_context);
4617 if (!cb_access_context) return skip;
4618
4619 const auto *context = cb_access_context->GetCurrentAccessContext();
4620 assert(context);
4621 if (!context) return skip;
4622
locke-lunarg61870c22020-06-09 14:51:50 -06004623 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4624 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
4625 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
4626 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004627
4628 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4629 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4630 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004631 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004632 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004633}
4634
4635void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4636 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004637 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004638 auto *cb_access_context = GetAccessContext(commandBuffer);
4639 assert(cb_access_context);
4640 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4641 auto *context = cb_access_context->GetCurrentAccessContext();
4642 assert(context);
4643
locke-lunarg61870c22020-06-09 14:51:50 -06004644 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4645 cb_access_context->RecordDrawSubpassAttachment(tag);
4646 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004647
4648 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4649 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4650 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004651 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004652}
4653
4654bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4655 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4656 uint32_t stride, const char *function) const {
4657 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004658 const auto *cb_access_context = GetAccessContext(commandBuffer);
4659 assert(cb_access_context);
4660 if (!cb_access_context) return skip;
4661
4662 const auto *context = cb_access_context->GetCurrentAccessContext();
4663 assert(context);
4664 if (!context) return skip;
4665
locke-lunarg61870c22020-06-09 14:51:50 -06004666 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4667 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
4668 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
4669 function);
4670 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004671
4672 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4673 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4674 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004675 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004676 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004677}
4678
4679bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4680 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4681 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004682 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4683 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004684}
4685
4686void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4687 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4688 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004689 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4690 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004691 auto *cb_access_context = GetAccessContext(commandBuffer);
4692 assert(cb_access_context);
4693 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
4694 auto *context = cb_access_context->GetCurrentAccessContext();
4695 assert(context);
4696
locke-lunarg61870c22020-06-09 14:51:50 -06004697 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4698 cb_access_context->RecordDrawSubpassAttachment(tag);
4699 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4700 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004701
4702 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4703 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4704 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004705 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004706}
4707
4708bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4709 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4710 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004711 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4712 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004713}
4714
4715void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4716 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4717 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004718 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4719 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004720 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004721}
4722
4723bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4724 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4725 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004726 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4727 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004728}
4729
4730void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4731 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4732 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004733 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4734 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004735 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4736}
4737
4738bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4739 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4740 uint32_t stride, const char *function) const {
4741 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004742 const auto *cb_access_context = GetAccessContext(commandBuffer);
4743 assert(cb_access_context);
4744 if (!cb_access_context) return skip;
4745
4746 const auto *context = cb_access_context->GetCurrentAccessContext();
4747 assert(context);
4748 if (!context) return skip;
4749
locke-lunarg61870c22020-06-09 14:51:50 -06004750 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4751 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
4752 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
4753 stride, function);
4754 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004755
4756 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4757 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4758 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004759 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004760 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004761}
4762
4763bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4764 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4765 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004766 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4767 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004768}
4769
4770void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4771 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4772 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004773 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4774 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004775 auto *cb_access_context = GetAccessContext(commandBuffer);
4776 assert(cb_access_context);
4777 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4778 auto *context = cb_access_context->GetCurrentAccessContext();
4779 assert(context);
4780
locke-lunarg61870c22020-06-09 14:51:50 -06004781 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4782 cb_access_context->RecordDrawSubpassAttachment(tag);
4783 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4784 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004785
4786 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4787 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004788 // We will update the index and vertex buffer in SubmitQueue in the future.
4789 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004790}
4791
4792bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4793 VkDeviceSize offset, VkBuffer countBuffer,
4794 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4795 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004796 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4797 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004798}
4799
4800void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4801 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4802 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004803 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4804 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004805 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4806}
4807
4808bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4809 VkDeviceSize offset, VkBuffer countBuffer,
4810 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4811 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004812 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4813 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004814}
4815
4816void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4817 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4818 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004819 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4820 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004821 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4822}
4823
4824bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4825 const VkClearColorValue *pColor, uint32_t rangeCount,
4826 const VkImageSubresourceRange *pRanges) const {
4827 bool skip = false;
4828 const auto *cb_access_context = GetAccessContext(commandBuffer);
4829 assert(cb_access_context);
4830 if (!cb_access_context) return skip;
4831
4832 const auto *context = cb_access_context->GetCurrentAccessContext();
4833 assert(context);
4834 if (!context) return skip;
4835
4836 const auto *image_state = Get<IMAGE_STATE>(image);
4837
4838 for (uint32_t index = 0; index < rangeCount; index++) {
4839 const auto &range = pRanges[index];
4840 if (image_state) {
4841 auto hazard =
4842 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4843 if (hazard.hazard) {
4844 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004845 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004846 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004847 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004848 }
4849 }
4850 }
4851 return skip;
4852}
4853
4854void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4855 const VkClearColorValue *pColor, uint32_t rangeCount,
4856 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004857 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004858 auto *cb_access_context = GetAccessContext(commandBuffer);
4859 assert(cb_access_context);
4860 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4861 auto *context = cb_access_context->GetCurrentAccessContext();
4862 assert(context);
4863
4864 const auto *image_state = Get<IMAGE_STATE>(image);
4865
4866 for (uint32_t index = 0; index < rangeCount; index++) {
4867 const auto &range = pRanges[index];
4868 if (image_state) {
4869 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4870 tag);
4871 }
4872 }
4873}
4874
4875bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4876 VkImageLayout imageLayout,
4877 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4878 const VkImageSubresourceRange *pRanges) const {
4879 bool skip = false;
4880 const auto *cb_access_context = GetAccessContext(commandBuffer);
4881 assert(cb_access_context);
4882 if (!cb_access_context) return skip;
4883
4884 const auto *context = cb_access_context->GetCurrentAccessContext();
4885 assert(context);
4886 if (!context) return skip;
4887
4888 const auto *image_state = Get<IMAGE_STATE>(image);
4889
4890 for (uint32_t index = 0; index < rangeCount; index++) {
4891 const auto &range = pRanges[index];
4892 if (image_state) {
4893 auto hazard =
4894 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4895 if (hazard.hazard) {
4896 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004897 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004898 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004899 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004900 }
4901 }
4902 }
4903 return skip;
4904}
4905
4906void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4907 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4908 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004909 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004910 auto *cb_access_context = GetAccessContext(commandBuffer);
4911 assert(cb_access_context);
4912 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4913 auto *context = cb_access_context->GetCurrentAccessContext();
4914 assert(context);
4915
4916 const auto *image_state = Get<IMAGE_STATE>(image);
4917
4918 for (uint32_t index = 0; index < rangeCount; index++) {
4919 const auto &range = pRanges[index];
4920 if (image_state) {
4921 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4922 tag);
4923 }
4924 }
4925}
4926
4927bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4928 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4929 VkDeviceSize dstOffset, VkDeviceSize stride,
4930 VkQueryResultFlags flags) const {
4931 bool skip = false;
4932 const auto *cb_access_context = GetAccessContext(commandBuffer);
4933 assert(cb_access_context);
4934 if (!cb_access_context) return skip;
4935
4936 const auto *context = cb_access_context->GetCurrentAccessContext();
4937 assert(context);
4938 if (!context) return skip;
4939
4940 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4941
4942 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004943 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004944 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4945 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004946 skip |=
4947 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4948 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4949 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004950 }
4951 }
locke-lunargff255f92020-05-13 18:53:52 -06004952
4953 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004954 return skip;
4955}
4956
4957void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4958 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4959 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004960 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4961 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004962 auto *cb_access_context = GetAccessContext(commandBuffer);
4963 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004964 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004965 auto *context = cb_access_context->GetCurrentAccessContext();
4966 assert(context);
4967
4968 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4969
4970 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004971 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004972 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4973 }
locke-lunargff255f92020-05-13 18:53:52 -06004974
4975 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004976}
4977
4978bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4979 VkDeviceSize size, uint32_t data) const {
4980 bool skip = false;
4981 const auto *cb_access_context = GetAccessContext(commandBuffer);
4982 assert(cb_access_context);
4983 if (!cb_access_context) return skip;
4984
4985 const auto *context = cb_access_context->GetCurrentAccessContext();
4986 assert(context);
4987 if (!context) return skip;
4988
4989 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4990
4991 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004992 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004993 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4994 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004995 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004996 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004997 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004998 }
4999 }
5000 return skip;
5001}
5002
5003void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5004 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005005 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06005006 auto *cb_access_context = GetAccessContext(commandBuffer);
5007 assert(cb_access_context);
5008 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
5009 auto *context = cb_access_context->GetCurrentAccessContext();
5010 assert(context);
5011
5012 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5013
5014 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005015 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06005016 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
5017 }
5018}
5019
5020bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5021 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5022 const VkImageResolve *pRegions) const {
5023 bool skip = false;
5024 const auto *cb_access_context = GetAccessContext(commandBuffer);
5025 assert(cb_access_context);
5026 if (!cb_access_context) return skip;
5027
5028 const auto *context = cb_access_context->GetCurrentAccessContext();
5029 assert(context);
5030 if (!context) return skip;
5031
5032 const auto *src_image = Get<IMAGE_STATE>(srcImage);
5033 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
5034
5035 for (uint32_t region = 0; region < regionCount; region++) {
5036 const auto &resolve_region = pRegions[region];
5037 if (src_image) {
5038 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5039 resolve_region.srcOffset, resolve_region.extent);
5040 if (hazard.hazard) {
5041 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005042 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005043 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06005044 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005045 }
5046 }
5047
5048 if (dst_image) {
5049 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5050 resolve_region.dstOffset, resolve_region.extent);
5051 if (hazard.hazard) {
5052 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005053 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005054 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06005055 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005056 }
5057 if (skip) break;
5058 }
5059 }
5060
5061 return skip;
5062}
5063
5064void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5065 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5066 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005067 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
5068 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06005069 auto *cb_access_context = GetAccessContext(commandBuffer);
5070 assert(cb_access_context);
5071 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
5072 auto *context = cb_access_context->GetCurrentAccessContext();
5073 assert(context);
5074
5075 auto *src_image = Get<IMAGE_STATE>(srcImage);
5076 auto *dst_image = Get<IMAGE_STATE>(dstImage);
5077
5078 for (uint32_t region = 0; region < regionCount; region++) {
5079 const auto &resolve_region = pRegions[region];
5080 if (src_image) {
5081 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5082 resolve_region.srcOffset, resolve_region.extent, tag);
5083 }
5084 if (dst_image) {
5085 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5086 resolve_region.dstOffset, resolve_region.extent, tag);
5087 }
5088 }
5089}
5090
Jeff Leger178b1e52020-10-05 12:22:23 -04005091bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5092 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
5093 bool skip = false;
5094 const auto *cb_access_context = GetAccessContext(commandBuffer);
5095 assert(cb_access_context);
5096 if (!cb_access_context) return skip;
5097
5098 const auto *context = cb_access_context->GetCurrentAccessContext();
5099 assert(context);
5100 if (!context) return skip;
5101
5102 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5103 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5104
5105 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5106 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5107 if (src_image) {
5108 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5109 resolve_region.srcOffset, resolve_region.extent);
5110 if (hazard.hazard) {
5111 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
5112 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
5113 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
5114 region, string_UsageTag(hazard).c_str());
5115 }
5116 }
5117
5118 if (dst_image) {
5119 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5120 resolve_region.dstOffset, resolve_region.extent);
5121 if (hazard.hazard) {
5122 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
5123 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
5124 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
5125 region, string_UsageTag(hazard).c_str());
5126 }
5127 if (skip) break;
5128 }
5129 }
5130
5131 return skip;
5132}
5133
5134void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5135 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5136 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5137 auto *cb_access_context = GetAccessContext(commandBuffer);
5138 assert(cb_access_context);
5139 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
5140 auto *context = cb_access_context->GetCurrentAccessContext();
5141 assert(context);
5142
5143 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5144 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5145
5146 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5147 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5148 if (src_image) {
5149 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5150 resolve_region.srcOffset, resolve_region.extent, tag);
5151 }
5152 if (dst_image) {
5153 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5154 resolve_region.dstOffset, resolve_region.extent, tag);
5155 }
5156 }
5157}
5158
locke-lunarge1a67022020-04-29 00:15:36 -06005159bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5160 VkDeviceSize dataSize, const void *pData) const {
5161 bool skip = false;
5162 const auto *cb_access_context = GetAccessContext(commandBuffer);
5163 assert(cb_access_context);
5164 if (!cb_access_context) return skip;
5165
5166 const auto *context = cb_access_context->GetCurrentAccessContext();
5167 assert(context);
5168 if (!context) return skip;
5169
5170 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5171
5172 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005173 // VK_WHOLE_SIZE not allowed
5174 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06005175 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
5176 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005177 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005178 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06005179 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005180 }
5181 }
5182 return skip;
5183}
5184
5185void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5186 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005187 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005188 auto *cb_access_context = GetAccessContext(commandBuffer);
5189 assert(cb_access_context);
5190 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5191 auto *context = cb_access_context->GetCurrentAccessContext();
5192 assert(context);
5193
5194 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5195
5196 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005197 // VK_WHOLE_SIZE not allowed
5198 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06005199 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
5200 }
5201}
locke-lunargff255f92020-05-13 18:53:52 -06005202
5203bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5204 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5205 bool skip = false;
5206 const auto *cb_access_context = GetAccessContext(commandBuffer);
5207 assert(cb_access_context);
5208 if (!cb_access_context) return skip;
5209
5210 const auto *context = cb_access_context->GetCurrentAccessContext();
5211 assert(context);
5212 if (!context) return skip;
5213
5214 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5215
5216 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005217 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06005218 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
5219 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005220 skip |=
5221 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5222 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
5223 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005224 }
5225 }
5226 return skip;
5227}
5228
5229void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5230 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005231 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005232 auto *cb_access_context = GetAccessContext(commandBuffer);
5233 assert(cb_access_context);
5234 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5235 auto *context = cb_access_context->GetCurrentAccessContext();
5236 assert(context);
5237
5238 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5239
5240 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005241 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06005242 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
5243 }
5244}
John Zulauf49beb112020-11-04 16:06:31 -07005245
5246bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5247 bool skip = false;
5248 const auto *cb_context = GetAccessContext(commandBuffer);
5249 assert(cb_context);
5250 if (!cb_context) return skip;
5251
5252 return cb_context->ValidateSetEvent(commandBuffer, event, stageMask);
5253}
5254
5255void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5256 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5257 auto *cb_context = GetAccessContext(commandBuffer);
5258 assert(cb_context);
5259 if (!cb_context) return;
John Zulauf4a6105a2020-11-17 15:11:05 -07005260 const auto tag = cb_context->NextCommandTag(CMD_SETEVENT);
5261 cb_context->RecordSetEvent(commandBuffer, event, stageMask, tag);
John Zulauf49beb112020-11-04 16:06:31 -07005262}
5263
5264bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5265 VkPipelineStageFlags stageMask) const {
5266 bool skip = false;
5267 const auto *cb_context = GetAccessContext(commandBuffer);
5268 assert(cb_context);
5269 if (!cb_context) return skip;
5270
5271 return cb_context->ValidateResetEvent(commandBuffer, event, stageMask);
5272}
5273
5274void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5275 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5276 auto *cb_context = GetAccessContext(commandBuffer);
5277 assert(cb_context);
5278 if (!cb_context) return;
5279
5280 cb_context->RecordResetEvent(commandBuffer, event, stageMask);
5281}
5282
5283bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5284 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5285 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5286 uint32_t bufferMemoryBarrierCount,
5287 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5288 uint32_t imageMemoryBarrierCount,
5289 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5290 bool skip = false;
5291 const auto *cb_context = GetAccessContext(commandBuffer);
5292 assert(cb_context);
5293 if (!cb_context) return skip;
5294
John Zulauf4a6105a2020-11-17 15:11:05 -07005295 return cb_context->ValidateWaitEvents(eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount, pMemoryBarriers,
5296 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
John Zulauf49beb112020-11-04 16:06:31 -07005297 pImageMemoryBarriers);
5298}
5299
5300void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5301 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5302 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5303 uint32_t bufferMemoryBarrierCount,
5304 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5305 uint32_t imageMemoryBarrierCount,
5306 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5307 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5308 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5309 imageMemoryBarrierCount, pImageMemoryBarriers);
5310
5311 auto *cb_context = GetAccessContext(commandBuffer);
5312 assert(cb_context);
5313 if (!cb_context) return;
5314
John Zulauf4a6105a2020-11-17 15:11:05 -07005315 const auto tag = cb_context->NextCommandTag(CMD_WAITEVENTS);
John Zulauf49beb112020-11-04 16:06:31 -07005316 cb_context->RecordWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5317 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
John Zulauf4a6105a2020-11-17 15:11:05 -07005318 pImageMemoryBarriers, tag);
5319}
5320
5321void SyncEventState::ResetFirstScope() {
5322 for (const auto address_type : kAddressTypes) {
5323 first_scope[static_cast<size_t>(address_type)].clear();
5324 }
5325 stage_mask = 0U;
5326 exec_scope = 0U;
5327 stage_accesses.reset();
5328}
5329
5330// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
5331SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(VkPipelineStageFlags srcStageMask) const {
5332 IgnoreReason reason = NotIgnored;
5333
5334 if (last_command == CMD_RESETEVENT && !HasBarrier(0U, 0U)) {
5335 reason = ResetWaitRace;
5336 } else if (unsynchronized_set) {
5337 reason = SetRace;
5338 } else {
5339 const VkPipelineStageFlags missing_bits = stage_mask_param & ~srcStageMask;
5340 if (missing_bits) reason = MissingStageBits;
5341 }
5342
5343 return reason;
5344}
5345
5346bool SyncEventState::HasBarrier(VkPipelineStageFlags stageMask, VkPipelineStageFlags exec_scope_arg) const {
5347 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5348 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5349 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005350}