blob: 7564faa890023d20ea35a3dc66c8b9ff0b49c55c [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
152 out << "command: " << CommandTypeString(tag.command);
153 out << ", seq_no: " << ((tag.index >> 1) & UINT32_MAX) << ", reset_no: " << (tag.index >> 33);
154 if (tag.index & 1) {
155 out << ", subcmd: " << (tag.index & 1);
156 }
157 return out.str();
158}
159
John Zulauf37ceaed2020-07-03 16:18:15 -0600160static std::string string_UsageTag(const HazardResult &hazard) {
161 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600162 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
163 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600164 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600165 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
166 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf59e25072020-07-17 10:55:21 -0600167 out << "(usage: " << usage_info.name << ", prior_usage: " << stage_access_name;
168 if (IsHazardVsRead(hazard.hazard)) {
169 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
170 out << ", read_barriers: " << string_VkPipelineStageFlags(barriers);
171 } else {
172 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
173 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
174 }
175
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700176 out << ", " << string_UsageTag(tag) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600177 return out.str();
178}
179
John Zulaufd14743a2020-07-03 09:42:39 -0600180// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
181// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
182// also reflects this special case for read hazard detection (using access instead of exec scope)
John Zulaufb027cdb2020-05-21 14:25:22 -0600183static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700184static const SyncStageAccessFlags kColorAttachmentAccessScope =
185 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
186 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
187 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
188 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600189static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
190 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700191static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
192 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
193 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
194 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600195
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700196static const SyncOrderingBarrier kColorAttachmentRasterOrder = {kColorAttachmentExecScope, kColorAttachmentAccessScope};
197static const SyncOrderingBarrier kDepthStencilAttachmentRasterOrder = {kDepthStencilAttachmentExecScope,
198 kDepthStencilAttachmentAccessScope};
199static const SyncOrderingBarrier kAttachmentRasterOrder = {kDepthStencilAttachmentExecScope | kColorAttachmentExecScope,
200 kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope};
John Zulauf7635de32020-05-29 17:14:15 -0600201// Sometimes we have an internal access conflict, and we using the kCurrentCommandTag to set and detect in temporary/proxy contexts
John Zulaufcc6fecb2020-06-17 15:24:54 -0600202static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600203
John Zulaufb02c1eb2020-10-06 16:33:36 -0600204static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) {
205 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
206}
207
208static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
209
locke-lunarg3c038002020-04-30 23:08:08 -0600210inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
211 if (size == VK_WHOLE_SIZE) {
212 return (whole_size - offset);
213 }
214 return size;
215}
216
John Zulauf3e86bf02020-09-12 10:47:57 -0600217static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
218 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
219}
220
John Zulauf16adfc92020-04-08 10:28:33 -0600221template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600222static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600223 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
224}
225
John Zulauf355e49b2020-04-24 15:11:15 -0600226static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600227
John Zulauf3e86bf02020-09-12 10:47:57 -0600228static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
229 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
230}
231
232static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
233 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
234}
235
John Zulauf4a6105a2020-11-17 15:11:05 -0700236// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
237//
John Zulauf10f1f522020-12-18 12:00:35 -0700238// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
239//
John Zulauf4a6105a2020-11-17 15:11:05 -0700240// Usage:
241// Constructor() -- initializes the generator to point to the begin of the space declared.
242// * -- the current range of the generator empty signfies end
243// ++ -- advance to the next non-empty range (or end)
244
245// A wrapper for a single range with the same semantics as the actual generators below
246template <typename KeyType>
247class SingleRangeGenerator {
248 public:
249 SingleRangeGenerator(const KeyType &range) : current_(range) {}
250 KeyType &operator*() const { return *current_; }
251 KeyType *operator->() const { return &*current_; }
252 SingleRangeGenerator &operator++() {
253 current_ = KeyType(); // just one real range
254 return *this;
255 }
256
257 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
258
259 private:
260 SingleRangeGenerator() = default;
261 const KeyType range_;
262 KeyType current_;
263};
264
265// Generate the ranges that are the intersection of range and the entries in the FilterMap
266template <typename FilterMap, typename KeyType = typename FilterMap::key_type>
267class FilteredRangeGenerator {
268 public:
269 FilteredRangeGenerator(const FilterMap &filter, const KeyType &range)
270 : range_(range), filter_(&filter), filter_pos_(), current_() {
271 SeekBegin();
272 }
273 const KeyType &operator*() const { return current_; }
274 const KeyType *operator->() const { return &current_; }
275 FilteredRangeGenerator &operator++() {
276 ++filter_pos_;
277 UpdateCurrent();
278 return *this;
279 }
280
281 bool operator==(const FilteredRangeGenerator &other) const { return current_ == other.current_; }
282
283 private:
284 FilteredRangeGenerator() = default;
285 void UpdateCurrent() {
286 if (filter_pos_ != filter_->cend()) {
287 current_ = range_ & filter_pos_->first;
288 } else {
289 current_ = KeyType();
290 }
291 }
292 void SeekBegin() {
293 filter_pos_ = filter_->lower_bound(range_);
294 UpdateCurrent();
295 }
296 const KeyType range_;
297 const FilterMap *filter_;
298 typename FilterMap::const_iterator filter_pos_;
299 KeyType current_;
300};
301using EventSimpleRangeGenerator = FilteredRangeGenerator<SyncEventState::ScopeMap>;
302
303// Templated to allow for different Range generators or map sources...
304
305// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulauf4a6105a2020-11-17 15:11:05 -0700306template <typename FilterMap, typename RangeGen, typename KeyType = typename FilterMap::key_type>
307class FilteredGeneratorGenerator {
308 public:
309 FilteredGeneratorGenerator(const FilterMap &filter, RangeGen &gen) : filter_(&filter), gen_(&gen), filter_pos_(), current_() {
310 SeekBegin();
311 }
312 const KeyType &operator*() const { return current_; }
313 const KeyType *operator->() const { return &current_; }
314 FilteredGeneratorGenerator &operator++() {
315 KeyType gen_range = GenRange();
316 KeyType filter_range = FilterRange();
317 current_ = KeyType();
318 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
319 if (gen_range.end > filter_range.end) {
320 // if the generated range is beyond the filter_range, advance the filter range
321 filter_range = AdvanceFilter();
322 } else {
323 gen_range = AdvanceGen();
324 }
325 current_ = gen_range & filter_range;
326 }
327 return *this;
328 }
329
330 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
331
332 private:
333 KeyType AdvanceFilter() {
334 ++filter_pos_;
335 auto filter_range = FilterRange();
336 if (filter_range.valid()) {
337 FastForwardGen(filter_range);
338 }
339 return filter_range;
340 }
341 KeyType AdvanceGen() {
342 ++(*gen_);
343 auto gen_range = GenRange();
344 if (gen_range.valid()) {
345 FastForwardFilter(gen_range);
346 }
347 return gen_range;
348 }
349
350 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
351 KeyType GenRange() const { return *(*gen_); }
352
353 KeyType FastForwardFilter(const KeyType &range) {
354 auto filter_range = FilterRange();
355 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700356 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700357 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
358 if (retry_count < kRetryLimit) {
359 ++filter_pos_;
360 filter_range = FilterRange();
361 retry_count++;
362 } else {
363 // Okay we've tried walking, do a seek.
364 filter_pos_ = filter_->lower_bound(range);
365 break;
366 }
367 }
368 return FilterRange();
369 }
370
371 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
372 // faster.
373 KeyType FastForwardGen(const KeyType &range) {
374 auto gen_range = GenRange();
375 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
376 ++(*gen_);
377 gen_range = GenRange();
378 }
379 return gen_range;
380 }
381
382 void SeekBegin() {
383 auto gen_range = GenRange();
384 if (gen_range.empty()) {
385 current_ = KeyType();
386 filter_pos_ = filter_->cend();
387 } else {
388 filter_pos_ = filter_->lower_bound(gen_range);
389 current_ = gen_range & FilterRange();
390 }
391 }
392
393 FilteredGeneratorGenerator() = default;
394 const FilterMap *filter_;
395 RangeGen *const gen_;
396 typename FilterMap::const_iterator filter_pos_;
397 KeyType current_;
398};
399
400using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
401
John Zulauf0cb5be22020-01-23 12:18:22 -0700402// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
403VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
404 VkPipelineStageFlags expanded = stage_mask;
405 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
406 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
407 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
408 if (all_commands.first & queue_flags) {
409 expanded |= all_commands.second;
410 }
411 }
412 }
413 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
414 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
415 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
416 }
417 return expanded;
418}
419
John Zulauf36bcf6a2020-02-03 15:12:52 -0700420VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
Jeremy Gebben91c36902020-11-09 08:17:08 -0700421 const std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
John Zulauf36bcf6a2020-02-03 15:12:52 -0700422 VkPipelineStageFlags unscanned = stage_mask;
423 VkPipelineStageFlags related = 0;
Jonah Ryan-Davis185189c2020-07-14 10:28:52 -0400424 for (const auto &entry : map) {
425 const auto &stage = entry.first;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700426 if (stage & unscanned) {
427 related = related | entry.second;
428 unscanned = unscanned & ~stage;
429 if (!unscanned) break;
430 }
431 }
432 return related;
433}
434
435VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
436 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
437}
438
439VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
440 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
441}
442
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700443static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700444
John Zulauf3e86bf02020-09-12 10:47:57 -0600445ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
446 VkDeviceSize stride) {
447 VkDeviceSize range_start = offset + first_index * stride;
448 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600449 if (count == UINT32_MAX) {
450 range_size = buf_whole_size - range_start;
451 } else {
452 range_size = count * stride;
453 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600454 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600455}
456
locke-lunarg654e3692020-06-04 17:19:15 -0600457SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
458 VkShaderStageFlagBits stage_flag) {
459 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
460 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
461 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
462 }
463 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
464 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
465 assert(0);
466 }
467 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
468 return stage_access->second.uniform_read;
469 }
470
471 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
472 // Because if write hazard happens, read hazard might or might not happen.
473 // But if write hazard doesn't happen, read hazard is impossible to happen.
474 if (descriptor_data.is_writable) {
475 return stage_access->second.shader_write;
476 }
477 return stage_access->second.shader_read;
478}
479
locke-lunarg37047832020-06-12 13:44:45 -0600480bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
481 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
482 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
483 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
484 ? true
485 : false;
486}
487
488bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
489 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
490 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
491 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
492 ? true
493 : false;
494}
495
John Zulauf355e49b2020-04-24 15:11:15 -0600496// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600497template <typename Action>
498static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
499 Action &action) {
500 // At this point the "apply over range" logic only supports a single memory binding
501 if (!SimpleBinding(image_state)) return;
502 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600503 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700504 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
505 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600506 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700507 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600508 }
509}
510
John Zulauf7635de32020-05-29 17:14:15 -0600511// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
512// Used by both validation and record operations
513//
514// The signature for Action() reflect the needs of both uses.
515template <typename Action>
516void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
517 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
518 VkExtent3D extent = CastTo3D(render_area.extent);
519 VkOffset3D offset = CastTo3D(render_area.offset);
520 const auto &rp_ci = rp_state.createInfo;
521 const auto *attachment_ci = rp_ci.pAttachments;
522 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
523
524 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
525 const auto *color_attachments = subpass_ci.pColorAttachments;
526 const auto *color_resolve = subpass_ci.pResolveAttachments;
527 if (color_resolve && color_attachments) {
528 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
529 const auto &color_attach = color_attachments[i].attachment;
530 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
531 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
532 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
533 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
534 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
535 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
536 }
537 }
538 }
539
540 // Depth stencil resolve only if the extension is present
541 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
542 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
543 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
544 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
545 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
546 const auto src_ci = attachment_ci[src_at];
547 // The formats are required to match so we can pick either
548 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
549 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
550 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
551 VkImageAspectFlags aspect_mask = 0u;
552
553 // Figure out which aspects are actually touched during resolve operations
554 const char *aspect_string = nullptr;
555 if (resolve_depth && resolve_stencil) {
556 // Validate all aspects together
557 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
558 aspect_string = "depth/stencil";
559 } else if (resolve_depth) {
560 // Validate depth only
561 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
562 aspect_string = "depth";
563 } else if (resolve_stencil) {
564 // Validate all stencil only
565 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
566 aspect_string = "stencil";
567 }
568
569 if (aspect_mask) {
570 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
Jeremy Gebbenec5cd382020-11-16 15:53:45 -0700571 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kAttachmentRasterOrder, offset, extent,
John Zulauf7635de32020-05-29 17:14:15 -0600572 aspect_mask);
573 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
574 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
575 }
576 }
577}
578
579// Action for validating resolve operations
580class ValidateResolveAction {
581 public:
582 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
583 const char *func_name)
584 : render_pass_(render_pass),
585 subpass_(subpass),
586 context_(context),
587 sync_state_(sync_state),
588 func_name_(func_name),
589 skip_(false) {}
590 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
591 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
592 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
593 HazardResult hazard;
594 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
595 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600596 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
597 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -0600598 " to resolve attachment %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -0600599 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
John Zulauf37ceaed2020-07-03 16:18:15 -0600600 src_at, dst_at, string_UsageTag(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600601 }
602 }
603 // Providing a mechanism for the constructing caller to get the result of the validation
604 bool GetSkip() const { return skip_; }
605
606 private:
607 VkRenderPass render_pass_;
608 const uint32_t subpass_;
609 const AccessContext &context_;
610 const SyncValidator &sync_state_;
611 const char *func_name_;
612 bool skip_;
613};
614
615// Update action for resolve operations
616class UpdateStateResolveAction {
617 public:
618 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
619 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
620 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
621 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
622 // Ignores validation only arguments...
623 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
624 }
625
626 private:
627 AccessContext &context_;
628 const ResourceUsageTag &tag_;
629};
630
John Zulauf59e25072020-07-17 10:55:21 -0600631void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700632 const SyncStageAccessFlags &prior_, const ResourceUsageTag &tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600633 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
634 usage_index = usage_index_;
635 hazard = hazard_;
636 prior_access = prior_;
637 tag = tag_;
638}
639
John Zulauf540266b2020-04-06 18:54:53 -0600640AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
641 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600642 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600643 Reset();
644 const auto &subpass_dep = dependencies[subpass];
645 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600646 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600647 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600648 const auto prev_pass = prev_dep.first->pass;
649 const auto &prev_barriers = prev_dep.second;
650 assert(prev_dep.second.size());
651 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
652 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700653 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600654
655 async_.reserve(subpass_dep.async.size());
656 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700657 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600658 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600659 if (subpass_dep.barrier_from_external.size()) {
660 src_external_ = TrackBack(external_context, queue_flags, subpass_dep.barrier_from_external);
John Zulaufe5da6e52020-03-18 15:32:18 -0600661 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600662 if (subpass_dep.barrier_to_external.size()) {
663 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600664 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700665}
666
John Zulauf5f13a792020-03-10 07:31:21 -0600667template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700668HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600669 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600670 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600671 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600672
673 HazardResult hazard;
674 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
675 hazard = detector.Detect(prev);
676 }
677 return hazard;
678}
679
John Zulauf4a6105a2020-11-17 15:11:05 -0700680template <typename Action>
681void AccessContext::ForAll(Action &&action) {
682 for (const auto address_type : kAddressTypes) {
683 auto &accesses = GetAccessStateMap(address_type);
684 for (const auto &access : accesses) {
685 action(address_type, access);
686 }
687 }
688}
689
John Zulauf3d84f1b2020-03-09 13:33:25 -0600690// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
691// the DAG of the contexts (for example subpasses)
692template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700693HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600694 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600695 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600696
John Zulauf1a224292020-06-30 14:52:13 -0600697 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600698 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
699 // so we'll check these first
700 for (const auto &async_context : async_) {
701 hazard = async_context->DetectAsyncHazard(type, detector, range);
702 if (hazard.hazard) return hazard;
703 }
John Zulauf5f13a792020-03-10 07:31:21 -0600704 }
705
John Zulauf1a224292020-06-30 14:52:13 -0600706 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600707
John Zulauf69133422020-05-20 14:55:53 -0600708 const auto &accesses = GetAccessStateMap(type);
709 const auto from = accesses.lower_bound(range);
710 const auto to = accesses.upper_bound(range);
711 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600712
John Zulauf69133422020-05-20 14:55:53 -0600713 for (auto pos = from; pos != to; ++pos) {
714 // Cover any leading gap, or gap between entries
715 if (detect_prev) {
716 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
717 // Cover any leading gap, or gap between entries
718 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600719 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600720 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600721 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600722 if (hazard.hazard) return hazard;
723 }
John Zulauf69133422020-05-20 14:55:53 -0600724 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
725 gap.begin = pos->first.end;
726 }
727
728 hazard = detector.Detect(pos);
729 if (hazard.hazard) return hazard;
730 }
731
732 if (detect_prev) {
733 // Detect in the trailing empty as needed
734 gap.end = range.end;
735 if (gap.non_empty()) {
736 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600737 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600738 }
739
740 return hazard;
741}
742
743// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
744template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700745HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
746 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600747 auto &accesses = GetAccessStateMap(type);
748 const auto from = accesses.lower_bound(range);
749 const auto to = accesses.upper_bound(range);
750
John Zulauf3d84f1b2020-03-09 13:33:25 -0600751 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600752 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700753 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600754 }
John Zulauf16adfc92020-04-08 10:28:33 -0600755
John Zulauf3d84f1b2020-03-09 13:33:25 -0600756 return hazard;
757}
758
John Zulaufb02c1eb2020-10-06 16:33:36 -0600759struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700760 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600761 void operator()(ResourceAccessState *access) const {
762 assert(access);
763 access->ApplyBarriers(barriers, true);
764 }
765 const std::vector<SyncBarrier> &barriers;
766};
767
768struct ApplyTrackbackBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700769 explicit ApplyTrackbackBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600770 void operator()(ResourceAccessState *access) const {
771 assert(access);
772 assert(!access->HasPendingState());
773 access->ApplyBarriers(barriers, false);
774 access->ApplyPendingBarriers(kCurrentCommandTag);
775 }
776 const std::vector<SyncBarrier> &barriers;
777};
778
779// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
780// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
781// *different* map from dest.
782// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
783// range [first, last)
784template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600785static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
786 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600787 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600788 auto at = entry;
789 for (auto pos = first; pos != last; ++pos) {
790 // Every member of the input iterator range must fit within the remaining portion of entry
791 assert(at->first.includes(pos->first));
792 assert(at != dest->end());
793 // Trim up at to the same size as the entry to resolve
794 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600795 auto access = pos->second; // intentional copy
796 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600797 at->second.Resolve(access);
798 ++at; // Go to the remaining unused section of entry
799 }
800}
801
John Zulaufa0a98292020-09-18 09:30:10 -0600802static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
803 SyncBarrier merged = {};
804 for (const auto &barrier : barriers) {
805 merged.Merge(barrier);
806 }
807 return merged;
808}
809
John Zulaufb02c1eb2020-10-06 16:33:36 -0600810template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700811void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600812 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
813 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600814 if (!range.non_empty()) return;
815
John Zulauf355e49b2020-04-24 15:11:15 -0600816 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
817 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600818 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600819 if (current->pos_B->valid) {
820 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600821 auto access = src_pos->second; // intentional copy
822 barrier_action(&access);
823
John Zulauf16adfc92020-04-08 10:28:33 -0600824 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600825 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
826 trimmed->second.Resolve(access);
827 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600828 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600829 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600830 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600831 }
John Zulauf16adfc92020-04-08 10:28:33 -0600832 } else {
833 // we have to descend to fill this gap
834 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600835 if (current->pos_A->valid) {
836 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
837 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600838 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600839 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -0600840 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600841 // There isn't anything in dest in current)range, so we can accumulate directly into it.
842 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600843 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
844 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
845 barrier_action(&pos->second);
John Zulauf355e49b2020-04-24 15:11:15 -0600846 }
847 }
848 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
849 // iterator of the outer while.
850
851 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
852 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
853 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600854 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
855 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600856 current.seek(seek_to);
857 } else if (!current->pos_A->valid && infill_state) {
858 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
859 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
860 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600861 }
John Zulauf5f13a792020-03-10 07:31:21 -0600862 }
John Zulauf16adfc92020-04-08 10:28:33 -0600863 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600864 }
John Zulauf1a224292020-06-30 14:52:13 -0600865
866 // Infill if range goes passed both the current and resolve map prior contents
867 if (recur_to_infill && (current->range.end < range.end)) {
868 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
869 ResourceAccessRangeMap gap_map;
870 const auto the_end = resolve_map->end();
871 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
872 for (auto &access : gap_map) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600873 barrier_action(&access.second);
John Zulauf1a224292020-06-30 14:52:13 -0600874 resolve_map->insert(the_end, access);
875 }
876 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600877}
878
John Zulauf43cc7462020-12-03 12:33:12 -0700879void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
880 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600881 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600882 if (range.non_empty() && infill_state) {
883 descent_map->insert(std::make_pair(range, *infill_state));
884 }
885 } else {
886 // Look for something to fill the gap further along.
887 for (const auto &prev_dep : prev_) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600888 const ApplyTrackbackBarriersAction barrier_action(prev_dep.barriers);
889 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600890 }
891
John Zulaufe5da6e52020-03-18 15:32:18 -0600892 if (src_external_.context) {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600893 const ApplyTrackbackBarriersAction barrier_action(src_external_.barriers);
894 src_external_.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600895 }
896 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600897}
898
John Zulauf4a6105a2020-11-17 15:11:05 -0700899// Non-lazy import of all accesses, WaitEvents needs this.
900void AccessContext::ResolvePreviousAccesses() {
901 ResourceAccessState default_state;
902 for (const auto address_type : kAddressTypes) {
903 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
904 }
905}
906
John Zulauf43cc7462020-12-03 12:33:12 -0700907AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
908 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -0600909}
910
John Zulauf1507ee42020-05-18 11:33:09 -0600911static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
912 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
913 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
914 return stage_access;
915}
916static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
917 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
918 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
919 return stage_access;
920}
921
John Zulauf7635de32020-05-29 17:14:15 -0600922// Caller must manage returned pointer
923static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
924 uint32_t subpass, const VkRect2D &render_area,
925 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
926 auto *proxy = new AccessContext(context);
927 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600928 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600929 return proxy;
930}
931
John Zulaufb02c1eb2020-10-06 16:33:36 -0600932template <typename BarrierAction>
John Zulauf52446eb2020-10-22 16:40:08 -0600933class ResolveAccessRangeFunctor {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600934 public:
John Zulauf43cc7462020-12-03 12:33:12 -0700935 ResolveAccessRangeFunctor(const AccessContext &context, AccessAddressType address_type, ResourceAccessRangeMap *descent_map,
936 const ResourceAccessState *infill_state, BarrierAction &barrier_action)
John Zulauf52446eb2020-10-22 16:40:08 -0600937 : context_(context),
938 address_type_(address_type),
939 descent_map_(descent_map),
940 infill_state_(infill_state),
941 barrier_action_(barrier_action) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600942 ResolveAccessRangeFunctor() = delete;
943 void operator()(const ResourceAccessRange &range) const {
944 context_.ResolveAccessRange(address_type_, range, barrier_action_, descent_map_, infill_state_);
945 }
946
947 private:
John Zulauf52446eb2020-10-22 16:40:08 -0600948 const AccessContext &context_;
John Zulauf43cc7462020-12-03 12:33:12 -0700949 const AccessAddressType address_type_;
John Zulauf52446eb2020-10-22 16:40:08 -0600950 ResourceAccessRangeMap *const descent_map_;
951 const ResourceAccessState *infill_state_;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600952 BarrierAction &barrier_action_;
953};
954
John Zulaufb02c1eb2020-10-06 16:33:36 -0600955template <typename BarrierAction>
956void AccessContext::ResolveAccessRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -0700957 BarrierAction &barrier_action, AccessAddressType address_type,
958 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const {
John Zulaufb02c1eb2020-10-06 16:33:36 -0600959 const ResolveAccessRangeFunctor<BarrierAction> action(*this, address_type, descent_map, infill_state, barrier_action);
960 ApplyOverImageRange(image_state, subresource_range, action);
John Zulauf62f10592020-04-03 12:20:02 -0600961}
962
John Zulauf7635de32020-05-29 17:14:15 -0600963// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600964bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600965 const VkRect2D &render_area, uint32_t subpass,
966 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
967 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600968 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600969 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
970 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
971 // those affects have not been recorded yet.
972 //
973 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
974 // to apply and only copy then, if this proves a hot spot.
975 std::unique_ptr<AccessContext> proxy_for_prev;
976 TrackBack proxy_track_back;
977
John Zulauf355e49b2020-04-24 15:11:15 -0600978 const auto &transitions = rp_state.subpass_transitions[subpass];
979 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600980 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
981
982 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
983 if (prev_needs_proxy) {
984 if (!proxy_for_prev) {
985 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
986 render_area, attachment_views));
987 proxy_track_back = *track_back;
988 proxy_track_back.context = proxy_for_prev.get();
989 }
990 track_back = &proxy_track_back;
991 }
992 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600993 if (hazard.hazard) {
John Zulauf389c34b2020-07-28 11:19:35 -0600994 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
995 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
996 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
997 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
998 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
999 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001000 }
1001 }
1002 return skip;
1003}
1004
John Zulauf1507ee42020-05-18 11:33:09 -06001005bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001006 const VkRect2D &render_area, uint32_t subpass,
1007 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1008 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001009 bool skip = false;
1010 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1011 VkExtent3D extent = CastTo3D(render_area.extent);
1012 VkOffset3D offset = CastTo3D(render_area.offset);
John Zulaufa0a98292020-09-18 09:30:10 -06001013
John Zulauf1507ee42020-05-18 11:33:09 -06001014 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1015 if (subpass == rp_state.attachment_first_subpass[i]) {
1016 if (attachment_views[i] == nullptr) continue;
1017 const IMAGE_VIEW_STATE &view = *attachment_views[i];
1018 const IMAGE_STATE *image = view.image_state.get();
1019 if (image == nullptr) continue;
1020 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001021
1022 // Need check in the following way
1023 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1024 // vs. transition
1025 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1026 // for each aspect loaded.
1027
1028 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001029 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001030 const bool is_color = !(has_depth || has_stencil);
1031
1032 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001033 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001034
John Zulaufaff20662020-06-01 14:07:58 -06001035 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001036 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001037
John Zulaufb02c1eb2020-10-06 16:33:36 -06001038 auto hazard_range = view.normalized_subresource_range;
1039 bool checked_stencil = false;
1040 if (is_color) {
John Zulauf859089b2020-10-29 17:37:03 -06001041 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, kColorAttachmentRasterOrder, offset,
1042 extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001043 aspect = "color";
1044 } else {
1045 if (has_depth) {
1046 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
John Zulauf859089b2020-10-29 17:37:03 -06001047 hazard = DetectHazard(*image, load_index, hazard_range, kDepthStencilAttachmentRasterOrder, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001048 aspect = "depth";
1049 }
1050 if (!hazard.hazard && has_stencil) {
1051 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
John Zulauf859089b2020-10-29 17:37:03 -06001052 hazard =
1053 DetectHazard(*image, stencil_load_index, hazard_range, kDepthStencilAttachmentRasterOrder, offset, extent);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001054 aspect = "stencil";
1055 checked_stencil = true;
1056 }
1057 }
1058
1059 if (hazard.hazard) {
1060 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
1061 if (hazard.tag == kCurrentCommandTag) {
1062 // Hazard vs. ILT
1063 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1064 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1065 " aspect %s during load with loadOp %s.",
1066 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1067 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06001068 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1069 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001070 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001071 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -06001072 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001073 }
1074 }
1075 }
1076 }
1077 return skip;
1078}
1079
John Zulaufaff20662020-06-01 14:07:58 -06001080// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1081// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1082// store is part of the same Next/End operation.
1083// The latter is handled in layout transistion validation directly
1084bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
1085 const VkRect2D &render_area, uint32_t subpass,
1086 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1087 const char *func_name) const {
1088 bool skip = false;
1089 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1090 VkExtent3D extent = CastTo3D(render_area.extent);
1091 VkOffset3D offset = CastTo3D(render_area.offset);
1092
1093 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1094 if (subpass == rp_state.attachment_last_subpass[i]) {
1095 if (attachment_views[i] == nullptr) continue;
1096 const IMAGE_VIEW_STATE &view = *attachment_views[i];
1097 const IMAGE_STATE *image = view.image_state.get();
1098 if (image == nullptr) continue;
1099 const auto &ci = attachment_ci[i];
1100
1101 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1102 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1103 // sake, we treat DONT_CARE as writing.
1104 const bool has_depth = FormatHasDepth(ci.format);
1105 const bool has_stencil = FormatHasStencil(ci.format);
1106 const bool is_color = !(has_depth || has_stencil);
1107 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1108 if (!has_stencil && !store_op_stores) continue;
1109
1110 HazardResult hazard;
1111 const char *aspect = nullptr;
1112 bool checked_stencil = false;
1113 if (is_color) {
1114 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1115 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
1116 aspect = "color";
1117 } else {
1118 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1119 auto hazard_range = view.normalized_subresource_range;
1120 if (has_depth && store_op_stores) {
1121 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1122 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
1123 kAttachmentRasterOrder, offset, extent);
1124 aspect = "depth";
1125 }
1126 if (!hazard.hazard && has_stencil && stencil_op_stores) {
1127 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1128 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
1129 kAttachmentRasterOrder, offset, extent);
1130 aspect = "stencil";
1131 checked_stencil = true;
1132 }
1133 }
1134
1135 if (hazard.hazard) {
1136 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1137 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -06001138 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
1139 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001140 " %s aspect during store with %s %s. Access info %s",
John Zulauf1dae9192020-06-16 15:46:44 -06001141 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -06001142 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001143 }
1144 }
1145 }
1146 return skip;
1147}
1148
John Zulaufb027cdb2020-05-21 14:25:22 -06001149bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
1150 const VkRect2D &render_area,
1151 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
1152 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -06001153 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
1154 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
1155 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001156}
1157
John Zulauf3d84f1b2020-03-09 13:33:25 -06001158class HazardDetector {
1159 SyncStageAccessIndex usage_index_;
1160
1161 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001162 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001163 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1164 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001165 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001166 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001167};
1168
John Zulauf69133422020-05-20 14:55:53 -06001169class HazardDetectorWithOrdering {
1170 const SyncStageAccessIndex usage_index_;
1171 const SyncOrderingBarrier &ordering_;
1172
1173 public:
1174 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1175 return pos->second.DetectHazard(usage_index_, ordering_);
1176 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001177 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1178 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001179 }
1180 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
1181 : usage_index_(usage), ordering_(ordering) {}
1182};
1183
John Zulauf16adfc92020-04-08 10:28:33 -06001184HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001185 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001186 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001187 const auto base_address = ResourceBaseAddress(buffer);
1188 HazardDetector detector(usage_index);
1189 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001190}
1191
John Zulauf69133422020-05-20 14:55:53 -06001192template <typename Detector>
1193HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1194 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1195 const VkExtent3D &extent, DetectOptions options) const {
1196 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001197 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001198 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1199 base_address);
1200 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001201 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001202 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001203 if (hazard.hazard) return hazard;
1204 }
1205 return HazardResult();
1206}
1207
John Zulauf540266b2020-04-06 18:54:53 -06001208HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1209 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1210 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001211 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1212 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -06001213 return DetectHazard(image, current_usage, subresource_range, offset, extent);
1214}
1215
1216HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1217 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1218 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -06001219 HazardDetector detector(current_usage);
1220 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
1221}
1222
1223HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1224 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
1225 const VkOffset3D &offset, const VkExtent3D &extent) const {
1226 HazardDetectorWithOrdering detector(current_usage, ordering);
1227 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001228}
1229
John Zulaufb027cdb2020-05-21 14:25:22 -06001230// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
1231// should have reported the issue regarding an invalid attachment entry
1232HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
1233 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
1234 VkImageAspectFlags aspect_mask) const {
1235 if (view != nullptr) {
1236 const IMAGE_STATE *image = view->image_state.get();
1237 if (image != nullptr) {
1238 auto *detect_range = &view->normalized_subresource_range;
1239 VkImageSubresourceRange masked_range;
1240 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1241 masked_range = view->normalized_subresource_range;
1242 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1243 detect_range = &masked_range;
1244 }
1245
1246 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
1247 if (detect_range->aspectMask) {
1248 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
1249 }
1250 }
1251 }
1252 return HazardResult();
1253}
John Zulauf43cc7462020-12-03 12:33:12 -07001254
John Zulauf3d84f1b2020-03-09 13:33:25 -06001255class BarrierHazardDetector {
1256 public:
1257 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1258 SyncStageAccessFlags src_access_scope)
1259 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1260
John Zulauf5f13a792020-03-10 07:31:21 -06001261 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1262 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001263 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001264 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001265 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001266 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001267 }
1268
1269 private:
1270 SyncStageAccessIndex usage_index_;
1271 VkPipelineStageFlags src_exec_scope_;
1272 SyncStageAccessFlags src_access_scope_;
1273};
1274
John Zulauf4a6105a2020-11-17 15:11:05 -07001275class EventBarrierHazardDetector {
1276 public:
1277 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1278 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
1279 const ResourceUsageTag &scope_tag)
1280 : usage_index_(usage_index),
1281 src_exec_scope_(src_exec_scope),
1282 src_access_scope_(src_access_scope),
1283 event_scope_(event_scope),
1284 scope_pos_(event_scope.cbegin()),
1285 scope_end_(event_scope.cend()),
1286 scope_tag_(scope_tag) {}
1287
1288 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1289 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1290 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1291 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1292 if (scope_pos_ == scope_end_) return HazardResult();
1293 if (!scope_pos_->first.intersects(pos->first)) {
1294 event_scope_.lower_bound(pos->first);
1295 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1296 }
1297
1298 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1299 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1300 }
1301 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1302 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1303 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1304 }
1305
1306 private:
1307 SyncStageAccessIndex usage_index_;
1308 VkPipelineStageFlags src_exec_scope_;
1309 SyncStageAccessFlags src_access_scope_;
1310 const SyncEventState::ScopeMap &event_scope_;
1311 SyncEventState::ScopeMap::const_iterator scope_pos_;
1312 SyncEventState::ScopeMap::const_iterator scope_end_;
1313 const ResourceUsageTag &scope_tag_;
1314};
1315
1316HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1317 const SyncStageAccessFlags &src_access_scope,
1318 const VkImageSubresourceRange &subresource_range,
1319 const SyncEventState &sync_event, DetectOptions options) const {
1320 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1321 // first access scope map to use, and there's no easy way to plumb it in below.
1322 const auto address_type = ImageAddressType(image);
1323 const auto &event_scope = sync_event.FirstScope(address_type);
1324
1325 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1326 event_scope, sync_event.first_scope_tag);
1327 VkOffset3D zero_offset = {0, 0, 0};
1328 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
1329}
1330
John Zulauf16adfc92020-04-08 10:28:33 -06001331HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001332 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001333 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001334 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001335 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
1336 VkOffset3D zero_offset = {0, 0, 0};
1337 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001338}
1339
John Zulauf355e49b2020-04-24 15:11:15 -06001340HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001341 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001342 const VkImageMemoryBarrier &barrier) const {
1343 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1344 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1345 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1346}
1347
John Zulauf9cb530d2019-09-30 14:14:10 -06001348template <typename Flags, typename Map>
1349SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1350 SyncStageAccessFlags scope = 0;
1351 for (const auto &bit_scope : map) {
1352 if (flag_mask < bit_scope.first) break;
1353
1354 if (flag_mask & bit_scope.first) {
1355 scope |= bit_scope.second;
1356 }
1357 }
1358 return scope;
1359}
1360
1361SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1362 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1363}
1364
1365SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1366 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1367}
1368
1369// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1370SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001371 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1372 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1373 // of the union of all stage/access types for all the stages and the same unions for the access mask...
John Zulauf9cb530d2019-09-30 14:14:10 -06001374 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1375}
1376
1377template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001378void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001379 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1380 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001381 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001382 auto pos = accesses->lower_bound(range);
1383 if (pos == accesses->end() || !pos->first.intersects(range)) {
1384 // The range is empty, fill it with a default value.
1385 pos = action.Infill(accesses, pos, range);
1386 } else if (range.begin < pos->first.begin) {
1387 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001388 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001389 } else if (pos->first.begin < range.begin) {
1390 // Trim the beginning if needed
1391 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1392 ++pos;
1393 }
1394
1395 const auto the_end = accesses->end();
1396 while ((pos != the_end) && pos->first.intersects(range)) {
1397 if (pos->first.end > range.end) {
1398 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1399 }
1400
1401 pos = action(accesses, pos);
1402 if (pos == the_end) break;
1403
1404 auto next = pos;
1405 ++next;
1406 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1407 // Need to infill if next is disjoint
1408 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001409 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001410 next = action.Infill(accesses, next, new_range);
1411 }
1412 pos = next;
1413 }
1414}
John Zulauf4a6105a2020-11-17 15:11:05 -07001415template <typename Action, typename RangeGen>
1416void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1417 assert(range_gen_arg);
1418 auto &range_gen = *range_gen_arg; // Non-const references must be * by style requirement but deref-ing * iterator is a pain
1419 for (; range_gen->non_empty(); ++range_gen) {
1420 UpdateMemoryAccessState(accesses, *range_gen, action);
1421 }
1422}
John Zulauf9cb530d2019-09-30 14:14:10 -06001423
1424struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001425 using Iterator = ResourceAccessRangeMap::iterator;
1426 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001427 // this is only called on gaps, and never returns a gap.
1428 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001429 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001430 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001431 }
John Zulauf5f13a792020-03-10 07:31:21 -06001432
John Zulauf5c5e88d2019-12-26 11:22:02 -07001433 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001434 auto &access_state = pos->second;
1435 access_state.Update(usage, tag);
1436 return pos;
1437 }
1438
John Zulauf43cc7462020-12-03 12:33:12 -07001439 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001440 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001441 : type(type_), context(context_), usage(usage_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001442 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001443 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001444 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001445 const ResourceUsageTag &tag;
1446};
1447
John Zulauf4a6105a2020-11-17 15:11:05 -07001448// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001449struct PipelineBarrierOp {
1450 SyncBarrier barrier;
1451 bool layout_transition;
1452 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1453 : barrier(barrier_), layout_transition(layout_transition_) {}
1454 PipelineBarrierOp() = default;
1455 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1456};
John Zulauf4a6105a2020-11-17 15:11:05 -07001457// The barrier operation for wait events
1458struct WaitEventBarrierOp {
1459 const ResourceUsageTag *scope_tag;
1460 SyncBarrier barrier;
1461 bool layout_transition;
1462 WaitEventBarrierOp(const ResourceUsageTag &scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1463 : scope_tag(&scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
1464 WaitEventBarrierOp() = default;
1465 void operator()(ResourceAccessState *access_state) const {
1466 assert(scope_tag); // Not valid to have a non-scope op executed, default construct included for std::vector support
1467 access_state->ApplyBarrier(*scope_tag, barrier, layout_transition);
1468 }
1469};
John Zulauf1e331ec2020-12-04 18:29:38 -07001470
John Zulauf4a6105a2020-11-17 15:11:05 -07001471// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1472// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1473// of a collection is known/present.
John Zulauf1e331ec2020-12-04 18:29:38 -07001474template <typename BarrierOp>
John Zulauf89311b42020-09-29 16:28:47 -06001475class ApplyBarrierOpsFunctor {
1476 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001477 using Iterator = ResourceAccessRangeMap::iterator;
1478 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001479
John Zulauf5c5e88d2019-12-26 11:22:02 -07001480 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001481 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001482 for (const auto &op : barrier_ops_) {
1483 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001484 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001485
John Zulauf89311b42020-09-29 16:28:47 -06001486 if (resolve_) {
1487 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1488 // another walk
1489 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001490 }
1491 return pos;
1492 }
1493
John Zulauf89311b42020-09-29 16:28:47 -06001494 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulauf1e331ec2020-12-04 18:29:38 -07001495 ApplyBarrierOpsFunctor(bool resolve, const std::vector<BarrierOp> &barrier_ops, const ResourceUsageTag &tag)
1496 : resolve_(resolve), barrier_ops_(barrier_ops), tag_(tag) {}
John Zulauf89311b42020-09-29 16:28:47 -06001497
1498 private:
1499 bool resolve_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001500 const std::vector<BarrierOp> &barrier_ops_;
1501 const ResourceUsageTag &tag_;
1502};
1503
John Zulauf4a6105a2020-11-17 15:11:05 -07001504// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1505// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1506template <typename BarrierOp>
1507class ApplyBarrierFunctor {
1508 public:
1509 using Iterator = ResourceAccessRangeMap::iterator;
1510 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1511
1512 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1513 auto &access_state = pos->second;
1514 barrier_op_(&access_state);
1515 return pos;
1516 }
1517
1518 ApplyBarrierFunctor(const BarrierOp &barrier_op) : barrier_op_(barrier_op) {}
1519
1520 private:
1521 const BarrierOp barrier_op_;
1522};
1523
John Zulauf1e331ec2020-12-04 18:29:38 -07001524// This functor resolves the pendinging state.
1525class ResolvePendingBarrierFunctor {
1526 public:
1527 using Iterator = ResourceAccessRangeMap::iterator;
1528 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1529
1530 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1531 auto &access_state = pos->second;
1532 access_state.ApplyPendingBarriers(tag_);
1533 return pos;
1534 }
1535
1536 ResolvePendingBarrierFunctor(const ResourceUsageTag &tag) : tag_(tag) {}
1537
1538 private:
John Zulauf89311b42020-09-29 16:28:47 -06001539 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001540};
1541
John Zulauf43cc7462020-12-03 12:33:12 -07001542void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -06001543 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001544 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1545 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001546}
1547
John Zulauf16adfc92020-04-08 10:28:33 -06001548void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001549 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001550 if (!SimpleBinding(buffer)) return;
1551 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf43cc7462020-12-03 12:33:12 -07001552 UpdateAccessState(AccessAddressType::kLinear, current_usage, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001553}
John Zulauf355e49b2020-04-24 15:11:15 -06001554
John Zulauf540266b2020-04-06 18:54:53 -06001555void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001556 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001557 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001558 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001559 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001560 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1561 base_address);
1562 const auto address_type = ImageAddressType(image);
John Zulauf16adfc92020-04-08 10:28:33 -06001563 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001564 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001565 UpdateMemoryAccessState(&GetAccessStateMap(address_type), *range_gen, action);
John Zulauf5f13a792020-03-10 07:31:21 -06001566 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001567}
John Zulauf7635de32020-05-29 17:14:15 -06001568void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1569 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1570 if (view != nullptr) {
1571 const IMAGE_STATE *image = view->image_state.get();
1572 if (image != nullptr) {
1573 auto *update_range = &view->normalized_subresource_range;
1574 VkImageSubresourceRange masked_range;
1575 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1576 masked_range = view->normalized_subresource_range;
1577 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1578 update_range = &masked_range;
1579 }
1580 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1581 }
1582 }
1583}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001584
John Zulauf355e49b2020-04-24 15:11:15 -06001585void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1586 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1587 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001588 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1589 subresource.layerCount};
1590 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1591}
1592
John Zulauf540266b2020-04-06 18:54:53 -06001593template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001594void AccessContext::UpdateResourceAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001595 if (!SimpleBinding(buffer)) return;
1596 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf43cc7462020-12-03 12:33:12 -07001597 UpdateMemoryAccessState(&GetAccessStateMap(AccessAddressType::kLinear), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001598}
1599
1600template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -06001601void AccessContext::UpdateResourceAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1602 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001603 if (!SimpleBinding(image)) return;
1604 const auto address_type = ImageAddressType(image);
1605 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001606
John Zulauf16adfc92020-04-08 10:28:33 -06001607 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001608 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
1609 image.createInfo.extent, base_address);
1610
John Zulauf540266b2020-04-06 18:54:53 -06001611 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001612 UpdateMemoryAccessState(accesses, *range_gen, action);
John Zulauf540266b2020-04-06 18:54:53 -06001613 }
1614}
1615
John Zulauf7635de32020-05-29 17:14:15 -06001616void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1617 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1618 const ResourceUsageTag &tag) {
1619 UpdateStateResolveAction update(*this, tag);
1620 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1621}
1622
John Zulaufaff20662020-06-01 14:07:58 -06001623void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1624 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1625 const ResourceUsageTag &tag) {
1626 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1627 VkExtent3D extent = CastTo3D(render_area.extent);
1628 VkOffset3D offset = CastTo3D(render_area.offset);
1629
1630 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1631 if (rp_state.attachment_last_subpass[i] == subpass) {
1632 if (attachment_views[i] == nullptr) continue; // UNUSED
1633 const auto &view = *attachment_views[i];
1634 const IMAGE_STATE *image = view.image_state.get();
1635 if (image == nullptr) continue;
1636
1637 const auto &ci = attachment_ci[i];
1638 const bool has_depth = FormatHasDepth(ci.format);
1639 const bool has_stencil = FormatHasStencil(ci.format);
1640 const bool is_color = !(has_depth || has_stencil);
1641 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1642
1643 if (is_color && store_op_stores) {
1644 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1645 offset, extent, tag);
1646 } else {
1647 auto update_range = view.normalized_subresource_range;
1648 if (has_depth && store_op_stores) {
1649 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1650 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1651 tag);
1652 }
1653 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1654 if (has_stencil && stencil_op_stores) {
1655 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1656 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1657 tag);
1658 }
1659 }
1660 }
1661 }
1662}
1663
John Zulauf540266b2020-04-06 18:54:53 -06001664template <typename Action>
1665void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1666 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001667 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001668 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001669 }
1670}
1671
1672void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001673 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1674 auto &context = contexts[subpass_index];
John Zulaufb02c1eb2020-10-06 16:33:36 -06001675 ApplyTrackbackBarriersAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001676 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001677 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001678 }
1679 }
1680}
1681
John Zulauf355e49b2020-04-24 15:11:15 -06001682// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001683HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001684 if (!attach_view) return HazardResult();
1685 const auto image_state = attach_view->image_state.get();
1686 if (!image_state) return HazardResult();
1687
John Zulauf355e49b2020-04-24 15:11:15 -06001688 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001689 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001690
1691 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001692 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1693 const auto merged_barrier = MergeBarriers(track_back.barriers);
1694 HazardResult hazard =
1695 track_back.context->DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
1696 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001697 if (!hazard.hazard) {
1698 // The Async hazard check is against the current context's async set.
John Zulaufa0a98292020-09-18 09:30:10 -06001699 hazard = DetectImageBarrierHazard(*image_state, merged_barrier.src_exec_scope, merged_barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001700 attach_view->normalized_subresource_range, kDetectAsync);
1701 }
John Zulaufa0a98292020-09-18 09:30:10 -06001702
John Zulauf355e49b2020-04-24 15:11:15 -06001703 return hazard;
1704}
1705
John Zulaufb02c1eb2020-10-06 16:33:36 -06001706void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
1707 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
1708 const ResourceUsageTag &tag) {
1709 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001710 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001711 for (const auto &transition : transitions) {
1712 const auto prev_pass = transition.prev_pass;
1713 const auto attachment_view = attachment_views[transition.attachment];
1714 if (!attachment_view) continue;
1715 const auto *image = attachment_view->image_state.get();
1716 if (!image) continue;
1717 if (!SimpleBinding(*image)) continue;
1718
1719 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1720 assert(trackback);
1721
1722 // Import the attachments into the current context
1723 const auto *prev_context = trackback->context;
1724 assert(prev_context);
1725 const auto address_type = ImageAddressType(*image);
1726 auto &target_map = GetAccessStateMap(address_type);
1727 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
1728 prev_context->ResolveAccessRange(*image, attachment_view->normalized_subresource_range, barrier_action, address_type,
John Zulauf646cc292020-10-23 09:16:45 -06001729 &target_map, &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001730 }
1731
John Zulauf86356ca2020-10-19 11:46:41 -06001732 // If there were no transitions skip this global map walk
1733 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001734 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulauf86356ca2020-10-19 11:46:41 -06001735 ApplyGlobalBarriers(apply_pending_action);
1736 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001737}
John Zulauf4a6105a2020-11-17 15:11:05 -07001738void CommandBufferAccessContext::ApplyBufferBarriers(const SyncEventState &sync_event, VkPipelineStageFlags dst_exec_scope,
1739 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
1740 const VkBufferMemoryBarrier *barriers) {
1741 const auto &scope_tag = sync_event.first_scope_tag;
1742 auto *access_context = GetCurrentAccessContext();
1743 const auto address_type = AccessAddressType::kLinear;
1744 for (uint32_t index = 0; index < barrier_count; index++) {
1745 auto barrier = barriers[index]; // barrier is a copy
1746 const auto *buffer = sync_state_->Get<BUFFER_STATE>(barrier.buffer);
1747 if (!buffer) continue;
1748 const auto base_address = ResourceBaseAddress(*buffer);
1749 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
1750 const ResourceAccessRange range = MakeRange(barrier) + base_address;
1751 const auto src_access_scope = SyncStageAccess::AccessScope(sync_event.stage_accesses, barrier.srcAccessMask);
1752 const auto dst_access_scope = SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1753 const SyncBarrier sync_barrier(sync_event.exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1754 const ApplyBarrierFunctor<WaitEventBarrierOp> barrier_action({scope_tag, sync_barrier, false /* layout_transition */});
1755 EventSimpleRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), range);
1756 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barrier_action, &filtered_range_gen);
1757 }
1758}
1759
1760void CommandBufferAccessContext::ApplyGlobalBarriers(SyncEventState &sync_event, VkPipelineStageFlags dstStageMask,
1761 VkPipelineStageFlags dst_exec_scope,
1762 const SyncStageAccessFlags &dst_stage_accesses, uint32_t memory_barrier_count,
1763 const VkMemoryBarrier *pMemoryBarriers, const ResourceUsageTag &tag) {
1764 std::vector<WaitEventBarrierOp> barrier_ops;
1765 barrier_ops.reserve(std::min<uint32_t>(memory_barrier_count, 1));
1766 const auto &scope_tag = sync_event.first_scope_tag;
1767 auto *access_context = GetCurrentAccessContext();
1768 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
1769 const auto &barrier = pMemoryBarriers[barrier_index];
1770 SyncBarrier sync_barrier(sync_event.exec_scope,
1771 SyncStageAccess::AccessScope(sync_event.stage_accesses, barrier.srcAccessMask), dst_exec_scope,
1772 SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
1773 barrier_ops.emplace_back(scope_tag, sync_barrier, false);
1774 }
1775 if (0 == memory_barrier_count) {
1776 // If there are no global memory barriers, force an exec barrier
1777 barrier_ops.emplace_back(scope_tag, SyncBarrier(sync_event.exec_scope, 0, dst_exec_scope, 0), false);
1778 }
1779 ApplyBarrierOpsFunctor<WaitEventBarrierOp> barriers_functor(false /* don't resolve */, barrier_ops, tag);
1780 for (const auto address_type : kAddressTypes) {
1781 EventSimpleRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), kFullRange);
1782 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &filtered_range_gen);
1783 }
1784
1785 // Apply the global barrier to the event itself (for race condition tracking)
1786 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
1787 sync_event.barriers = dstStageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1788 sync_event.barriers |= dst_exec_scope;
1789}
1790
1791void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags src_exec_scope,
1792 VkPipelineStageFlags dstStageMask,
1793 VkPipelineStageFlags dst_exec_scope) {
1794 const bool all_commands_bit = 0 != (srcStageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
1795 for (auto &event_pair : event_state_) {
1796 assert(event_pair.second); // Shouldn't be storing empty
1797 auto &sync_event = *event_pair.second;
1798 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
1799 if ((sync_event.barriers & src_exec_scope) || all_commands_bit) {
1800 sync_event.barriers |= dst_exec_scope;
1801 sync_event.barriers |= dstStageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
1802 }
1803 }
1804}
1805
1806void CommandBufferAccessContext::ApplyImageBarriers(const SyncEventState &sync_event, VkPipelineStageFlags dst_exec_scope,
1807 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
1808 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
1809 const auto &scope_tag = sync_event.first_scope_tag;
1810 auto *access_context = GetCurrentAccessContext();
1811 for (uint32_t index = 0; index < barrier_count; index++) {
1812 const auto &barrier = barriers[index];
1813 const auto *image = sync_state_->Get<IMAGE_STATE>(barrier.image);
1814 if (!image) continue;
1815 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
1816 bool layout_transition = barrier.oldLayout != barrier.newLayout;
1817 const auto src_access_scope = SyncStageAccess::AccessScope(sync_event.stage_accesses, barrier.srcAccessMask);
1818 const auto dst_access_scope = SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1819 const SyncBarrier sync_barrier(sync_event.exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1820 const ApplyBarrierFunctor<WaitEventBarrierOp> barrier_action({scope_tag, sync_barrier, layout_transition});
1821 const auto base_address = ResourceBaseAddress(*image);
1822 subresource_adapter::ImageRangeGenerator range_gen(*image->fragment_encoder.get(), subresource_range, {0, 0, 0},
1823 image->createInfo.extent, base_address);
1824 const auto address_type = AccessContext::ImageAddressType(*image);
1825 EventImageRangeGenerator filtered_range_gen(sync_event.FirstScope(address_type), range_gen);
1826 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barrier_action, &filtered_range_gen);
1827 }
1828}
John Zulaufb02c1eb2020-10-06 16:33:36 -06001829
John Zulauf355e49b2020-04-24 15:11:15 -06001830// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1831bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1832
1833 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001834 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001835 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1836 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06001837
John Zulauf86356ca2020-10-19 11:46:41 -06001838 assert(pRenderPassBegin);
1839 if (nullptr == pRenderPassBegin) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06001840
John Zulauf86356ca2020-10-19 11:46:41 -06001841 const uint32_t subpass = 0;
John Zulauf355e49b2020-04-24 15:11:15 -06001842
John Zulauf86356ca2020-10-19 11:46:41 -06001843 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
1844 // hasn't happened yet)
1845 const std::vector<AccessContext> empty_context_vector;
1846 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1847 const_cast<AccessContext *>(&cb_access_context_));
John Zulauf355e49b2020-04-24 15:11:15 -06001848
John Zulauf86356ca2020-10-19 11:46:41 -06001849 // Create a view list
1850 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1851 assert(fb_state);
1852 if (nullptr == fb_state) return skip;
1853 // NOTE: Must not use COMMAND_BUFFER_STATE variant of this as RecordCmdBeginRenderPass hasn't run and thus
1854 // the activeRenderPass.* fields haven't been set.
1855 const auto views = sync_state_->GetAttachmentViews(*pRenderPassBegin, *fb_state);
1856
1857 // Validate transitions
1858 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
1859
1860 // Validate load operations if there were no layout transition hazards
1861 if (!skip) {
1862 temp_context.RecordLayoutTransitions(rp_state, subpass, views, kCurrentCommandTag);
1863 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, subpass, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001864 }
John Zulauf86356ca2020-10-19 11:46:41 -06001865
John Zulauf355e49b2020-04-24 15:11:15 -06001866 return skip;
1867}
1868
locke-lunarg61870c22020-06-09 14:51:50 -06001869bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1870 const char *func_name) const {
1871 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001872 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001873 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001874 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
1875 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001876 return skip;
1877 }
1878
1879 using DescriptorClass = cvdescriptorset::DescriptorClass;
1880 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1881 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1882 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1883 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1884
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001885 for (const auto &stage_state : pipe->stage_state) {
1886 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
1887 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001888 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001889 }
locke-lunarg61870c22020-06-09 14:51:50 -06001890 for (const auto &set_binding : stage_state.descriptor_uses) {
1891 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1892 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1893 set_binding.first.second);
1894 const auto descriptor_type = binding_it.GetType();
1895 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1896 auto array_idx = 0;
1897
1898 if (binding_it.IsVariableDescriptorCount()) {
1899 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1900 }
1901 SyncStageAccessIndex sync_index =
1902 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1903
1904 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1905 uint32_t index = i - index_range.start;
1906 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1907 switch (descriptor->GetClass()) {
1908 case DescriptorClass::ImageSampler:
1909 case DescriptorClass::Image: {
1910 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001911 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001912 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001913 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1914 img_view_state = image_sampler_descriptor->GetImageViewState();
1915 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001916 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001917 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1918 img_view_state = image_descriptor->GetImageViewState();
1919 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001920 }
1921 if (!img_view_state) continue;
1922 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1923 VkExtent3D extent = {};
1924 VkOffset3D offset = {};
1925 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1926 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1927 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1928 } else {
1929 extent = img_state->createInfo.extent;
1930 }
John Zulauf361fb532020-07-22 10:45:39 -06001931 HazardResult hazard;
1932 const auto &subresource_range = img_view_state->normalized_subresource_range;
1933 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
1934 // Input attachments are subject to raster ordering rules
1935 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
1936 kAttachmentRasterOrder, offset, extent);
1937 } else {
1938 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range, offset, extent);
1939 }
John Zulauf33fc1d52020-07-17 11:01:10 -06001940 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001941 skip |= sync_state_->LogError(
1942 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001943 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1944 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001945 func_name, string_SyncHazard(hazard.hazard),
1946 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1947 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001948 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001949 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1950 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
1951 set_binding.first.second, index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001952 }
1953 break;
1954 }
1955 case DescriptorClass::TexelBuffer: {
1956 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1957 if (!buf_view_state) continue;
1958 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001959 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001960 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001961 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001962 skip |= sync_state_->LogError(
1963 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001964 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1965 func_name, string_SyncHazard(hazard.hazard),
locke-lunarg88dbb542020-06-23 22:05:42 -06001966 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1967 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001968 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001969 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1970 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1971 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001972 }
1973 break;
1974 }
1975 case DescriptorClass::GeneralBuffer: {
1976 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1977 auto buf_state = buffer_descriptor->GetBufferState();
1978 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001979 const ResourceAccessRange range =
1980 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001981 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001982 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001983 skip |= sync_state_->LogError(
1984 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001985 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1986 func_name, string_SyncHazard(hazard.hazard),
1987 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
locke-lunarg88dbb542020-06-23 22:05:42 -06001988 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001989 sync_state_->report_data->FormatHandle(pipe->pipeline).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001990 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1991 string_VkDescriptorType(descriptor_type), set_binding.first.second, index,
1992 string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001993 }
1994 break;
1995 }
1996 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1997 default:
1998 break;
1999 }
2000 }
2001 }
2002 }
2003 return skip;
2004}
2005
2006void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
2007 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002008 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06002009 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002010 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pipe, &per_sets);
2011 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06002012 return;
2013 }
2014
2015 using DescriptorClass = cvdescriptorset::DescriptorClass;
2016 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
2017 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
2018 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
2019 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
2020
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002021 for (const auto &stage_state : pipe->stage_state) {
2022 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->graphicsPipelineCI.pRasterizationState &&
2023 pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06002024 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002025 }
locke-lunarg61870c22020-06-09 14:51:50 -06002026 for (const auto &set_binding : stage_state.descriptor_uses) {
2027 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
2028 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
2029 set_binding.first.second);
2030 const auto descriptor_type = binding_it.GetType();
2031 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2032 auto array_idx = 0;
2033
2034 if (binding_it.IsVariableDescriptorCount()) {
2035 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2036 }
2037 SyncStageAccessIndex sync_index =
2038 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
2039
2040 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2041 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2042 switch (descriptor->GetClass()) {
2043 case DescriptorClass::ImageSampler:
2044 case DescriptorClass::Image: {
2045 const IMAGE_VIEW_STATE *img_view_state = nullptr;
2046 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
2047 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
2048 } else {
2049 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
2050 }
2051 if (!img_view_state) continue;
2052 const IMAGE_STATE *img_state = img_view_state->image_state.get();
2053 VkExtent3D extent = {};
2054 VkOffset3D offset = {};
2055 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
2056 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
2057 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
2058 } else {
2059 extent = img_state->createInfo.extent;
2060 }
2061 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
2062 offset, extent, tag);
2063 break;
2064 }
2065 case DescriptorClass::TexelBuffer: {
2066 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
2067 if (!buf_view_state) continue;
2068 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002069 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06002070 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
2071 break;
2072 }
2073 case DescriptorClass::GeneralBuffer: {
2074 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
2075 auto buf_state = buffer_descriptor->GetBufferState();
2076 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002077 const ResourceAccessRange range =
2078 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06002079 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
2080 break;
2081 }
2082 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2083 default:
2084 break;
2085 }
2086 }
2087 }
2088 }
2089}
2090
2091bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2092 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002093 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2094 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002095 return skip;
2096 }
2097
2098 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2099 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002100 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002101
2102 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002103 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002104 if (binding_description.binding < binding_buffers_size) {
2105 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002106 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002107
locke-lunarg1ae57d62020-11-18 10:49:19 -07002108 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002109 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2110 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06002111 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
2112 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002113 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002114 buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002115 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002116 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002117 }
2118 }
2119 }
2120 return skip;
2121}
2122
2123void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002124 const auto *pipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
2125 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002126 return;
2127 }
2128 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2129 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002130 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002131
2132 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002133 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002134 if (binding_description.binding < binding_buffers_size) {
2135 const auto &binding_buffer = binding_buffers[binding_description.binding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07002136 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->destroyed) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002137
locke-lunarg1ae57d62020-11-18 10:49:19 -07002138 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002139 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2140 vertexCount, binding_description.stride);
locke-lunarg61870c22020-06-09 14:51:50 -06002141 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
2142 }
2143 }
2144}
2145
2146bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2147 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002148 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002149 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002150 }
locke-lunarg61870c22020-06-09 14:51:50 -06002151
locke-lunarg1ae57d62020-11-18 10:49:19 -07002152 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002153 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002154 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2155 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06002156 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
2157 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002158 skip |= sync_state_->LogError(
John Zulauf59e25072020-07-17 10:55:21 -06002159 index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002160 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002161 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002162 }
2163
2164 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2165 // We will detect more accurate range in the future.
2166 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2167 return skip;
2168}
2169
2170void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002171 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->destroyed) return;
locke-lunarg61870c22020-06-09 14:51:50 -06002172
locke-lunarg1ae57d62020-11-18 10:49:19 -07002173 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002174 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002175 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2176 firstIndex, indexCount, index_size);
locke-lunarg61870c22020-06-09 14:51:50 -06002177 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
2178
2179 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2180 // We will detect more accurate range in the future.
2181 RecordDrawVertex(UINT32_MAX, 0, tag);
2182}
2183
2184bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002185 bool skip = false;
2186 if (!current_renderpass_context_) return skip;
2187 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
2188 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
2189 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002190}
2191
2192void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002193 if (current_renderpass_context_) {
locke-lunarg7077d502020-06-18 21:37:26 -06002194 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
2195 tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002196 }
locke-lunarg61870c22020-06-09 14:51:50 -06002197}
2198
John Zulauf355e49b2020-04-24 15:11:15 -06002199bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002200 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06002201 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06002202 skip |=
2203 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002204
2205 return skip;
2206}
2207
2208bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
2209 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06002210 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002211 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06002212 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06002213 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
2214 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002215
2216 return skip;
2217}
2218
2219void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
2220 assert(sync_state_);
2221 if (!cb_state_) return;
2222
2223 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06002224 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06002225 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06002226 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002227 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002228}
2229
John Zulauf355e49b2020-04-24 15:11:15 -06002230void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002231 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06002232 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002233 current_context_ = &current_renderpass_context_->CurrentContext();
2234}
2235
John Zulauf355e49b2020-04-24 15:11:15 -06002236void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002237 assert(current_renderpass_context_);
2238 if (!current_renderpass_context_) return;
2239
John Zulauf1a224292020-06-30 14:52:13 -06002240 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002241 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002242 current_renderpass_context_ = nullptr;
2243}
2244
John Zulauf49beb112020-11-04 16:06:31 -07002245bool CommandBufferAccessContext::ValidateSetEvent(VkCommandBuffer commandBuffer, VkEvent event,
2246 VkPipelineStageFlags stageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002247 // I'll put this here just in case we need to pass this in for future extension support
2248 const auto cmd = CMD_SETEVENT;
2249 bool skip = false;
2250 const auto *sync_event = GetEventState(event);
2251 if (!sync_event) return false; // Core, Lifetimes, or Param check needs to catch invalid events.
2252
2253 const char *const reset_set =
2254 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
2255 "hazards.";
2256 const char *const wait =
2257 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
2258
2259 const auto exec_scope = WithEarlierPipelineStages(ExpandPipelineStages(GetQueueFlags(), stageMask));
2260 if (!sync_event->HasBarrier(stageMask, exec_scope)) {
2261 const char *vuid = nullptr;
2262 const char *message = nullptr;
2263 switch (sync_event->last_command) {
2264 case CMD_RESETEVENT:
2265 // Needs a barrier between reset and set
2266 vuid = "SYNC-vkCmdSetEvent-missingbarrier-reset";
2267 message = reset_set;
2268 break;
2269 case CMD_SETEVENT:
2270 // Needs a barrier between set and set
2271 vuid = "SYNC-vkCmdSetEvent-missingbarrier-set";
2272 message = reset_set;
2273 break;
2274 case CMD_WAITEVENTS:
2275 // Needs a barrier or is in second execution scope
2276 vuid = "SYNC-vkCmdSetEvent-missingbarrier-wait";
2277 message = wait;
2278 break;
2279 default:
2280 // The only other valid last command that wasn't one.
2281 assert(sync_event->last_command == CMD_NONE);
2282 break;
2283 }
2284 if (vuid) {
2285 assert(nullptr != message);
2286 const char *const cmd_name = CommandTypeString(cmd);
2287 skip |= sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2288 cmd_name, CommandTypeString(sync_event->last_command));
2289 }
2290 }
2291
2292 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002293}
2294
John Zulauf4a6105a2020-11-17 15:11:05 -07002295void CommandBufferAccessContext::RecordSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask,
2296 const ResourceUsageTag &tag) {
2297 auto *sync_event = GetEventState(event);
2298 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
2299
2300 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
2301 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
2302 // any issues caused by naive scope setting here.
2303
2304 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
2305 // Given:
2306 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
2307 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
2308
2309 const auto stage_mask = ExpandPipelineStages(GetQueueFlags(), stageMask);
2310 const auto exec_scope = WithEarlierPipelineStages(stage_mask);
2311
2312 sync_event->stage_mask_param = stageMask;
2313
2314 if (!sync_event->HasBarrier(stageMask, exec_scope)) {
2315 sync_event->unsynchronized_set = sync_event->last_command;
2316 sync_event->ResetFirstScope();
2317 } else if (sync_event->exec_scope == 0) {
2318 // We only set the scope if there isn't one
2319 sync_event->stage_mask = stage_mask;
2320 sync_event->exec_scope = exec_scope;
2321 sync_event->stage_accesses = SyncStageAccess::AccessScopeByStage(sync_event->stage_mask);
2322
2323 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
2324 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
2325 if (access.second.InSourceScopeOrChain(sync_event->exec_scope, sync_event->stage_accesses)) {
2326 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
2327 }
2328 };
2329 GetCurrentAccessContext()->ForAll(set_scope);
2330 sync_event->unsynchronized_set = CMD_NONE;
2331 sync_event->first_scope_tag = tag;
2332 }
2333 sync_event->last_command = CMD_SETEVENT;
2334 sync_event->barriers = 0U;
2335}
John Zulauf49beb112020-11-04 16:06:31 -07002336
2337bool CommandBufferAccessContext::ValidateResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
2338 VkPipelineStageFlags stageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002339 // I'll put this here just in case we need to pass this in for future extension support
2340 const auto cmd = CMD_RESETEVENT;
2341
2342 bool skip = false;
2343 // TODO: EVENTS:
2344 // What is it we need to check... that we've had a reset since a set? Set/Set seems ill formed...
2345 const auto *sync_event = GetEventState(event);
2346 if (!sync_event) return false; // Core, Lifetimes, or Param check needs to catch invalid events.
2347
2348 const char *const set_wait =
2349 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
2350 "hazards.";
2351 const char *message = set_wait; // Only one message this call.
2352 const auto exec_scope = WithEarlierPipelineStages(ExpandPipelineStages(GetQueueFlags(), stageMask));
2353 if (!sync_event->HasBarrier(stageMask, exec_scope)) {
2354 const char *vuid = nullptr;
2355 switch (sync_event->last_command) {
2356 case CMD_SETEVENT:
2357 // Needs a barrier between set and reset
2358 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
2359 break;
2360 case CMD_WAITEVENTS: {
2361 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
2362 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
2363 break;
2364 }
2365 default:
2366 // The only other valid last command that wasn't one.
2367 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT));
2368 break;
2369 }
2370 if (vuid) {
2371 const char *const cmd_name = CommandTypeString(cmd);
2372 skip |= sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2373 cmd_name, CommandTypeString(sync_event->last_command));
2374 }
2375 }
2376 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002377}
2378
John Zulauf4a6105a2020-11-17 15:11:05 -07002379void CommandBufferAccessContext::RecordResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
2380 const auto cmd = CMD_RESETEVENT;
2381 auto *sync_event = GetEventState(event);
2382 if (!sync_event) return;
John Zulauf49beb112020-11-04 16:06:31 -07002383
John Zulauf4a6105a2020-11-17 15:11:05 -07002384 // Clear out the first sync scope, any races vs. wait or set are reported, so we'll keep the bookkeeping simple assuming
2385 // the safe case
2386 for (const auto address_type : kAddressTypes) {
2387 sync_event->first_scope[static_cast<size_t>(address_type)].clear();
2388 }
2389
2390 // Update the event state
2391 sync_event->last_command = cmd;
2392 sync_event->unsynchronized_set = CMD_NONE;
2393 sync_event->ResetFirstScope();
2394 sync_event->barriers = 0U;
2395}
2396
2397bool CommandBufferAccessContext::ValidateWaitEvents(uint32_t eventCount, const VkEvent *pEvents, VkPipelineStageFlags srcStageMask,
2398 VkPipelineStageFlags dstStageMask, uint32_t memoryBarrierCount,
2399 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
John Zulauf49beb112020-11-04 16:06:31 -07002400 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2401 uint32_t imageMemoryBarrierCount,
2402 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002403 const auto cmd = CMD_WAITEVENTS;
2404 const char *const ignored = "Wait operation is ignored for this event.";
2405 bool skip = false;
2406
2407 if (srcStageMask & VK_PIPELINE_STAGE_HOST_BIT) {
2408 const char *const cmd_name = CommandTypeString(cmd);
2409 const char *const vuid = "SYNC-vkCmdWaitEvents-hostevent-unsupported";
John Zulauffe757512020-12-18 12:17:47 -07002410 skip = sync_state_->LogInfo(cb_state_->commandBuffer, vuid,
2411 "%s, srcStageMask includes %s, unsupported by synchronization validaton.", cmd_name,
2412 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT), ignored);
John Zulauf4a6105a2020-11-17 15:11:05 -07002413 }
2414
2415 VkPipelineStageFlags event_stage_masks = 0U;
John Zulauffe757512020-12-18 12:17:47 -07002416 bool events_not_found = false;
John Zulauf4a6105a2020-11-17 15:11:05 -07002417 for (uint32_t event_index = 0; event_index < eventCount; event_index++) {
2418 const auto event = pEvents[event_index];
2419 const auto *sync_event = GetEventState(event);
John Zulauffe757512020-12-18 12:17:47 -07002420 if (!sync_event) {
2421 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
2422 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives.
2423
2424 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
2425 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002426
2427 event_stage_masks |= sync_event->stage_mask_param;
2428 const auto ignore_reason = sync_event->IsIgnoredByWait(srcStageMask);
2429 if (ignore_reason) {
2430 switch (ignore_reason) {
2431 case SyncEventState::ResetWaitRace: {
2432 const char *const cmd_name = CommandTypeString(cmd);
2433 const char *const vuid = "SYNC-vkCmdWaitEvents-missingbarrier-reset";
2434 const char *const message =
2435 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
2436 skip |=
2437 sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2438 cmd_name, CommandTypeString(sync_event->last_command), ignored);
2439 break;
2440 }
2441 case SyncEventState::SetRace: {
2442 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for this
2443 // event
2444 const char *const cmd_name = CommandTypeString(cmd);
2445 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
2446 const char *const message =
2447 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, % %s";
2448 const char *const reason = "First synchronization scope is undefined.";
2449 skip |=
2450 sync_state_->LogError(event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2451 CommandTypeString(sync_event->last_command), reason, ignored);
2452 break;
2453 }
2454 case SyncEventState::MissingStageBits: {
2455 const VkPipelineStageFlags missing_bits = sync_event->stage_mask_param & ~srcStageMask;
2456 // Issue error message that event waited for is not in wait events scope
2457 const char *const cmd_name = CommandTypeString(cmd);
2458 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
2459 const char *const message =
2460 "%s: %s stageMask 0x%" PRIx32 " includes bits not present in srcStageMask 0x%" PRIx32
2461 ". Bits missing from srcStageMask %s. %s";
2462 skip |= sync_state_->LogError(
2463 event, vuid, message, cmd_name, sync_state_->report_data->FormatHandle(event).c_str(),
2464 sync_event->stage_mask_param, srcStageMask, string_VkPipelineStageFlags(missing_bits).c_str(), ignored);
2465 break;
2466 }
2467 default:
2468 assert(ignore_reason == SyncEventState::NotIgnored);
2469 }
2470 } else if (imageMemoryBarrierCount) {
2471 const auto *context = GetCurrentAccessContext();
2472 assert(context);
2473 for (uint32_t barrier_index = 0; barrier_index < imageMemoryBarrierCount; barrier_index++) {
2474 const auto &barrier = pImageMemoryBarriers[barrier_index];
2475 if (barrier.oldLayout == barrier.newLayout) continue;
2476 const auto *image_state = sync_state_->Get<IMAGE_STATE>(barrier.image);
2477 if (!image_state) continue;
2478 auto subresource_range = NormalizeSubresourceRange(image_state->createInfo, barrier.subresourceRange);
2479 const auto src_access_scope = SyncStageAccess::AccessScope(sync_event->stage_accesses, barrier.srcAccessMask);
2480 const auto hazard =
2481 context->DetectImageBarrierHazard(*image_state, sync_event->exec_scope, src_access_scope, subresource_range,
2482 *sync_event, AccessContext::DetectOptions::kDetectAll);
2483 if (hazard.hazard) {
2484 const char *const cmd_name = CommandTypeString(cmd);
2485 skip |= sync_state_->LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
2486 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", cmd_name,
2487 string_SyncHazard(hazard.hazard), barrier_index,
2488 sync_state_->report_data->FormatHandle(barrier.image).c_str(),
2489 string_UsageTag(hazard).c_str());
2490 break;
2491 }
2492 }
2493 }
2494 }
2495
2496 // Note that we can't check for HOST in pEvents as we don't track that set event type
2497 const auto extra_stage_bits = (srcStageMask & ~VK_PIPELINE_STAGE_HOST_BIT) & ~event_stage_masks;
2498 if (extra_stage_bits) {
2499 // Issue error message that event waited for is not in wait events scope
2500 const char *const cmd_name = CommandTypeString(cmd);
2501 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
2502 const char *const message =
John Zulauffe757512020-12-18 12:17:47 -07002503 "%s: srcStageMask 0x%" PRIx32 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
2504 if (events_not_found) {
2505 skip |= sync_state_->LogInfo(cb_state_->commandBuffer, vuid, message, cmd_name, srcStageMask,
2506 string_VkPipelineStageFlags(extra_stage_bits).c_str(),
2507 " vkCmdSetEvent may be in previously submitted command buffer.");
2508 } else {
2509 skip |= sync_state_->LogError(cb_state_->commandBuffer, vuid, message, cmd_name, srcStageMask,
2510 string_VkPipelineStageFlags(extra_stage_bits).c_str(), "");
2511 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002512 }
2513 return skip;
John Zulauf49beb112020-11-04 16:06:31 -07002514}
2515
2516void CommandBufferAccessContext::RecordWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
2517 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
2518 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2519 uint32_t bufferMemoryBarrierCount,
2520 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2521 uint32_t imageMemoryBarrierCount,
John Zulauf4a6105a2020-11-17 15:11:05 -07002522 const VkImageMemoryBarrier *pImageMemoryBarriers, const ResourceUsageTag &tag) {
2523 auto *access_context = GetCurrentAccessContext();
2524 assert(access_context);
2525 if (!access_context) return;
2526
2527 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
2528 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
2529 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
2530 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here
2531 access_context->ResolvePreviousAccesses();
2532
2533 const auto dst_stage_mask = ExpandPipelineStages(GetQueueFlags(), dstStageMask);
2534 auto dst_stage_accesses = SyncStageAccess::AccessScopeByStage(dst_stage_mask);
2535 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2536 for (uint32_t event_index = 0; event_index < eventCount; event_index++) {
2537 const auto event = pEvents[event_index];
2538 auto *sync_event = GetEventState(event);
2539 if (!sync_event) continue;
2540
2541 sync_event->last_command = CMD_WAITEVENTS;
2542
2543 if (!sync_event->IsIgnoredByWait(srcStageMask)) {
2544 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
2545 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
2546 // of the barriers is maintained.
2547 ApplyBufferBarriers(*sync_event, dst_exec_scope, dst_stage_accesses, bufferMemoryBarrierCount, pBufferMemoryBarriers);
2548 ApplyImageBarriers(*sync_event, dst_exec_scope, dst_stage_accesses, imageMemoryBarrierCount, pImageMemoryBarriers, tag);
2549 ApplyGlobalBarriers(*sync_event, dstStageMask, dst_exec_scope, dst_stage_accesses, memoryBarrierCount, pMemoryBarriers,
2550 tag);
2551 } else {
2552 // We ignored this wait, so we don't have any effective synchronization barriers for it.
2553 sync_event->barriers = 0U;
2554 }
2555 }
2556
2557 // Apply the pending barriers
2558 ResolvePendingBarrierFunctor apply_pending_action(tag);
2559 access_context->ApplyGlobalBarriers(apply_pending_action);
2560}
2561
2562void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2563 // Erase is okay with the key not being
2564 event_state_.erase(event);
2565}
2566
2567SyncEventState *CommandBufferAccessContext::GetEventState(VkEvent event) {
2568 auto &event_up = event_state_[event];
2569 if (!event_up) {
2570 auto event_atate = sync_state_->GetShared<EVENT_STATE>(event);
2571 event_up.reset(new SyncEventState(event_atate));
2572 }
2573 return event_up.get();
2574}
2575
2576const SyncEventState *CommandBufferAccessContext::GetEventState(VkEvent event) const {
2577 auto event_it = event_state_.find(event);
2578 if (event_it == event_state_.cend()) {
2579 return nullptr;
2580 }
2581 return event_it->second.get();
2582}
John Zulauf49beb112020-11-04 16:06:31 -07002583
locke-lunarg61870c22020-06-09 14:51:50 -06002584bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
2585 const VkRect2D &render_area, const char *func_name) const {
2586 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002587 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2588 if (!pipe ||
2589 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002590 return skip;
2591 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002592 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002593 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2594 VkExtent3D extent = CastTo3D(render_area.extent);
2595 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06002596
John Zulauf1a224292020-06-30 14:52:13 -06002597 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002598 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002599 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2600 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002601 if (location >= subpass.colorAttachmentCount ||
2602 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002603 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002604 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002605 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002606 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
2607 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06002608 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002609 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002610 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002611 func_name, string_SyncHazard(hazard.hazard),
2612 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2613 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002614 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002615 }
2616 }
2617 }
locke-lunarg37047832020-06-12 13:44:45 -06002618
2619 // PHASE1 TODO: Add layout based read/vs. write selection.
2620 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002621 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002622 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002623 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002624 bool depth_write = false, stencil_write = false;
2625
2626 // PHASE1 TODO: These validation should be in core_checks.
2627 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002628 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2629 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002630 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2631 depth_write = true;
2632 }
2633 // PHASE1 TODO: It needs to check if stencil is writable.
2634 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2635 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2636 // PHASE1 TODO: These validation should be in core_checks.
2637 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002638 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002639 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2640 stencil_write = true;
2641 }
2642
2643 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2644 if (depth_write) {
2645 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002646 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2647 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002648 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002649 skip |= sync_state.LogError(
2650 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002651 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002652 func_name, string_SyncHazard(hazard.hazard),
2653 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2654 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002655 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002656 }
2657 }
2658 if (stencil_write) {
2659 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06002660 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2661 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002662 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002663 skip |= sync_state.LogError(
2664 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002665 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002666 func_name, string_SyncHazard(hazard.hazard),
2667 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
2668 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06002669 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002670 }
locke-lunarg61870c22020-06-09 14:51:50 -06002671 }
2672 }
2673 return skip;
2674}
2675
locke-lunarg96dc9632020-06-10 17:22:18 -06002676void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
2677 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002678 const auto *pipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
2679 if (!pipe ||
2680 (pipe->graphicsPipelineCI.pRasterizationState && pipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002681 return;
2682 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002683 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002684 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
2685 VkExtent3D extent = CastTo3D(render_area.extent);
2686 VkOffset3D offset = CastTo3D(render_area.offset);
2687
John Zulauf1a224292020-06-30 14:52:13 -06002688 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002689 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002690 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2691 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002692 if (location >= subpass.colorAttachmentCount ||
2693 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002694 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002695 }
locke-lunarg96dc9632020-06-10 17:22:18 -06002696 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06002697 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
2698 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002699 }
2700 }
locke-lunarg37047832020-06-12 13:44:45 -06002701
2702 // PHASE1 TODO: Add layout based read/vs. write selection.
2703 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002704 if (pipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
locke-lunarg37047832020-06-12 13:44:45 -06002705 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06002706 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06002707 bool depth_write = false, stencil_write = false;
2708
2709 // PHASE1 TODO: These validation should be in core_checks.
2710 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002711 pipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
2712 pipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002713 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2714 depth_write = true;
2715 }
2716 // PHASE1 TODO: It needs to check if stencil is writable.
2717 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2718 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2719 // PHASE1 TODO: These validation should be in core_checks.
2720 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002721 pipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002722 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2723 stencil_write = true;
2724 }
2725
2726 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2727 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002728 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2729 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002730 }
2731 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06002732 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
2733 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002734 }
locke-lunarg61870c22020-06-09 14:51:50 -06002735 }
2736}
2737
John Zulauf1507ee42020-05-18 11:33:09 -06002738bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
2739 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002740 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002741 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06002742 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2743 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002744 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2745 func_name);
2746
John Zulauf355e49b2020-04-24 15:11:15 -06002747 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002748 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06002749 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002750 if (!skip) {
2751 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2752 // on a copy of the (empty) next context.
2753 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2754 AccessContext temp_context(next_context);
2755 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
2756 skip |= temp_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
2757 }
John Zulauf7635de32020-05-29 17:14:15 -06002758 return skip;
2759}
2760bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
2761 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002762 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002763 bool skip = false;
2764 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
2765 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06002766 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
2767 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06002768 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002769 return skip;
2770}
2771
John Zulauf7635de32020-05-29 17:14:15 -06002772AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
2773 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
2774}
2775
2776bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
2777 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002778 bool skip = false;
2779
John Zulauf7635de32020-05-29 17:14:15 -06002780 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2781 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2782 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2783 // to apply and only copy then, if this proves a hot spot.
2784 std::unique_ptr<AccessContext> proxy_for_current;
2785
John Zulauf355e49b2020-04-24 15:11:15 -06002786 // Validate the "finalLayout" transitions to external
2787 // Get them from where there we're hidding in the extra entry.
2788 const auto &final_transitions = rp_state_->subpass_transitions.back();
2789 for (const auto &transition : final_transitions) {
2790 const auto &attach_view = attachment_views_[transition.attachment];
2791 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2792 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002793 auto *context = trackback.context;
2794
2795 if (transition.prev_pass == current_subpass_) {
2796 if (!proxy_for_current) {
2797 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
2798 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
2799 }
2800 context = proxy_for_current.get();
2801 }
2802
John Zulaufa0a98292020-09-18 09:30:10 -06002803 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2804 const auto merged_barrier = MergeBarriers(trackback.barriers);
2805 auto hazard = context->DetectImageBarrierHazard(*attach_view->image_state, merged_barrier.src_exec_scope,
2806 merged_barrier.src_access_scope, attach_view->normalized_subresource_range,
2807 AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002808 if (hazard.hazard) {
2809 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
2810 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf389c34b2020-07-28 11:19:35 -06002811 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06002812 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf389c34b2020-07-28 11:19:35 -06002813 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf37ceaed2020-07-03 16:18:15 -06002814 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002815 }
2816 }
2817 return skip;
2818}
2819
2820void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2821 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002822 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002823}
2824
John Zulauf1507ee42020-05-18 11:33:09 -06002825void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
2826 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2827 auto &subpass_context = subpass_contexts_[current_subpass_];
2828 VkExtent3D extent = CastTo3D(render_area.extent);
2829 VkOffset3D offset = CastTo3D(render_area.offset);
2830
2831 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2832 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
2833 if (attachment_views_[i] == nullptr) continue; // UNUSED
2834 const auto &view = *attachment_views_[i];
2835 const IMAGE_STATE *image = view.image_state.get();
2836 if (image == nullptr) continue;
2837
2838 const auto &ci = attachment_ci[i];
2839 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002840 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002841 const bool is_color = !(has_depth || has_stencil);
2842
2843 if (is_color) {
2844 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2845 extent, tag);
2846 } else {
2847 auto update_range = view.normalized_subresource_range;
2848 if (has_depth) {
2849 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2850 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2851 }
2852 if (has_stencil) {
2853 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2854 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2855 tag);
2856 }
2857 }
2858 }
2859 }
2860}
2861
John Zulauf355e49b2020-04-24 15:11:15 -06002862void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002863 const AccessContext *external_context, VkQueueFlags queue_flags,
2864 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002865 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002866 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002867 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2868 // Add this for all subpasses here so that they exsist during next subpass validation
2869 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002870 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002871 }
2872 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2873
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002874 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002875 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002876 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002877}
John Zulauf1507ee42020-05-18 11:33:09 -06002878
2879void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002880 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2881 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002882 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002883
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002884 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2885 // subpass, so their tag needs to be different from the layout and load operations below.
2886 ResourceUsageTag next_tag = tag;
2887 next_tag.index++;
John Zulauf355e49b2020-04-24 15:11:15 -06002888 current_subpass_++;
2889 assert(current_subpass_ < subpass_contexts_.size());
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002890 subpass_contexts_[current_subpass_].SetStartTag(next_tag);
2891 RecordLayoutTransitions(next_tag);
2892 RecordLoadOperations(render_area, next_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002893}
2894
John Zulauf1a224292020-06-30 14:52:13 -06002895void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2896 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002897 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002898 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002899 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002900
John Zulauf355e49b2020-04-24 15:11:15 -06002901 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002902 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002903
2904 // Add the "finalLayout" transitions to external
2905 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002906 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2907 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2908 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002909 const auto &final_transitions = rp_state_->subpass_transitions.back();
2910 for (const auto &transition : final_transitions) {
2911 const auto &attachment = attachment_views_[transition.attachment];
2912 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002913 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf1e331ec2020-12-04 18:29:38 -07002914 std::vector<PipelineBarrierOp> barrier_ops;
2915 barrier_ops.reserve(last_trackback.barriers.size());
2916 for (const auto &barrier : last_trackback.barriers) {
2917 barrier_ops.emplace_back(barrier, true);
2918 }
2919 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, barrier_ops, tag);
2920 external_context->UpdateResourceAccess(*attachment->image_state, attachment->normalized_subresource_range, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002921 }
2922}
2923
John Zulauf3d84f1b2020-03-09 13:33:25 -06002924SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2925 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2926 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2927 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2928 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2929 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2930 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2931}
2932
John Zulaufb02c1eb2020-10-06 16:33:36 -06002933// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2934void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2935 for (const auto &barrier : barriers) {
2936 ApplyBarrier(barrier, layout_transition);
2937 }
2938}
2939
John Zulauf89311b42020-09-29 16:28:47 -06002940// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2941// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2942// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002943void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2944 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002945 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002946 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002947 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002948 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002949 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002950 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002951}
John Zulauf9cb530d2019-09-30 14:14:10 -06002952HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2953 HazardResult hazard;
2954 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002955 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002956 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002957 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002958 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002959 }
2960 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002961 // Write operation:
2962 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2963 // If reads exists -- test only against them because either:
2964 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2965 // * 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
2966 // the current write happens after the reads, so just test the write against the reades
2967 // Otherwise test against last_write
2968 //
2969 // Look for casus belli for WAR
2970 if (last_read_count) {
2971 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2972 const auto &read_access = last_reads[read_index];
2973 if (IsReadHazard(usage_stage, read_access)) {
2974 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2975 break;
2976 }
2977 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002978 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002979 // Write-After-Write check -- if we have a previous write to test against
2980 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002981 }
2982 }
2983 return hazard;
2984}
2985
John Zulauf69133422020-05-20 14:55:53 -06002986HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2987 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2988 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002989 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002990 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002991 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2992 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002993 if (IsRead(usage_bit)) {
2994 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2995 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2996 if (is_raw_hazard) {
2997 // NOTE: we know last_write is non-zero
2998 // See if the ordering rules save us from the simple RAW check above
2999 // First check to see if the current usage is covered by the ordering rules
3000 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
3001 const bool usage_is_ordered =
3002 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
3003 if (usage_is_ordered) {
3004 // Now see of the most recent write (or a subsequent read) are ordered
3005 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
3006 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06003007 }
3008 }
John Zulauf4285ee92020-09-23 10:20:52 -06003009 if (is_raw_hazard) {
3010 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
3011 }
John Zulauf361fb532020-07-22 10:45:39 -06003012 } else {
3013 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003014 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulauf361fb532020-07-22 10:45:39 -06003015 if (last_read_count) {
John Zulauf361fb532020-07-22 10:45:39 -06003016 // Look for any WAR hazards outside the ordered set of stages
John Zulauf4285ee92020-09-23 10:20:52 -06003017 VkPipelineStageFlags ordered_stages = 0;
3018 if (usage_write_is_ordered) {
3019 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
3020 ordered_stages = GetOrderedStages(ordering);
3021 }
3022 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
3023 if ((ordered_stages & last_read_stages) != last_read_stages) {
3024 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
3025 const auto &read_access = last_reads[read_index];
3026 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
3027 if (IsReadHazard(usage_stage, read_access)) {
3028 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3029 break;
3030 }
John Zulaufd14743a2020-07-03 09:42:39 -06003031 }
3032 }
John Zulauf4285ee92020-09-23 10:20:52 -06003033 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003034 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003035 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06003036 }
John Zulauf69133422020-05-20 14:55:53 -06003037 }
3038 }
3039 return hazard;
3040}
3041
John Zulauf2f952d22020-02-10 11:34:51 -07003042// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003043HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07003044 HazardResult hazard;
3045 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003046 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
3047 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
3048 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07003049 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003050 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06003051 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07003052 }
3053 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003054 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06003055 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07003056 } else if (last_read_count > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003057 // Any reads during the other subpass will conflict with this write, so we need to check them all.
3058 for (uint32_t i = 0; i < last_read_count; i++) {
3059 if (last_reads[i].tag.index >= start_tag.index) {
3060 hazard.Set(this, usage_index, WRITE_RACING_READ, last_reads[i].access, last_reads[i].tag);
3061 break;
3062 }
3063 }
John Zulauf2f952d22020-02-10 11:34:51 -07003064 }
3065 }
3066 return hazard;
3067}
3068
John Zulauf36bcf6a2020-02-03 15:12:52 -07003069HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003070 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07003071 // Only supporting image layout transitions for now
3072 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3073 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06003074 // only test for WAW if there no intervening read operations.
3075 // See DetectHazard(SyncStagetAccessIndex) above for more details.
3076 if (last_read_count) {
John Zulauf355e49b2020-04-24 15:11:15 -06003077 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07003078 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07003079 const auto &read_access = last_reads[read_index];
John Zulauf4a6105a2020-11-17 15:11:05 -07003080 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06003081 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07003082 break;
3083 }
3084 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003085 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3086 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3087 }
3088
3089 return hazard;
3090}
3091
3092HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
3093 const SyncStageAccessFlags &src_access_scope,
3094 const ResourceUsageTag &event_tag) const {
3095 // Only supporting image layout transitions for now
3096 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3097 HazardResult hazard;
3098 // only test for WAW if there no intervening read operations.
3099 // See DetectHazard(SyncStagetAccessIndex) above for more details.
3100
3101 if (last_read_count) {
3102 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
3103 // first scope, or they are a hazard.
3104 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
3105 const auto &read_access = last_reads[read_index];
3106 if (read_access.tag.IsBefore(event_tag)) {
3107 // The read is in the events first synchronization scope, so we use a barrier hazard check
3108 // If the read stage is not in the src sync scope
3109 // *AND* not execution chained with an existing sync barrier (that's the or)
3110 // then the barrier access is unsafe (R/W after R)
3111 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3112 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3113 break;
3114 }
3115 } else {
3116 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3117 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3118 }
3119 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003120 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003121 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
3122 if (write_tag.IsBefore(event_tag)) {
3123 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3124 // So do a normal barrier hazard check
3125 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3126 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3127 }
3128 } else {
3129 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003130 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3131 }
John Zulaufd14743a2020-07-03 09:42:39 -06003132 }
John Zulauf361fb532020-07-22 10:45:39 -06003133
John Zulauf0cb5be22020-01-23 12:18:22 -07003134 return hazard;
3135}
3136
John Zulauf5f13a792020-03-10 07:31:21 -06003137// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3138// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3139// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3140void ResourceAccessState::Resolve(const ResourceAccessState &other) {
3141 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003142 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3143 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003144 *this = other;
3145 } else if (!other.write_tag.IsBefore(write_tag)) {
3146 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
3147 // dependency chaining logic or any stage expansion)
3148 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003149 pending_write_barriers |= other.pending_write_barriers;
3150 pending_layout_transition |= other.pending_layout_transition;
3151 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003152
John Zulaufd14743a2020-07-03 09:42:39 -06003153 // Merge the read states
John Zulauf4285ee92020-09-23 10:20:52 -06003154 const auto pre_merge_count = last_read_count;
3155 const auto pre_merge_stages = last_read_stages;
John Zulauf5f13a792020-03-10 07:31:21 -06003156 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
3157 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003158 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003159 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003160 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3161 // but we should wait on profiling data for that.
3162 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003163 auto &my_read = last_reads[my_read_index];
3164 if (other_read.stage == my_read.stage) {
3165 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003166 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003167 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003168 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003169 my_read.pending_dep_chain = other_read.pending_dep_chain;
3170 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3171 // May require tracking more than one access per stage.
3172 my_read.barriers = other_read.barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003173 if (my_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
3174 // Since I'm overwriting the fragement stage read, also update the input attachment info
3175 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003176 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003177 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003178 } else if (other_read.tag.IsBefore(my_read.tag)) {
3179 // The read tags match so merge the barriers
3180 my_read.barriers |= other_read.barriers;
3181 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003182 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003183
John Zulauf5f13a792020-03-10 07:31:21 -06003184 break;
3185 }
3186 }
3187 } else {
3188 // The other read stage doesn't exist in this, so add it.
3189 last_reads[last_read_count] = other_read;
3190 last_read_count++;
3191 last_read_stages |= other_read.stage;
John Zulauf4285ee92020-09-23 10:20:52 -06003192 if (other_read.stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06003193 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003194 }
John Zulauf5f13a792020-03-10 07:31:21 -06003195 }
3196 }
John Zulauf361fb532020-07-22 10:45:39 -06003197 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003198 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3199 // ignore it.
John Zulauf5f13a792020-03-10 07:31:21 -06003200}
3201
John Zulauf9cb530d2019-09-30 14:14:10 -06003202void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
3203 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3204 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003205 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003206 // Mulitple outstanding reads may be of interest and do dependency chains independently
3207 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3208 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003209 uint32_t update_index = kStageCount;
John Zulauf9cb530d2019-09-30 14:14:10 -06003210 if (usage_stage & last_read_stages) {
3211 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf4285ee92020-09-23 10:20:52 -06003212 if (last_reads[read_index].stage == usage_stage) {
3213 update_index = read_index;
John Zulauf9cb530d2019-09-30 14:14:10 -06003214 break;
3215 }
3216 }
John Zulauf4285ee92020-09-23 10:20:52 -06003217 assert(update_index < last_read_count);
John Zulauf9cb530d2019-09-30 14:14:10 -06003218 } else {
John Zulauf9cb530d2019-09-30 14:14:10 -06003219 assert(last_read_count < last_reads.size());
John Zulauf4285ee92020-09-23 10:20:52 -06003220 update_index = last_read_count++;
John Zulauf9cb530d2019-09-30 14:14:10 -06003221 last_read_stages |= usage_stage;
3222 }
John Zulauf4285ee92020-09-23 10:20:52 -06003223 last_reads[update_index].Set(usage_stage, usage_bit, 0, tag);
3224
3225 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
3226 if (usage_stage == VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT) {
John Zulauff51fbb62020-10-02 14:43:24 -06003227 // TODO Revisit re: multiple reads for a given stage
3228 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003229 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003230 } else {
3231 // Assume write
3232 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003233 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003234 }
3235}
John Zulauf5f13a792020-03-10 07:31:21 -06003236
John Zulauf89311b42020-09-29 16:28:47 -06003237// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3238// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3239// We can overwrite them as *this* write is now after them.
3240//
3241// 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 -07003242void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulauf89311b42020-09-29 16:28:47 -06003243 last_read_count = 0;
3244 last_read_stages = 0;
3245 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003246 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003247
3248 write_barriers = 0;
3249 write_dependency_chain = 0;
3250 write_tag = tag;
3251 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003252}
3253
John Zulauf89311b42020-09-29 16:28:47 -06003254// Apply the memory barrier without updating the existing barriers. The execution barrier
3255// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3256// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3257// replace the current write barriers or add to them, so accumulate to pending as well.
3258void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3259 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3260 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003261 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3262 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3263 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3264 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulauf4a6105a2020-11-17 15:11:05 -07003265 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003266 pending_write_barriers |= barrier.dst_access_scope;
3267 pending_write_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003268 }
John Zulauf89311b42020-09-29 16:28:47 -06003269 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3270 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003271
John Zulauf89311b42020-09-29 16:28:47 -06003272 if (!pending_layout_transition) {
3273 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3274 // don't need to be tracked as we're just going to zero them.
John Zulaufa0a98292020-09-18 09:30:10 -06003275 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf89311b42020-09-29 16:28:47 -06003276 ReadState &access = last_reads[read_index];
3277 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
3278 if (barrier.src_exec_scope & (access.stage | access.barriers)) {
3279 access.pending_dep_chain |= barrier.dst_exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003280 }
3281 }
John Zulaufa0a98292020-09-18 09:30:10 -06003282 }
John Zulaufa0a98292020-09-18 09:30:10 -06003283}
3284
John Zulauf4a6105a2020-11-17 15:11:05 -07003285// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3286// changes the "chaining" state, but to keep barriers independent. See discussion above.
3287void ResourceAccessState::ApplyBarrier(const ResourceUsageTag &scope_tag, const SyncBarrier &barrier, bool layout_transition) {
3288 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3289 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3290 // in order to know if it's in the excecution scope
3291 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3292 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3293 // errors w.r.t. "most recent" accesses.
3294 if (layout_transition || ((write_tag.IsBefore(scope_tag)) && (barrier.src_access_scope & last_write).any())) {
3295 pending_write_barriers |= barrier.dst_access_scope;
3296 pending_write_dep_chain |= barrier.dst_exec_scope;
3297 }
3298 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3299 pending_layout_transition |= layout_transition;
3300
3301 if (!pending_layout_transition) {
3302 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3303 // don't need to be tracked as we're just going to zero them.
3304 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
3305 ReadState &access = last_reads[read_index];
3306 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3307 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3308 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3309 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3310 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3311 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3312 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
3313 if (access.tag.IsBefore(scope_tag) && (barrier.src_exec_scope & (access.stage | access.barriers))) {
3314 access.pending_dep_chain |= barrier.dst_exec_scope;
3315 }
3316 }
3317 }
3318}
John Zulauf89311b42020-09-29 16:28:47 -06003319void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
3320 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06003321 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
3322 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
3323 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003324 }
John Zulauf89311b42020-09-29 16:28:47 -06003325
3326 // Apply the accumulate execution barriers (and thus update chaining information)
3327 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
3328 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
3329 ReadState &access = last_reads[read_index];
3330 access.barriers |= access.pending_dep_chain;
3331 read_execution_barriers |= access.barriers;
3332 access.pending_dep_chain = 0;
3333 }
3334
3335 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3336 write_dependency_chain |= pending_write_dep_chain;
3337 write_barriers |= pending_write_barriers;
3338 pending_write_dep_chain = 0;
3339 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003340}
3341
John Zulauf59e25072020-07-17 10:55:21 -06003342// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003343VkPipelineStageFlags ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
John Zulauf59e25072020-07-17 10:55:21 -06003344 VkPipelineStageFlags barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003345
3346 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
3347 const auto &read_access = last_reads[read_index];
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003348 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003349 barriers = read_access.barriers;
3350 break;
John Zulauf59e25072020-07-17 10:55:21 -06003351 }
3352 }
John Zulauf4285ee92020-09-23 10:20:52 -06003353
John Zulauf59e25072020-07-17 10:55:21 -06003354 return barriers;
3355}
3356
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003357inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlagBits usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003358 assert(IsRead(usage));
3359 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3360 // * the previous reads are not hazards, and thus last_write must be visible and available to
3361 // any reads that happen after.
3362 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3363 // 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 -07003364 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003365}
3366
John Zulauf4285ee92020-09-23 10:20:52 -06003367VkPipelineStageFlags ResourceAccessState::GetOrderedStages(const SyncOrderingBarrier &ordering) const {
3368 // Whether the stage are in the ordering scope only matters if the current write is ordered
3369 VkPipelineStageFlags ordered_stages = last_read_stages & ordering.exec_scope;
3370 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003371 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003372 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003373 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
3374 ordered_stages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
3375 }
3376
3377 return ordered_stages;
3378}
3379
3380inline ResourceAccessState::ReadState *ResourceAccessState::GetReadStateForStage(VkPipelineStageFlagBits stage,
3381 uint32_t search_limit) {
3382 ReadState *read_state = nullptr;
3383 search_limit = std::min(search_limit, last_read_count);
3384 for (uint32_t i = 0; i < search_limit; i++) {
3385 if (last_reads[i].stage == stage) {
3386 read_state = &last_reads[i];
3387 break;
3388 }
3389 }
3390 return read_state;
3391}
3392
John Zulaufd1f85d42020-04-15 12:23:15 -06003393void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003394 auto *access_context = GetAccessContextNoInsert(command_buffer);
3395 if (access_context) {
3396 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003397 }
3398}
3399
John Zulaufd1f85d42020-04-15 12:23:15 -06003400void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3401 auto access_found = cb_access_state.find(command_buffer);
3402 if (access_found != cb_access_state.end()) {
3403 access_found->second->Reset();
3404 cb_access_state.erase(access_found);
3405 }
3406}
3407
John Zulauf89311b42020-09-29 16:28:47 -06003408void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
3409 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags src_access_scope,
3410 SyncStageAccessFlags dst_access_scope, uint32_t memory_barrier_count,
3411 const VkMemoryBarrier *pMemoryBarriers, const ResourceUsageTag &tag) {
John Zulauf1e331ec2020-12-04 18:29:38 -07003412 std::vector<PipelineBarrierOp> barrier_ops;
3413 barrier_ops.reserve(std::min<uint32_t>(1, memory_barrier_count));
John Zulauf89311b42020-09-29 16:28:47 -06003414 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
3415 const auto &barrier = pMemoryBarriers[barrier_index];
3416 SyncBarrier sync_barrier(src_exec_scope, SyncStageAccess::AccessScope(src_access_scope, barrier.srcAccessMask),
3417 dst_exec_scope, SyncStageAccess::AccessScope(dst_access_scope, barrier.dstAccessMask));
John Zulauf1e331ec2020-12-04 18:29:38 -07003418 barrier_ops.emplace_back(sync_barrier, false);
John Zulauf89311b42020-09-29 16:28:47 -06003419 }
3420 if (0 == memory_barrier_count) {
3421 // If there are no global memory barriers, force an exec barrier
John Zulauf1e331ec2020-12-04 18:29:38 -07003422 barrier_ops.emplace_back(SyncBarrier(src_exec_scope, 0, dst_exec_scope, 0), false);
John Zulauf89311b42020-09-29 16:28:47 -06003423 }
John Zulauf1e331ec2020-12-04 18:29:38 -07003424 ApplyBarrierOpsFunctor<PipelineBarrierOp> barriers_functor(true /* resolve */, barrier_ops, tag);
John Zulauf540266b2020-04-06 18:54:53 -06003425 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06003426}
3427
John Zulauf540266b2020-04-06 18:54:53 -06003428void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003429 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
3430 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06003431 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003432 for (uint32_t index = 0; index < barrier_count; index++) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003433 auto barrier = barriers[index]; // barrier is a copy
John Zulauf9cb530d2019-09-30 14:14:10 -06003434 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
3435 if (!buffer) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06003436 barrier.size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
3437 const ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06003438 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
3439 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06003440 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf4a6105a2020-11-17 15:11:05 -07003441 const ApplyBarrierFunctor<PipelineBarrierOp> update_action({sync_barrier, false /* layout_transition */});
John Zulauf89311b42020-09-29 16:28:47 -06003442 context->UpdateResourceAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06003443 }
3444}
3445
John Zulauf540266b2020-04-06 18:54:53 -06003446void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003447 const SyncStageAccessFlags &src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
3448 const SyncStageAccessFlags &dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06003449 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07003450 for (uint32_t index = 0; index < barrier_count; index++) {
3451 const auto &barrier = barriers[index];
3452 const auto *image = Get<IMAGE_STATE>(barrier.image);
3453 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06003454 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06003455 bool layout_transition = barrier.oldLayout != barrier.newLayout;
3456 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
3457 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
John Zulauf89311b42020-09-29 16:28:47 -06003458 const SyncBarrier sync_barrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf4a6105a2020-11-17 15:11:05 -07003459 const ApplyBarrierFunctor<PipelineBarrierOp> barrier_action({sync_barrier, layout_transition});
John Zulauf89311b42020-09-29 16:28:47 -06003460 context->UpdateResourceAccess(*image, subresource_range, barrier_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06003461 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003462}
3463
3464bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3465 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3466 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003467 const auto *cb_context = GetAccessContext(commandBuffer);
3468 assert(cb_context);
3469 if (!cb_context) return skip;
3470 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003471
John Zulauf3d84f1b2020-03-09 13:33:25 -06003472 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003473 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003474 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003475
3476 for (uint32_t region = 0; region < regionCount; region++) {
3477 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003478 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003479 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003480 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003481 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003482 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003483 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003484 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003485 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003486 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003487 }
John Zulauf16adfc92020-04-08 10:28:33 -06003488 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003489 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf355e49b2020-04-24 15:11:15 -06003490 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003491 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003492 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003493 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003494 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003495 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003496 }
3497 }
3498 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003499 }
3500 return skip;
3501}
3502
3503void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3504 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003505 auto *cb_context = GetAccessContext(commandBuffer);
3506 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003507 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003508 auto *context = cb_context->GetCurrentAccessContext();
3509
John Zulauf9cb530d2019-09-30 14:14:10 -06003510 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003511 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003512
3513 for (uint32_t region = 0; region < regionCount; region++) {
3514 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003515 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003516 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003517 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003518 }
John Zulauf16adfc92020-04-08 10:28:33 -06003519 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003520 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
John Zulauf16adfc92020-04-08 10:28:33 -06003521 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003522 }
3523 }
3524}
3525
John Zulauf4a6105a2020-11-17 15:11:05 -07003526void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3527 // Clear out events from the command buffer contexts
3528 for (auto &cb_context : cb_access_state) {
3529 cb_context.second->RecordDestroyEvent(event);
3530 }
3531}
3532
Jeff Leger178b1e52020-10-05 12:22:23 -04003533bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3534 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3535 bool skip = false;
3536 const auto *cb_context = GetAccessContext(commandBuffer);
3537 assert(cb_context);
3538 if (!cb_context) return skip;
3539 const auto *context = cb_context->GetCurrentAccessContext();
3540
3541 // If we have no previous accesses, we have no hazards
3542 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3543 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3544
3545 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3546 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3547 if (src_buffer) {
3548 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
3549 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
3550 if (hazard.hazard) {
3551 // TODO -- add tag information to log msg when useful.
3552 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3553 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3554 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
3555 region, string_UsageTag(hazard).c_str());
3556 }
3557 }
3558 if (dst_buffer && !skip) {
3559 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
3560 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
3561 if (hazard.hazard) {
3562 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3563 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3564 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
3565 region, string_UsageTag(hazard).c_str());
3566 }
3567 }
3568 if (skip) break;
3569 }
3570 return skip;
3571}
3572
3573void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3574 auto *cb_context = GetAccessContext(commandBuffer);
3575 assert(cb_context);
3576 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3577 auto *context = cb_context->GetCurrentAccessContext();
3578
3579 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3580 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3581
3582 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3583 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3584 if (src_buffer) {
3585 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
3586 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
3587 }
3588 if (dst_buffer) {
3589 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
3590 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
3591 }
3592 }
3593}
3594
John Zulauf5c5e88d2019-12-26 11:22:02 -07003595bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3596 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3597 const VkImageCopy *pRegions) const {
3598 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003599 const auto *cb_access_context = GetAccessContext(commandBuffer);
3600 assert(cb_access_context);
3601 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003602
John Zulauf3d84f1b2020-03-09 13:33:25 -06003603 const auto *context = cb_access_context->GetCurrentAccessContext();
3604 assert(context);
3605 if (!context) return skip;
3606
3607 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3608 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003609 for (uint32_t region = 0; region < regionCount; region++) {
3610 const auto &copy_region = pRegions[region];
3611 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003612 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003613 copy_region.srcOffset, copy_region.extent);
3614 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003615 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003616 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003617 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003618 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003619 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003620 }
3621
3622 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003623 VkExtent3D dst_copy_extent =
3624 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06003625 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003626 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003627 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003628 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003629 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003630 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003631 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003632 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003633 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003634 }
3635 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003636
John Zulauf5c5e88d2019-12-26 11:22:02 -07003637 return skip;
3638}
3639
3640void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3641 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3642 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003643 auto *cb_access_context = GetAccessContext(commandBuffer);
3644 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003645 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003646 auto *context = cb_access_context->GetCurrentAccessContext();
3647 assert(context);
3648
John Zulauf5c5e88d2019-12-26 11:22:02 -07003649 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003650 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003651
3652 for (uint32_t region = 0; region < regionCount; region++) {
3653 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003654 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06003655 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
3656 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003657 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003658 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003659 VkExtent3D dst_copy_extent =
3660 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06003661 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
3662 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003663 }
3664 }
3665}
3666
Jeff Leger178b1e52020-10-05 12:22:23 -04003667bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3668 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3669 bool skip = false;
3670 const auto *cb_access_context = GetAccessContext(commandBuffer);
3671 assert(cb_access_context);
3672 if (!cb_access_context) return skip;
3673
3674 const auto *context = cb_access_context->GetCurrentAccessContext();
3675 assert(context);
3676 if (!context) return skip;
3677
3678 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3679 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3680 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3681 const auto &copy_region = pCopyImageInfo->pRegions[region];
3682 if (src_image) {
3683 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
3684 copy_region.srcOffset, copy_region.extent);
3685 if (hazard.hazard) {
3686 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3687 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3688 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
3689 region, string_UsageTag(hazard).c_str());
3690 }
3691 }
3692
3693 if (dst_image) {
3694 VkExtent3D dst_copy_extent =
3695 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
3696 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
3697 copy_region.dstOffset, dst_copy_extent);
3698 if (hazard.hazard) {
3699 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3700 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3701 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
3702 region, string_UsageTag(hazard).c_str());
3703 }
3704 if (skip) break;
3705 }
3706 }
3707
3708 return skip;
3709}
3710
3711void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3712 auto *cb_access_context = GetAccessContext(commandBuffer);
3713 assert(cb_access_context);
3714 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3715 auto *context = cb_access_context->GetCurrentAccessContext();
3716 assert(context);
3717
3718 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3719 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3720
3721 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3722 const auto &copy_region = pCopyImageInfo->pRegions[region];
3723 if (src_image) {
3724 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
3725 copy_region.extent, tag);
3726 }
3727 if (dst_image) {
3728 VkExtent3D dst_copy_extent =
3729 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
3730 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
3731 dst_copy_extent, tag);
3732 }
3733 }
3734}
3735
John Zulauf9cb530d2019-09-30 14:14:10 -06003736bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3737 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3738 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3739 uint32_t bufferMemoryBarrierCount,
3740 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3741 uint32_t imageMemoryBarrierCount,
3742 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3743 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003744 const auto *cb_access_context = GetAccessContext(commandBuffer);
3745 assert(cb_access_context);
3746 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003747
John Zulauf3d84f1b2020-03-09 13:33:25 -06003748 const auto *context = cb_access_context->GetCurrentAccessContext();
3749 assert(context);
3750 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003751
John Zulauf3d84f1b2020-03-09 13:33:25 -06003752 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003753 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3754 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07003755 // Validate Image Layout transitions
3756 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
3757 const auto &barrier = pImageMemoryBarriers[index];
3758 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
3759 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
3760 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06003761 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07003762 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06003763 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06003764 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003765 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003766 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003767 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07003768 }
3769 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003770
3771 return skip;
3772}
3773
3774void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3775 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3776 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3777 uint32_t bufferMemoryBarrierCount,
3778 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3779 uint32_t imageMemoryBarrierCount,
3780 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003781 auto *cb_access_context = GetAccessContext(commandBuffer);
3782 assert(cb_access_context);
3783 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06003784 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003785 auto access_context = cb_access_context->GetCurrentAccessContext();
3786 assert(access_context);
3787 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003788
John Zulauf3d84f1b2020-03-09 13:33:25 -06003789 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003790 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003791 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07003792 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
3793 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
3794 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf89311b42020-09-29 16:28:47 -06003795
3796 // These two apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
3797 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
3798 // of the barriers is maintained.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003799 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
3800 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06003801 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06003802 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003803
John Zulauf89311b42020-09-29 16:28:47 -06003804 // Apply the global barriers last as is it walks all memory, it can also clean up the "pending" state without requiring an
3805 // additional pass, updating the dependency chains *last* as it goes along.
3806 // This is needed to guarantee order independence of the three lists.
John Zulauf3d84f1b2020-03-09 13:33:25 -06003807 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf89311b42020-09-29 16:28:47 -06003808 pMemoryBarriers, tag);
John Zulauf4a6105a2020-11-17 15:11:05 -07003809
3810 // Need to pass the unexpanded stage masks as the ALL_COMMANDS bit is removed during expansion.
3811 cb_access_context->ApplyGlobalBarriersToEvents(srcStageMask, src_exec_scope, dstStageMask, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06003812}
3813
3814void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3815 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3816 // The state tracker sets up the device state
3817 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3818
John Zulauf5f13a792020-03-10 07:31:21 -06003819 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3820 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003821 // TODO: Find a good way to do this hooklessly.
3822 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3823 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3824 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3825
John Zulaufd1f85d42020-04-15 12:23:15 -06003826 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3827 sync_device_state->ResetCommandBufferCallback(command_buffer);
3828 });
3829 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3830 sync_device_state->FreeCommandBufferCallback(command_buffer);
3831 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003832}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003833
John Zulauf355e49b2020-04-24 15:11:15 -06003834bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003835 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003836 bool skip = false;
3837 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
3838 auto cb_context = GetAccessContext(commandBuffer);
3839
3840 if (rp_state && cb_context) {
3841 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
3842 }
3843
3844 return skip;
3845}
3846
3847bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3848 VkSubpassContents contents) const {
3849 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3850 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3851 subpass_begin_info.contents = contents;
3852 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
3853 return skip;
3854}
3855
3856bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003857 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003858 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3859 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
3860 return skip;
3861}
3862
3863bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3864 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003865 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003866 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
3867 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
3868 return skip;
3869}
3870
John Zulauf3d84f1b2020-03-09 13:33:25 -06003871void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3872 VkResult result) {
3873 // The state tracker sets up the command buffer state
3874 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3875
3876 // Create/initialize the structure that trackers accesses at the command buffer scope.
3877 auto cb_access_context = GetAccessContext(commandBuffer);
3878 assert(cb_access_context);
3879 cb_access_context->Reset();
3880}
3881
3882void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06003883 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003884 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003885 if (cb_context) {
3886 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003887 }
3888}
3889
3890void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3891 VkSubpassContents contents) {
3892 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
3893 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3894 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003895 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003896}
3897
3898void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3899 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3900 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003901 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003902}
3903
3904void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3905 const VkRenderPassBeginInfo *pRenderPassBegin,
3906 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3907 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003908 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
3909}
3910
Mike Schuchardt2df08912020-12-15 16:28:09 -08003911bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3912 const VkSubpassEndInfo *pSubpassEndInfo, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003913 bool skip = false;
3914
3915 auto cb_context = GetAccessContext(commandBuffer);
3916 assert(cb_context);
3917 auto cb_state = cb_context->GetCommandBufferState();
3918 if (!cb_state) return skip;
3919
3920 auto rp_state = cb_state->activeRenderPass;
3921 if (!rp_state) return skip;
3922
3923 skip |= cb_context->ValidateNextSubpass(func_name);
3924
3925 return skip;
3926}
3927
3928bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3929 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
3930 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3931 subpass_begin_info.contents = contents;
3932 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
3933 return skip;
3934}
3935
Mike Schuchardt2df08912020-12-15 16:28:09 -08003936bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3937 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003938 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3939 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
3940 return skip;
3941}
3942
3943bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3944 const VkSubpassEndInfo *pSubpassEndInfo) const {
3945 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
3946 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
3947 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003948}
3949
3950void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003951 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003952 auto cb_context = GetAccessContext(commandBuffer);
3953 assert(cb_context);
3954 auto cb_state = cb_context->GetCommandBufferState();
3955 if (!cb_state) return;
3956
3957 auto rp_state = cb_state->activeRenderPass;
3958 if (!rp_state) return;
3959
John Zulauf355e49b2020-04-24 15:11:15 -06003960 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06003961}
3962
3963void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3964 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
3965 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
3966 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003967 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003968}
3969
3970void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3971 const VkSubpassEndInfo *pSubpassEndInfo) {
3972 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003973 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003974}
3975
3976void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3977 const VkSubpassEndInfo *pSubpassEndInfo) {
3978 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003979 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003980}
3981
Mike Schuchardt2df08912020-12-15 16:28:09 -08003982bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06003983 const char *func_name) const {
3984 bool skip = false;
3985
3986 auto cb_context = GetAccessContext(commandBuffer);
3987 assert(cb_context);
3988 auto cb_state = cb_context->GetCommandBufferState();
3989 if (!cb_state) return skip;
3990
3991 auto rp_state = cb_state->activeRenderPass;
3992 if (!rp_state) return skip;
3993
3994 skip |= cb_context->ValidateEndRenderpass(func_name);
3995 return skip;
3996}
3997
3998bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3999 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
4000 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
4001 return skip;
4002}
4003
Mike Schuchardt2df08912020-12-15 16:28:09 -08004004bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06004005 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
4006 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
4007 return skip;
4008}
4009
4010bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08004011 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06004012 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
4013 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
4014 return skip;
4015}
4016
4017void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
4018 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06004019 // Resolve the all subpass contexts to the command buffer contexts
4020 auto cb_context = GetAccessContext(commandBuffer);
4021 assert(cb_context);
4022 auto cb_state = cb_context->GetCommandBufferState();
4023 if (!cb_state) return;
4024
locke-lunargaecf2152020-05-12 17:15:41 -06004025 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06004026 if (!rp_state) return;
4027
John Zulauf355e49b2020-04-24 15:11:15 -06004028 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06004029}
John Zulauf3d84f1b2020-03-09 13:33:25 -06004030
John Zulauf33fc1d52020-07-17 11:01:10 -06004031// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
4032// updates to a resource which do not conflict at the byte level.
4033// TODO: Revisit this rule to see if it needs to be tighter or looser
4034// TODO: Add programatic control over suppression heuristics
4035bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
4036 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
4037}
4038
John Zulauf3d84f1b2020-03-09 13:33:25 -06004039void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004040 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06004041 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004042}
4043
4044void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06004045 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06004046 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004047}
4048
4049void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06004050 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06004051 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004052}
locke-lunarga19c71d2020-03-02 18:17:04 -07004053
Jeff Leger178b1e52020-10-05 12:22:23 -04004054template <typename BufferImageCopyRegionType>
4055bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4056 VkImageLayout dstImageLayout, uint32_t regionCount,
4057 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004058 bool skip = false;
4059 const auto *cb_access_context = GetAccessContext(commandBuffer);
4060 assert(cb_access_context);
4061 if (!cb_access_context) return skip;
4062
Jeff Leger178b1e52020-10-05 12:22:23 -04004063 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4064 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
4065
locke-lunarga19c71d2020-03-02 18:17:04 -07004066 const auto *context = cb_access_context->GetCurrentAccessContext();
4067 assert(context);
4068 if (!context) return skip;
4069
4070 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07004071 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4072
4073 for (uint32_t region = 0; region < regionCount; region++) {
4074 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06004075 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004076 ResourceAccessRange src_range =
4077 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004078 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07004079 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06004080 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06004081 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004082 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004083 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004084 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004085 }
4086 }
4087 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004088 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004089 copy_region.imageOffset, copy_region.imageExtent);
4090 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004091 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004092 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004093 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004094 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004095 }
4096 if (skip) break;
4097 }
4098 if (skip) break;
4099 }
4100 return skip;
4101}
4102
Jeff Leger178b1e52020-10-05 12:22:23 -04004103bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4104 VkImageLayout dstImageLayout, uint32_t regionCount,
4105 const VkBufferImageCopy *pRegions) const {
4106 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
4107 COPY_COMMAND_VERSION_1);
4108}
4109
4110bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4111 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
4112 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4113 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4114 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4115}
4116
4117template <typename BufferImageCopyRegionType>
4118void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4119 VkImageLayout dstImageLayout, uint32_t regionCount,
4120 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004121 auto *cb_access_context = GetAccessContext(commandBuffer);
4122 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004123
4124 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4125 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
4126
4127 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004128 auto *context = cb_access_context->GetCurrentAccessContext();
4129 assert(context);
4130
4131 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06004132 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004133
4134 for (uint32_t region = 0; region < regionCount; region++) {
4135 const auto &copy_region = pRegions[region];
4136 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004137 ResourceAccessRange src_range =
4138 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004139 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004140 }
4141 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004142 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06004143 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004144 }
4145 }
4146}
4147
Jeff Leger178b1e52020-10-05 12:22:23 -04004148void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4149 VkImageLayout dstImageLayout, uint32_t regionCount,
4150 const VkBufferImageCopy *pRegions) {
4151 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
4152 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4153}
4154
4155void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4156 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4157 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4158 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4159 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4160 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4161}
4162
4163template <typename BufferImageCopyRegionType>
4164bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4165 VkBuffer dstBuffer, uint32_t regionCount,
4166 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004167 bool skip = false;
4168 const auto *cb_access_context = GetAccessContext(commandBuffer);
4169 assert(cb_access_context);
4170 if (!cb_access_context) return skip;
4171
Jeff Leger178b1e52020-10-05 12:22:23 -04004172 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4173 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
4174
locke-lunarga19c71d2020-03-02 18:17:04 -07004175 const auto *context = cb_access_context->GetCurrentAccessContext();
4176 assert(context);
4177 if (!context) return skip;
4178
4179 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4180 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4181 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
4182 for (uint32_t region = 0; region < regionCount; region++) {
4183 const auto &copy_region = pRegions[region];
4184 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004185 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004186 copy_region.imageOffset, copy_region.imageExtent);
4187 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004188 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004189 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004190 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004191 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004192 }
4193 }
4194 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06004195 ResourceAccessRange dst_range =
4196 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004197 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07004198 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004199 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004200 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004201 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004202 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004203 }
4204 }
4205 if (skip) break;
4206 }
4207 return skip;
4208}
4209
Jeff Leger178b1e52020-10-05 12:22:23 -04004210bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4211 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4212 const VkBufferImageCopy *pRegions) const {
4213 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
4214 COPY_COMMAND_VERSION_1);
4215}
4216
4217bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4218 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4219 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4220 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4221 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4222}
4223
4224template <typename BufferImageCopyRegionType>
4225void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4226 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
4227 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004228 auto *cb_access_context = GetAccessContext(commandBuffer);
4229 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004230
4231 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4232 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
4233
4234 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004235 auto *context = cb_access_context->GetCurrentAccessContext();
4236 assert(context);
4237
4238 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004239 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4240 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 -06004241 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004242
4243 for (uint32_t region = 0; region < regionCount; region++) {
4244 const auto &copy_region = pRegions[region];
4245 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06004246 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06004247 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004248 }
4249 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004250 ResourceAccessRange dst_range =
4251 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06004252 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004253 }
4254 }
4255}
4256
Jeff Leger178b1e52020-10-05 12:22:23 -04004257void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4258 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4259 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
4260 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4261}
4262
4263void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4264 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4265 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4266 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4267 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4268 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4269}
4270
4271template <typename RegionType>
4272bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4273 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4274 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004275 bool skip = false;
4276 const auto *cb_access_context = GetAccessContext(commandBuffer);
4277 assert(cb_access_context);
4278 if (!cb_access_context) return skip;
4279
4280 const auto *context = cb_access_context->GetCurrentAccessContext();
4281 assert(context);
4282 if (!context) return skip;
4283
4284 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4285 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4286
4287 for (uint32_t region = 0; region < regionCount; region++) {
4288 const auto &blit_region = pRegions[region];
4289 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004290 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4291 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4292 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4293 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4294 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4295 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
4296 auto hazard =
4297 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004298 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004299 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004300 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004301 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004302 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004303 }
4304 }
4305
4306 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004307 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4308 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4309 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4310 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4311 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4312 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
4313 auto hazard =
4314 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004315 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004316 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004317 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004318 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06004319 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004320 }
4321 if (skip) break;
4322 }
4323 }
4324
4325 return skip;
4326}
4327
Jeff Leger178b1e52020-10-05 12:22:23 -04004328bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4329 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4330 const VkImageBlit *pRegions, VkFilter filter) const {
4331 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4332 "vkCmdBlitImage");
4333}
4334
4335bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4336 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4337 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4338 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4339 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4340}
4341
4342template <typename RegionType>
4343void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4344 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4345 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004346 auto *cb_access_context = GetAccessContext(commandBuffer);
4347 assert(cb_access_context);
4348 auto *context = cb_access_context->GetCurrentAccessContext();
4349 assert(context);
4350
4351 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004352 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004353
4354 for (uint32_t region = 0; region < regionCount; region++) {
4355 const auto &blit_region = pRegions[region];
4356 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004357 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4358 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4359 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4360 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4361 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4362 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
4363 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004364 }
4365 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004366 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4367 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4368 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4369 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4370 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4371 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
4372 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004373 }
4374 }
4375}
locke-lunarg36ba2592020-04-03 09:42:04 -06004376
Jeff Leger178b1e52020-10-05 12:22:23 -04004377void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4378 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4379 const VkImageBlit *pRegions, VkFilter filter) {
4380 auto *cb_access_context = GetAccessContext(commandBuffer);
4381 assert(cb_access_context);
4382 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4383 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4384 pRegions, filter);
4385 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4386}
4387
4388void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4389 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4390 auto *cb_access_context = GetAccessContext(commandBuffer);
4391 assert(cb_access_context);
4392 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4393 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4394 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4395 pBlitImageInfo->filter, tag);
4396}
4397
locke-lunarg61870c22020-06-09 14:51:50 -06004398bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
4399 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
4400 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004401 bool skip = false;
4402 if (drawCount == 0) return skip;
4403
4404 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4405 VkDeviceSize size = struct_size;
4406 if (drawCount == 1 || stride == size) {
4407 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004408 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004409 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4410 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004411 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004412 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004413 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06004414 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004415 }
4416 } else {
4417 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004418 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004419 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4420 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004421 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004422 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4423 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
4424 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004425 break;
4426 }
4427 }
4428 }
4429 return skip;
4430}
4431
locke-lunarg61870c22020-06-09 14:51:50 -06004432void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
4433 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4434 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06004435 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4436 VkDeviceSize size = struct_size;
4437 if (drawCount == 1 || stride == size) {
4438 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004439 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004440 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
4441 } else {
4442 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004443 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004444 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
4445 }
4446 }
4447}
4448
locke-lunarg61870c22020-06-09 14:51:50 -06004449bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
4450 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004451 bool skip = false;
4452
4453 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004454 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004455 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4456 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004457 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004458 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004459 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06004460 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004461 }
4462 return skip;
4463}
4464
locke-lunarg61870c22020-06-09 14:51:50 -06004465void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06004466 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004467 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004468 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
4469}
4470
locke-lunarg36ba2592020-04-03 09:42:04 -06004471bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004472 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004473 const auto *cb_access_context = GetAccessContext(commandBuffer);
4474 assert(cb_access_context);
4475 if (!cb_access_context) return skip;
4476
locke-lunarg61870c22020-06-09 14:51:50 -06004477 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004478 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004479}
4480
4481void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004482 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004483 auto *cb_access_context = GetAccessContext(commandBuffer);
4484 assert(cb_access_context);
4485 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004486
locke-lunarg61870c22020-06-09 14:51:50 -06004487 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004488}
locke-lunarge1a67022020-04-29 00:15:36 -06004489
4490bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004491 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004492 const auto *cb_access_context = GetAccessContext(commandBuffer);
4493 assert(cb_access_context);
4494 if (!cb_access_context) return skip;
4495
4496 const auto *context = cb_access_context->GetCurrentAccessContext();
4497 assert(context);
4498 if (!context) return skip;
4499
locke-lunarg61870c22020-06-09 14:51:50 -06004500 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
4501 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
4502 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004503 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004504}
4505
4506void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004507 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004508 auto *cb_access_context = GetAccessContext(commandBuffer);
4509 assert(cb_access_context);
4510 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4511 auto *context = cb_access_context->GetCurrentAccessContext();
4512 assert(context);
4513
locke-lunarg61870c22020-06-09 14:51:50 -06004514 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4515 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004516}
4517
4518bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4519 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004520 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004521 const auto *cb_access_context = GetAccessContext(commandBuffer);
4522 assert(cb_access_context);
4523 if (!cb_access_context) return skip;
4524
locke-lunarg61870c22020-06-09 14:51:50 -06004525 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4526 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4527 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004528 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004529}
4530
4531void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4532 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004533 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004534 auto *cb_access_context = GetAccessContext(commandBuffer);
4535 assert(cb_access_context);
4536 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004537
locke-lunarg61870c22020-06-09 14:51:50 -06004538 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4539 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4540 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004541}
4542
4543bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4544 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004545 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004546 const auto *cb_access_context = GetAccessContext(commandBuffer);
4547 assert(cb_access_context);
4548 if (!cb_access_context) return skip;
4549
locke-lunarg61870c22020-06-09 14:51:50 -06004550 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4551 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4552 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004553 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004554}
4555
4556void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4557 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004558 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004559 auto *cb_access_context = GetAccessContext(commandBuffer);
4560 assert(cb_access_context);
4561 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004562
locke-lunarg61870c22020-06-09 14:51:50 -06004563 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4564 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4565 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004566}
4567
4568bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4569 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004570 bool skip = false;
4571 if (drawCount == 0) return skip;
4572
locke-lunargff255f92020-05-13 18:53:52 -06004573 const auto *cb_access_context = GetAccessContext(commandBuffer);
4574 assert(cb_access_context);
4575 if (!cb_access_context) return skip;
4576
4577 const auto *context = cb_access_context->GetCurrentAccessContext();
4578 assert(context);
4579 if (!context) return skip;
4580
locke-lunarg61870c22020-06-09 14:51:50 -06004581 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4582 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
4583 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
4584 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004585
4586 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4587 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4588 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004589 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004590 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004591}
4592
4593void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4594 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004595 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004596 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004597 auto *cb_access_context = GetAccessContext(commandBuffer);
4598 assert(cb_access_context);
4599 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4600 auto *context = cb_access_context->GetCurrentAccessContext();
4601 assert(context);
4602
locke-lunarg61870c22020-06-09 14:51:50 -06004603 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4604 cb_access_context->RecordDrawSubpassAttachment(tag);
4605 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004606
4607 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4608 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4609 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004610 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004611}
4612
4613bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4614 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004615 bool skip = false;
4616 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004617 const auto *cb_access_context = GetAccessContext(commandBuffer);
4618 assert(cb_access_context);
4619 if (!cb_access_context) return skip;
4620
4621 const auto *context = cb_access_context->GetCurrentAccessContext();
4622 assert(context);
4623 if (!context) return skip;
4624
locke-lunarg61870c22020-06-09 14:51:50 -06004625 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4626 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
4627 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
4628 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004629
4630 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4631 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4632 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004633 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004634 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004635}
4636
4637void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4638 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004639 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004640 auto *cb_access_context = GetAccessContext(commandBuffer);
4641 assert(cb_access_context);
4642 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4643 auto *context = cb_access_context->GetCurrentAccessContext();
4644 assert(context);
4645
locke-lunarg61870c22020-06-09 14:51:50 -06004646 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4647 cb_access_context->RecordDrawSubpassAttachment(tag);
4648 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004649
4650 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4651 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4652 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004653 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004654}
4655
4656bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4657 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4658 uint32_t stride, const char *function) const {
4659 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004660 const auto *cb_access_context = GetAccessContext(commandBuffer);
4661 assert(cb_access_context);
4662 if (!cb_access_context) return skip;
4663
4664 const auto *context = cb_access_context->GetCurrentAccessContext();
4665 assert(context);
4666 if (!context) return skip;
4667
locke-lunarg61870c22020-06-09 14:51:50 -06004668 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4669 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
4670 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
4671 function);
4672 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004673
4674 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4675 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4676 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004677 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004678 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004679}
4680
4681bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4682 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4683 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004684 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4685 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004686}
4687
4688void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4689 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4690 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004691 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4692 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004693 auto *cb_access_context = GetAccessContext(commandBuffer);
4694 assert(cb_access_context);
4695 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
4696 auto *context = cb_access_context->GetCurrentAccessContext();
4697 assert(context);
4698
locke-lunarg61870c22020-06-09 14:51:50 -06004699 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4700 cb_access_context->RecordDrawSubpassAttachment(tag);
4701 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4702 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004703
4704 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4705 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4706 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004707 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004708}
4709
4710bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4711 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4712 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004713 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4714 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004715}
4716
4717void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4718 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4719 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004720 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4721 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004722 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004723}
4724
4725bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4726 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4727 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004728 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4729 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004730}
4731
4732void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4733 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4734 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004735 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4736 stride);
locke-lunargff255f92020-05-13 18:53:52 -06004737 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4738}
4739
4740bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4741 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4742 uint32_t stride, const char *function) const {
4743 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004744 const auto *cb_access_context = GetAccessContext(commandBuffer);
4745 assert(cb_access_context);
4746 if (!cb_access_context) return skip;
4747
4748 const auto *context = cb_access_context->GetCurrentAccessContext();
4749 assert(context);
4750 if (!context) return skip;
4751
locke-lunarg61870c22020-06-09 14:51:50 -06004752 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4753 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
4754 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
4755 stride, function);
4756 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004757
4758 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4759 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4760 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004761 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004762 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004763}
4764
4765bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4766 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4767 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004768 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4769 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004770}
4771
4772void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4773 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4774 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004775 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4776 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004777 auto *cb_access_context = GetAccessContext(commandBuffer);
4778 assert(cb_access_context);
4779 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
4780 auto *context = cb_access_context->GetCurrentAccessContext();
4781 assert(context);
4782
locke-lunarg61870c22020-06-09 14:51:50 -06004783 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4784 cb_access_context->RecordDrawSubpassAttachment(tag);
4785 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4786 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004787
4788 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4789 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004790 // We will update the index and vertex buffer in SubmitQueue in the future.
4791 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004792}
4793
4794bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4795 VkDeviceSize offset, VkBuffer countBuffer,
4796 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4797 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004798 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4799 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004800}
4801
4802void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4803 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4804 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004805 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4806 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004807 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4808}
4809
4810bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4811 VkDeviceSize offset, VkBuffer countBuffer,
4812 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4813 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004814 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4815 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004816}
4817
4818void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4819 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4820 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004821 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4822 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06004823 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
4824}
4825
4826bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4827 const VkClearColorValue *pColor, uint32_t rangeCount,
4828 const VkImageSubresourceRange *pRanges) const {
4829 bool skip = false;
4830 const auto *cb_access_context = GetAccessContext(commandBuffer);
4831 assert(cb_access_context);
4832 if (!cb_access_context) return skip;
4833
4834 const auto *context = cb_access_context->GetCurrentAccessContext();
4835 assert(context);
4836 if (!context) return skip;
4837
4838 const auto *image_state = Get<IMAGE_STATE>(image);
4839
4840 for (uint32_t index = 0; index < rangeCount; index++) {
4841 const auto &range = pRanges[index];
4842 if (image_state) {
4843 auto hazard =
4844 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4845 if (hazard.hazard) {
4846 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004847 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004848 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004849 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004850 }
4851 }
4852 }
4853 return skip;
4854}
4855
4856void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4857 const VkClearColorValue *pColor, uint32_t rangeCount,
4858 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004859 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004860 auto *cb_access_context = GetAccessContext(commandBuffer);
4861 assert(cb_access_context);
4862 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4863 auto *context = cb_access_context->GetCurrentAccessContext();
4864 assert(context);
4865
4866 const auto *image_state = Get<IMAGE_STATE>(image);
4867
4868 for (uint32_t index = 0; index < rangeCount; index++) {
4869 const auto &range = pRanges[index];
4870 if (image_state) {
4871 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4872 tag);
4873 }
4874 }
4875}
4876
4877bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4878 VkImageLayout imageLayout,
4879 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4880 const VkImageSubresourceRange *pRanges) const {
4881 bool skip = false;
4882 const auto *cb_access_context = GetAccessContext(commandBuffer);
4883 assert(cb_access_context);
4884 if (!cb_access_context) return skip;
4885
4886 const auto *context = cb_access_context->GetCurrentAccessContext();
4887 assert(context);
4888 if (!context) return skip;
4889
4890 const auto *image_state = Get<IMAGE_STATE>(image);
4891
4892 for (uint32_t index = 0; index < rangeCount; index++) {
4893 const auto &range = pRanges[index];
4894 if (image_state) {
4895 auto hazard =
4896 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
4897 if (hazard.hazard) {
4898 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004899 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004900 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06004901 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004902 }
4903 }
4904 }
4905 return skip;
4906}
4907
4908void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4909 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4910 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004911 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004912 auto *cb_access_context = GetAccessContext(commandBuffer);
4913 assert(cb_access_context);
4914 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4915 auto *context = cb_access_context->GetCurrentAccessContext();
4916 assert(context);
4917
4918 const auto *image_state = Get<IMAGE_STATE>(image);
4919
4920 for (uint32_t index = 0; index < rangeCount; index++) {
4921 const auto &range = pRanges[index];
4922 if (image_state) {
4923 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
4924 tag);
4925 }
4926 }
4927}
4928
4929bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4930 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4931 VkDeviceSize dstOffset, VkDeviceSize stride,
4932 VkQueryResultFlags flags) const {
4933 bool skip = false;
4934 const auto *cb_access_context = GetAccessContext(commandBuffer);
4935 assert(cb_access_context);
4936 if (!cb_access_context) return skip;
4937
4938 const auto *context = cb_access_context->GetCurrentAccessContext();
4939 assert(context);
4940 if (!context) return skip;
4941
4942 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4943
4944 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004945 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004946 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4947 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004948 skip |=
4949 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4950 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
4951 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004952 }
4953 }
locke-lunargff255f92020-05-13 18:53:52 -06004954
4955 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004956 return skip;
4957}
4958
4959void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4960 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4961 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004962 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4963 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004964 auto *cb_access_context = GetAccessContext(commandBuffer);
4965 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004966 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004967 auto *context = cb_access_context->GetCurrentAccessContext();
4968 assert(context);
4969
4970 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4971
4972 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004973 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06004974 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
4975 }
locke-lunargff255f92020-05-13 18:53:52 -06004976
4977 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004978}
4979
4980bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4981 VkDeviceSize size, uint32_t data) const {
4982 bool skip = false;
4983 const auto *cb_access_context = GetAccessContext(commandBuffer);
4984 assert(cb_access_context);
4985 if (!cb_access_context) return skip;
4986
4987 const auto *context = cb_access_context->GetCurrentAccessContext();
4988 assert(context);
4989 if (!context) return skip;
4990
4991 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4992
4993 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004994 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06004995 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
4996 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004997 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004998 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06004999 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005000 }
5001 }
5002 return skip;
5003}
5004
5005void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5006 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005007 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06005008 auto *cb_access_context = GetAccessContext(commandBuffer);
5009 assert(cb_access_context);
5010 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
5011 auto *context = cb_access_context->GetCurrentAccessContext();
5012 assert(context);
5013
5014 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5015
5016 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005017 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
locke-lunarge1a67022020-04-29 00:15:36 -06005018 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
5019 }
5020}
5021
5022bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5023 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5024 const VkImageResolve *pRegions) const {
5025 bool skip = false;
5026 const auto *cb_access_context = GetAccessContext(commandBuffer);
5027 assert(cb_access_context);
5028 if (!cb_access_context) return skip;
5029
5030 const auto *context = cb_access_context->GetCurrentAccessContext();
5031 assert(context);
5032 if (!context) return skip;
5033
5034 const auto *src_image = Get<IMAGE_STATE>(srcImage);
5035 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
5036
5037 for (uint32_t region = 0; region < regionCount; region++) {
5038 const auto &resolve_region = pRegions[region];
5039 if (src_image) {
5040 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5041 resolve_region.srcOffset, resolve_region.extent);
5042 if (hazard.hazard) {
5043 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005044 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005045 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06005046 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005047 }
5048 }
5049
5050 if (dst_image) {
5051 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5052 resolve_region.dstOffset, resolve_region.extent);
5053 if (hazard.hazard) {
5054 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005055 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005056 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06005057 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005058 }
5059 if (skip) break;
5060 }
5061 }
5062
5063 return skip;
5064}
5065
5066void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5067 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5068 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005069 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
5070 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06005071 auto *cb_access_context = GetAccessContext(commandBuffer);
5072 assert(cb_access_context);
5073 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
5074 auto *context = cb_access_context->GetCurrentAccessContext();
5075 assert(context);
5076
5077 auto *src_image = Get<IMAGE_STATE>(srcImage);
5078 auto *dst_image = Get<IMAGE_STATE>(dstImage);
5079
5080 for (uint32_t region = 0; region < regionCount; region++) {
5081 const auto &resolve_region = pRegions[region];
5082 if (src_image) {
5083 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5084 resolve_region.srcOffset, resolve_region.extent, tag);
5085 }
5086 if (dst_image) {
5087 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5088 resolve_region.dstOffset, resolve_region.extent, tag);
5089 }
5090 }
5091}
5092
Jeff Leger178b1e52020-10-05 12:22:23 -04005093bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5094 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
5095 bool skip = false;
5096 const auto *cb_access_context = GetAccessContext(commandBuffer);
5097 assert(cb_access_context);
5098 if (!cb_access_context) return skip;
5099
5100 const auto *context = cb_access_context->GetCurrentAccessContext();
5101 assert(context);
5102 if (!context) return skip;
5103
5104 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5105 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5106
5107 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5108 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5109 if (src_image) {
5110 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5111 resolve_region.srcOffset, resolve_region.extent);
5112 if (hazard.hazard) {
5113 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
5114 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
5115 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
5116 region, string_UsageTag(hazard).c_str());
5117 }
5118 }
5119
5120 if (dst_image) {
5121 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5122 resolve_region.dstOffset, resolve_region.extent);
5123 if (hazard.hazard) {
5124 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
5125 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
5126 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
5127 region, string_UsageTag(hazard).c_str());
5128 }
5129 if (skip) break;
5130 }
5131 }
5132
5133 return skip;
5134}
5135
5136void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5137 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5138 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5139 auto *cb_access_context = GetAccessContext(commandBuffer);
5140 assert(cb_access_context);
5141 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
5142 auto *context = cb_access_context->GetCurrentAccessContext();
5143 assert(context);
5144
5145 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5146 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5147
5148 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5149 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5150 if (src_image) {
5151 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
5152 resolve_region.srcOffset, resolve_region.extent, tag);
5153 }
5154 if (dst_image) {
5155 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
5156 resolve_region.dstOffset, resolve_region.extent, tag);
5157 }
5158 }
5159}
5160
locke-lunarge1a67022020-04-29 00:15:36 -06005161bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5162 VkDeviceSize dataSize, const void *pData) const {
5163 bool skip = false;
5164 const auto *cb_access_context = GetAccessContext(commandBuffer);
5165 assert(cb_access_context);
5166 if (!cb_access_context) return skip;
5167
5168 const auto *context = cb_access_context->GetCurrentAccessContext();
5169 assert(context);
5170 if (!context) return skip;
5171
5172 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5173
5174 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005175 // VK_WHOLE_SIZE not allowed
5176 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06005177 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
5178 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005179 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005180 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06005181 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005182 }
5183 }
5184 return skip;
5185}
5186
5187void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5188 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005189 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005190 auto *cb_access_context = GetAccessContext(commandBuffer);
5191 assert(cb_access_context);
5192 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5193 auto *context = cb_access_context->GetCurrentAccessContext();
5194 assert(context);
5195
5196 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5197
5198 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005199 // VK_WHOLE_SIZE not allowed
5200 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
locke-lunarge1a67022020-04-29 00:15:36 -06005201 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
5202 }
5203}
locke-lunargff255f92020-05-13 18:53:52 -06005204
5205bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5206 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5207 bool skip = false;
5208 const auto *cb_access_context = GetAccessContext(commandBuffer);
5209 assert(cb_access_context);
5210 if (!cb_access_context) return skip;
5211
5212 const auto *context = cb_access_context->GetCurrentAccessContext();
5213 assert(context);
5214 if (!context) return skip;
5215
5216 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5217
5218 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005219 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06005220 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
5221 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005222 skip |=
5223 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5224 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
5225 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005226 }
5227 }
5228 return skip;
5229}
5230
5231void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5232 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005233 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005234 auto *cb_access_context = GetAccessContext(commandBuffer);
5235 assert(cb_access_context);
5236 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5237 auto *context = cb_access_context->GetCurrentAccessContext();
5238 assert(context);
5239
5240 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5241
5242 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005243 const ResourceAccessRange range = MakeRange(dstOffset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06005244 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
5245 }
5246}
John Zulauf49beb112020-11-04 16:06:31 -07005247
5248bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5249 bool skip = false;
5250 const auto *cb_context = GetAccessContext(commandBuffer);
5251 assert(cb_context);
5252 if (!cb_context) return skip;
5253
5254 return cb_context->ValidateSetEvent(commandBuffer, event, stageMask);
5255}
5256
5257void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5258 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5259 auto *cb_context = GetAccessContext(commandBuffer);
5260 assert(cb_context);
5261 if (!cb_context) return;
John Zulauf4a6105a2020-11-17 15:11:05 -07005262 const auto tag = cb_context->NextCommandTag(CMD_SETEVENT);
5263 cb_context->RecordSetEvent(commandBuffer, event, stageMask, tag);
John Zulauf49beb112020-11-04 16:06:31 -07005264}
5265
5266bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5267 VkPipelineStageFlags stageMask) const {
5268 bool skip = false;
5269 const auto *cb_context = GetAccessContext(commandBuffer);
5270 assert(cb_context);
5271 if (!cb_context) return skip;
5272
5273 return cb_context->ValidateResetEvent(commandBuffer, event, stageMask);
5274}
5275
5276void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5277 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5278 auto *cb_context = GetAccessContext(commandBuffer);
5279 assert(cb_context);
5280 if (!cb_context) return;
5281
5282 cb_context->RecordResetEvent(commandBuffer, event, stageMask);
5283}
5284
5285bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5286 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5287 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5288 uint32_t bufferMemoryBarrierCount,
5289 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5290 uint32_t imageMemoryBarrierCount,
5291 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5292 bool skip = false;
5293 const auto *cb_context = GetAccessContext(commandBuffer);
5294 assert(cb_context);
5295 if (!cb_context) return skip;
5296
John Zulauf4a6105a2020-11-17 15:11:05 -07005297 return cb_context->ValidateWaitEvents(eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount, pMemoryBarriers,
5298 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
John Zulauf49beb112020-11-04 16:06:31 -07005299 pImageMemoryBarriers);
5300}
5301
5302void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5303 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5304 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5305 uint32_t bufferMemoryBarrierCount,
5306 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5307 uint32_t imageMemoryBarrierCount,
5308 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5309 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5310 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5311 imageMemoryBarrierCount, pImageMemoryBarriers);
5312
5313 auto *cb_context = GetAccessContext(commandBuffer);
5314 assert(cb_context);
5315 if (!cb_context) return;
5316
John Zulauf4a6105a2020-11-17 15:11:05 -07005317 const auto tag = cb_context->NextCommandTag(CMD_WAITEVENTS);
John Zulauf49beb112020-11-04 16:06:31 -07005318 cb_context->RecordWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5319 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
John Zulauf4a6105a2020-11-17 15:11:05 -07005320 pImageMemoryBarriers, tag);
5321}
5322
5323void SyncEventState::ResetFirstScope() {
5324 for (const auto address_type : kAddressTypes) {
5325 first_scope[static_cast<size_t>(address_type)].clear();
5326 }
5327 stage_mask = 0U;
5328 exec_scope = 0U;
5329 stage_accesses.reset();
5330}
5331
5332// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
5333SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(VkPipelineStageFlags srcStageMask) const {
5334 IgnoreReason reason = NotIgnored;
5335
5336 if (last_command == CMD_RESETEVENT && !HasBarrier(0U, 0U)) {
5337 reason = ResetWaitRace;
5338 } else if (unsynchronized_set) {
5339 reason = SetRace;
5340 } else {
5341 const VkPipelineStageFlags missing_bits = stage_mask_param & ~srcStageMask;
5342 if (missing_bits) reason = MissingStageBits;
5343 }
5344
5345 return reason;
5346}
5347
5348bool SyncEventState::HasBarrier(VkPipelineStageFlags stageMask, VkPipelineStageFlags exec_scope_arg) const {
5349 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5350 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5351 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005352}