blob: 273b7abac4932e906e4cdfe5317cbe5f8f6d4ff7 [file] [log] [blame]
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001/* Copyright (c) 2019-2022 The Khronos Group Inc.
2 * Copyright (c) 2019-2022 Valve Corporation
3 * Copyright (c) 2019-2022 LunarG, Inc.
John Zulauf9cb530d2019-09-30 14:14:10 -06004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: John Zulauf <jzulauf@lunarg.com>
John Zulaufab7756b2020-12-29 16:10:16 -070018 * Author: Locke Lin <locke@lunarg.com>
19 * Author: Jeremy Gebben <jeremyg@lunarg.com>
John Zulauf9cb530d2019-09-30 14:14:10 -060020 */
21
22#include <limits>
23#include <vector>
locke-lunarg296a3c92020-03-25 01:04:29 -060024#include <memory>
25#include <bitset>
John Zulauf9cb530d2019-09-30 14:14:10 -060026#include "synchronization_validation.h"
Jeremy Gebben5f585ae2021-02-02 09:03:06 -070027#include "sync_utils.h"
John Zulauf9cb530d2019-09-30 14:14:10 -060028
Jeremy Gebben6fbf8242021-06-21 09:14:46 -060029static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.Binding(); }
John Zulauf264cce02021-02-05 14:40:47 -070030
John Zulauf29d00532021-03-04 13:28:54 -070031static bool SimpleBinding(const IMAGE_STATE &image_state) {
Jeremy Gebben62c3bf42021-07-21 15:38:24 -060032 bool simple =
Jeremy Gebben82e11d52021-07-26 09:19:37 -060033 SimpleBinding(static_cast<const BINDABLE &>(image_state)) || image_state.IsSwapchainImage() || image_state.bind_swapchain;
John Zulauf29d00532021-03-04 13:28:54 -070034
35 // If it's not simple we must have an encoder.
36 assert(!simple || image_state.fragment_encoder.get());
37 return simple;
38}
39
John Zulauf4fa68462021-04-26 21:04:22 -060040static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
41static const std::array<AccessAddressType, static_cast<size_t>(AccessAddressType::kTypeCount)> kAddressTypes = {
John Zulauf43cc7462020-12-03 12:33:12 -070042 AccessAddressType::kLinear, AccessAddressType::kIdealized};
43
John Zulaufd5115702021-01-18 12:34:33 -070044static constexpr AccessAddressType GetAccessAddressType(const BUFFER_STATE &) { return AccessAddressType::kLinear; };
John Zulauf264cce02021-02-05 14:40:47 -070045static AccessAddressType GetAccessAddressType(const IMAGE_STATE &image) {
46 return SimpleBinding(image) ? AccessContext::ImageAddressType(image) : AccessAddressType::kIdealized;
47}
John Zulaufd5115702021-01-18 12:34:33 -070048
John Zulauf9cb530d2019-09-30 14:14:10 -060049static const char *string_SyncHazardVUID(SyncHazard hazard) {
50 switch (hazard) {
51 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070052 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060053 break;
54 case SyncHazard::READ_AFTER_WRITE:
55 return "SYNC-HAZARD-READ_AFTER_WRITE";
56 break;
57 case SyncHazard::WRITE_AFTER_READ:
58 return "SYNC-HAZARD-WRITE_AFTER_READ";
59 break;
60 case SyncHazard::WRITE_AFTER_WRITE:
61 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
62 break;
John Zulauf2f952d22020-02-10 11:34:51 -070063 case SyncHazard::READ_RACING_WRITE:
64 return "SYNC-HAZARD-READ-RACING-WRITE";
65 break;
66 case SyncHazard::WRITE_RACING_WRITE:
67 return "SYNC-HAZARD-WRITE-RACING-WRITE";
68 break;
69 case SyncHazard::WRITE_RACING_READ:
70 return "SYNC-HAZARD-WRITE-RACING-READ";
71 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060072 default:
73 assert(0);
74 }
75 return "SYNC-HAZARD-INVALID";
76}
77
John Zulauf59e25072020-07-17 10:55:21 -060078static bool IsHazardVsRead(SyncHazard hazard) {
79 switch (hazard) {
80 case SyncHazard::NONE:
81 return false;
82 break;
83 case SyncHazard::READ_AFTER_WRITE:
84 return false;
85 break;
86 case SyncHazard::WRITE_AFTER_READ:
87 return true;
88 break;
89 case SyncHazard::WRITE_AFTER_WRITE:
90 return false;
91 break;
92 case SyncHazard::READ_RACING_WRITE:
93 return false;
94 break;
95 case SyncHazard::WRITE_RACING_WRITE:
96 return false;
97 break;
98 case SyncHazard::WRITE_RACING_READ:
99 return true;
100 break;
101 default:
102 assert(0);
103 }
104 return false;
105}
106
John Zulauf9cb530d2019-09-30 14:14:10 -0600107static const char *string_SyncHazard(SyncHazard hazard) {
108 switch (hazard) {
109 case SyncHazard::NONE:
110 return "NONR";
111 break;
112 case SyncHazard::READ_AFTER_WRITE:
113 return "READ_AFTER_WRITE";
114 break;
115 case SyncHazard::WRITE_AFTER_READ:
116 return "WRITE_AFTER_READ";
117 break;
118 case SyncHazard::WRITE_AFTER_WRITE:
119 return "WRITE_AFTER_WRITE";
120 break;
John Zulauf2f952d22020-02-10 11:34:51 -0700121 case SyncHazard::READ_RACING_WRITE:
122 return "READ_RACING_WRITE";
123 break;
124 case SyncHazard::WRITE_RACING_WRITE:
125 return "WRITE_RACING_WRITE";
126 break;
127 case SyncHazard::WRITE_RACING_READ:
128 return "WRITE_RACING_READ";
129 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600130 default:
131 assert(0);
132 }
133 return "INVALID HAZARD";
134}
135
John Zulauf37ceaed2020-07-03 16:18:15 -0600136static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
137 // Return the info for the first bit found
138 const SyncStageAccessInfoType *info = nullptr;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700139 for (size_t i = 0; i < flags.size(); i++) {
140 if (flags.test(i)) {
141 info = &syncStageAccessInfoByStageAccessIndex[i];
142 break;
John Zulauf37ceaed2020-07-03 16:18:15 -0600143 }
144 }
145 return info;
146}
147
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700148static std::string string_SyncStageAccessFlags(const SyncStageAccessFlags &flags, const char *sep = "|") {
John Zulauf59e25072020-07-17 10:55:21 -0600149 std::string out_str;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700150 if (flags.none()) {
John Zulauf389c34b2020-07-28 11:19:35 -0600151 out_str = "0";
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700152 } else {
153 for (size_t i = 0; i < syncStageAccessInfoByStageAccessIndex.size(); i++) {
154 const auto &info = syncStageAccessInfoByStageAccessIndex[i];
155 if ((flags & info.stage_access_bit).any()) {
156 if (!out_str.empty()) {
157 out_str.append(sep);
158 }
159 out_str.append(info.name);
John Zulauf59e25072020-07-17 10:55:21 -0600160 }
John Zulauf59e25072020-07-17 10:55:21 -0600161 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700162 if (out_str.length() == 0) {
163 out_str.append("Unhandled SyncStageAccess");
164 }
John Zulauf59e25072020-07-17 10:55:21 -0600165 }
166 return out_str;
167}
168
John Zulauf14940722021-04-12 15:19:02 -0600169static std::string string_UsageTag(const ResourceUsageRecord &tag) {
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700170 std::stringstream out;
171
John Zulauffaea0ee2021-01-14 14:01:32 -0700172 out << "command: " << CommandTypeString(tag.command);
173 out << ", seq_no: " << tag.seq_num;
174 if (tag.sub_command != 0) {
175 out << ", subcmd: " << tag.sub_command;
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700176 }
177 return out.str();
178}
John Zulauf4fa68462021-04-26 21:04:22 -0600179static std::string string_UsageIndex(SyncStageAccessIndex usage_index) {
180 const char *stage_access_name = "INVALID_STAGE_ACCESS";
181 if (usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size())) {
182 stage_access_name = syncStageAccessInfoByStageAccessIndex[usage_index].name;
183 }
184 return std::string(stage_access_name);
185}
186
187struct NoopBarrierAction {
188 explicit NoopBarrierAction() {}
189 void operator()(ResourceAccessState *access) const {}
John Zulauf5c628d02021-05-04 15:46:36 -0600190 const bool layout_transition = false;
John Zulauf4fa68462021-04-26 21:04:22 -0600191};
192
193// NOTE: Make sure the proxy doesn't outlive from, as the proxy is pointing directly to access contexts owned by from.
194CommandBufferAccessContext::CommandBufferAccessContext(const CommandBufferAccessContext &from, AsProxyContext dummy)
195 : CommandBufferAccessContext(from.sync_state_) {
196 // Copy only the needed fields out of from for a temporary, proxy command buffer context
197 cb_state_ = from.cb_state_;
198 queue_flags_ = from.queue_flags_;
199 destroyed_ = from.destroyed_;
200 access_log_ = from.access_log_; // potentially large, but no choice given tagging lookup.
201 command_number_ = from.command_number_;
202 subcommand_number_ = from.subcommand_number_;
203 reset_count_ = from.reset_count_;
204
205 const auto *from_context = from.GetCurrentAccessContext();
206 assert(from_context);
207
208 // Construct a fully resolved single access context out of from
209 const NoopBarrierAction noop_barrier;
210 for (AccessAddressType address_type : kAddressTypes) {
211 from_context->ResolveAccessRange(address_type, kFullRange, noop_barrier,
212 &cb_access_context_.GetAccessStateMap(address_type), nullptr);
213 }
214 // The proxy has flatten the current render pass context (if any), but the async contexts are needed for hazard detection
215 cb_access_context_.ImportAsyncContexts(*from_context);
216
217 events_context_ = from.events_context_;
218
219 // We don't want to copy the full render_pass_context_ history just for the proxy.
220}
221
222std::string CommandBufferAccessContext::FormatUsage(const ResourceUsageTag tag) const {
223 std::stringstream out;
224 assert(tag < access_log_.size());
225 const auto &record = access_log_[tag];
226 out << string_UsageTag(record);
227 if (record.cb_state != cb_state_.get()) {
228 out << ", command_buffer: " << sync_state_->report_data->FormatHandle(record.cb_state->commandBuffer()).c_str();
229 if (record.cb_state->Destroyed()) {
230 out << " (destroyed)";
231 }
232
John Zulauf3c2a0b32021-07-14 11:14:52 -0600233 out << ", reset_no: " << std::to_string(record.reset_count);
John Zulauf4fa68462021-04-26 21:04:22 -0600234 } else {
235 out << ", reset_no: " << std::to_string(reset_count_);
236 }
237 return out.str();
238}
239std::string CommandBufferAccessContext::FormatUsage(const ResourceFirstAccess &access) const {
240 std::stringstream out;
241 out << "(recorded_usage: " << string_UsageIndex(access.usage_index);
242 out << ", " << FormatUsage(access.tag) << ")";
243 return out.str();
244}
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700245
John Zulauffaea0ee2021-01-14 14:01:32 -0700246std::string CommandBufferAccessContext::FormatUsage(const HazardResult &hazard) const {
John Zulauf37ceaed2020-07-03 16:18:15 -0600247 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600248 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
249 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600250 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600251 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
252 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf4fa68462021-04-26 21:04:22 -0600253 out << "(";
254 if (!hazard.recorded_access.get()) {
255 // if we have a recorded usage the usage is reported from the recorded contexts point of view
256 out << "usage: " << usage_info.name << ", ";
257 }
258 out << "prior_usage: " << stage_access_name;
John Zulauf59e25072020-07-17 10:55:21 -0600259 if (IsHazardVsRead(hazard.hazard)) {
260 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
Jeremy Gebben40a22942020-12-22 14:22:06 -0700261 out << ", read_barriers: " << string_VkPipelineStageFlags2KHR(barriers);
John Zulauf59e25072020-07-17 10:55:21 -0600262 } else {
263 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
264 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
265 }
266
John Zulauf14940722021-04-12 15:19:02 -0600267 assert(tag < access_log_.size());
John Zulauf4fa68462021-04-26 21:04:22 -0600268 out << ", " << FormatUsage(tag) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600269 return out.str();
270}
271
John Zulaufd14743a2020-07-03 09:42:39 -0600272// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
273// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
274// also reflects this special case for read hazard detection (using access instead of exec scope)
Jeremy Gebben40a22942020-12-22 14:22:06 -0700275static constexpr VkPipelineStageFlags2KHR kColorAttachmentExecScope = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700276static const SyncStageAccessFlags kColorAttachmentAccessScope =
277 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
278 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
279 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
280 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebben40a22942020-12-22 14:22:06 -0700281static constexpr VkPipelineStageFlags2KHR kDepthStencilAttachmentExecScope =
282 VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT_KHR | VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700283static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
284 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
285 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
286 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -0700287static constexpr VkPipelineStageFlags2KHR kRasterAttachmentExecScope = kDepthStencilAttachmentExecScope | kColorAttachmentExecScope;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700288static const SyncStageAccessFlags kRasterAttachmentAccessScope = kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope;
John Zulaufb027cdb2020-05-21 14:25:22 -0600289
John Zulauf8e3c3e92021-01-06 11:19:36 -0700290ResourceAccessState::OrderingBarriers ResourceAccessState::kOrderingRules = {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700291 {{VK_PIPELINE_STAGE_2_NONE_KHR, SyncStageAccessFlags()},
John Zulauf8e3c3e92021-01-06 11:19:36 -0700292 {kColorAttachmentExecScope, kColorAttachmentAccessScope},
293 {kDepthStencilAttachmentExecScope, kDepthStencilAttachmentAccessScope},
294 {kRasterAttachmentExecScope, kRasterAttachmentAccessScope}}};
295
John Zulauf7635de32020-05-29 17:14:15 -0600296// Sometimes we have an internal access conflict, and we using the kCurrentCommandTag to set and detect in temporary/proxy contexts
John Zulauf14940722021-04-12 15:19:02 -0600297static const ResourceUsageTag kCurrentCommandTag(ResourceUsageRecord::kMaxIndex);
John Zulaufb027cdb2020-05-21 14:25:22 -0600298
Jeremy Gebben62c3bf42021-07-21 15:38:24 -0600299static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) { return bindable.GetFakeBaseAddress(); }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600300
locke-lunarg3c038002020-04-30 23:08:08 -0600301inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
302 if (size == VK_WHOLE_SIZE) {
303 return (whole_size - offset);
304 }
305 return size;
306}
307
John Zulauf3e86bf02020-09-12 10:47:57 -0600308static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
309 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
310}
311
John Zulauf16adfc92020-04-08 10:28:33 -0600312template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600313static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600314 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
315}
316
John Zulauf355e49b2020-04-24 15:11:15 -0600317static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600318
John Zulauf3e86bf02020-09-12 10:47:57 -0600319static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
320 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
321}
322
323static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
324 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
325}
326
John Zulauf4a6105a2020-11-17 15:11:05 -0700327// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
328//
John Zulauf10f1f522020-12-18 12:00:35 -0700329// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
330//
John Zulauf4a6105a2020-11-17 15:11:05 -0700331// Usage:
332// Constructor() -- initializes the generator to point to the begin of the space declared.
333// * -- the current range of the generator empty signfies end
334// ++ -- advance to the next non-empty range (or end)
335
336// A wrapper for a single range with the same semantics as the actual generators below
337template <typename KeyType>
338class SingleRangeGenerator {
339 public:
340 SingleRangeGenerator(const KeyType &range) : current_(range) {}
John Zulaufd5115702021-01-18 12:34:33 -0700341 const KeyType &operator*() const { return current_; }
342 const KeyType *operator->() const { return &current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700343 SingleRangeGenerator &operator++() {
344 current_ = KeyType(); // just one real range
345 return *this;
346 }
347
348 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
349
350 private:
351 SingleRangeGenerator() = default;
352 const KeyType range_;
353 KeyType current_;
354};
355
John Zulaufae842002021-04-15 18:20:55 -0600356// Generate the ranges that are the intersection of range and the entries in the RangeMap
357template <typename RangeMap, typename KeyType = typename RangeMap::key_type>
358class MapRangesRangeGenerator {
John Zulauf4a6105a2020-11-17 15:11:05 -0700359 public:
John Zulaufd5115702021-01-18 12:34:33 -0700360 // Default constructed is safe to dereference for "empty" test, but for no other operation.
John Zulaufae842002021-04-15 18:20:55 -0600361 MapRangesRangeGenerator() : range_(), map_(nullptr), map_pos_(), current_() {
John Zulaufd5115702021-01-18 12:34:33 -0700362 // Default construction for KeyType *must* be empty range
363 assert(current_.empty());
364 }
John Zulaufae842002021-04-15 18:20:55 -0600365 MapRangesRangeGenerator(const RangeMap &filter, const KeyType &range) : range_(range), map_(&filter), map_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700366 SeekBegin();
367 }
John Zulaufae842002021-04-15 18:20:55 -0600368 MapRangesRangeGenerator(const MapRangesRangeGenerator &from) = default;
John Zulaufd5115702021-01-18 12:34:33 -0700369
John Zulauf4a6105a2020-11-17 15:11:05 -0700370 const KeyType &operator*() const { return current_; }
371 const KeyType *operator->() const { return &current_; }
John Zulaufae842002021-04-15 18:20:55 -0600372 MapRangesRangeGenerator &operator++() {
373 ++map_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700374 UpdateCurrent();
375 return *this;
376 }
377
John Zulaufae842002021-04-15 18:20:55 -0600378 bool operator==(const MapRangesRangeGenerator &other) const { return current_ == other.current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700379
John Zulaufae842002021-04-15 18:20:55 -0600380 protected:
John Zulauf4a6105a2020-11-17 15:11:05 -0700381 void UpdateCurrent() {
John Zulaufae842002021-04-15 18:20:55 -0600382 if (map_pos_ != map_->cend()) {
383 current_ = range_ & map_pos_->first;
John Zulauf4a6105a2020-11-17 15:11:05 -0700384 } else {
385 current_ = KeyType();
386 }
387 }
388 void SeekBegin() {
John Zulaufae842002021-04-15 18:20:55 -0600389 map_pos_ = map_->lower_bound(range_);
John Zulauf4a6105a2020-11-17 15:11:05 -0700390 UpdateCurrent();
391 }
John Zulaufae842002021-04-15 18:20:55 -0600392
393 // Adding this functionality here, to avoid gratuitous Base:: qualifiers in the derived class
394 // Note: Not exposed in this classes public interface to encourage using a consistent ++/empty generator semantic
395 template <typename Pred>
396 MapRangesRangeGenerator &PredicatedIncrement(Pred &pred) {
397 do {
398 ++map_pos_;
399 } while (map_pos_ != map_->cend() && map_pos_->first.intersects(range_) && !pred(map_pos_));
400 UpdateCurrent();
401 return *this;
402 }
403
John Zulauf4a6105a2020-11-17 15:11:05 -0700404 const KeyType range_;
John Zulaufae842002021-04-15 18:20:55 -0600405 const RangeMap *map_;
406 typename RangeMap::const_iterator map_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700407 KeyType current_;
408};
John Zulaufd5115702021-01-18 12:34:33 -0700409using SingleAccessRangeGenerator = SingleRangeGenerator<ResourceAccessRange>;
John Zulaufae842002021-04-15 18:20:55 -0600410using EventSimpleRangeGenerator = MapRangesRangeGenerator<SyncEventState::ScopeMap>;
John Zulauf4a6105a2020-11-17 15:11:05 -0700411
John Zulaufae842002021-04-15 18:20:55 -0600412// Generate the ranges for entries meeting the predicate that are the intersection of range and the entries in the RangeMap
413template <typename RangeMap, typename Predicate, typename KeyType = typename RangeMap::key_type>
414class PredicatedMapRangesRangeGenerator : public MapRangesRangeGenerator<RangeMap, KeyType> {
415 public:
416 using Base = MapRangesRangeGenerator<RangeMap, KeyType>;
417 // Default constructed is safe to dereference for "empty" test, but for no other operation.
418 PredicatedMapRangesRangeGenerator() : Base(), pred_() {}
419 PredicatedMapRangesRangeGenerator(const RangeMap &filter, const KeyType &range, Predicate pred)
420 : Base(filter, range), pred_(pred) {}
421 PredicatedMapRangesRangeGenerator(const PredicatedMapRangesRangeGenerator &from) = default;
422
423 PredicatedMapRangesRangeGenerator &operator++() {
424 Base::PredicatedIncrement(pred_);
425 return *this;
426 }
427
428 protected:
429 Predicate pred_;
430};
John Zulauf4a6105a2020-11-17 15:11:05 -0700431
432// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulaufae842002021-04-15 18:20:55 -0600433// Templated to allow for different Range generators or map sources...
434template <typename RangeMap, typename RangeGen, typename KeyType = typename RangeMap::key_type>
John Zulauf4a6105a2020-11-17 15:11:05 -0700435class FilteredGeneratorGenerator {
436 public:
John Zulaufd5115702021-01-18 12:34:33 -0700437 // Default constructed is safe to dereference for "empty" test, but for no other operation.
438 FilteredGeneratorGenerator() : filter_(nullptr), gen_(), filter_pos_(), current_() {
439 // Default construction for KeyType *must* be empty range
440 assert(current_.empty());
441 }
John Zulaufae842002021-04-15 18:20:55 -0600442 FilteredGeneratorGenerator(const RangeMap &filter, RangeGen &gen) : filter_(&filter), gen_(gen), filter_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700443 SeekBegin();
444 }
John Zulaufd5115702021-01-18 12:34:33 -0700445 FilteredGeneratorGenerator(const FilteredGeneratorGenerator &from) = default;
John Zulauf4a6105a2020-11-17 15:11:05 -0700446 const KeyType &operator*() const { return current_; }
447 const KeyType *operator->() const { return &current_; }
448 FilteredGeneratorGenerator &operator++() {
449 KeyType gen_range = GenRange();
450 KeyType filter_range = FilterRange();
451 current_ = KeyType();
452 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
453 if (gen_range.end > filter_range.end) {
454 // if the generated range is beyond the filter_range, advance the filter range
455 filter_range = AdvanceFilter();
456 } else {
457 gen_range = AdvanceGen();
458 }
459 current_ = gen_range & filter_range;
460 }
461 return *this;
462 }
463
464 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
465
466 private:
467 KeyType AdvanceFilter() {
468 ++filter_pos_;
469 auto filter_range = FilterRange();
470 if (filter_range.valid()) {
471 FastForwardGen(filter_range);
472 }
473 return filter_range;
474 }
475 KeyType AdvanceGen() {
John Zulaufd5115702021-01-18 12:34:33 -0700476 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700477 auto gen_range = GenRange();
478 if (gen_range.valid()) {
479 FastForwardFilter(gen_range);
480 }
481 return gen_range;
482 }
483
484 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
John Zulaufd5115702021-01-18 12:34:33 -0700485 KeyType GenRange() const { return *gen_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700486
487 KeyType FastForwardFilter(const KeyType &range) {
488 auto filter_range = FilterRange();
489 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700490 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700491 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
492 if (retry_count < kRetryLimit) {
493 ++filter_pos_;
494 filter_range = FilterRange();
495 retry_count++;
496 } else {
497 // Okay we've tried walking, do a seek.
498 filter_pos_ = filter_->lower_bound(range);
499 break;
500 }
501 }
502 return FilterRange();
503 }
504
505 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
506 // faster.
507 KeyType FastForwardGen(const KeyType &range) {
508 auto gen_range = GenRange();
509 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
John Zulaufd5115702021-01-18 12:34:33 -0700510 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700511 gen_range = GenRange();
512 }
513 return gen_range;
514 }
515
516 void SeekBegin() {
517 auto gen_range = GenRange();
518 if (gen_range.empty()) {
519 current_ = KeyType();
520 filter_pos_ = filter_->cend();
521 } else {
522 filter_pos_ = filter_->lower_bound(gen_range);
523 current_ = gen_range & FilterRange();
524 }
525 }
526
John Zulaufae842002021-04-15 18:20:55 -0600527 const RangeMap *filter_;
John Zulaufd5115702021-01-18 12:34:33 -0700528 RangeGen gen_;
John Zulaufae842002021-04-15 18:20:55 -0600529 typename RangeMap::const_iterator filter_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700530 KeyType current_;
531};
532
533using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
534
John Zulauf5c5e88d2019-12-26 11:22:02 -0700535
John Zulauf3e86bf02020-09-12 10:47:57 -0600536ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
537 VkDeviceSize stride) {
538 VkDeviceSize range_start = offset + first_index * stride;
539 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600540 if (count == UINT32_MAX) {
541 range_size = buf_whole_size - range_start;
542 } else {
543 range_size = count * stride;
544 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600545 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600546}
547
locke-lunarg654e3692020-06-04 17:19:15 -0600548SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
549 VkShaderStageFlagBits stage_flag) {
550 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
551 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
552 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
553 }
554 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
555 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
556 assert(0);
557 }
558 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
559 return stage_access->second.uniform_read;
560 }
561
562 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
563 // Because if write hazard happens, read hazard might or might not happen.
564 // But if write hazard doesn't happen, read hazard is impossible to happen.
565 if (descriptor_data.is_writable) {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700566 return stage_access->second.storage_write;
locke-lunarg654e3692020-06-04 17:19:15 -0600567 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700568 // TODO: sampled_read
569 return stage_access->second.storage_read;
locke-lunarg654e3692020-06-04 17:19:15 -0600570}
571
locke-lunarg37047832020-06-12 13:44:45 -0600572bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
573 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
574 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
575 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
576 ? true
577 : false;
578}
579
580bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
581 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
582 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
583 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
584 ? true
585 : false;
586}
587
John Zulauf355e49b2020-04-24 15:11:15 -0600588// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600589template <typename Action>
590static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
591 Action &action) {
592 // At this point the "apply over range" logic only supports a single memory binding
593 if (!SimpleBinding(image_state)) return;
594 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600595 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700596 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
597 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600598 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700599 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600600 }
601}
602
John Zulauf7635de32020-05-29 17:14:15 -0600603// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
604// Used by both validation and record operations
605//
606// The signature for Action() reflect the needs of both uses.
607template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700608void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
609 uint32_t subpass) {
John Zulauf7635de32020-05-29 17:14:15 -0600610 const auto &rp_ci = rp_state.createInfo;
611 const auto *attachment_ci = rp_ci.pAttachments;
612 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
613
614 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
615 const auto *color_attachments = subpass_ci.pColorAttachments;
616 const auto *color_resolve = subpass_ci.pResolveAttachments;
617 if (color_resolve && color_attachments) {
618 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
619 const auto &color_attach = color_attachments[i].attachment;
620 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
621 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
622 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700623 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ,
624 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600625 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700626 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
627 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600628 }
629 }
630 }
631
632 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700633 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600634 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
635 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
636 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
637 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
638 const auto src_ci = attachment_ci[src_at];
639 // The formats are required to match so we can pick either
640 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
641 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
642 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
John Zulauf7635de32020-05-29 17:14:15 -0600643
644 // Figure out which aspects are actually touched during resolve operations
645 const char *aspect_string = nullptr;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700646 AttachmentViewGen::Gen gen_type = AttachmentViewGen::Gen::kRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600647 if (resolve_depth && resolve_stencil) {
John Zulauf7635de32020-05-29 17:14:15 -0600648 aspect_string = "depth/stencil";
649 } else if (resolve_depth) {
650 // Validate depth only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700651 gen_type = AttachmentViewGen::Gen::kDepthOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600652 aspect_string = "depth";
653 } else if (resolve_stencil) {
654 // Validate all stencil only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700655 gen_type = AttachmentViewGen::Gen::kStencilOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600656 aspect_string = "stencil";
657 }
658
John Zulaufd0ec59f2021-03-13 14:25:08 -0700659 if (aspect_string) {
660 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at], gen_type,
661 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster);
662 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at], gen_type,
663 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulauf7635de32020-05-29 17:14:15 -0600664 }
665 }
666}
667
668// Action for validating resolve operations
669class ValidateResolveAction {
670 public:
John Zulauffaea0ee2021-01-14 14:01:32 -0700671 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context,
John Zulauf64ffe552021-02-06 10:25:07 -0700672 const CommandExecutionContext &ex_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600673 : render_pass_(render_pass),
674 subpass_(subpass),
675 context_(context),
John Zulauf64ffe552021-02-06 10:25:07 -0700676 ex_context_(ex_context),
John Zulauf7635de32020-05-29 17:14:15 -0600677 func_name_(func_name),
678 skip_(false) {}
679 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
John Zulaufd0ec59f2021-03-13 14:25:08 -0700680 const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage,
681 SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600682 HazardResult hazard;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700683 hazard = context_.DetectHazard(view_gen, gen_type, current_usage, ordering_rule);
John Zulauf7635de32020-05-29 17:14:15 -0600684 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -0700685 skip_ |=
John Zulauf64ffe552021-02-06 10:25:07 -0700686 ex_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700687 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
688 " to resolve attachment %" PRIu32 ". Access info %s.",
689 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name,
John Zulauf64ffe552021-02-06 10:25:07 -0700690 attachment_name, src_at, dst_at, ex_context_.FormatUsage(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600691 }
692 }
693 // Providing a mechanism for the constructing caller to get the result of the validation
694 bool GetSkip() const { return skip_; }
695
696 private:
697 VkRenderPass render_pass_;
698 const uint32_t subpass_;
699 const AccessContext &context_;
John Zulauf64ffe552021-02-06 10:25:07 -0700700 const CommandExecutionContext &ex_context_;
John Zulauf7635de32020-05-29 17:14:15 -0600701 const char *func_name_;
702 bool skip_;
703};
704
705// Update action for resolve operations
706class UpdateStateResolveAction {
707 public:
John Zulauf14940722021-04-12 15:19:02 -0600708 UpdateStateResolveAction(AccessContext &context, ResourceUsageTag tag) : context_(context), tag_(tag) {}
John Zulaufd0ec59f2021-03-13 14:25:08 -0700709 void operator()(const char *, const char *, uint32_t, uint32_t, const AttachmentViewGen &view_gen,
710 AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600711 // Ignores validation only arguments...
John Zulaufd0ec59f2021-03-13 14:25:08 -0700712 context_.UpdateAccessState(view_gen, gen_type, current_usage, ordering_rule, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600713 }
714
715 private:
716 AccessContext &context_;
John Zulauf14940722021-04-12 15:19:02 -0600717 const ResourceUsageTag tag_;
John Zulauf7635de32020-05-29 17:14:15 -0600718};
719
John Zulauf59e25072020-07-17 10:55:21 -0600720void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
John Zulauf14940722021-04-12 15:19:02 -0600721 const SyncStageAccessFlags &prior_, const ResourceUsageTag tag_) {
John Zulauf4fa68462021-04-26 21:04:22 -0600722 access_state = layer_data::make_unique<const ResourceAccessState>(*access_state_);
John Zulauf59e25072020-07-17 10:55:21 -0600723 usage_index = usage_index_;
724 hazard = hazard_;
725 prior_access = prior_;
726 tag = tag_;
727}
728
John Zulauf4fa68462021-04-26 21:04:22 -0600729void HazardResult::AddRecordedAccess(const ResourceFirstAccess &first_access) {
730 recorded_access = layer_data::make_unique<const ResourceFirstAccess>(first_access);
731}
732
John Zulauf540266b2020-04-06 18:54:53 -0600733AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
734 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600735 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600736 Reset();
737 const auto &subpass_dep = dependencies[subpass];
John Zulauf22aefed2021-03-11 18:14:35 -0700738 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
739 prev_.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
John Zulauf355e49b2020-04-24 15:11:15 -0600740 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600741 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600742 const auto prev_pass = prev_dep.first->pass;
743 const auto &prev_barriers = prev_dep.second;
744 assert(prev_dep.second.size());
745 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
746 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700747 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600748
749 async_.reserve(subpass_dep.async.size());
750 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700751 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600752 }
John Zulauf22aefed2021-03-11 18:14:35 -0700753 if (has_barrier_from_external) {
754 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
755 prev_.emplace_back(external_context, queue_flags, subpass_dep.barrier_from_external);
756 src_external_ = &prev_.back();
John Zulaufe5da6e52020-03-18 15:32:18 -0600757 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600758 if (subpass_dep.barrier_to_external.size()) {
759 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600760 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700761}
762
John Zulauf5f13a792020-03-10 07:31:21 -0600763template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700764HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600765 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600766 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600767 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600768
769 HazardResult hazard;
770 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
771 hazard = detector.Detect(prev);
772 }
773 return hazard;
774}
775
John Zulauf4a6105a2020-11-17 15:11:05 -0700776template <typename Action>
777void AccessContext::ForAll(Action &&action) {
778 for (const auto address_type : kAddressTypes) {
779 auto &accesses = GetAccessStateMap(address_type);
780 for (const auto &access : accesses) {
781 action(address_type, access);
782 }
783 }
784}
785
John Zulauf3d84f1b2020-03-09 13:33:25 -0600786// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
787// the DAG of the contexts (for example subpasses)
788template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700789HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600790 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600791 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600792
John Zulauf1a224292020-06-30 14:52:13 -0600793 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600794 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
795 // so we'll check these first
796 for (const auto &async_context : async_) {
797 hazard = async_context->DetectAsyncHazard(type, detector, range);
798 if (hazard.hazard) return hazard;
799 }
John Zulauf5f13a792020-03-10 07:31:21 -0600800 }
801
John Zulauf1a224292020-06-30 14:52:13 -0600802 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600803
John Zulauf69133422020-05-20 14:55:53 -0600804 const auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600805 const auto the_end = accesses.cend(); // End is not invalidated
806 auto pos = accesses.lower_bound(range);
John Zulauf69133422020-05-20 14:55:53 -0600807 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600808
John Zulauf3cafbf72021-03-26 16:55:19 -0600809 while (pos != the_end && pos->first.begin < range.end) {
John Zulauf69133422020-05-20 14:55:53 -0600810 // Cover any leading gap, or gap between entries
811 if (detect_prev) {
812 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
813 // Cover any leading gap, or gap between entries
814 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600815 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600816 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600817 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600818 if (hazard.hazard) return hazard;
819 }
John Zulauf69133422020-05-20 14:55:53 -0600820 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
821 gap.begin = pos->first.end;
822 }
823
824 hazard = detector.Detect(pos);
825 if (hazard.hazard) return hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600826 ++pos;
John Zulauf69133422020-05-20 14:55:53 -0600827 }
828
829 if (detect_prev) {
830 // Detect in the trailing empty as needed
831 gap.end = range.end;
832 if (gap.non_empty()) {
833 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600834 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600835 }
836
837 return hazard;
838}
839
840// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
841template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700842HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
843 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600844 auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600845 auto pos = accesses.lower_bound(range);
846 const auto the_end = accesses.end();
John Zulauf16adfc92020-04-08 10:28:33 -0600847
John Zulauf3d84f1b2020-03-09 13:33:25 -0600848 HazardResult hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600849 while (pos != the_end && pos->first.begin < range.end) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700850 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3cafbf72021-03-26 16:55:19 -0600851 if (hazard.hazard) break;
852 ++pos;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600853 }
John Zulauf16adfc92020-04-08 10:28:33 -0600854
John Zulauf3d84f1b2020-03-09 13:33:25 -0600855 return hazard;
856}
857
John Zulaufb02c1eb2020-10-06 16:33:36 -0600858struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700859 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600860 void operator()(ResourceAccessState *access) const {
861 assert(access);
862 access->ApplyBarriers(barriers, true);
863 }
864 const std::vector<SyncBarrier> &barriers;
865};
866
John Zulauf22aefed2021-03-11 18:14:35 -0700867struct ApplyTrackbackStackAction {
868 explicit ApplyTrackbackStackAction(const std::vector<SyncBarrier> &barriers_,
869 const ResourceAccessStateFunction *previous_barrier_ = nullptr)
870 : barriers(barriers_), previous_barrier(previous_barrier_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600871 void operator()(ResourceAccessState *access) const {
872 assert(access);
873 assert(!access->HasPendingState());
874 access->ApplyBarriers(barriers, false);
875 access->ApplyPendingBarriers(kCurrentCommandTag);
John Zulauf22aefed2021-03-11 18:14:35 -0700876 if (previous_barrier) {
877 assert(bool(*previous_barrier));
878 (*previous_barrier)(access);
879 }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600880 }
881 const std::vector<SyncBarrier> &barriers;
John Zulauf22aefed2021-03-11 18:14:35 -0700882 const ResourceAccessStateFunction *previous_barrier;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600883};
884
885// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
886// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
887// *different* map from dest.
888// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
889// range [first, last)
890template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600891static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
892 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600893 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600894 auto at = entry;
895 for (auto pos = first; pos != last; ++pos) {
896 // Every member of the input iterator range must fit within the remaining portion of entry
897 assert(at->first.includes(pos->first));
898 assert(at != dest->end());
899 // Trim up at to the same size as the entry to resolve
900 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600901 auto access = pos->second; // intentional copy
902 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600903 at->second.Resolve(access);
904 ++at; // Go to the remaining unused section of entry
905 }
906}
907
John Zulaufa0a98292020-09-18 09:30:10 -0600908static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
909 SyncBarrier merged = {};
910 for (const auto &barrier : barriers) {
911 merged.Merge(barrier);
912 }
913 return merged;
914}
915
John Zulaufb02c1eb2020-10-06 16:33:36 -0600916template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700917void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600918 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
919 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600920 if (!range.non_empty()) return;
921
John Zulauf355e49b2020-04-24 15:11:15 -0600922 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
923 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600924 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600925 if (current->pos_B->valid) {
926 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600927 auto access = src_pos->second; // intentional copy
928 barrier_action(&access);
929
John Zulauf16adfc92020-04-08 10:28:33 -0600930 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600931 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
932 trimmed->second.Resolve(access);
933 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600934 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600935 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600936 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600937 }
John Zulauf16adfc92020-04-08 10:28:33 -0600938 } else {
939 // we have to descend to fill this gap
940 if (recur_to_infill) {
John Zulauf22aefed2021-03-11 18:14:35 -0700941 ResourceAccessRange recurrence_range = current_range;
942 // The current context is empty for the current range, so recur to fill the gap.
943 // Since we will be recurring back up the DAG, expand the gap descent to cover the full range for which B
944 // is not valid, to minimize that recurrence
945 if (current->pos_B.at_end()) {
946 // Do the remainder here....
947 recurrence_range.end = range.end;
John Zulauf355e49b2020-04-24 15:11:15 -0600948 } else {
John Zulauf22aefed2021-03-11 18:14:35 -0700949 // Recur only over the range until B becomes valid (within the limits of range).
950 recurrence_range.end = std::min(range.end, current->pos_B->lower_bound->first.begin);
John Zulauf355e49b2020-04-24 15:11:15 -0600951 }
John Zulauf22aefed2021-03-11 18:14:35 -0700952 ResolvePreviousAccessStack(type, recurrence_range, resolve_map, infill_state, barrier_action);
953
John Zulauf355e49b2020-04-24 15:11:15 -0600954 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
955 // iterator of the outer while.
956
957 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
958 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
959 // we stepped on the dest map
John Zulauf22aefed2021-03-11 18:14:35 -0700960 const auto seek_to = recurrence_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
locke-lunarg88dbb542020-06-23 22:05:42 -0600961 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600962 current.seek(seek_to);
963 } else if (!current->pos_A->valid && infill_state) {
964 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
965 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
966 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600967 }
John Zulauf5f13a792020-03-10 07:31:21 -0600968 }
John Zulauf16adfc92020-04-08 10:28:33 -0600969 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600970 }
John Zulauf1a224292020-06-30 14:52:13 -0600971
972 // Infill if range goes passed both the current and resolve map prior contents
973 if (recur_to_infill && (current->range.end < range.end)) {
974 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
John Zulauf22aefed2021-03-11 18:14:35 -0700975 ResolvePreviousAccessStack<BarrierAction>(type, trailing_fill_range, resolve_map, infill_state, barrier_action);
John Zulauf1a224292020-06-30 14:52:13 -0600976 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600977}
978
John Zulauf22aefed2021-03-11 18:14:35 -0700979template <typename BarrierAction>
980void AccessContext::ResolvePreviousAccessStack(AccessAddressType type, const ResourceAccessRange &range,
981 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
982 const BarrierAction &previous_barrier) const {
983 ResourceAccessStateFunction stacked_barrier(std::ref(previous_barrier));
984 ResolvePreviousAccess(type, range, descent_map, infill_state, &stacked_barrier);
985}
986
John Zulauf43cc7462020-12-03 12:33:12 -0700987void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
John Zulauf22aefed2021-03-11 18:14:35 -0700988 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
989 const ResourceAccessStateFunction *previous_barrier) const {
990 if (prev_.size() == 0) {
John Zulauf5f13a792020-03-10 07:31:21 -0600991 if (range.non_empty() && infill_state) {
John Zulauf22aefed2021-03-11 18:14:35 -0700992 // Fill the empty poritions of descent_map with the default_state with the barrier function applied (iff present)
993 ResourceAccessState state_copy;
994 if (previous_barrier) {
995 assert(bool(*previous_barrier));
996 state_copy = *infill_state;
997 (*previous_barrier)(&state_copy);
998 infill_state = &state_copy;
999 }
1000 sparse_container::update_range_value(*descent_map, range, *infill_state,
1001 sparse_container::value_precedence::prefer_dest);
John Zulauf5f13a792020-03-10 07:31:21 -06001002 }
1003 } else {
1004 // Look for something to fill the gap further along.
1005 for (const auto &prev_dep : prev_) {
John Zulauf22aefed2021-03-11 18:14:35 -07001006 const ApplyTrackbackStackAction barrier_action(prev_dep.barriers, previous_barrier);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001007 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001008 }
John Zulauf5f13a792020-03-10 07:31:21 -06001009 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001010}
1011
John Zulauf4a6105a2020-11-17 15:11:05 -07001012// Non-lazy import of all accesses, WaitEvents needs this.
1013void AccessContext::ResolvePreviousAccesses() {
1014 ResourceAccessState default_state;
John Zulauf22aefed2021-03-11 18:14:35 -07001015 if (!prev_.size()) return; // If no previous contexts, nothing to do
1016
John Zulauf4a6105a2020-11-17 15:11:05 -07001017 for (const auto address_type : kAddressTypes) {
1018 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
1019 }
1020}
1021
John Zulauf43cc7462020-12-03 12:33:12 -07001022AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
1023 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -06001024}
1025
John Zulauf1507ee42020-05-18 11:33:09 -06001026static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -06001027 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
1028 ? SYNC_ACCESS_INDEX_NONE
1029 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
1030 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -06001031 return stage_access;
1032}
1033static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -06001034 const auto stage_access =
1035 (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
1036 ? SYNC_ACCESS_INDEX_NONE
1037 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
1038 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -06001039 return stage_access;
1040}
1041
John Zulauf7635de32020-05-29 17:14:15 -06001042// Caller must manage returned pointer
1043static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001044 uint32_t subpass, const AttachmentViewGenVector &attachment_views) {
John Zulauf7635de32020-05-29 17:14:15 -06001045 auto *proxy = new AccessContext(context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001046 proxy->UpdateAttachmentResolveAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
1047 proxy->UpdateAttachmentStoreAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -06001048 return proxy;
1049}
1050
John Zulaufb02c1eb2020-10-06 16:33:36 -06001051template <typename BarrierAction>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001052void AccessContext::ResolveAccessRange(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1053 BarrierAction &barrier_action, ResourceAccessRangeMap *descent_map,
1054 const ResourceAccessState *infill_state) const {
1055 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1056 if (!attachment_gen) return;
1057
1058 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1059 const AccessAddressType address_type = view_gen.GetAddressType();
1060 for (; range_gen->non_empty(); ++range_gen) {
1061 ResolveAccessRange(address_type, *range_gen, barrier_action, descent_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001062 }
John Zulauf62f10592020-04-03 12:20:02 -06001063}
1064
John Zulauf7635de32020-05-29 17:14:15 -06001065// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf64ffe552021-02-06 10:25:07 -07001066bool AccessContext::ValidateLayoutTransitions(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001067 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001068 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001069 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -06001070 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
1071 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
1072 // those affects have not been recorded yet.
1073 //
1074 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1075 // to apply and only copy then, if this proves a hot spot.
1076 std::unique_ptr<AccessContext> proxy_for_prev;
1077 TrackBack proxy_track_back;
1078
John Zulauf355e49b2020-04-24 15:11:15 -06001079 const auto &transitions = rp_state.subpass_transitions[subpass];
1080 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -06001081 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
1082
1083 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
John Zulauf22aefed2021-03-11 18:14:35 -07001084 assert(track_back);
John Zulauf7635de32020-05-29 17:14:15 -06001085 if (prev_needs_proxy) {
1086 if (!proxy_for_prev) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001087 proxy_for_prev.reset(
1088 CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass, attachment_views));
John Zulauf7635de32020-05-29 17:14:15 -06001089 proxy_track_back = *track_back;
1090 proxy_track_back.context = proxy_for_prev.get();
1091 }
1092 track_back = &proxy_track_back;
1093 }
1094 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -06001095 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001096 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001097 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1098 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
1099 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1100 string_VkImageLayout(transition.old_layout),
1101 string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07001102 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001103 }
1104 }
1105 return skip;
1106}
1107
John Zulauf64ffe552021-02-06 10:25:07 -07001108bool AccessContext::ValidateLoadOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001109 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001110 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001111 bool skip = false;
1112 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufa0a98292020-09-18 09:30:10 -06001113
John Zulauf1507ee42020-05-18 11:33:09 -06001114 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1115 if (subpass == rp_state.attachment_first_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001116 const auto &view_gen = attachment_views[i];
1117 if (!view_gen.IsValid()) continue;
John Zulauf1507ee42020-05-18 11:33:09 -06001118 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001119
1120 // Need check in the following way
1121 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1122 // vs. transition
1123 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1124 // for each aspect loaded.
1125
1126 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001127 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001128 const bool is_color = !(has_depth || has_stencil);
1129
1130 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001131 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001132
John Zulaufaff20662020-06-01 14:07:58 -06001133 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001134 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001135
John Zulaufb02c1eb2020-10-06 16:33:36 -06001136 bool checked_stencil = false;
John Zulauf57261402021-08-13 11:32:06 -06001137 if (is_color && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001138 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea, load_index, SyncOrdering::kColorAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001139 aspect = "color";
1140 } else {
John Zulauf57261402021-08-13 11:32:06 -06001141 if (has_depth && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001142 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_index,
1143 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001144 aspect = "depth";
1145 }
John Zulauf57261402021-08-13 11:32:06 -06001146 if (!hazard.hazard && has_stencil && (stencil_load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001147 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, stencil_load_index,
1148 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001149 aspect = "stencil";
1150 checked_stencil = true;
1151 }
1152 }
1153
1154 if (hazard.hazard) {
1155 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001156 const auto &sync_state = ex_context.GetSyncState();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001157 if (hazard.tag == kCurrentCommandTag) {
1158 // Hazard vs. ILT
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001159 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulaufb02c1eb2020-10-06 16:33:36 -06001160 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1161 " aspect %s during load with loadOp %s.",
1162 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1163 } else {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001164 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauf1507ee42020-05-18 11:33:09 -06001165 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001166 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001167 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf64ffe552021-02-06 10:25:07 -07001168 ex_context.FormatUsage(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001169 }
1170 }
1171 }
1172 }
1173 return skip;
1174}
1175
John Zulaufaff20662020-06-01 14:07:58 -06001176// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1177// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1178// store is part of the same Next/End operation.
1179// The latter is handled in layout transistion validation directly
John Zulauf64ffe552021-02-06 10:25:07 -07001180bool AccessContext::ValidateStoreOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufaff20662020-06-01 14:07:58 -06001181 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001182 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001183 bool skip = false;
1184 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001185
1186 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1187 if (subpass == rp_state.attachment_last_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001188 const AttachmentViewGen &view_gen = attachment_views[i];
1189 if (!view_gen.IsValid()) continue;
John Zulaufaff20662020-06-01 14:07:58 -06001190 const auto &ci = attachment_ci[i];
1191
1192 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1193 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1194 // sake, we treat DONT_CARE as writing.
1195 const bool has_depth = FormatHasDepth(ci.format);
1196 const bool has_stencil = FormatHasStencil(ci.format);
1197 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001198 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001199 if (!has_stencil && !store_op_stores) continue;
1200
1201 HazardResult hazard;
1202 const char *aspect = nullptr;
1203 bool checked_stencil = false;
1204 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001205 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
1206 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001207 aspect = "color";
1208 } else {
John Zulauf57261402021-08-13 11:32:06 -06001209 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001210 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001211 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1212 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001213 aspect = "depth";
1214 }
1215 if (!hazard.hazard && has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001216 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1217 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001218 aspect = "stencil";
1219 checked_stencil = true;
1220 }
1221 }
1222
1223 if (hazard.hazard) {
1224 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1225 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001226 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001227 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1228 " %s aspect during store with %s %s. Access info %s",
1229 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect,
John Zulauf64ffe552021-02-06 10:25:07 -07001230 op_type_string, store_op_string, ex_context.FormatUsage(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001231 }
1232 }
1233 }
1234 return skip;
1235}
1236
John Zulauf64ffe552021-02-06 10:25:07 -07001237bool AccessContext::ValidateResolveOperations(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001238 const VkRect2D &render_area, const AttachmentViewGenVector &attachment_views,
1239 const char *func_name, uint32_t subpass) const {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001240 ValidateResolveAction validate_action(rp_state.renderPass(), subpass, *this, ex_context, func_name);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001241 ResolveOperation(validate_action, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001242 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001243}
1244
John Zulauf3d84f1b2020-03-09 13:33:25 -06001245class HazardDetector {
1246 SyncStageAccessIndex usage_index_;
1247
1248 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001249 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf14940722021-04-12 15:19:02 -06001250 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001251 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001252 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001253 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001254};
1255
John Zulauf69133422020-05-20 14:55:53 -06001256class HazardDetectorWithOrdering {
1257 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001258 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001259
1260 public:
1261 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001262 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001263 }
John Zulauf14940722021-04-12 15:19:02 -06001264 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001265 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001266 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001267 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001268};
1269
John Zulauf16adfc92020-04-08 10:28:33 -06001270HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001271 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001272 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001273 const auto base_address = ResourceBaseAddress(buffer);
1274 HazardDetector detector(usage_index);
1275 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001276}
1277
John Zulauf69133422020-05-20 14:55:53 -06001278template <typename Detector>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001279HazardResult AccessContext::DetectHazard(Detector &detector, const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1280 DetectOptions options) const {
1281 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1282 if (!attachment_gen) return HazardResult();
1283
1284 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1285 const auto address_type = view_gen.GetAddressType();
1286 for (; range_gen->non_empty(); ++range_gen) {
1287 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1288 if (hazard.hazard) return hazard;
1289 }
1290
1291 return HazardResult();
1292}
1293
1294template <typename Detector>
John Zulauf69133422020-05-20 14:55:53 -06001295HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1296 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1297 const VkExtent3D &extent, DetectOptions options) const {
1298 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001299 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001300 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1301 base_address);
1302 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001303 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001304 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001305 if (hazard.hazard) return hazard;
1306 }
1307 return HazardResult();
1308}
John Zulauf110413c2021-03-20 05:38:38 -06001309template <typename Detector>
1310HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1311 const VkImageSubresourceRange &subresource_range, DetectOptions options) const {
1312 if (!SimpleBinding(image)) return HazardResult();
1313 const auto base_address = ResourceBaseAddress(image);
1314 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1315 const auto address_type = ImageAddressType(image);
1316 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf110413c2021-03-20 05:38:38 -06001317 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1318 if (hazard.hazard) return hazard;
1319 }
1320 return HazardResult();
1321}
John Zulauf69133422020-05-20 14:55:53 -06001322
John Zulauf540266b2020-04-06 18:54:53 -06001323HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1324 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1325 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001326 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1327 subresource.layerCount};
John Zulauf110413c2021-03-20 05:38:38 -06001328 HazardDetector detector(current_usage);
1329 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf1507ee42020-05-18 11:33:09 -06001330}
1331
1332HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf110413c2021-03-20 05:38:38 -06001333 const VkImageSubresourceRange &subresource_range) const {
John Zulauf69133422020-05-20 14:55:53 -06001334 HazardDetector detector(current_usage);
John Zulauf110413c2021-03-20 05:38:38 -06001335 return DetectHazard(detector, image, subresource_range, DetectOptions::kDetectAll);
John Zulauf69133422020-05-20 14:55:53 -06001336}
1337
John Zulaufd0ec59f2021-03-13 14:25:08 -07001338HazardResult AccessContext::DetectHazard(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1339 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) const {
1340 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
1341 return DetectHazard(detector, view_gen, gen_type, DetectOptions::kDetectAll);
1342}
1343
John Zulauf69133422020-05-20 14:55:53 -06001344HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001345 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001346 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001347 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001348 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001349}
1350
John Zulauf3d84f1b2020-03-09 13:33:25 -06001351class BarrierHazardDetector {
1352 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001353 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001354 SyncStageAccessFlags src_access_scope)
1355 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1356
John Zulauf5f13a792020-03-10 07:31:21 -06001357 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1358 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001359 }
John Zulauf14940722021-04-12 15:19:02 -06001360 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001361 // 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 -07001362 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001363 }
1364
1365 private:
1366 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001367 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001368 SyncStageAccessFlags src_access_scope_;
1369};
1370
John Zulauf4a6105a2020-11-17 15:11:05 -07001371class EventBarrierHazardDetector {
1372 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001373 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001374 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
John Zulauf14940722021-04-12 15:19:02 -06001375 ResourceUsageTag scope_tag)
John Zulauf4a6105a2020-11-17 15:11:05 -07001376 : usage_index_(usage_index),
1377 src_exec_scope_(src_exec_scope),
1378 src_access_scope_(src_access_scope),
1379 event_scope_(event_scope),
1380 scope_pos_(event_scope.cbegin()),
1381 scope_end_(event_scope.cend()),
1382 scope_tag_(scope_tag) {}
1383
1384 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1385 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1386 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1387 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1388 if (scope_pos_ == scope_end_) return HazardResult();
1389 if (!scope_pos_->first.intersects(pos->first)) {
1390 event_scope_.lower_bound(pos->first);
1391 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1392 }
1393
1394 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1395 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1396 }
John Zulauf14940722021-04-12 15:19:02 -06001397 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07001398 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1399 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1400 }
1401
1402 private:
1403 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001404 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001405 SyncStageAccessFlags src_access_scope_;
1406 const SyncEventState::ScopeMap &event_scope_;
1407 SyncEventState::ScopeMap::const_iterator scope_pos_;
1408 SyncEventState::ScopeMap::const_iterator scope_end_;
John Zulauf14940722021-04-12 15:19:02 -06001409 const ResourceUsageTag scope_tag_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001410};
1411
Jeremy Gebben40a22942020-12-22 14:22:06 -07001412HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001413 const SyncStageAccessFlags &src_access_scope,
1414 const VkImageSubresourceRange &subresource_range,
1415 const SyncEventState &sync_event, DetectOptions options) const {
1416 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1417 // first access scope map to use, and there's no easy way to plumb it in below.
1418 const auto address_type = ImageAddressType(image);
1419 const auto &event_scope = sync_event.FirstScope(address_type);
1420
1421 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1422 event_scope, sync_event.first_scope_tag);
John Zulauf110413c2021-03-20 05:38:38 -06001423 return DetectHazard(detector, image, subresource_range, options);
John Zulauf4a6105a2020-11-17 15:11:05 -07001424}
1425
John Zulaufd0ec59f2021-03-13 14:25:08 -07001426HazardResult AccessContext::DetectImageBarrierHazard(const AttachmentViewGen &view_gen, const SyncBarrier &barrier,
1427 DetectOptions options) const {
1428 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, barrier.src_exec_scope.exec_scope,
1429 barrier.src_access_scope);
1430 return DetectHazard(detector, view_gen, AttachmentViewGen::Gen::kViewSubresource, options);
1431}
1432
Jeremy Gebben40a22942020-12-22 14:22:06 -07001433HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001434 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001435 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001436 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001437 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
John Zulauf110413c2021-03-20 05:38:38 -06001438 return DetectHazard(detector, image, subresource_range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001439}
1440
Jeremy Gebben40a22942020-12-22 14:22:06 -07001441HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001442 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001443 const VkImageMemoryBarrier &barrier) const {
1444 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1445 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1446 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1447}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001448HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07001449 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope.exec_scope,
John Zulauf110413c2021-03-20 05:38:38 -06001450 image_barrier.barrier.src_access_scope, image_barrier.range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001451}
John Zulauf355e49b2020-04-24 15:11:15 -06001452
John Zulauf9cb530d2019-09-30 14:14:10 -06001453template <typename Flags, typename Map>
1454SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1455 SyncStageAccessFlags scope = 0;
1456 for (const auto &bit_scope : map) {
1457 if (flag_mask < bit_scope.first) break;
1458
1459 if (flag_mask & bit_scope.first) {
1460 scope |= bit_scope.second;
1461 }
1462 }
1463 return scope;
1464}
1465
Jeremy Gebben40a22942020-12-22 14:22:06 -07001466SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags2KHR stages) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001467 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1468}
1469
Jeremy Gebben40a22942020-12-22 14:22:06 -07001470SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags2KHR accesses) {
1471 return AccessScopeImpl(sync_utils::ExpandAccessFlags(accesses), syncStageAccessMaskByAccessBit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001472}
1473
Jeremy Gebben40a22942020-12-22 14:22:06 -07001474// Getting from stage mask and access mask to stage/access masks is something we need to be good at...
1475SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001476 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1477 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1478 // 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 -06001479 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1480}
1481
1482template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001483void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001484 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1485 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001486 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001487 auto pos = accesses->lower_bound(range);
1488 if (pos == accesses->end() || !pos->first.intersects(range)) {
1489 // The range is empty, fill it with a default value.
1490 pos = action.Infill(accesses, pos, range);
1491 } else if (range.begin < pos->first.begin) {
1492 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001493 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001494 } else if (pos->first.begin < range.begin) {
1495 // Trim the beginning if needed
1496 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1497 ++pos;
1498 }
1499
1500 const auto the_end = accesses->end();
1501 while ((pos != the_end) && pos->first.intersects(range)) {
1502 if (pos->first.end > range.end) {
1503 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1504 }
1505
1506 pos = action(accesses, pos);
1507 if (pos == the_end) break;
1508
1509 auto next = pos;
1510 ++next;
1511 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1512 // Need to infill if next is disjoint
1513 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001514 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001515 next = action.Infill(accesses, next, new_range);
1516 }
1517 pos = next;
1518 }
1519}
John Zulaufd5115702021-01-18 12:34:33 -07001520
1521// Give a comparable interface for range generators and ranges
1522template <typename Action>
1523inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1524 assert(range);
1525 UpdateMemoryAccessState(accesses, *range, action);
1526}
1527
John Zulauf4a6105a2020-11-17 15:11:05 -07001528template <typename Action, typename RangeGen>
1529void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1530 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001531 RangeGen &range_gen = *range_gen_arg; // Non-const references must be * by style requirement but deref-ing * iterator is a pain
John Zulauf4a6105a2020-11-17 15:11:05 -07001532 for (; range_gen->non_empty(); ++range_gen) {
1533 UpdateMemoryAccessState(accesses, *range_gen, action);
1534 }
1535}
John Zulauf9cb530d2019-09-30 14:14:10 -06001536
John Zulaufd0ec59f2021-03-13 14:25:08 -07001537template <typename Action, typename RangeGen>
1538void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, const RangeGen &range_gen_prebuilt) {
1539 RangeGen range_gen(range_gen_prebuilt); // RangeGenerators can be expensive to create from scratch... initialize from built
1540 for (; range_gen->non_empty(); ++range_gen) {
1541 UpdateMemoryAccessState(accesses, *range_gen, action);
1542 }
1543}
John Zulauf9cb530d2019-09-30 14:14:10 -06001544struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001545 using Iterator = ResourceAccessRangeMap::iterator;
1546 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001547 // this is only called on gaps, and never returns a gap.
1548 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001549 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001550 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001551 }
John Zulauf5f13a792020-03-10 07:31:21 -06001552
John Zulauf5c5e88d2019-12-26 11:22:02 -07001553 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001554 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001555 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001556 return pos;
1557 }
1558
John Zulauf43cc7462020-12-03 12:33:12 -07001559 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf14940722021-04-12 15:19:02 -06001560 SyncOrdering ordering_rule_, ResourceUsageTag tag_)
John Zulauf8e3c3e92021-01-06 11:19:36 -07001561 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001562 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001563 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001564 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001565 const SyncOrdering ordering_rule;
John Zulauf14940722021-04-12 15:19:02 -06001566 const ResourceUsageTag tag;
John Zulauf9cb530d2019-09-30 14:14:10 -06001567};
1568
John Zulauf4a6105a2020-11-17 15:11:05 -07001569// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001570struct PipelineBarrierOp {
1571 SyncBarrier barrier;
1572 bool layout_transition;
1573 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1574 : barrier(barrier_), layout_transition(layout_transition_) {}
1575 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001576 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001577 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1578};
John Zulauf4a6105a2020-11-17 15:11:05 -07001579// The barrier operation for wait events
1580struct WaitEventBarrierOp {
John Zulauf14940722021-04-12 15:19:02 -06001581 ResourceUsageTag scope_tag;
John Zulauf4a6105a2020-11-17 15:11:05 -07001582 SyncBarrier barrier;
1583 bool layout_transition;
John Zulauf14940722021-04-12 15:19:02 -06001584 WaitEventBarrierOp(const ResourceUsageTag scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1585 : scope_tag(scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
John Zulauf4a6105a2020-11-17 15:11:05 -07001586 WaitEventBarrierOp() = default;
John Zulauf14940722021-04-12 15:19:02 -06001587 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(scope_tag, barrier, layout_transition); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001588};
John Zulauf1e331ec2020-12-04 18:29:38 -07001589
John Zulauf4a6105a2020-11-17 15:11:05 -07001590// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1591// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1592// of a collection is known/present.
John Zulauf5c628d02021-05-04 15:46:36 -06001593template <typename BarrierOp, typename OpVector = std::vector<BarrierOp>>
John Zulauf89311b42020-09-29 16:28:47 -06001594class ApplyBarrierOpsFunctor {
1595 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001596 using Iterator = ResourceAccessRangeMap::iterator;
John Zulauf5c628d02021-05-04 15:46:36 -06001597 // Only called with a gap, and pos at the lower_bound(range)
1598 inline Iterator Infill(ResourceAccessRangeMap *accesses, const Iterator &pos, const ResourceAccessRange &range) const {
1599 if (!infill_default_) {
1600 return pos;
1601 }
1602 ResourceAccessState default_state;
1603 auto inserted = accesses->insert(pos, std::make_pair(range, default_state));
1604 return inserted;
1605 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001606
John Zulauf5c628d02021-05-04 15:46:36 -06001607 Iterator operator()(ResourceAccessRangeMap *accesses, const Iterator &pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001608 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001609 for (const auto &op : barrier_ops_) {
1610 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001611 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001612
John Zulauf89311b42020-09-29 16:28:47 -06001613 if (resolve_) {
1614 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1615 // another walk
1616 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001617 }
1618 return pos;
1619 }
1620
John Zulauf89311b42020-09-29 16:28:47 -06001621 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulauf5c628d02021-05-04 15:46:36 -06001622 ApplyBarrierOpsFunctor(bool resolve, typename OpVector::size_type size_hint, ResourceUsageTag tag)
1623 : resolve_(resolve), infill_default_(false), barrier_ops_(), tag_(tag) {
John Zulaufd5115702021-01-18 12:34:33 -07001624 barrier_ops_.reserve(size_hint);
1625 }
John Zulauf5c628d02021-05-04 15:46:36 -06001626 void EmplaceBack(const BarrierOp &op) {
1627 barrier_ops_.emplace_back(op);
1628 infill_default_ |= op.layout_transition;
1629 }
John Zulauf89311b42020-09-29 16:28:47 -06001630
1631 private:
1632 bool resolve_;
John Zulauf5c628d02021-05-04 15:46:36 -06001633 bool infill_default_;
1634 OpVector barrier_ops_;
John Zulauf14940722021-04-12 15:19:02 -06001635 const ResourceUsageTag tag_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001636};
1637
John Zulauf4a6105a2020-11-17 15:11:05 -07001638// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1639// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1640template <typename BarrierOp>
John Zulauf5c628d02021-05-04 15:46:36 -06001641class ApplyBarrierFunctor : public ApplyBarrierOpsFunctor<BarrierOp, small_vector<BarrierOp, 1>> {
1642 using Base = ApplyBarrierOpsFunctor<BarrierOp, small_vector<BarrierOp, 1>>;
1643
John Zulauf4a6105a2020-11-17 15:11:05 -07001644 public:
John Zulauf5c628d02021-05-04 15:46:36 -06001645 ApplyBarrierFunctor(const BarrierOp &barrier_op) : Base(false, 1, kCurrentCommandTag) { Base::EmplaceBack(barrier_op); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001646};
1647
John Zulauf1e331ec2020-12-04 18:29:38 -07001648// This functor resolves the pendinging state.
John Zulauf5c628d02021-05-04 15:46:36 -06001649class ResolvePendingBarrierFunctor : public ApplyBarrierOpsFunctor<NoopBarrierAction, small_vector<NoopBarrierAction, 1>> {
1650 using Base = ApplyBarrierOpsFunctor<NoopBarrierAction, small_vector<NoopBarrierAction, 1>>;
1651
John Zulauf1e331ec2020-12-04 18:29:38 -07001652 public:
John Zulauf5c628d02021-05-04 15:46:36 -06001653 ResolvePendingBarrierFunctor(ResourceUsageTag tag) : Base(true, 0, tag) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001654};
1655
John Zulauf8e3c3e92021-01-06 11:19:36 -07001656void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001657 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001658 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001659 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001660}
1661
John Zulauf8e3c3e92021-01-06 11:19:36 -07001662void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001663 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001664 if (!SimpleBinding(buffer)) return;
1665 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001666 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001667}
John Zulauf355e49b2020-04-24 15:11:15 -06001668
John Zulauf8e3c3e92021-01-06 11:19:36 -07001669void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf110413c2021-03-20 05:38:38 -06001670 const VkImageSubresourceRange &subresource_range, const ResourceUsageTag &tag) {
1671 if (!SimpleBinding(image)) return;
1672 const auto base_address = ResourceBaseAddress(image);
1673 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1674 const auto address_type = ImageAddressType(image);
1675 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1676 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
1677}
1678void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001679 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001680 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001681 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001682 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001683 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1684 base_address);
1685 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001686 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf110413c2021-03-20 05:38:38 -06001687 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001688}
John Zulaufd0ec59f2021-03-13 14:25:08 -07001689
1690void AccessContext::UpdateAccessState(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
John Zulauf14940722021-04-12 15:19:02 -06001691 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001692 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1693 if (!gen) return;
1694 subresource_adapter::ImageRangeGenerator range_gen(*gen);
1695 const auto address_type = view_gen.GetAddressType();
1696 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1697 ApplyUpdateAction(address_type, action, &range_gen);
John Zulauf7635de32020-05-29 17:14:15 -06001698}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001699
John Zulauf8e3c3e92021-01-06 11:19:36 -07001700void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001701 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001702 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001703 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1704 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001705 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001706}
1707
John Zulaufd0ec59f2021-03-13 14:25:08 -07001708template <typename Action, typename RangeGen>
1709void AccessContext::ApplyUpdateAction(AccessAddressType address_type, const Action &action, RangeGen *range_gen_arg) {
1710 assert(range_gen_arg); // Old Google C++ styleguide require non-const object pass by * not &, but this isn't an optional arg.
1711 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, range_gen_arg);
John Zulauf540266b2020-04-06 18:54:53 -06001712}
1713
1714template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001715void AccessContext::ApplyUpdateAction(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, const Action &action) {
1716 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1717 if (!gen) return;
1718 UpdateMemoryAccessState(&GetAccessStateMap(view_gen.GetAddressType()), action, *gen);
John Zulauf540266b2020-04-06 18:54:53 -06001719}
1720
John Zulaufd0ec59f2021-03-13 14:25:08 -07001721void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state,
1722 const AttachmentViewGenVector &attachment_views, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001723 const ResourceUsageTag tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001724 UpdateStateResolveAction update(*this, tag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001725 ResolveOperation(update, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001726}
1727
John Zulaufd0ec59f2021-03-13 14:25:08 -07001728void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
John Zulauf14940722021-04-12 15:19:02 -06001729 uint32_t subpass, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001730 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001731
1732 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1733 if (rp_state.attachment_last_subpass[i] == subpass) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001734 const auto &view_gen = attachment_views[i];
1735 if (!view_gen.IsValid()) continue; // UNUSED
John Zulaufaff20662020-06-01 14:07:58 -06001736
1737 const auto &ci = attachment_ci[i];
1738 const bool has_depth = FormatHasDepth(ci.format);
1739 const bool has_stencil = FormatHasStencil(ci.format);
1740 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001741 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001742
1743 if (is_color && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001744 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
1745 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001746 } else {
John Zulaufaff20662020-06-01 14:07:58 -06001747 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001748 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1749 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001750 }
John Zulauf57261402021-08-13 11:32:06 -06001751 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001752 if (has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001753 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1754 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001755 }
1756 }
1757 }
1758 }
1759}
1760
John Zulauf540266b2020-04-06 18:54:53 -06001761template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001762void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001763 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001764 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001765 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001766 }
1767}
1768
1769void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001770 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1771 auto &context = contexts[subpass_index];
John Zulauf22aefed2021-03-11 18:14:35 -07001772 ApplyTrackbackStackAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001773 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001774 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001775 }
1776 }
1777}
1778
John Zulauf4fa68462021-04-26 21:04:22 -06001779// Caller must ensure that lifespan of this is less than from
1780void AccessContext::ImportAsyncContexts(const AccessContext &from) { async_ = from.async_; }
1781
John Zulauf355e49b2020-04-24 15:11:15 -06001782// Suitable only for *subpass* access contexts
John Zulaufd0ec59f2021-03-13 14:25:08 -07001783HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const AttachmentViewGen &attach_view) const {
1784 if (!attach_view.IsValid()) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001785
John Zulauf355e49b2020-04-24 15:11:15 -06001786 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001787 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001788
1789 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001790 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1791 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001792 HazardResult hazard = track_back.context->DetectImageBarrierHazard(attach_view, merged_barrier, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001793 if (!hazard.hazard) {
1794 // The Async hazard check is against the current context's async set.
John Zulaufd0ec59f2021-03-13 14:25:08 -07001795 hazard = DetectImageBarrierHazard(attach_view, merged_barrier, kDetectAsync);
John Zulauf355e49b2020-04-24 15:11:15 -06001796 }
John Zulaufa0a98292020-09-18 09:30:10 -06001797
John Zulauf355e49b2020-04-24 15:11:15 -06001798 return hazard;
1799}
1800
John Zulaufb02c1eb2020-10-06 16:33:36 -06001801void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001802 const AttachmentViewGenVector &attachment_views, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001803 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001804 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001805 for (const auto &transition : transitions) {
1806 const auto prev_pass = transition.prev_pass;
John Zulaufd0ec59f2021-03-13 14:25:08 -07001807 const auto &view_gen = attachment_views[transition.attachment];
1808 if (!view_gen.IsValid()) continue;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001809
1810 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1811 assert(trackback);
1812
1813 // Import the attachments into the current context
1814 const auto *prev_context = trackback->context;
1815 assert(prev_context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001816 const auto address_type = view_gen.GetAddressType();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001817 auto &target_map = GetAccessStateMap(address_type);
1818 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001819 prev_context->ResolveAccessRange(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action, &target_map,
1820 &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001821 }
1822
John Zulauf86356ca2020-10-19 11:46:41 -06001823 // If there were no transitions skip this global map walk
1824 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001825 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001826 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001827 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001828}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001829
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001830void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulauf669dfd52021-01-27 17:15:28 -07001831 auto *events_context = GetCurrentEventsContext();
1832 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06001833 events_context->ApplyBarrier(src, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001834}
1835
locke-lunarg61870c22020-06-09 14:51:50 -06001836bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1837 const char *func_name) const {
1838 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001839 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001840 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001841 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001842 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001843 return skip;
1844 }
1845
1846 using DescriptorClass = cvdescriptorset::DescriptorClass;
1847 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1848 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
locke-lunarg61870c22020-06-09 14:51:50 -06001849 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1850
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001851 for (const auto &stage_state : pipe->stage_state) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06001852 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->create_info.graphics.pRasterizationState &&
1853 pipe->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001854 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001855 }
locke-lunarg61870c22020-06-09 14:51:50 -06001856 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001857 const auto *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001858 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001859 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001860 const auto descriptor_type = binding_it.GetType();
1861 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1862 auto array_idx = 0;
1863
1864 if (binding_it.IsVariableDescriptorCount()) {
1865 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1866 }
1867 SyncStageAccessIndex sync_index =
1868 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1869
1870 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1871 uint32_t index = i - index_range.start;
1872 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1873 switch (descriptor->GetClass()) {
1874 case DescriptorClass::ImageSampler:
1875 case DescriptorClass::Image: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07001876 if (descriptor->Invalid()) {
1877 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001878 }
Jeremy Gebbena08da232022-02-01 15:14:52 -07001879
1880 // NOTE: ImageSamplerDescriptor inherits from ImageDescriptor, so this cast works for both types.
1881 const auto *image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1882 const auto *img_view_state = image_descriptor->GetImageViewState();
1883 VkImageLayout image_layout = image_descriptor->GetImageLayout();
1884
John Zulauf361fb532020-07-22 10:45:39 -06001885 HazardResult hazard;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06001886 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
1887 // Descriptors, so we do not have to worry about depth slicing here.
1888 // See: VUID 00343
1889 assert(!img_view_state->IsDepthSliced());
John Zulauf110413c2021-03-20 05:38:38 -06001890 const IMAGE_STATE *img_state = img_view_state->image_state.get();
John Zulauf361fb532020-07-22 10:45:39 -06001891 const auto &subresource_range = img_view_state->normalized_subresource_range;
John Zulauf110413c2021-03-20 05:38:38 -06001892
1893 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1894 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1895 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
John Zulauf361fb532020-07-22 10:45:39 -06001896 // Input attachments are subject to raster ordering rules
1897 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001898 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001899 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001900 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range);
John Zulauf361fb532020-07-22 10:45:39 -06001901 }
John Zulauf110413c2021-03-20 05:38:38 -06001902
John Zulauf33fc1d52020-07-17 11:01:10 -06001903 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001904 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001905 img_view_state->image_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001906 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1907 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001908 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001909 sync_state_->report_data->FormatHandle(img_view_state->image_view()).c_str(),
1910 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1911 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001912 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1913 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001914 set_binding.first.binding, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001915 }
1916 break;
1917 }
1918 case DescriptorClass::TexelBuffer: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07001919 const auto *texel_descriptor = static_cast<const TexelDescriptor *>(descriptor);
1920 if (texel_descriptor->Invalid()) {
1921 continue;
1922 }
1923 const auto *buf_view_state = texel_descriptor->GetBufferViewState();
1924 const auto *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001925 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001926 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001927 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001928 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001929 buf_view_state->buffer_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001930 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1931 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001932 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view()).c_str(),
1933 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1934 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001935 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001936 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001937 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001938 }
1939 break;
1940 }
1941 case DescriptorClass::GeneralBuffer: {
1942 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
Jeremy Gebbena08da232022-02-01 15:14:52 -07001943 if (buffer_descriptor->Invalid()) {
1944 continue;
1945 }
1946 const auto *buf_state = buffer_descriptor->GetBufferState();
John Zulauf3e86bf02020-09-12 10:47:57 -06001947 const ResourceAccessRange range =
1948 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001949 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001950 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001951 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001952 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001953 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1954 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001955 sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
1956 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1957 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001958 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001959 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001960 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001961 }
1962 break;
1963 }
1964 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1965 default:
1966 break;
1967 }
1968 }
1969 }
1970 }
1971 return skip;
1972}
1973
1974void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
John Zulauf14940722021-04-12 15:19:02 -06001975 const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001976 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001977 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001978 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001979 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001980 return;
1981 }
1982
1983 using DescriptorClass = cvdescriptorset::DescriptorClass;
1984 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1985 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
locke-lunarg61870c22020-06-09 14:51:50 -06001986 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1987
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001988 for (const auto &stage_state : pipe->stage_state) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06001989 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->create_info.graphics.pRasterizationState &&
1990 pipe->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001991 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001992 }
locke-lunarg61870c22020-06-09 14:51:50 -06001993 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001994 const auto *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001995 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001996 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001997 const auto descriptor_type = binding_it.GetType();
1998 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1999 auto array_idx = 0;
2000
2001 if (binding_it.IsVariableDescriptorCount()) {
2002 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2003 }
2004 SyncStageAccessIndex sync_index =
2005 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
2006
2007 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2008 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2009 switch (descriptor->GetClass()) {
2010 case DescriptorClass::ImageSampler:
2011 case DescriptorClass::Image: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07002012 // NOTE: ImageSamplerDescriptor inherits from ImageDescriptor, so this cast works for both types.
2013 const auto *image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
2014 if (image_descriptor->Invalid()) {
2015 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002016 }
Jeremy Gebbena08da232022-02-01 15:14:52 -07002017 const auto *img_view_state = image_descriptor->GetImageViewState();
Jeremy Gebben11a68a32021-07-29 11:59:22 -06002018 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
2019 // Descriptors, so we do not have to worry about depth slicing here.
2020 // See: VUID 00343
2021 assert(!img_view_state->IsDepthSliced());
locke-lunarg61870c22020-06-09 14:51:50 -06002022 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002023 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
John Zulauf110413c2021-03-20 05:38:38 -06002024 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
2025 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
2026 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kRaster,
2027 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002028 } else {
John Zulauf110413c2021-03-20 05:38:38 -06002029 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kNonAttachment,
2030 img_view_state->normalized_subresource_range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002031 }
locke-lunarg61870c22020-06-09 14:51:50 -06002032 break;
2033 }
2034 case DescriptorClass::TexelBuffer: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07002035 const auto *texel_descriptor = static_cast<const TexelDescriptor *>(descriptor);
2036 if (texel_descriptor->Invalid()) {
2037 continue;
2038 }
2039 const auto *buf_view_state = texel_descriptor->GetBufferViewState();
2040 const auto *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002041 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07002042 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002043 break;
2044 }
2045 case DescriptorClass::GeneralBuffer: {
2046 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
Jeremy Gebbena08da232022-02-01 15:14:52 -07002047 if (buffer_descriptor->Invalid()) {
2048 continue;
2049 }
2050 const auto *buf_state = buffer_descriptor->GetBufferState();
John Zulauf3e86bf02020-09-12 10:47:57 -06002051 const ResourceAccessRange range =
2052 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07002053 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002054 break;
2055 }
2056 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2057 default:
2058 break;
2059 }
2060 }
2061 }
2062 }
2063}
2064
2065bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2066 bool skip = false;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002067 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002068 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002069 return skip;
2070 }
2071
2072 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2073 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002074 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002075
2076 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002077 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002078 if (binding_description.binding < binding_buffers_size) {
2079 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002080 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002081
locke-lunarg1ae57d62020-11-18 10:49:19 -07002082 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002083 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2084 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002085 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002086 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002087 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002088 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
2089 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
2090 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002091 }
2092 }
2093 }
2094 return skip;
2095}
2096
John Zulauf14940722021-04-12 15:19:02 -06002097void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002098 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002099 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002100 return;
2101 }
2102 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2103 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002104 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002105
2106 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002107 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002108 if (binding_description.binding < binding_buffers_size) {
2109 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002110 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002111
locke-lunarg1ae57d62020-11-18 10:49:19 -07002112 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002113 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2114 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002115 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2116 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002117 }
2118 }
2119}
2120
2121bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2122 bool skip = false;
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002123 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->Destroyed()) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002124 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002125 }
locke-lunarg61870c22020-06-09 14:51:50 -06002126
locke-lunarg1ae57d62020-11-18 10:49:19 -07002127 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002128 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002129 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2130 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002131 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002132 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002133 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002134 index_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
2135 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer()).c_str(),
2136 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002137 }
2138
2139 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2140 // We will detect more accurate range in the future.
2141 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2142 return skip;
2143}
2144
John Zulauf14940722021-04-12 15:19:02 -06002145void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag tag) {
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002146 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 -06002147
locke-lunarg1ae57d62020-11-18 10:49:19 -07002148 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002149 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002150 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2151 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002152 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002153
2154 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2155 // We will detect more accurate range in the future.
2156 RecordDrawVertex(UINT32_MAX, 0, tag);
2157}
2158
2159bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002160 bool skip = false;
2161 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002162 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002163 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002164}
2165
John Zulauf14940722021-04-12 15:19:02 -06002166void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002167 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002168 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002169 }
locke-lunarg61870c22020-06-09 14:51:50 -06002170}
2171
John Zulauf64ffe552021-02-06 10:25:07 -07002172void CommandBufferAccessContext::RecordBeginRenderPass(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2173 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
John Zulauf14940722021-04-12 15:19:02 -06002174 const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002175 // Create an access context the current renderpass.
John Zulauf64ffe552021-02-06 10:25:07 -07002176 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002177 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf64ffe552021-02-06 10:25:07 -07002178 current_renderpass_context_->RecordBeginRenderPass(tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002179 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002180}
2181
John Zulauf8eda1562021-04-13 17:06:41 -06002182void CommandBufferAccessContext::RecordNextSubpass(ResourceUsageTag prev_tag, ResourceUsageTag next_tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002183 assert(current_renderpass_context_);
John Zulauf64ffe552021-02-06 10:25:07 -07002184 current_renderpass_context_->RecordNextSubpass(prev_tag, next_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002185 current_context_ = &current_renderpass_context_->CurrentContext();
2186}
2187
John Zulauf8eda1562021-04-13 17:06:41 -06002188void CommandBufferAccessContext::RecordEndRenderPass(const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002189 assert(current_renderpass_context_);
2190 if (!current_renderpass_context_) return;
2191
John Zulauf8eda1562021-04-13 17:06:41 -06002192 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002193 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002194 current_renderpass_context_ = nullptr;
2195}
2196
John Zulauf4a6105a2020-11-17 15:11:05 -07002197void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2198 // Erase is okay with the key not being
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002199 auto event_state = sync_state_->Get<EVENT_STATE>(event);
John Zulauf669dfd52021-01-27 17:15:28 -07002200 if (event_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002201 GetCurrentEventsContext()->Destroy(event_state.get());
John Zulaufd5115702021-01-18 12:34:33 -07002202 }
2203}
2204
John Zulaufae842002021-04-15 18:20:55 -06002205// The is the recorded cb context
John Zulauf4fa68462021-04-26 21:04:22 -06002206bool CommandBufferAccessContext::ValidateFirstUse(CommandBufferAccessContext *proxy_context, const char *func_name,
2207 uint32_t index) const {
2208 assert(proxy_context);
2209 auto *events_context = proxy_context->GetCurrentEventsContext();
2210 auto *access_context = proxy_context->GetCurrentAccessContext();
2211 const ResourceUsageTag base_tag = proxy_context->GetTagLimit();
John Zulaufae842002021-04-15 18:20:55 -06002212 bool skip = false;
2213 ResourceUsageRange tag_range = {0, 0};
2214 const AccessContext *recorded_context = GetCurrentAccessContext();
2215 assert(recorded_context);
2216 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002217 auto log_msg = [this](const HazardResult &hazard, const CommandBufferAccessContext &active_context, const char *func_name,
John Zulaufae842002021-04-15 18:20:55 -06002218 uint32_t index) {
2219 const auto cb_handle = active_context.cb_state_->commandBuffer();
2220 const auto recorded_handle = cb_state_->commandBuffer();
John Zulauf4fa68462021-04-26 21:04:22 -06002221 const auto *report_data = sync_state_->report_data;
John Zulaufae842002021-04-15 18:20:55 -06002222 return sync_state_->LogError(cb_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf4fa68462021-04-26 21:04:22 -06002223 "%s: Hazard %s for entry %" PRIu32 ", %s, Recorded access info %s. Access info %s.", func_name,
2224 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(recorded_handle).c_str(),
2225 FormatUsage(*hazard.recorded_access).c_str(), active_context.FormatUsage(hazard).c_str());
John Zulaufae842002021-04-15 18:20:55 -06002226 };
2227 for (const auto &sync_op : sync_ops_) {
John Zulauf4fa68462021-04-26 21:04:22 -06002228 // we update the range to any include layout transition first use writes,
2229 // as they are stored along with the source scope (as effective barrier) when recorded
2230 tag_range.end = sync_op.tag + 1;
John Zulauf610e28c2021-08-03 17:46:23 -06002231 skip |= sync_op.sync_op->ReplayValidate(sync_op.tag, *this, base_tag, proxy_context);
John Zulauf4fa68462021-04-26 21:04:22 -06002232
John Zulaufae842002021-04-15 18:20:55 -06002233 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context);
2234 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002235 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002236 }
2237 // NOTE: Add call to replay validate here when we add support for syncop with non-trivial replay
John Zulauf4fa68462021-04-26 21:04:22 -06002238 // Record the barrier into the proxy context.
2239 sync_op.sync_op->DoRecord(base_tag + sync_op.tag, access_context, events_context);
2240 tag_range.begin = tag_range.end;
John Zulaufae842002021-04-15 18:20:55 -06002241 }
2242
2243 // and anything after the last syncop
John Zulaufae842002021-04-15 18:20:55 -06002244 tag_range.end = ResourceUsageRecord::kMaxIndex;
2245 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context);
2246 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002247 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002248 }
2249
2250 return skip;
2251}
2252
John Zulauf4fa68462021-04-26 21:04:22 -06002253void CommandBufferAccessContext::RecordExecutedCommandBuffer(const CommandBufferAccessContext &recorded_cb_context, CMD_TYPE cmd) {
2254 auto *events_context = GetCurrentEventsContext();
2255 auto *access_context = GetCurrentAccessContext();
2256 const AccessContext *recorded_context = recorded_cb_context.GetCurrentAccessContext();
2257 assert(recorded_context);
2258
2259 // Just run through the barriers ignoring the usage from the recorded context, as Resolve will overwrite outdated state
2260 const ResourceUsageTag base_tag = GetTagLimit();
2261 for (const auto &sync_op : recorded_cb_context.sync_ops_) {
2262 // we update the range to any include layout transition first use writes,
2263 // as they are stored along with the source scope (as effective barrier) when recorded
2264 sync_op.sync_op->DoRecord(base_tag + sync_op.tag, access_context, events_context);
2265 }
2266
2267 ResourceUsageRange tag_range = ImportRecordedAccessLog(recorded_cb_context);
2268 assert(base_tag == tag_range.begin); // to ensure the to offset calculation agree
2269 ResolveRecordedContext(*recorded_context, tag_range.begin);
2270}
2271
2272void CommandBufferAccessContext::ResolveRecordedContext(const AccessContext &recorded_context, ResourceUsageTag offset) {
2273 auto tag_offset = [offset](ResourceAccessState *access) { access->OffsetTag(offset); };
2274
2275 auto *access_context = GetCurrentAccessContext();
2276 for (auto address_type : kAddressTypes) {
2277 recorded_context.ResolveAccessRange(address_type, kFullRange, tag_offset, &access_context->GetAccessStateMap(address_type),
2278 nullptr, false);
2279 }
2280}
2281
2282ResourceUsageRange CommandBufferAccessContext::ImportRecordedAccessLog(const CommandBufferAccessContext &recorded_context) {
2283 // The execution references ensure lifespan for the referenced child CB's...
2284 ResourceUsageRange tag_range(GetTagLimit(), 0);
John Zulauf3c2a0b32021-07-14 11:14:52 -06002285 cbs_referenced_.emplace(recorded_context.cb_state_);
John Zulauf4fa68462021-04-26 21:04:22 -06002286 access_log_.insert(access_log_.end(), recorded_context.access_log_.cbegin(), recorded_context.access_log_.end());
2287 tag_range.end = access_log_.size();
2288 return tag_range;
2289}
2290
John Zulaufae842002021-04-15 18:20:55 -06002291class HazardDetectFirstUse {
2292 public:
2293 HazardDetectFirstUse(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range)
2294 : recorded_use_(recorded_use), tag_range_(tag_range) {}
2295 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
2296 return pos->second.DetectHazard(recorded_use_, tag_range_);
2297 }
2298 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
2299 return pos->second.DetectAsyncHazard(recorded_use_, tag_range_, start_tag);
2300 }
2301
2302 private:
2303 const ResourceAccessState &recorded_use_;
2304 const ResourceUsageRange &tag_range_;
2305};
2306
2307// This is called with the *recorded* command buffers access context, with the *active* access context pass in, againsts which
2308// hazards will be detected
2309HazardResult AccessContext::DetectFirstUseHazard(const ResourceUsageRange &tag_range, const AccessContext &access_context) const {
2310 HazardResult hazard;
2311 for (const auto address_type : kAddressTypes) {
2312 const auto &recorded_access_map = GetAccessStateMap(address_type);
2313 for (const auto &recorded_access : recorded_access_map) {
2314 // Cull any entries not in the current tag range
2315 if (!recorded_access.second.FirstAccessInTagRange(tag_range)) continue;
2316 HazardDetectFirstUse detector(recorded_access.second, tag_range);
2317 hazard = access_context.DetectHazard(address_type, detector, recorded_access.first, DetectOptions::kDetectAll);
2318 if (hazard.hazard) break;
2319 }
2320 }
2321
2322 return hazard;
2323}
2324
John Zulauf64ffe552021-02-06 10:25:07 -07002325bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
John Zulauffaea0ee2021-01-14 14:01:32 -07002326 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002327 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002328 const auto &sync_state = ex_context.GetSyncState();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002329 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002330 if (!pipe) {
2331 return skip;
2332 }
2333
2334 const auto &create_info = pipe->create_info.graphics;
2335 if (create_info.pRasterizationState && create_info.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002336 return skip;
2337 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002338 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002339 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg37047832020-06-12 13:44:45 -06002340
John Zulauf1a224292020-06-30 14:52:13 -06002341 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002342 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002343 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2344 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002345 if (location >= subpass.colorAttachmentCount ||
2346 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002347 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002348 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002349 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2350 if (!view_gen.IsValid()) continue;
2351 HazardResult hazard =
2352 current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
2353 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment);
locke-lunarg96dc9632020-06-10 17:22:18 -06002354 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002355 const VkImageView view_handle = view_gen.GetViewState()->image_view();
John Zulaufd0ec59f2021-03-13 14:25:08 -07002356 skip |= sync_state.LogError(view_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002357 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002358 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002359 sync_state.report_data->FormatHandle(view_handle).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002360 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002361 location, ex_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002362 }
2363 }
2364 }
locke-lunarg37047832020-06-12 13:44:45 -06002365
2366 // PHASE1 TODO: Add layout based read/vs. write selection.
2367 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002368 const uint32_t depth_stencil_attachment =
Jeremy Gebben11af9792021-08-20 10:20:09 -06002369 GetSubpassDepthStencilAttachmentIndex(pipe->create_info.graphics.pDepthStencilState, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002370
2371 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2372 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2373 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002374 bool depth_write = false, stencil_write = false;
2375
2376 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002377 if (!FormatIsStencilOnly(view_state.create_info.format) && create_info.pDepthStencilState->depthTestEnable &&
2378 create_info.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002379 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2380 depth_write = true;
2381 }
2382 // PHASE1 TODO: It needs to check if stencil is writable.
2383 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2384 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2385 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002386 if (!FormatIsDepthOnly(view_state.create_info.format) && create_info.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002387 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2388 stencil_write = true;
2389 }
2390
2391 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2392 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002393 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2394 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2395 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002396 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002397 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002398 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002399 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002400 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002401 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2402 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002403 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002404 }
2405 }
2406 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002407 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2408 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2409 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002410 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002411 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002412 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002413 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002414 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002415 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2416 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002417 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002418 }
locke-lunarg61870c22020-06-09 14:51:50 -06002419 }
2420 }
2421 return skip;
2422}
2423
John Zulauf14940722021-04-12 15:19:02 -06002424void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002425 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002426 if (!pipe) {
2427 return;
2428 }
2429
2430 const auto &create_info = pipe->create_info.graphics;
2431 if (create_info.pRasterizationState && create_info.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002432 return;
2433 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002434 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002435 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002436
John Zulauf1a224292020-06-30 14:52:13 -06002437 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002438 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002439 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2440 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002441 if (location >= subpass.colorAttachmentCount ||
2442 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002443 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002444 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002445 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2446 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2447 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2448 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002449 }
2450 }
locke-lunarg37047832020-06-12 13:44:45 -06002451
2452 // PHASE1 TODO: Add layout based read/vs. write selection.
2453 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002454 const uint32_t depth_stencil_attachment =
Jeremy Gebben11af9792021-08-20 10:20:09 -06002455 GetSubpassDepthStencilAttachmentIndex(create_info.pDepthStencilState, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002456 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2457 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2458 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002459 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002460 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2461 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002462
2463 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002464 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) && create_info.pDepthStencilState->depthTestEnable &&
2465 create_info.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002466 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2467 depth_write = true;
2468 }
2469 // PHASE1 TODO: It needs to check if stencil is writable.
2470 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2471 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2472 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002473 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) && create_info.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002474 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2475 stencil_write = true;
2476 }
2477
John Zulaufd0ec59f2021-03-13 14:25:08 -07002478 if (depth_write || stencil_write) {
2479 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2480 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2481 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2482 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002483 }
locke-lunarg61870c22020-06-09 14:51:50 -06002484 }
2485}
2486
John Zulauf64ffe552021-02-06 10:25:07 -07002487bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002488 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002489 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002490 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002491 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002492 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002493 func_name);
2494
John Zulauf355e49b2020-04-24 15:11:15 -06002495 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002496 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002497 skip |=
2498 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002499 if (!skip) {
2500 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2501 // on a copy of the (empty) next context.
2502 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2503 AccessContext temp_context(next_context);
2504 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002505 skip |=
2506 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002507 }
John Zulauf7635de32020-05-29 17:14:15 -06002508 return skip;
2509}
John Zulauf64ffe552021-02-06 10:25:07 -07002510bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002511 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002512 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002513 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002514 current_subpass_);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002515 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_,
2516
2517 attachment_views_, func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002518 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002519 return skip;
2520}
2521
John Zulauf64ffe552021-02-06 10:25:07 -07002522AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002523 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002524}
2525
John Zulauf64ffe552021-02-06 10:25:07 -07002526bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2527 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002528 bool skip = false;
2529
John Zulauf7635de32020-05-29 17:14:15 -06002530 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2531 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2532 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2533 // to apply and only copy then, if this proves a hot spot.
2534 std::unique_ptr<AccessContext> proxy_for_current;
2535
John Zulauf355e49b2020-04-24 15:11:15 -06002536 // Validate the "finalLayout" transitions to external
2537 // Get them from where there we're hidding in the extra entry.
2538 const auto &final_transitions = rp_state_->subpass_transitions.back();
2539 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002540 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002541 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2542 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002543 auto *context = trackback.context;
2544
2545 if (transition.prev_pass == current_subpass_) {
2546 if (!proxy_for_current) {
2547 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
John Zulauf64ffe552021-02-06 10:25:07 -07002548 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002549 }
2550 context = proxy_for_current.get();
2551 }
2552
John Zulaufa0a98292020-09-18 09:30:10 -06002553 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2554 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002555 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002556 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002557 skip |= ex_context.GetSyncState().LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002558 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07002559 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2560 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2561 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2562 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002563 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002564 }
2565 }
2566 return skip;
2567}
2568
John Zulauf14940722021-04-12 15:19:02 -06002569void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002570 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002571 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002572}
2573
John Zulauf14940722021-04-12 15:19:02 -06002574void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002575 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2576 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002577
2578 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2579 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002580 const AttachmentViewGen &view_gen = attachment_views_[i];
2581 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002582
2583 const auto &ci = attachment_ci[i];
2584 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002585 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002586 const bool is_color = !(has_depth || has_stencil);
2587
2588 if (is_color) {
John Zulauf57261402021-08-13 11:32:06 -06002589 const SyncStageAccessIndex load_op = ColorLoadUsage(ci.loadOp);
2590 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2591 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, load_op,
2592 SyncOrdering::kColorAttachment, tag);
2593 }
John Zulauf1507ee42020-05-18 11:33:09 -06002594 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002595 if (has_depth) {
John Zulauf57261402021-08-13 11:32:06 -06002596 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.loadOp);
2597 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2598 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_op,
2599 SyncOrdering::kDepthStencilAttachment, tag);
2600 }
John Zulauf1507ee42020-05-18 11:33:09 -06002601 }
2602 if (has_stencil) {
John Zulauf57261402021-08-13 11:32:06 -06002603 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.stencilLoadOp);
2604 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2605 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, load_op,
2606 SyncOrdering::kDepthStencilAttachment, tag);
2607 }
John Zulauf1507ee42020-05-18 11:33:09 -06002608 }
2609 }
2610 }
2611 }
2612}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002613AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2614 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2615 AttachmentViewGenVector view_gens;
2616 VkExtent3D extent = CastTo3D(render_area.extent);
2617 VkOffset3D offset = CastTo3D(render_area.offset);
2618 view_gens.reserve(attachment_views.size());
2619 for (const auto *view : attachment_views) {
2620 view_gens.emplace_back(view, offset, extent);
2621 }
2622 return view_gens;
2623}
John Zulauf64ffe552021-02-06 10:25:07 -07002624RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2625 VkQueueFlags queue_flags,
2626 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2627 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002628 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002629 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002630 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002631 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002632 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002633 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002634 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002635}
John Zulauf14940722021-04-12 15:19:02 -06002636void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag tag) {
John Zulauf64ffe552021-02-06 10:25:07 -07002637 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002638 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002639 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002640 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002641}
John Zulauf1507ee42020-05-18 11:33:09 -06002642
John Zulauf14940722021-04-12 15:19:02 -06002643void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag prev_subpass_tag, const ResourceUsageTag next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002644 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulaufd0ec59f2021-03-13 14:25:08 -07002645 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
2646 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002647
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002648 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2649 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002650 current_subpass_++;
2651 assert(current_subpass_ < subpass_contexts_.size());
John Zulauffaea0ee2021-01-14 14:01:32 -07002652 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2653 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002654 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002655}
2656
John Zulauf14940722021-04-12 15:19:02 -06002657void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002658 // Add the resolve and store accesses
John Zulaufd0ec59f2021-03-13 14:25:08 -07002659 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, tag);
2660 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002661
John Zulauf355e49b2020-04-24 15:11:15 -06002662 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002663 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002664
2665 // Add the "finalLayout" transitions to external
2666 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002667 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2668 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2669 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002670 const auto &final_transitions = rp_state_->subpass_transitions.back();
2671 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002672 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002673 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002674 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002675 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002676 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002677 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002678 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002679 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002680 }
2681}
2682
Jeremy Gebben40a22942020-12-22 14:22:06 -07002683SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002684 SyncExecScope result;
2685 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002686 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2687 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002688 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2689 return result;
2690}
2691
Jeremy Gebben40a22942020-12-22 14:22:06 -07002692SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002693 SyncExecScope result;
2694 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002695 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2696 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002697 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2698 return result;
2699}
2700
2701SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002702 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002703 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002704 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002705 dst_access_scope = 0;
2706}
2707
2708template <typename Barrier>
2709SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002710 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002711 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002712 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002713 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2714}
2715
2716SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002717 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2718 if (barrier) {
2719 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002720 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002721 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002722
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002723 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002724 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002725 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2726
2727 } else {
2728 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002729 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002730 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2731
2732 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002733 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002734 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2735 }
2736}
2737
2738template <typename Barrier>
2739SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2740 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2741 src_exec_scope = src.exec_scope;
2742 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2743
2744 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002745 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002746 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002747}
2748
John Zulaufb02c1eb2020-10-06 16:33:36 -06002749// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2750void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2751 for (const auto &barrier : barriers) {
2752 ApplyBarrier(barrier, layout_transition);
2753 }
2754}
2755
John Zulauf89311b42020-09-29 16:28:47 -06002756// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2757// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2758// lazily, s.t. no previous access reports should need layout transitions.
John Zulauf14940722021-04-12 15:19:02 -06002759void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06002760 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002761 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002762 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002763 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002764 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002765 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002766 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002767}
John Zulauf9cb530d2019-09-30 14:14:10 -06002768HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2769 HazardResult hazard;
2770 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002771 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002772 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002773 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002774 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002775 }
2776 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002777 // Write operation:
2778 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2779 // If reads exists -- test only against them because either:
2780 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2781 // * 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
2782 // the current write happens after the reads, so just test the write against the reades
2783 // Otherwise test against last_write
2784 //
2785 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002786 if (last_reads.size()) {
2787 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002788 if (IsReadHazard(usage_stage, read_access)) {
2789 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2790 break;
2791 }
2792 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002793 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002794 // Write-After-Write check -- if we have a previous write to test against
2795 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002796 }
2797 }
2798 return hazard;
2799}
2800
John Zulauf4fa68462021-04-26 21:04:22 -06002801HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering ordering_rule) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002802 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf4fa68462021-04-26 21:04:22 -06002803 return DetectHazard(usage_index, ordering);
2804}
2805
2806HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const OrderingBarrier &ordering) const {
John Zulauf69133422020-05-20 14:55:53 -06002807 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2808 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002809 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002810 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002811 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2812 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002813 if (IsRead(usage_bit)) {
2814 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2815 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2816 if (is_raw_hazard) {
2817 // NOTE: we know last_write is non-zero
2818 // See if the ordering rules save us from the simple RAW check above
2819 // First check to see if the current usage is covered by the ordering rules
2820 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2821 const bool usage_is_ordered =
2822 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2823 if (usage_is_ordered) {
2824 // Now see of the most recent write (or a subsequent read) are ordered
2825 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2826 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002827 }
2828 }
John Zulauf4285ee92020-09-23 10:20:52 -06002829 if (is_raw_hazard) {
2830 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2831 }
John Zulauf5c628d02021-05-04 15:46:36 -06002832 } else if (usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2833 // For Image layout transitions, the barrier represents the first synchronization/access scope of the layout transition
2834 return DetectBarrierHazard(usage_index, ordering.exec_scope, ordering.access_scope);
John Zulauf361fb532020-07-22 10:45:39 -06002835 } else {
2836 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002837 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002838 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002839 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002840 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002841 if (usage_write_is_ordered) {
2842 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2843 ordered_stages = GetOrderedStages(ordering);
2844 }
2845 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2846 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002847 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002848 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2849 if (IsReadHazard(usage_stage, read_access)) {
2850 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2851 break;
2852 }
John Zulaufd14743a2020-07-03 09:42:39 -06002853 }
2854 }
John Zulauf2a344ca2021-09-09 17:07:19 -06002855 } else if (last_write.any() && !(last_write_is_ordered && usage_write_is_ordered)) {
2856 bool ilt_ilt_hazard = false;
2857 if ((usage_index == SYNC_IMAGE_LAYOUT_TRANSITION) && (usage_bit == last_write)) {
2858 // ILT after ILT is a special case where we check the 2nd access scope of the first ILT against the first access
2859 // scope of the second ILT, which has been passed (smuggled?) in the ordering barrier
2860 ilt_ilt_hazard = !(write_barriers & ordering.access_scope).any();
2861 }
2862 if (ilt_ilt_hazard || IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002863 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002864 }
John Zulauf69133422020-05-20 14:55:53 -06002865 }
2866 }
2867 return hazard;
2868}
2869
John Zulaufae842002021-04-15 18:20:55 -06002870HazardResult ResourceAccessState::DetectHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range) const {
2871 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002872 using Size = FirstAccesses::size_type;
2873 const auto &recorded_accesses = recorded_use.first_accesses_;
2874 Size count = recorded_accesses.size();
2875 if (count) {
2876 const auto &last_access = recorded_accesses.back();
2877 bool do_write_last = IsWrite(last_access.usage_index);
2878 if (do_write_last) --count;
John Zulaufae842002021-04-15 18:20:55 -06002879
John Zulauf4fa68462021-04-26 21:04:22 -06002880 for (Size i = 0; i < count; ++count) {
2881 const auto &first = recorded_accesses[i];
2882 // Skip and quit logic
2883 if (first.tag < tag_range.begin) continue;
2884 if (first.tag >= tag_range.end) {
2885 do_write_last = false; // ignore last since we know it can't be in tag_range
2886 break;
2887 }
2888
2889 hazard = DetectHazard(first.usage_index, first.ordering_rule);
2890 if (hazard.hazard) {
2891 hazard.AddRecordedAccess(first);
2892 break;
2893 }
2894 }
2895
2896 if (do_write_last && tag_range.includes(last_access.tag)) {
2897 // Writes are a bit special... both for the "most recent" access logic, and layout transition specific logic
2898 OrderingBarrier barrier = GetOrderingRules(last_access.ordering_rule);
2899 if (last_access.usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2900 // Or in the layout first access scope as a barrier... IFF the usage is an ILT
2901 // this was saved off in the "apply barriers" logic to simplify ILT access checks as they straddle
2902 // the barrier that applies them
2903 barrier |= recorded_use.first_write_layout_ordering_;
2904 }
2905 // Any read stages present in the recorded context (this) are most recent to the write, and thus mask those stages in
2906 // the active context
2907 if (recorded_use.first_read_stages_) {
2908 // we need to ignore the first use read stage in the active context (so we add them to the ordering rule),
2909 // reads in the active context are not "most recent" as all recorded context operations are *after* them
2910 // This supresses only RAW checks for stages present in the recorded context, but not those only present in the
2911 // active context.
2912 barrier.exec_scope |= recorded_use.first_read_stages_;
2913 // if there are any first use reads, we suppress WAW by injecting the active context write in the ordering rule
2914 barrier.access_scope |= FlagBit(last_access.usage_index);
2915 }
2916 hazard = DetectHazard(last_access.usage_index, barrier);
2917 if (hazard.hazard) {
2918 hazard.AddRecordedAccess(last_access);
2919 }
2920 }
John Zulaufae842002021-04-15 18:20:55 -06002921 }
2922 return hazard;
2923}
2924
John Zulauf2f952d22020-02-10 11:34:51 -07002925// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf14940722021-04-12 15:19:02 -06002926HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002927 HazardResult hazard;
2928 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002929 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2930 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2931 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002932 if (IsRead(usage)) {
John Zulauf14940722021-04-12 15:19:02 -06002933 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002934 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002935 }
2936 } else {
John Zulauf14940722021-04-12 15:19:02 -06002937 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002938 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002939 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002940 // Any reads during the other subpass will conflict with this write, so we need to check them all.
John Zulaufab7756b2020-12-29 16:10:16 -07002941 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06002942 if (read_access.tag >= start_tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002943 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002944 break;
2945 }
2946 }
John Zulauf2f952d22020-02-10 11:34:51 -07002947 }
2948 }
2949 return hazard;
2950}
2951
John Zulaufae842002021-04-15 18:20:55 -06002952HazardResult ResourceAccessState::DetectAsyncHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
2953 ResourceUsageTag start_tag) const {
2954 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002955 for (const auto &first : recorded_use.first_accesses_) {
John Zulaufae842002021-04-15 18:20:55 -06002956 // Skip and quit logic
2957 if (first.tag < tag_range.begin) continue;
2958 if (first.tag >= tag_range.end) break;
John Zulaufae842002021-04-15 18:20:55 -06002959
2960 hazard = DetectAsyncHazard(first.usage_index, start_tag);
John Zulauf4fa68462021-04-26 21:04:22 -06002961 if (hazard.hazard) {
2962 hazard.AddRecordedAccess(first);
2963 break;
2964 }
John Zulaufae842002021-04-15 18:20:55 -06002965 }
2966 return hazard;
2967}
2968
Jeremy Gebben40a22942020-12-22 14:22:06 -07002969HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002970 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002971 // Only supporting image layout transitions for now
2972 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2973 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002974 // only test for WAW if there no intervening read operations.
2975 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002976 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002977 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002978 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002979 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002980 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002981 break;
2982 }
2983 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002984 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2985 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2986 }
2987
2988 return hazard;
2989}
2990
Jeremy Gebben40a22942020-12-22 14:22:06 -07002991HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07002992 const SyncStageAccessFlags &src_access_scope,
John Zulauf14940722021-04-12 15:19:02 -06002993 const ResourceUsageTag event_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002994 // Only supporting image layout transitions for now
2995 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2996 HazardResult hazard;
2997 // only test for WAW if there no intervening read operations.
2998 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2999
John Zulaufab7756b2020-12-29 16:10:16 -07003000 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003001 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
3002 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07003003 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06003004 if (read_access.tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003005 // The read is in the events first synchronization scope, so we use a barrier hazard check
3006 // If the read stage is not in the src sync scope
3007 // *AND* not execution chained with an existing sync barrier (that's the or)
3008 // then the barrier access is unsafe (R/W after R)
3009 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3010 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3011 break;
3012 }
3013 } else {
3014 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3015 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3016 }
3017 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003018 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003019 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
John Zulauf14940722021-04-12 15:19:02 -06003020 if (write_tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003021 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3022 // So do a normal barrier hazard check
3023 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3024 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3025 }
3026 } else {
3027 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003028 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3029 }
John Zulaufd14743a2020-07-03 09:42:39 -06003030 }
John Zulauf361fb532020-07-22 10:45:39 -06003031
John Zulauf0cb5be22020-01-23 12:18:22 -07003032 return hazard;
3033}
3034
John Zulauf5f13a792020-03-10 07:31:21 -06003035// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3036// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3037// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3038void ResourceAccessState::Resolve(const ResourceAccessState &other) {
John Zulauf14940722021-04-12 15:19:02 -06003039 if (write_tag < other.write_tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003040 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3041 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003042 *this = other;
John Zulauf14940722021-04-12 15:19:02 -06003043 } else if (other.write_tag == write_tag) {
3044 // In the *equals* case for write operations, we merged the write barriers and the read state (but without the
John Zulauf5f13a792020-03-10 07:31:21 -06003045 // dependency chaining logic or any stage expansion)
3046 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003047 pending_write_barriers |= other.pending_write_barriers;
3048 pending_layout_transition |= other.pending_layout_transition;
3049 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf4fa68462021-04-26 21:04:22 -06003050 pending_layout_ordering_ |= other.pending_layout_ordering_;
John Zulauf5f13a792020-03-10 07:31:21 -06003051
John Zulaufd14743a2020-07-03 09:42:39 -06003052 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07003053 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06003054 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07003055 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003056 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003057 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003058 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003059 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3060 // but we should wait on profiling data for that.
3061 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003062 auto &my_read = last_reads[my_read_index];
3063 if (other_read.stage == my_read.stage) {
John Zulauf14940722021-04-12 15:19:02 -06003064 if (my_read.tag < other_read.tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003065 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003066 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003067 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003068 my_read.pending_dep_chain = other_read.pending_dep_chain;
3069 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3070 // May require tracking more than one access per stage.
3071 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003072 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06003073 // Since I'm overwriting the fragement stage read, also update the input attachment info
3074 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003075 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003076 }
John Zulauf14940722021-04-12 15:19:02 -06003077 } else if (other_read.tag == my_read.tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06003078 // The read tags match so merge the barriers
3079 my_read.barriers |= other_read.barriers;
3080 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003081 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003082
John Zulauf5f13a792020-03-10 07:31:21 -06003083 break;
3084 }
3085 }
3086 } else {
3087 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07003088 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06003089 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003090 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003091 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003092 }
John Zulauf5f13a792020-03-10 07:31:21 -06003093 }
3094 }
John Zulauf361fb532020-07-22 10:45:39 -06003095 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003096 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3097 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07003098
3099 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
3100 // of the copy and other into this using the update first logic.
3101 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
3102 // of the other first_accesses... )
3103 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
3104 FirstAccesses firsts(std::move(first_accesses_));
3105 first_accesses_.clear();
3106 first_read_stages_ = 0U;
3107 auto a = firsts.begin();
3108 auto a_end = firsts.end();
3109 for (auto &b : other.first_accesses_) {
John Zulauf14940722021-04-12 15:19:02 -06003110 // TODO: Determine whether some tag offset will be needed for PHASE II
3111 while ((a != a_end) && (a->tag < b.tag)) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003112 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3113 ++a;
3114 }
3115 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
3116 }
3117 for (; a != a_end; ++a) {
3118 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3119 }
3120 }
John Zulauf5f13a792020-03-10 07:31:21 -06003121}
3122
John Zulauf14940722021-04-12 15:19:02 -06003123void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003124 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3125 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003126 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003127 // Mulitple outstanding reads may be of interest and do dependency chains independently
3128 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3129 const auto usage_stage = PipelineStageBit(usage_index);
3130 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003131 for (auto &read_access : last_reads) {
3132 if (read_access.stage == usage_stage) {
3133 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003134 break;
3135 }
3136 }
3137 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07003138 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003139 last_read_stages |= usage_stage;
3140 }
John Zulauf4285ee92020-09-23 10:20:52 -06003141
3142 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
Jeremy Gebben40a22942020-12-22 14:22:06 -07003143 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003144 // TODO Revisit re: multiple reads for a given stage
3145 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003146 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003147 } else {
3148 // Assume write
3149 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003150 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003151 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003152 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06003153}
John Zulauf5f13a792020-03-10 07:31:21 -06003154
John Zulauf89311b42020-09-29 16:28:47 -06003155// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3156// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3157// We can overwrite them as *this* write is now after them.
3158//
3159// Note: intentionally ignore pending barriers and chains (i.e. don't apply or clear them), let ApplyPendingBarriers handle them.
John Zulauf14940722021-04-12 15:19:02 -06003160void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003161 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06003162 last_read_stages = 0;
3163 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003164 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003165
3166 write_barriers = 0;
3167 write_dependency_chain = 0;
3168 write_tag = tag;
3169 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003170}
3171
John Zulauf89311b42020-09-29 16:28:47 -06003172// Apply the memory barrier without updating the existing barriers. The execution barrier
3173// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3174// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3175// replace the current write barriers or add to them, so accumulate to pending as well.
3176void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3177 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3178 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003179 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3180 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3181 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3182 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07003183 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003184 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003185 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003186 if (layout_transition) {
3187 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3188 }
John Zulaufa0a98292020-09-18 09:30:10 -06003189 }
John Zulauf89311b42020-09-29 16:28:47 -06003190 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3191 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003192
John Zulauf89311b42020-09-29 16:28:47 -06003193 if (!pending_layout_transition) {
3194 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3195 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003196 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06003197 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
John Zulaufc523bf62021-02-16 08:20:34 -07003198 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
3199 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003200 }
3201 }
John Zulaufa0a98292020-09-18 09:30:10 -06003202 }
John Zulaufa0a98292020-09-18 09:30:10 -06003203}
3204
John Zulauf4a6105a2020-11-17 15:11:05 -07003205// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3206// changes the "chaining" state, but to keep barriers independent. See discussion above.
John Zulauf14940722021-04-12 15:19:02 -06003207void ResourceAccessState::ApplyBarrier(const ResourceUsageTag scope_tag, const SyncBarrier &barrier, bool layout_transition) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003208 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3209 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3210 // in order to know if it's in the excecution scope
3211 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3212 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3213 // errors w.r.t. "most recent" accesses.
John Zulauf14940722021-04-12 15:19:02 -06003214 if (layout_transition || ((write_tag < scope_tag) && (barrier.src_access_scope & last_write).any())) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003215 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003216 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003217 if (layout_transition) {
3218 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3219 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003220 }
3221 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3222 pending_layout_transition |= layout_transition;
3223
3224 if (!pending_layout_transition) {
3225 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3226 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003227 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003228 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3229 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3230 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3231 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3232 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3233 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3234 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulauf14940722021-04-12 15:19:02 -06003235 if ((read_access.tag < scope_tag) && (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers))) {
John Zulaufc523bf62021-02-16 08:20:34 -07003236 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003237 }
3238 }
3239 }
3240}
John Zulauf14940722021-04-12 15:19:02 -06003241void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag tag) {
John Zulauf89311b42020-09-29 16:28:47 -06003242 if (pending_layout_transition) {
John Zulauf4fa68462021-04-26 21:04:22 -06003243 // SetWrite clobbers the last_reads array, and thus we don't have to clear the read_state out.
John Zulauf89311b42020-09-29 16:28:47 -06003244 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07003245 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf4fa68462021-04-26 21:04:22 -06003246 TouchupFirstForLayoutTransition(tag, pending_layout_ordering_);
3247 pending_layout_ordering_ = OrderingBarrier();
John Zulauf89311b42020-09-29 16:28:47 -06003248 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003249 }
John Zulauf89311b42020-09-29 16:28:47 -06003250
3251 // Apply the accumulate execution barriers (and thus update chaining information)
John Zulauf4fa68462021-04-26 21:04:22 -06003252 // for layout transition, last_reads is reset by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003253 for (auto &read_access : last_reads) {
3254 read_access.barriers |= read_access.pending_dep_chain;
3255 read_execution_barriers |= read_access.barriers;
3256 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003257 }
3258
3259 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3260 write_dependency_chain |= pending_write_dep_chain;
3261 write_barriers |= pending_write_barriers;
3262 pending_write_dep_chain = 0;
3263 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003264}
3265
John Zulaufae842002021-04-15 18:20:55 -06003266bool ResourceAccessState::FirstAccessInTagRange(const ResourceUsageRange &tag_range) const {
3267 if (!first_accesses_.size()) return false;
3268 const ResourceUsageRange first_access_range = {first_accesses_.front().tag, first_accesses_.back().tag + 1};
3269 return tag_range.intersects(first_access_range);
3270}
3271
John Zulauf59e25072020-07-17 10:55:21 -06003272// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07003273VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
3274 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003275
John Zulaufab7756b2020-12-29 16:10:16 -07003276 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003277 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003278 barriers = read_access.barriers;
3279 break;
John Zulauf59e25072020-07-17 10:55:21 -06003280 }
3281 }
John Zulauf4285ee92020-09-23 10:20:52 -06003282
John Zulauf59e25072020-07-17 10:55:21 -06003283 return barriers;
3284}
3285
Jeremy Gebben40a22942020-12-22 14:22:06 -07003286inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003287 assert(IsRead(usage));
3288 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3289 // * the previous reads are not hazards, and thus last_write must be visible and available to
3290 // any reads that happen after.
3291 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3292 // 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 -07003293 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003294}
3295
Jeremy Gebben40a22942020-12-22 14:22:06 -07003296VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003297 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07003298 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06003299 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003300 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003301 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003302 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
Jeremy Gebben40a22942020-12-22 14:22:06 -07003303 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06003304 }
3305
3306 return ordered_stages;
3307}
3308
John Zulauf14940722021-04-12 15:19:02 -06003309void ResourceAccessState::UpdateFirst(const ResourceUsageTag tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003310 // Only record until we record a write.
3311 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003312 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003313 if (0 == (usage_stage & first_read_stages_)) {
3314 // If this is a read we haven't seen or a write, record.
John Zulauf4fa68462021-04-26 21:04:22 -06003315 // We always need to know what stages were found prior to write
John Zulauffaea0ee2021-01-14 14:01:32 -07003316 first_read_stages_ |= usage_stage;
John Zulauf4fa68462021-04-26 21:04:22 -06003317 if (0 == (read_execution_barriers & usage_stage)) {
3318 // If this stage isn't masked then we add it (since writes map to usage_stage 0, this also records writes)
3319 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3320 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003321 }
3322 }
3323}
3324
John Zulauf4fa68462021-04-26 21:04:22 -06003325void ResourceAccessState::TouchupFirstForLayoutTransition(ResourceUsageTag tag, const OrderingBarrier &layout_ordering) {
3326 // Only call this after recording an image layout transition
3327 assert(first_accesses_.size());
3328 if (first_accesses_.back().tag == tag) {
3329 // If this layout transition is the the first write, add the additional ordering rules that guard the ILT
Samuel Iglesias Gonsálvez9b4660b2021-10-21 08:50:39 +02003330 assert(first_accesses_.back().usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
John Zulauf4fa68462021-04-26 21:04:22 -06003331 first_write_layout_ordering_ = layout_ordering;
3332 }
3333}
3334
John Zulaufd1f85d42020-04-15 12:23:15 -06003335void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003336 auto *access_context = GetAccessContextNoInsert(command_buffer);
3337 if (access_context) {
3338 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003339 }
3340}
3341
John Zulaufd1f85d42020-04-15 12:23:15 -06003342void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3343 auto access_found = cb_access_state.find(command_buffer);
3344 if (access_found != cb_access_state.end()) {
3345 access_found->second->Reset();
John Zulauf4fa68462021-04-26 21:04:22 -06003346 access_found->second->MarkDestroyed();
John Zulaufd1f85d42020-04-15 12:23:15 -06003347 cb_access_state.erase(access_found);
3348 }
3349}
3350
John Zulauf9cb530d2019-09-30 14:14:10 -06003351bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3352 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3353 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003354 const auto *cb_context = GetAccessContext(commandBuffer);
3355 assert(cb_context);
3356 if (!cb_context) return skip;
3357 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003358
John Zulauf3d84f1b2020-03-09 13:33:25 -06003359 // If we have no previous accesses, we have no hazards
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003360 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3361 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003362
3363 for (uint32_t region = 0; region < regionCount; region++) {
3364 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003365 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003366 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003367 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003368 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003369 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003370 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003371 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003372 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003373 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003374 }
John Zulauf16adfc92020-04-08 10:28:33 -06003375 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003376 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003377 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003378 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003379 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003380 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003381 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003382 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003383 }
3384 }
3385 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003386 }
3387 return skip;
3388}
3389
3390void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3391 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003392 auto *cb_context = GetAccessContext(commandBuffer);
3393 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003394 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003395 auto *context = cb_context->GetCurrentAccessContext();
3396
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003397 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3398 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003399
3400 for (uint32_t region = 0; region < regionCount; region++) {
3401 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003402 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003403 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003404 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003405 }
John Zulauf16adfc92020-04-08 10:28:33 -06003406 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003407 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003408 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003409 }
3410 }
3411}
3412
John Zulauf4a6105a2020-11-17 15:11:05 -07003413void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3414 // Clear out events from the command buffer contexts
3415 for (auto &cb_context : cb_access_state) {
3416 cb_context.second->RecordDestroyEvent(event);
3417 }
3418}
3419
Tony-LunarGef035472021-11-02 10:23:33 -06003420bool SyncValidator::ValidateCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos,
3421 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04003422 bool skip = false;
3423 const auto *cb_context = GetAccessContext(commandBuffer);
3424 assert(cb_context);
3425 if (!cb_context) return skip;
3426 const auto *context = cb_context->GetCurrentAccessContext();
Tony-LunarGef035472021-11-02 10:23:33 -06003427 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003428
3429 // If we have no previous accesses, we have no hazards
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003430 auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3431 auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003432
3433 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3434 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3435 if (src_buffer) {
3436 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003437 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003438 if (hazard.hazard) {
3439 // TODO -- add tag information to log msg when useful.
3440 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
Tony-LunarGef035472021-11-02 10:23:33 -06003441 "%s(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003442 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003443 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003444 }
3445 }
3446 if (dst_buffer && !skip) {
3447 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003448 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003449 if (hazard.hazard) {
3450 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
Tony-LunarGef035472021-11-02 10:23:33 -06003451 "%s(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003452 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003453 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003454 }
3455 }
3456 if (skip) break;
3457 }
3458 return skip;
3459}
3460
Tony-LunarGef035472021-11-02 10:23:33 -06003461bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3462 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3463 return ValidateCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2KHR);
3464}
3465
3466bool SyncValidator::PreCallValidateCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos) const {
3467 return ValidateCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2);
3468}
3469
3470void SyncValidator::RecordCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos, CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04003471 auto *cb_context = GetAccessContext(commandBuffer);
3472 assert(cb_context);
Tony-LunarGef035472021-11-02 10:23:33 -06003473 const auto tag = cb_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003474 auto *context = cb_context->GetCurrentAccessContext();
3475
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003476 auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3477 auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003478
3479 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3480 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3481 if (src_buffer) {
3482 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003483 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003484 }
3485 if (dst_buffer) {
3486 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003487 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003488 }
3489 }
3490}
3491
Tony-LunarGef035472021-11-02 10:23:33 -06003492void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3493 RecordCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2KHR);
3494}
3495
3496void SyncValidator::PreCallRecordCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos) {
3497 RecordCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2);
3498}
3499
John Zulauf5c5e88d2019-12-26 11:22:02 -07003500bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3501 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3502 const VkImageCopy *pRegions) const {
3503 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003504 const auto *cb_access_context = GetAccessContext(commandBuffer);
3505 assert(cb_access_context);
3506 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003507
John Zulauf3d84f1b2020-03-09 13:33:25 -06003508 const auto *context = cb_access_context->GetCurrentAccessContext();
3509 assert(context);
3510 if (!context) return skip;
3511
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003512 auto src_image = Get<IMAGE_STATE>(srcImage);
3513 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003514 for (uint32_t region = 0; region < regionCount; region++) {
3515 const auto &copy_region = pRegions[region];
3516 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003517 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003518 copy_region.srcOffset, copy_region.extent);
3519 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003520 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003521 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003522 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003523 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003524 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003525 }
3526
3527 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003528 VkExtent3D dst_copy_extent =
3529 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003530 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003531 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003532 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003533 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003534 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003535 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003536 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003537 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003538 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003539 }
3540 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003541
John Zulauf5c5e88d2019-12-26 11:22:02 -07003542 return skip;
3543}
3544
3545void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3546 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3547 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003548 auto *cb_access_context = GetAccessContext(commandBuffer);
3549 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003550 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003551 auto *context = cb_access_context->GetCurrentAccessContext();
3552 assert(context);
3553
Jeremy Gebben9f537102021-10-05 16:37:12 -06003554 auto src_image = Get<IMAGE_STATE>(srcImage);
3555 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003556
3557 for (uint32_t region = 0; region < regionCount; region++) {
3558 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003559 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003560 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003561 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003562 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003563 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003564 VkExtent3D dst_copy_extent =
3565 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003566 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003567 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003568 }
3569 }
3570}
3571
Tony-LunarGb61514a2021-11-02 12:36:51 -06003572bool SyncValidator::ValidateCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo,
3573 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04003574 bool skip = false;
3575 const auto *cb_access_context = GetAccessContext(commandBuffer);
3576 assert(cb_access_context);
3577 if (!cb_access_context) return skip;
3578
3579 const auto *context = cb_access_context->GetCurrentAccessContext();
3580 assert(context);
3581 if (!context) return skip;
3582
Tony-LunarGb61514a2021-11-02 12:36:51 -06003583 const char *func_name = CommandTypeString(cmd_type);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003584 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3585 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Tony-LunarGb61514a2021-11-02 12:36:51 -06003586
Jeff Leger178b1e52020-10-05 12:22:23 -04003587 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3588 const auto &copy_region = pCopyImageInfo->pRegions[region];
3589 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003590 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003591 copy_region.srcOffset, copy_region.extent);
3592 if (hazard.hazard) {
3593 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
sfricke-samsung71f04e32022-03-16 01:21:21 -05003594 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003595 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003596 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003597 }
3598 }
3599
3600 if (dst_image) {
3601 VkExtent3D dst_copy_extent =
3602 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003603 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003604 copy_region.dstOffset, dst_copy_extent);
3605 if (hazard.hazard) {
3606 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
sfricke-samsung71f04e32022-03-16 01:21:21 -05003607 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003608 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003609 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003610 }
3611 if (skip) break;
3612 }
3613 }
3614
3615 return skip;
3616}
3617
Tony-LunarGb61514a2021-11-02 12:36:51 -06003618bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3619 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3620 return ValidateCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2KHR);
3621}
3622
3623bool SyncValidator::PreCallValidateCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo) const {
3624 return ValidateCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2);
3625}
3626
3627void SyncValidator::RecordCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo, CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04003628 auto *cb_access_context = GetAccessContext(commandBuffer);
3629 assert(cb_access_context);
Tony-LunarGb61514a2021-11-02 12:36:51 -06003630 const auto tag = cb_access_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003631 auto *context = cb_access_context->GetCurrentAccessContext();
3632 assert(context);
3633
Jeremy Gebben9f537102021-10-05 16:37:12 -06003634 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3635 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04003636
3637 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3638 const auto &copy_region = pCopyImageInfo->pRegions[region];
3639 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003640 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003641 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003642 }
3643 if (dst_image) {
3644 VkExtent3D dst_copy_extent =
3645 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003646 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003647 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003648 }
3649 }
3650}
3651
Tony-LunarGb61514a2021-11-02 12:36:51 -06003652void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3653 RecordCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2KHR);
3654}
3655
3656void SyncValidator::PreCallRecordCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo) {
3657 RecordCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2);
3658}
3659
John Zulauf9cb530d2019-09-30 14:14:10 -06003660bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3661 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3662 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3663 uint32_t bufferMemoryBarrierCount,
3664 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3665 uint32_t imageMemoryBarrierCount,
3666 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3667 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003668 const auto *cb_access_context = GetAccessContext(commandBuffer);
3669 assert(cb_access_context);
3670 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003671
John Zulauf36ef9282021-02-02 11:47:24 -07003672 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3673 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3674 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3675 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003676 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003677 return skip;
3678}
3679
3680void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3681 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3682 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3683 uint32_t bufferMemoryBarrierCount,
3684 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3685 uint32_t imageMemoryBarrierCount,
3686 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003687 auto *cb_access_context = GetAccessContext(commandBuffer);
3688 assert(cb_access_context);
3689 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003690
John Zulauf1bf30522021-09-03 15:39:06 -06003691 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(),
3692 srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount,
3693 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
3694 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06003695}
3696
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003697bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3698 const VkDependencyInfoKHR *pDependencyInfo) const {
3699 bool skip = false;
3700 const auto *cb_access_context = GetAccessContext(commandBuffer);
3701 assert(cb_access_context);
3702 if (!cb_access_context) return skip;
3703
3704 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3705 skip = pipeline_barrier.Validate(*cb_access_context);
3706 return skip;
3707}
3708
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07003709bool SyncValidator::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer,
3710 const VkDependencyInfo *pDependencyInfo) const {
3711 bool skip = false;
3712 const auto *cb_access_context = GetAccessContext(commandBuffer);
3713 assert(cb_access_context);
3714 if (!cb_access_context) return skip;
3715
3716 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3717 skip = pipeline_barrier.Validate(*cb_access_context);
3718 return skip;
3719}
3720
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003721void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3722 auto *cb_access_context = GetAccessContext(commandBuffer);
3723 assert(cb_access_context);
3724 if (!cb_access_context) return;
3725
John Zulauf1bf30522021-09-03 15:39:06 -06003726 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(),
3727 *pDependencyInfo);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003728}
3729
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07003730void SyncValidator::PreCallRecordCmdPipelineBarrier2(VkCommandBuffer commandBuffer, const VkDependencyInfo *pDependencyInfo) {
3731 auto *cb_access_context = GetAccessContext(commandBuffer);
3732 assert(cb_access_context);
3733 if (!cb_access_context) return;
3734
3735 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2, *this, cb_access_context->GetQueueFlags(),
3736 *pDependencyInfo);
3737}
3738
John Zulauf9cb530d2019-09-30 14:14:10 -06003739void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3740 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3741 // The state tracker sets up the device state
3742 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3743
John Zulauf5f13a792020-03-10 07:31:21 -06003744 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3745 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003746 // TODO: Find a good way to do this hooklessly.
3747 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3748 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3749 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3750
John Zulaufd1f85d42020-04-15 12:23:15 -06003751 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3752 sync_device_state->ResetCommandBufferCallback(command_buffer);
3753 });
3754 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3755 sync_device_state->FreeCommandBufferCallback(command_buffer);
3756 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003757}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003758
John Zulauf355e49b2020-04-24 15:11:15 -06003759bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003760 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003761 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003762 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003763 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003764 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003765 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003766 }
John Zulauf355e49b2020-04-24 15:11:15 -06003767 return skip;
3768}
3769
3770bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3771 VkSubpassContents contents) const {
3772 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003773 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003774 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003775 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003776 return skip;
3777}
3778
3779bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003780 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003781 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003782 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003783 return skip;
3784}
3785
3786bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3787 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003788 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003789 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003790 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003791 return skip;
3792}
3793
John Zulauf3d84f1b2020-03-09 13:33:25 -06003794void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3795 VkResult result) {
3796 // The state tracker sets up the command buffer state
3797 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3798
3799 // Create/initialize the structure that trackers accesses at the command buffer scope.
3800 auto cb_access_context = GetAccessContext(commandBuffer);
3801 assert(cb_access_context);
3802 cb_access_context->Reset();
3803}
3804
3805void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003806 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003807 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003808 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003809 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003810 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003811 }
3812}
3813
3814void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3815 VkSubpassContents contents) {
3816 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003817 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003818 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003819 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003820}
3821
3822void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3823 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3824 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003825 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003826}
3827
3828void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3829 const VkRenderPassBeginInfo *pRenderPassBegin,
3830 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3831 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003832 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003833}
3834
Mike Schuchardt2df08912020-12-15 16:28:09 -08003835bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003836 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003837 bool skip = false;
3838
3839 auto cb_context = GetAccessContext(commandBuffer);
3840 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003841 if (!cb_context) return skip;
sfricke-samsung85584a72021-09-30 21:43:38 -07003842 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003843 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003844}
3845
3846bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3847 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003848 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003849 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003850 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003851 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3852 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003853 return skip;
3854}
3855
Mike Schuchardt2df08912020-12-15 16:28:09 -08003856bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3857 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003858 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003859 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003860 return skip;
3861}
3862
3863bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3864 const VkSubpassEndInfo *pSubpassEndInfo) const {
3865 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003866 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003867 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003868}
3869
3870void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003871 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003872 auto cb_context = GetAccessContext(commandBuffer);
3873 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003874 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003875
sfricke-samsung85584a72021-09-30 21:43:38 -07003876 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003877 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003878}
3879
3880void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3881 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003882 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003883 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003884 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003885}
3886
3887void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3888 const VkSubpassEndInfo *pSubpassEndInfo) {
3889 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003890 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003891}
3892
3893void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3894 const VkSubpassEndInfo *pSubpassEndInfo) {
3895 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003896 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003897}
3898
sfricke-samsung85584a72021-09-30 21:43:38 -07003899bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3900 CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003901 bool skip = false;
3902
3903 auto cb_context = GetAccessContext(commandBuffer);
3904 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003905 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003906
sfricke-samsung85584a72021-09-30 21:43:38 -07003907 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003908 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003909 return skip;
3910}
3911
3912bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3913 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003914 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003915 return skip;
3916}
3917
Mike Schuchardt2df08912020-12-15 16:28:09 -08003918bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003919 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003920 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003921 return skip;
3922}
3923
3924bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003925 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003926 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003927 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003928 return skip;
3929}
3930
sfricke-samsung85584a72021-09-30 21:43:38 -07003931void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003932 // Resolve the all subpass contexts to the command buffer contexts
3933 auto cb_context = GetAccessContext(commandBuffer);
3934 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003935 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003936
sfricke-samsung85584a72021-09-30 21:43:38 -07003937 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003938 sync_op.Record(cb_context);
3939 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003940}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003941
John Zulauf33fc1d52020-07-17 11:01:10 -06003942// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3943// updates to a resource which do not conflict at the byte level.
3944// TODO: Revisit this rule to see if it needs to be tighter or looser
3945// TODO: Add programatic control over suppression heuristics
3946bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3947 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3948}
3949
John Zulauf3d84f1b2020-03-09 13:33:25 -06003950void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003951 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003952 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003953}
3954
3955void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003956 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003957 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003958}
3959
3960void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003961 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf5a1a5382020-06-22 17:23:25 -06003962 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003963}
locke-lunarga19c71d2020-03-02 18:17:04 -07003964
sfricke-samsung71f04e32022-03-16 01:21:21 -05003965template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04003966bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
sfricke-samsung71f04e32022-03-16 01:21:21 -05003967 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions,
3968 CMD_TYPE cmd_type) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003969 bool skip = false;
3970 const auto *cb_access_context = GetAccessContext(commandBuffer);
3971 assert(cb_access_context);
3972 if (!cb_access_context) return skip;
3973
Tony Barbour845d29b2021-11-09 11:43:14 -07003974 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003975
locke-lunarga19c71d2020-03-02 18:17:04 -07003976 const auto *context = cb_access_context->GetCurrentAccessContext();
3977 assert(context);
3978 if (!context) return skip;
3979
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003980 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3981 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003982
3983 for (uint32_t region = 0; region < regionCount; region++) {
3984 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003985 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003986 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003987 if (src_buffer) {
3988 ResourceAccessRange src_range =
3989 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003990 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07003991 if (hazard.hazard) {
3992 // PHASE1 TODO -- add tag information to log msg when useful.
3993 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3994 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3995 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003996 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003997 }
3998 }
3999
Jeremy Gebben40a22942020-12-22 14:22:06 -07004000 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07004001 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004002 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004003 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004004 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004005 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004006 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004007 }
4008 if (skip) break;
4009 }
4010 if (skip) break;
4011 }
4012 return skip;
4013}
4014
Jeff Leger178b1e52020-10-05 12:22:23 -04004015bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4016 VkImageLayout dstImageLayout, uint32_t regionCount,
4017 const VkBufferImageCopy *pRegions) const {
4018 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
Tony Barbour845d29b2021-11-09 11:43:14 -07004019 CMD_COPYBUFFERTOIMAGE);
Jeff Leger178b1e52020-10-05 12:22:23 -04004020}
4021
4022bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4023 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
4024 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4025 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
Tony Barbour845d29b2021-11-09 11:43:14 -07004026 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2KHR);
4027}
4028
4029bool SyncValidator::PreCallValidateCmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
4030 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) const {
4031 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4032 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4033 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004034}
4035
sfricke-samsung71f04e32022-03-16 01:21:21 -05004036template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004037void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004038 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions,
4039 CMD_TYPE cmd_type) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004040 auto *cb_access_context = GetAccessContext(commandBuffer);
4041 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004042
Jeff Leger178b1e52020-10-05 12:22:23 -04004043 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004044 auto *context = cb_access_context->GetCurrentAccessContext();
4045 assert(context);
4046
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004047 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
4048 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004049
4050 for (uint32_t region = 0; region < regionCount; region++) {
4051 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07004052 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07004053 if (src_buffer) {
4054 ResourceAccessRange src_range =
4055 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004056 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004057 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07004058 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004059 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004060 }
4061 }
4062}
4063
Jeff Leger178b1e52020-10-05 12:22:23 -04004064void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4065 VkImageLayout dstImageLayout, uint32_t regionCount,
4066 const VkBufferImageCopy *pRegions) {
4067 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
Tony Barbour845d29b2021-11-09 11:43:14 -07004068 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, CMD_COPYBUFFERTOIMAGE);
Jeff Leger178b1e52020-10-05 12:22:23 -04004069}
4070
4071void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4072 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4073 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4074 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4075 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
Tony Barbour845d29b2021-11-09 11:43:14 -07004076 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2KHR);
4077}
4078
4079void SyncValidator::PreCallRecordCmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
4080 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) {
4081 StateTracker::PreCallRecordCmdCopyBufferToImage2(commandBuffer, pCopyBufferToImageInfo);
4082 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4083 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4084 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004085}
4086
sfricke-samsung71f04e32022-03-16 01:21:21 -05004087template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004088bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004089 VkBuffer dstBuffer, uint32_t regionCount, const RegionType *pRegions,
4090 CMD_TYPE cmd_type) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004091 bool skip = false;
4092 const auto *cb_access_context = GetAccessContext(commandBuffer);
4093 assert(cb_access_context);
4094 if (!cb_access_context) return skip;
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004095 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04004096
locke-lunarga19c71d2020-03-02 18:17:04 -07004097 const auto *context = cb_access_context->GetCurrentAccessContext();
4098 assert(context);
4099 if (!context) return skip;
4100
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004101 auto src_image = Get<IMAGE_STATE>(srcImage);
4102 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004103 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
locke-lunarga19c71d2020-03-02 18:17:04 -07004104 for (uint32_t region = 0; region < regionCount; region++) {
4105 const auto &copy_region = pRegions[region];
4106 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004107 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004108 copy_region.imageOffset, copy_region.imageExtent);
4109 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004110 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004111 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004112 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004113 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004114 }
John Zulauf477700e2021-01-06 11:41:49 -07004115 if (dst_mem) {
4116 ResourceAccessRange dst_range =
4117 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004118 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07004119 if (hazard.hazard) {
4120 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4121 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4122 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004123 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07004124 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004125 }
4126 }
4127 if (skip) break;
4128 }
4129 return skip;
4130}
4131
Jeff Leger178b1e52020-10-05 12:22:23 -04004132bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4133 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4134 const VkBufferImageCopy *pRegions) const {
4135 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004136 CMD_COPYIMAGETOBUFFER);
Jeff Leger178b1e52020-10-05 12:22:23 -04004137}
4138
4139bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4140 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4141 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4142 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004143 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2KHR);
4144}
4145
4146bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
4147 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) const {
4148 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4149 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4150 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004151}
4152
sfricke-samsung71f04e32022-03-16 01:21:21 -05004153template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004154void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004155 VkBuffer dstBuffer, uint32_t regionCount, const RegionType *pRegions,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004156 CMD_TYPE cmd_type) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004157 auto *cb_access_context = GetAccessContext(commandBuffer);
4158 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004159
Jeff Leger178b1e52020-10-05 12:22:23 -04004160 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004161 auto *context = cb_access_context->GetCurrentAccessContext();
4162 assert(context);
4163
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004164 auto src_image = Get<IMAGE_STATE>(srcImage);
Jeremy Gebben9f537102021-10-05 16:37:12 -06004165 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004166 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06004167 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004168
4169 for (uint32_t region = 0; region < regionCount; region++) {
4170 const auto &copy_region = pRegions[region];
4171 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004172 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004173 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004174 if (dst_buffer) {
4175 ResourceAccessRange dst_range =
4176 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004177 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004178 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004179 }
4180 }
4181}
4182
Jeff Leger178b1e52020-10-05 12:22:23 -04004183void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4184 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4185 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004186 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, CMD_COPYIMAGETOBUFFER);
Jeff Leger178b1e52020-10-05 12:22:23 -04004187}
4188
4189void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4190 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4191 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4192 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4193 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004194 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2KHR);
4195}
4196
4197void SyncValidator::PreCallRecordCmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
4198 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) {
4199 StateTracker::PreCallRecordCmdCopyImageToBuffer2(commandBuffer, pCopyImageToBufferInfo);
4200 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4201 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4202 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004203}
4204
4205template <typename RegionType>
4206bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4207 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4208 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004209 bool skip = false;
4210 const auto *cb_access_context = GetAccessContext(commandBuffer);
4211 assert(cb_access_context);
4212 if (!cb_access_context) return skip;
4213
4214 const auto *context = cb_access_context->GetCurrentAccessContext();
4215 assert(context);
4216 if (!context) return skip;
4217
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004218 auto src_image = Get<IMAGE_STATE>(srcImage);
4219 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004220
4221 for (uint32_t region = 0; region < regionCount; region++) {
4222 const auto &blit_region = pRegions[region];
4223 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004224 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4225 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4226 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4227 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4228 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4229 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004230 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004231 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004232 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004233 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004234 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004235 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004236 }
4237 }
4238
4239 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004240 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4241 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4242 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4243 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4244 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4245 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004246 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004247 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004248 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004249 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004250 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004251 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004252 }
4253 if (skip) break;
4254 }
4255 }
4256
4257 return skip;
4258}
4259
Jeff Leger178b1e52020-10-05 12:22:23 -04004260bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4261 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4262 const VkImageBlit *pRegions, VkFilter filter) const {
4263 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4264 "vkCmdBlitImage");
4265}
4266
4267bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4268 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4269 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4270 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4271 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4272}
4273
Tony-LunarG542ae912021-11-04 16:06:44 -06004274bool SyncValidator::PreCallValidateCmdBlitImage2(VkCommandBuffer commandBuffer,
4275 const VkBlitImageInfo2 *pBlitImageInfo) const {
4276 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4277 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4278 pBlitImageInfo->filter, "vkCmdBlitImage2");
4279}
4280
Jeff Leger178b1e52020-10-05 12:22:23 -04004281template <typename RegionType>
4282void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4283 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4284 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004285 auto *cb_access_context = GetAccessContext(commandBuffer);
4286 assert(cb_access_context);
4287 auto *context = cb_access_context->GetCurrentAccessContext();
4288 assert(context);
4289
Jeremy Gebben9f537102021-10-05 16:37:12 -06004290 auto src_image = Get<IMAGE_STATE>(srcImage);
4291 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004292
4293 for (uint32_t region = 0; region < regionCount; region++) {
4294 const auto &blit_region = pRegions[region];
4295 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004296 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4297 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4298 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4299 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4300 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4301 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004302 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004303 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004304 }
4305 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004306 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4307 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4308 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4309 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4310 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4311 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004312 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004313 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004314 }
4315 }
4316}
locke-lunarg36ba2592020-04-03 09:42:04 -06004317
Jeff Leger178b1e52020-10-05 12:22:23 -04004318void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4319 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4320 const VkImageBlit *pRegions, VkFilter filter) {
4321 auto *cb_access_context = GetAccessContext(commandBuffer);
4322 assert(cb_access_context);
4323 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4324 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4325 pRegions, filter);
4326 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4327}
4328
4329void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4330 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4331 auto *cb_access_context = GetAccessContext(commandBuffer);
4332 assert(cb_access_context);
4333 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4334 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4335 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4336 pBlitImageInfo->filter, tag);
4337}
4338
Tony-LunarG542ae912021-11-04 16:06:44 -06004339void SyncValidator::PreCallRecordCmdBlitImage2(VkCommandBuffer commandBuffer, const VkBlitImageInfo2 *pBlitImageInfo) {
4340 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4341 auto *cb_access_context = GetAccessContext(commandBuffer);
4342 assert(cb_access_context);
4343 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2);
4344 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4345 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4346 pBlitImageInfo->filter, tag);
4347}
4348
John Zulauffaea0ee2021-01-14 14:01:32 -07004349bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4350 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
4351 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
4352 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004353 bool skip = false;
4354 if (drawCount == 0) return skip;
4355
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004356 auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004357 VkDeviceSize size = struct_size;
4358 if (drawCount == 1 || stride == size) {
4359 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004360 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004361 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4362 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004363 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004364 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004365 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004366 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004367 }
4368 } else {
4369 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004370 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004371 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4372 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004373 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004374 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4375 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004376 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004377 break;
4378 }
4379 }
4380 }
4381 return skip;
4382}
4383
John Zulauf14940722021-04-12 15:19:02 -06004384void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag tag, const VkDeviceSize struct_size,
locke-lunarg61870c22020-06-09 14:51:50 -06004385 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4386 uint32_t stride) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004387 auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004388 VkDeviceSize size = struct_size;
4389 if (drawCount == 1 || stride == size) {
4390 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004391 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004392 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004393 } else {
4394 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004395 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004396 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4397 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004398 }
4399 }
4400}
4401
John Zulauffaea0ee2021-01-14 14:01:32 -07004402bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4403 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4404 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004405 bool skip = false;
4406
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004407 auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004408 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004409 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4410 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004411 skip |= LogError(count_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004412 "%s: Hazard %s for countBuffer %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 Zulauffaea0ee2021-01-14 14:01:32 -07004414 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004415 }
4416 return skip;
4417}
4418
John Zulauf14940722021-04-12 15:19:02 -06004419void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag tag, VkBuffer buffer, VkDeviceSize offset) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004420 auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004421 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004422 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004423}
4424
locke-lunarg36ba2592020-04-03 09:42:04 -06004425bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004426 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004427 const auto *cb_access_context = GetAccessContext(commandBuffer);
4428 assert(cb_access_context);
4429 if (!cb_access_context) return skip;
4430
locke-lunarg61870c22020-06-09 14:51:50 -06004431 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004432 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004433}
4434
4435void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004436 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004437 auto *cb_access_context = GetAccessContext(commandBuffer);
4438 assert(cb_access_context);
4439 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004440
locke-lunarg61870c22020-06-09 14:51:50 -06004441 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004442}
locke-lunarge1a67022020-04-29 00:15:36 -06004443
4444bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004445 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004446 const auto *cb_access_context = GetAccessContext(commandBuffer);
4447 assert(cb_access_context);
4448 if (!cb_access_context) return skip;
4449
4450 const auto *context = cb_access_context->GetCurrentAccessContext();
4451 assert(context);
4452 if (!context) return skip;
4453
locke-lunarg61870c22020-06-09 14:51:50 -06004454 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004455 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4456 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004457 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004458}
4459
4460void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004461 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004462 auto *cb_access_context = GetAccessContext(commandBuffer);
4463 assert(cb_access_context);
4464 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4465 auto *context = cb_access_context->GetCurrentAccessContext();
4466 assert(context);
4467
locke-lunarg61870c22020-06-09 14:51:50 -06004468 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4469 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004470}
4471
4472bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4473 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004474 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004475 const auto *cb_access_context = GetAccessContext(commandBuffer);
4476 assert(cb_access_context);
4477 if (!cb_access_context) return skip;
4478
locke-lunarg61870c22020-06-09 14:51:50 -06004479 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4480 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4481 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004482 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004483}
4484
4485void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4486 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004487 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004488 auto *cb_access_context = GetAccessContext(commandBuffer);
4489 assert(cb_access_context);
4490 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004491
locke-lunarg61870c22020-06-09 14:51:50 -06004492 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4493 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4494 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004495}
4496
4497bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4498 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004499 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004500 const auto *cb_access_context = GetAccessContext(commandBuffer);
4501 assert(cb_access_context);
4502 if (!cb_access_context) return skip;
4503
locke-lunarg61870c22020-06-09 14:51:50 -06004504 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4505 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4506 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004507 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004508}
4509
4510void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4511 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004512 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004513 auto *cb_access_context = GetAccessContext(commandBuffer);
4514 assert(cb_access_context);
4515 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004516
locke-lunarg61870c22020-06-09 14:51:50 -06004517 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4518 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4519 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004520}
4521
4522bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4523 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004524 bool skip = false;
4525 if (drawCount == 0) return skip;
4526
locke-lunargff255f92020-05-13 18:53:52 -06004527 const auto *cb_access_context = GetAccessContext(commandBuffer);
4528 assert(cb_access_context);
4529 if (!cb_access_context) return skip;
4530
4531 const auto *context = cb_access_context->GetCurrentAccessContext();
4532 assert(context);
4533 if (!context) return skip;
4534
locke-lunarg61870c22020-06-09 14:51:50 -06004535 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4536 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004537 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4538 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004539
4540 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4541 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4542 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004543 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004544 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004545}
4546
4547void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4548 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004549 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004550 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004551 auto *cb_access_context = GetAccessContext(commandBuffer);
4552 assert(cb_access_context);
4553 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4554 auto *context = cb_access_context->GetCurrentAccessContext();
4555 assert(context);
4556
locke-lunarg61870c22020-06-09 14:51:50 -06004557 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4558 cb_access_context->RecordDrawSubpassAttachment(tag);
4559 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004560
4561 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4562 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4563 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004564 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004565}
4566
4567bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4568 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004569 bool skip = false;
4570 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004571 const auto *cb_access_context = GetAccessContext(commandBuffer);
4572 assert(cb_access_context);
4573 if (!cb_access_context) return skip;
4574
4575 const auto *context = cb_access_context->GetCurrentAccessContext();
4576 assert(context);
4577 if (!context) return skip;
4578
locke-lunarg61870c22020-06-09 14:51:50 -06004579 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4580 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004581 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4582 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004583
4584 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4585 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4586 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004587 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004588 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004589}
4590
4591void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4592 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004593 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004594 auto *cb_access_context = GetAccessContext(commandBuffer);
4595 assert(cb_access_context);
4596 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4597 auto *context = cb_access_context->GetCurrentAccessContext();
4598 assert(context);
4599
locke-lunarg61870c22020-06-09 14:51:50 -06004600 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4601 cb_access_context->RecordDrawSubpassAttachment(tag);
4602 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004603
4604 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4605 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4606 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004607 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004608}
4609
4610bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4611 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4612 uint32_t stride, const char *function) const {
4613 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004614 const auto *cb_access_context = GetAccessContext(commandBuffer);
4615 assert(cb_access_context);
4616 if (!cb_access_context) return skip;
4617
4618 const auto *context = cb_access_context->GetCurrentAccessContext();
4619 assert(context);
4620 if (!context) return skip;
4621
locke-lunarg61870c22020-06-09 14:51:50 -06004622 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4623 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004624 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4625 maxDrawCount, stride, function);
4626 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004627
4628 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4629 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4630 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004631 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004632 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004633}
4634
4635bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4636 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4637 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004638 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4639 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004640}
4641
sfricke-samsung85584a72021-09-30 21:43:38 -07004642void SyncValidator::RecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4643 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4644 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004645 auto *cb_access_context = GetAccessContext(commandBuffer);
4646 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004647 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004648 auto *context = cb_access_context->GetCurrentAccessContext();
4649 assert(context);
4650
locke-lunarg61870c22020-06-09 14:51:50 -06004651 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4652 cb_access_context->RecordDrawSubpassAttachment(tag);
4653 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4654 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004655
4656 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4657 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4658 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004659 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004660}
4661
sfricke-samsung85584a72021-09-30 21:43:38 -07004662void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4663 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4664 uint32_t stride) {
4665 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4666 stride);
4667 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4668 CMD_DRAWINDIRECTCOUNT);
4669}
locke-lunarge1a67022020-04-29 00:15:36 -06004670bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4671 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4672 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004673 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4674 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004675}
4676
4677void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4678 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4679 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004680 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4681 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004682 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4683 CMD_DRAWINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004684}
4685
4686bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4687 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4688 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004689 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4690 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004691}
4692
4693void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4694 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4695 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004696 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4697 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004698 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4699 CMD_DRAWINDIRECTCOUNTAMD);
locke-lunargff255f92020-05-13 18:53:52 -06004700}
4701
4702bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4703 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4704 uint32_t stride, const char *function) const {
4705 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004706 const auto *cb_access_context = GetAccessContext(commandBuffer);
4707 assert(cb_access_context);
4708 if (!cb_access_context) return skip;
4709
4710 const auto *context = cb_access_context->GetCurrentAccessContext();
4711 assert(context);
4712 if (!context) return skip;
4713
locke-lunarg61870c22020-06-09 14:51:50 -06004714 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4715 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004716 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4717 offset, maxDrawCount, stride, function);
4718 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004719
4720 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4721 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4722 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004723 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004724 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004725}
4726
4727bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4728 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4729 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004730 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4731 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004732}
4733
sfricke-samsung85584a72021-09-30 21:43:38 -07004734void SyncValidator::RecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4735 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4736 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004737 auto *cb_access_context = GetAccessContext(commandBuffer);
4738 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004739 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004740 auto *context = cb_access_context->GetCurrentAccessContext();
4741 assert(context);
4742
locke-lunarg61870c22020-06-09 14:51:50 -06004743 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4744 cb_access_context->RecordDrawSubpassAttachment(tag);
4745 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4746 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004747
4748 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4749 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004750 // We will update the index and vertex buffer in SubmitQueue in the future.
4751 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004752}
4753
sfricke-samsung85584a72021-09-30 21:43:38 -07004754void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4755 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4756 uint32_t maxDrawCount, uint32_t stride) {
4757 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4758 maxDrawCount, stride);
4759 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4760 CMD_DRAWINDEXEDINDIRECTCOUNT);
4761}
4762
locke-lunarge1a67022020-04-29 00:15:36 -06004763bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4764 VkDeviceSize offset, VkBuffer countBuffer,
4765 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4766 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004767 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4768 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004769}
4770
4771void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4772 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4773 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004774 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4775 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004776 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4777 CMD_DRAWINDEXEDINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004778}
4779
4780bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4781 VkDeviceSize offset, VkBuffer countBuffer,
4782 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4783 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004784 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4785 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004786}
4787
4788void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4789 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4790 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004791 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4792 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004793 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4794 CMD_DRAWINDEXEDINDIRECTCOUNTAMD);
locke-lunarge1a67022020-04-29 00:15:36 -06004795}
4796
4797bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4798 const VkClearColorValue *pColor, uint32_t rangeCount,
4799 const VkImageSubresourceRange *pRanges) const {
4800 bool skip = false;
4801 const auto *cb_access_context = GetAccessContext(commandBuffer);
4802 assert(cb_access_context);
4803 if (!cb_access_context) return skip;
4804
4805 const auto *context = cb_access_context->GetCurrentAccessContext();
4806 assert(context);
4807 if (!context) return skip;
4808
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004809 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004810
4811 for (uint32_t index = 0; index < rangeCount; index++) {
4812 const auto &range = pRanges[index];
4813 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004814 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004815 if (hazard.hazard) {
4816 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004817 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004818 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004819 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004820 }
4821 }
4822 }
4823 return skip;
4824}
4825
4826void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4827 const VkClearColorValue *pColor, uint32_t rangeCount,
4828 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004829 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004830 auto *cb_access_context = GetAccessContext(commandBuffer);
4831 assert(cb_access_context);
4832 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4833 auto *context = cb_access_context->GetCurrentAccessContext();
4834 assert(context);
4835
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004836 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004837
4838 for (uint32_t index = 0; index < rangeCount; index++) {
4839 const auto &range = pRanges[index];
4840 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004841 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004842 }
4843 }
4844}
4845
4846bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4847 VkImageLayout imageLayout,
4848 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4849 const VkImageSubresourceRange *pRanges) const {
4850 bool skip = false;
4851 const auto *cb_access_context = GetAccessContext(commandBuffer);
4852 assert(cb_access_context);
4853 if (!cb_access_context) return skip;
4854
4855 const auto *context = cb_access_context->GetCurrentAccessContext();
4856 assert(context);
4857 if (!context) return skip;
4858
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004859 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004860
4861 for (uint32_t index = 0; index < rangeCount; index++) {
4862 const auto &range = pRanges[index];
4863 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004864 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004865 if (hazard.hazard) {
4866 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004867 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004868 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004869 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004870 }
4871 }
4872 }
4873 return skip;
4874}
4875
4876void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4877 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4878 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004879 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004880 auto *cb_access_context = GetAccessContext(commandBuffer);
4881 assert(cb_access_context);
4882 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4883 auto *context = cb_access_context->GetCurrentAccessContext();
4884 assert(context);
4885
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004886 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004887
4888 for (uint32_t index = 0; index < rangeCount; index++) {
4889 const auto &range = pRanges[index];
4890 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004891 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004892 }
4893 }
4894}
4895
4896bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4897 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4898 VkDeviceSize dstOffset, VkDeviceSize stride,
4899 VkQueryResultFlags flags) const {
4900 bool skip = false;
4901 const auto *cb_access_context = GetAccessContext(commandBuffer);
4902 assert(cb_access_context);
4903 if (!cb_access_context) return skip;
4904
4905 const auto *context = cb_access_context->GetCurrentAccessContext();
4906 assert(context);
4907 if (!context) return skip;
4908
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004909 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004910
4911 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004912 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004913 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004914 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004915 skip |=
4916 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4917 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004918 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004919 }
4920 }
locke-lunargff255f92020-05-13 18:53:52 -06004921
4922 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004923 return skip;
4924}
4925
4926void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4927 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4928 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004929 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4930 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004931 auto *cb_access_context = GetAccessContext(commandBuffer);
4932 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004933 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004934 auto *context = cb_access_context->GetCurrentAccessContext();
4935 assert(context);
4936
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004937 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004938
4939 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004940 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004941 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004942 }
locke-lunargff255f92020-05-13 18:53:52 -06004943
4944 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004945}
4946
4947bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4948 VkDeviceSize size, uint32_t data) const {
4949 bool skip = false;
4950 const auto *cb_access_context = GetAccessContext(commandBuffer);
4951 assert(cb_access_context);
4952 if (!cb_access_context) return skip;
4953
4954 const auto *context = cb_access_context->GetCurrentAccessContext();
4955 assert(context);
4956 if (!context) return skip;
4957
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004958 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004959
4960 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004961 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004962 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004963 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004964 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004965 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004966 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004967 }
4968 }
4969 return skip;
4970}
4971
4972void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4973 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004974 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004975 auto *cb_access_context = GetAccessContext(commandBuffer);
4976 assert(cb_access_context);
4977 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4978 auto *context = cb_access_context->GetCurrentAccessContext();
4979 assert(context);
4980
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004981 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004982
4983 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004984 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004985 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004986 }
4987}
4988
4989bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4990 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4991 const VkImageResolve *pRegions) const {
4992 bool skip = false;
4993 const auto *cb_access_context = GetAccessContext(commandBuffer);
4994 assert(cb_access_context);
4995 if (!cb_access_context) return skip;
4996
4997 const auto *context = cb_access_context->GetCurrentAccessContext();
4998 assert(context);
4999 if (!context) return skip;
5000
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005001 auto src_image = Get<IMAGE_STATE>(srcImage);
5002 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06005003
5004 for (uint32_t region = 0; region < regionCount; region++) {
5005 const auto &resolve_region = pRegions[region];
5006 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005007 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06005008 resolve_region.srcOffset, resolve_region.extent);
5009 if (hazard.hazard) {
5010 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005011 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005012 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07005013 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005014 }
5015 }
5016
5017 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005018 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06005019 resolve_region.dstOffset, resolve_region.extent);
5020 if (hazard.hazard) {
5021 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005022 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005023 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07005024 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005025 }
5026 if (skip) break;
5027 }
5028 }
5029
5030 return skip;
5031}
5032
5033void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5034 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5035 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005036 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
5037 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06005038 auto *cb_access_context = GetAccessContext(commandBuffer);
5039 assert(cb_access_context);
5040 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
5041 auto *context = cb_access_context->GetCurrentAccessContext();
5042 assert(context);
5043
Jeremy Gebben9f537102021-10-05 16:37:12 -06005044 auto src_image = Get<IMAGE_STATE>(srcImage);
5045 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06005046
5047 for (uint32_t region = 0; region < regionCount; region++) {
5048 const auto &resolve_region = pRegions[region];
5049 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005050 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005051 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005052 }
5053 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005054 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005055 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005056 }
5057 }
5058}
5059
Tony-LunarG562fc102021-11-12 13:58:35 -07005060bool SyncValidator::ValidateCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo,
5061 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04005062 bool skip = false;
5063 const auto *cb_access_context = GetAccessContext(commandBuffer);
5064 assert(cb_access_context);
5065 if (!cb_access_context) return skip;
5066
5067 const auto *context = cb_access_context->GetCurrentAccessContext();
5068 assert(context);
5069 if (!context) return skip;
5070
Tony-LunarG562fc102021-11-12 13:58:35 -07005071 const char *func_name = CommandTypeString(cmd_type);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005072 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5073 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005074
5075 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5076 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5077 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005078 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04005079 resolve_region.srcOffset, resolve_region.extent);
5080 if (hazard.hazard) {
5081 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
Tony-LunarG562fc102021-11-12 13:58:35 -07005082 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04005083 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07005084 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04005085 }
5086 }
5087
5088 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005089 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04005090 resolve_region.dstOffset, resolve_region.extent);
5091 if (hazard.hazard) {
5092 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
Tony-LunarG562fc102021-11-12 13:58:35 -07005093 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04005094 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07005095 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04005096 }
5097 if (skip) break;
5098 }
5099 }
5100
5101 return skip;
5102}
5103
Tony-LunarG562fc102021-11-12 13:58:35 -07005104bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5105 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
5106 return ValidateCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2KHR);
5107}
5108
5109bool SyncValidator::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer,
5110 const VkResolveImageInfo2 *pResolveImageInfo) const {
5111 return ValidateCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2);
5112}
5113
5114void SyncValidator::RecordCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo,
5115 CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04005116 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5117 auto *cb_access_context = GetAccessContext(commandBuffer);
5118 assert(cb_access_context);
Tony-LunarG562fc102021-11-12 13:58:35 -07005119 const auto tag = cb_access_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04005120 auto *context = cb_access_context->GetCurrentAccessContext();
5121 assert(context);
5122
Jeremy Gebben9f537102021-10-05 16:37:12 -06005123 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5124 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005125
5126 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5127 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5128 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005129 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005130 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005131 }
5132 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005133 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005134 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005135 }
5136 }
5137}
5138
Tony-LunarG562fc102021-11-12 13:58:35 -07005139void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5140 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5141 RecordCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2KHR);
5142}
5143
5144void SyncValidator::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2 *pResolveImageInfo) {
5145 RecordCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2);
5146}
5147
locke-lunarge1a67022020-04-29 00:15:36 -06005148bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5149 VkDeviceSize dataSize, const void *pData) const {
5150 bool skip = false;
5151 const auto *cb_access_context = GetAccessContext(commandBuffer);
5152 assert(cb_access_context);
5153 if (!cb_access_context) return skip;
5154
5155 const auto *context = cb_access_context->GetCurrentAccessContext();
5156 assert(context);
5157 if (!context) return skip;
5158
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005159 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005160
5161 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005162 // VK_WHOLE_SIZE not allowed
5163 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005164 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005165 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005166 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005167 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005168 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005169 }
5170 }
5171 return skip;
5172}
5173
5174void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5175 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005176 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005177 auto *cb_access_context = GetAccessContext(commandBuffer);
5178 assert(cb_access_context);
5179 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5180 auto *context = cb_access_context->GetCurrentAccessContext();
5181 assert(context);
5182
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005183 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005184
5185 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005186 // VK_WHOLE_SIZE not allowed
5187 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005188 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005189 }
5190}
locke-lunargff255f92020-05-13 18:53:52 -06005191
5192bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5193 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5194 bool skip = false;
5195 const auto *cb_access_context = GetAccessContext(commandBuffer);
5196 assert(cb_access_context);
5197 if (!cb_access_context) return skip;
5198
5199 const auto *context = cb_access_context->GetCurrentAccessContext();
5200 assert(context);
5201 if (!context) return skip;
5202
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005203 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005204
5205 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005206 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005207 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06005208 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005209 skip |=
5210 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5211 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005212 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005213 }
5214 }
5215 return skip;
5216}
5217
5218void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5219 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005220 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005221 auto *cb_access_context = GetAccessContext(commandBuffer);
5222 assert(cb_access_context);
5223 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5224 auto *context = cb_access_context->GetCurrentAccessContext();
5225 assert(context);
5226
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005227 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005228
5229 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005230 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005231 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06005232 }
5233}
John Zulauf49beb112020-11-04 16:06:31 -07005234
5235bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5236 bool skip = false;
5237 const auto *cb_context = GetAccessContext(commandBuffer);
5238 assert(cb_context);
5239 if (!cb_context) return skip;
5240
John Zulauf36ef9282021-02-02 11:47:24 -07005241 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005242 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005243}
5244
5245void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5246 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5247 auto *cb_context = GetAccessContext(commandBuffer);
5248 assert(cb_context);
5249 if (!cb_context) return;
John Zulauf1bf30522021-09-03 15:39:06 -06005250 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005251}
5252
John Zulauf4edde622021-02-15 08:54:50 -07005253bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5254 const VkDependencyInfoKHR *pDependencyInfo) const {
5255 bool skip = false;
5256 const auto *cb_context = GetAccessContext(commandBuffer);
5257 assert(cb_context);
5258 if (!cb_context || !pDependencyInfo) return skip;
5259
5260 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5261 return set_event_op.Validate(*cb_context);
5262}
5263
Tony-LunarGc43525f2021-11-15 16:12:38 -07005264bool SyncValidator::PreCallValidateCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5265 const VkDependencyInfo *pDependencyInfo) const {
5266 bool skip = false;
5267 const auto *cb_context = GetAccessContext(commandBuffer);
5268 assert(cb_context);
5269 if (!cb_context || !pDependencyInfo) return skip;
5270
5271 SyncOpSetEvent set_event_op(CMD_SETEVENT2, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5272 return set_event_op.Validate(*cb_context);
5273}
5274
John Zulauf4edde622021-02-15 08:54:50 -07005275void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5276 const VkDependencyInfoKHR *pDependencyInfo) {
5277 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
5278 auto *cb_context = GetAccessContext(commandBuffer);
5279 assert(cb_context);
5280 if (!cb_context || !pDependencyInfo) return;
5281
John Zulauf1bf30522021-09-03 15:39:06 -06005282 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
John Zulauf4edde622021-02-15 08:54:50 -07005283}
5284
Tony-LunarGc43525f2021-11-15 16:12:38 -07005285void SyncValidator::PostCallRecordCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5286 const VkDependencyInfo *pDependencyInfo) {
5287 StateTracker::PostCallRecordCmdSetEvent2(commandBuffer, event, pDependencyInfo);
5288 auto *cb_context = GetAccessContext(commandBuffer);
5289 assert(cb_context);
5290 if (!cb_context || !pDependencyInfo) return;
5291
5292 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5293}
5294
John Zulauf49beb112020-11-04 16:06:31 -07005295bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5296 VkPipelineStageFlags stageMask) const {
5297 bool skip = false;
5298 const auto *cb_context = GetAccessContext(commandBuffer);
5299 assert(cb_context);
5300 if (!cb_context) return skip;
5301
John Zulauf36ef9282021-02-02 11:47:24 -07005302 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005303 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005304}
5305
5306void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5307 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5308 auto *cb_context = GetAccessContext(commandBuffer);
5309 assert(cb_context);
5310 if (!cb_context) return;
5311
John Zulauf1bf30522021-09-03 15:39:06 -06005312 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005313}
5314
John Zulauf4edde622021-02-15 08:54:50 -07005315bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5316 VkPipelineStageFlags2KHR stageMask) const {
5317 bool skip = false;
5318 const auto *cb_context = GetAccessContext(commandBuffer);
5319 assert(cb_context);
5320 if (!cb_context) return skip;
5321
5322 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
5323 return reset_event_op.Validate(*cb_context);
5324}
5325
Tony-LunarGa2662db2021-11-16 07:26:24 -07005326bool SyncValidator::PreCallValidateCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5327 VkPipelineStageFlags2 stageMask) const {
5328 bool skip = false;
5329 const auto *cb_context = GetAccessContext(commandBuffer);
5330 assert(cb_context);
5331 if (!cb_context) return skip;
5332
5333 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2, *this, cb_context->GetQueueFlags(), event, stageMask);
5334 return reset_event_op.Validate(*cb_context);
5335}
5336
John Zulauf4edde622021-02-15 08:54:50 -07005337void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5338 VkPipelineStageFlags2KHR stageMask) {
5339 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
5340 auto *cb_context = GetAccessContext(commandBuffer);
5341 assert(cb_context);
5342 if (!cb_context) return;
5343
John Zulauf1bf30522021-09-03 15:39:06 -06005344 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf4edde622021-02-15 08:54:50 -07005345}
5346
Tony-LunarGa2662db2021-11-16 07:26:24 -07005347void SyncValidator::PostCallRecordCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags2 stageMask) {
5348 StateTracker::PostCallRecordCmdResetEvent2(commandBuffer, event, stageMask);
5349 auto *cb_context = GetAccessContext(commandBuffer);
5350 assert(cb_context);
5351 if (!cb_context) return;
5352
5353 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2, *this, cb_context->GetQueueFlags(), event, stageMask);
5354}
5355
John Zulauf49beb112020-11-04 16:06:31 -07005356bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5357 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5358 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5359 uint32_t bufferMemoryBarrierCount,
5360 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5361 uint32_t imageMemoryBarrierCount,
5362 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5363 bool skip = false;
5364 const auto *cb_context = GetAccessContext(commandBuffer);
5365 assert(cb_context);
5366 if (!cb_context) return skip;
5367
John Zulauf36ef9282021-02-02 11:47:24 -07005368 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
5369 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
5370 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07005371 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005372}
5373
5374void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5375 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5376 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5377 uint32_t bufferMemoryBarrierCount,
5378 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5379 uint32_t imageMemoryBarrierCount,
5380 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5381 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5382 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5383 imageMemoryBarrierCount, pImageMemoryBarriers);
5384
5385 auto *cb_context = GetAccessContext(commandBuffer);
5386 assert(cb_context);
5387 if (!cb_context) return;
5388
John Zulauf1bf30522021-09-03 15:39:06 -06005389 cb_context->RecordSyncOp<SyncOpWaitEvents>(
John Zulauf610e28c2021-08-03 17:46:23 -06005390 CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
John Zulauf1bf30522021-09-03 15:39:06 -06005391 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf4a6105a2020-11-17 15:11:05 -07005392}
5393
John Zulauf4edde622021-02-15 08:54:50 -07005394bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5395 const VkDependencyInfoKHR *pDependencyInfos) const {
5396 bool skip = false;
5397 const auto *cb_context = GetAccessContext(commandBuffer);
5398 assert(cb_context);
5399 if (!cb_context) return skip;
5400
5401 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5402 skip |= wait_events_op.Validate(*cb_context);
5403 return skip;
5404}
5405
5406void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5407 const VkDependencyInfoKHR *pDependencyInfos) {
5408 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5409
5410 auto *cb_context = GetAccessContext(commandBuffer);
5411 assert(cb_context);
5412 if (!cb_context) return;
5413
John Zulauf1bf30522021-09-03 15:39:06 -06005414 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5415 pDependencyInfos);
John Zulauf4edde622021-02-15 08:54:50 -07005416}
5417
Tony-LunarG1364cf52021-11-17 16:10:11 -07005418bool SyncValidator::PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5419 const VkDependencyInfo *pDependencyInfos) const {
5420 bool skip = false;
5421 const auto *cb_context = GetAccessContext(commandBuffer);
5422 assert(cb_context);
5423 if (!cb_context) return skip;
5424
5425 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5426 skip |= wait_events_op.Validate(*cb_context);
5427 return skip;
5428}
5429
5430void SyncValidator::PostCallRecordCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5431 const VkDependencyInfo *pDependencyInfos) {
5432 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5433
5434 auto *cb_context = GetAccessContext(commandBuffer);
5435 assert(cb_context);
5436 if (!cb_context) return;
5437
5438 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5439 pDependencyInfos);
5440}
5441
John Zulauf4a6105a2020-11-17 15:11:05 -07005442void SyncEventState::ResetFirstScope() {
5443 for (const auto address_type : kAddressTypes) {
5444 first_scope[static_cast<size_t>(address_type)].clear();
5445 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005446 scope = SyncExecScope();
John Zulauf78b1f892021-09-20 15:02:09 -06005447 first_scope_set = false;
5448 first_scope_tag = 0;
John Zulauf4a6105a2020-11-17 15:11:05 -07005449}
5450
5451// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07005452SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005453 IgnoreReason reason = NotIgnored;
5454
Tony-LunarG1364cf52021-11-17 16:10:11 -07005455 if ((CMD_WAITEVENTS2KHR == cmd || CMD_WAITEVENTS2 == cmd) && (CMD_SETEVENT == last_command)) {
John Zulauf4edde622021-02-15 08:54:50 -07005456 reason = SetVsWait2;
5457 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
5458 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07005459 } else if (unsynchronized_set) {
5460 reason = SetRace;
John Zulauf78b1f892021-09-20 15:02:09 -06005461 } else if (first_scope_set) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005462 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005463 if (missing_bits) reason = MissingStageBits;
5464 }
5465
5466 return reason;
5467}
5468
Jeremy Gebben40a22942020-12-22 14:22:06 -07005469bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005470 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5471 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5472 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005473}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005474
John Zulauf36ef9282021-02-02 11:47:24 -07005475SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5476 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5477 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005478 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5479 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5480 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07005481 : SyncOpBase(cmd), barriers_(1) {
5482 auto &barrier_set = barriers_[0];
5483 barrier_set.dependency_flags = dependencyFlags;
5484 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
5485 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005486 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07005487 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
5488 pMemoryBarriers);
5489 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5490 bufferMemoryBarrierCount, pBufferMemoryBarriers);
5491 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5492 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005493}
5494
John Zulauf4edde622021-02-15 08:54:50 -07005495SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
5496 const VkDependencyInfoKHR *dep_infos)
5497 : SyncOpBase(cmd), barriers_(event_count) {
5498 for (uint32_t i = 0; i < event_count; i++) {
5499 const auto &dep_info = dep_infos[i];
5500 auto &barrier_set = barriers_[i];
5501 barrier_set.dependency_flags = dep_info.dependencyFlags;
5502 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5503 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5504 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5505 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5506 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5507 dep_info.pMemoryBarriers);
5508 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5509 dep_info.pBufferMemoryBarriers);
5510 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5511 dep_info.pImageMemoryBarriers);
5512 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005513}
5514
John Zulauf36ef9282021-02-02 11:47:24 -07005515SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005516 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5517 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5518 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5519 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5520 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005521 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005522 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5523
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005524SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5525 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005526 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005527
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005528bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5529 bool skip = false;
5530 const auto *context = cb_context.GetCurrentAccessContext();
5531 assert(context);
5532 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005533 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5534
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005535 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005536 const auto &barrier_set = barriers_[0];
5537 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5538 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5539 const auto *image_state = image_barrier.image.get();
5540 if (!image_state) continue;
5541 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5542 if (hazard.hazard) {
5543 // PHASE1 TODO -- add tag information to log msg when useful.
5544 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005545 const auto image_handle = image_state->image();
John Zulauf6fdf3d02021-03-05 16:50:47 -07005546 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5547 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5548 string_SyncHazard(hazard.hazard), image_barrier.index,
5549 sync_state.report_data->FormatHandle(image_handle).c_str(),
5550 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005551 }
5552 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005553 return skip;
5554}
5555
John Zulaufd5115702021-01-18 12:34:33 -07005556struct SyncOpPipelineBarrierFunctorFactory {
5557 using BarrierOpFunctor = PipelineBarrierOp;
5558 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5559 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5560 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5561 using BufferRange = ResourceAccessRange;
5562 using ImageRange = subresource_adapter::ImageRangeGenerator;
5563 using GlobalRange = ResourceAccessRange;
5564
5565 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5566 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5567 }
John Zulauf14940722021-04-12 15:19:02 -06005568 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005569 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5570 }
5571 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5572 return GlobalBarrierOpFunctor(barrier, false);
5573 }
5574
5575 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5576 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5577 const auto base_address = ResourceBaseAddress(buffer);
5578 return (range + base_address);
5579 }
John Zulauf110413c2021-03-20 05:38:38 -06005580 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005581 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005582
5583 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005584 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005585 return range_gen;
5586 }
5587 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5588};
5589
5590template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005591void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005592 AccessContext *context) {
5593 for (const auto &barrier : barriers) {
5594 const auto *state = barrier.GetState();
5595 if (state) {
5596 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5597 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5598 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5599 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5600 }
5601 }
5602}
5603
5604template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005605void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005606 AccessContext *access_context) {
5607 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5608 for (const auto &barrier : barriers) {
5609 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5610 }
5611 for (const auto address_type : kAddressTypes) {
5612 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5613 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5614 }
5615}
5616
John Zulauf8eda1562021-04-13 17:06:41 -06005617ResourceUsageTag SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005618 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf8eda1562021-04-13 17:06:41 -06005619 auto *events_context = cb_context->GetCurrentEventsContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005620 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf4fa68462021-04-26 21:04:22 -06005621 DoRecord(tag, access_context, events_context);
5622 return tag;
5623}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005624
John Zulauf4fa68462021-04-26 21:04:22 -06005625void SyncOpPipelineBarrier::DoRecord(const ResourceUsageTag tag, AccessContext *access_context,
5626 SyncEventsContext *events_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06005627 SyncOpPipelineBarrierFunctorFactory factory;
John Zulauf4edde622021-02-15 08:54:50 -07005628 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5629 assert(barriers_.size() == 1);
5630 const auto &barrier_set = barriers_[0];
5631 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5632 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5633 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulauf4edde622021-02-15 08:54:50 -07005634 if (barrier_set.single_exec_scope) {
John Zulauf8eda1562021-04-13 17:06:41 -06005635 events_context->ApplyBarrier(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005636 } else {
5637 for (const auto &barrier : barrier_set.memory_barriers) {
John Zulauf8eda1562021-04-13 17:06:41 -06005638 events_context->ApplyBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005639 }
5640 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005641}
5642
John Zulauf8eda1562021-04-13 17:06:41 -06005643bool SyncOpPipelineBarrier::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06005644 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf4fa68462021-04-26 21:04:22 -06005645 // No Validation for replay, as the layout transition accesses are checked directly, and the src*Mask ordering is captured
5646 // with first access information.
John Zulauf8eda1562021-04-13 17:06:41 -06005647 return false;
5648}
5649
John Zulauf4edde622021-02-15 08:54:50 -07005650void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5651 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5652 const VkMemoryBarrier *barriers) {
5653 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005654 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005655 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005656 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005657 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005658 }
5659 if (0 == memory_barrier_count) {
5660 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005661 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005662 }
John Zulauf4edde622021-02-15 08:54:50 -07005663 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005664}
5665
John Zulauf4edde622021-02-15 08:54:50 -07005666void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5667 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5668 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5669 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005670 for (uint32_t index = 0; index < barrier_count; index++) {
5671 const auto &barrier = barriers[index];
Jeremy Gebben9f537102021-10-05 16:37:12 -06005672 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005673 if (buffer) {
5674 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5675 const auto range = MakeRange(barrier.offset, barrier_size);
5676 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005677 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005678 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005679 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005680 }
5681 }
5682}
5683
John Zulauf4edde622021-02-15 08:54:50 -07005684void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005685 uint32_t memory_barrier_count, const VkMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005686 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005687 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005688 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005689 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5690 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5691 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005692 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005693 }
John Zulauf4edde622021-02-15 08:54:50 -07005694 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005695}
5696
John Zulauf4edde622021-02-15 08:54:50 -07005697void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5698 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005699 const VkBufferMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005700 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005701 for (uint32_t index = 0; index < barrier_count; index++) {
5702 const auto &barrier = barriers[index];
5703 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5704 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9f537102021-10-05 16:37:12 -06005705 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005706 if (buffer) {
5707 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5708 const auto range = MakeRange(barrier.offset, barrier_size);
5709 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005710 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005711 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005712 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005713 }
5714 }
5715}
5716
John Zulauf4edde622021-02-15 08:54:50 -07005717void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5718 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5719 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5720 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005721 for (uint32_t index = 0; index < barrier_count; index++) {
5722 const auto &barrier = barriers[index];
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005723 auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005724 if (image) {
5725 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5726 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005727 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005728 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005729 image_memory_barriers.emplace_back();
5730 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005731 }
5732 }
5733}
John Zulaufd5115702021-01-18 12:34:33 -07005734
John Zulauf4edde622021-02-15 08:54:50 -07005735void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5736 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005737 const VkImageMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005738 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005739 for (uint32_t index = 0; index < barrier_count; index++) {
5740 const auto &barrier = barriers[index];
5741 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5742 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005743 auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005744 if (image) {
5745 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5746 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005747 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005748 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005749 image_memory_barriers.emplace_back();
5750 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005751 }
5752 }
5753}
5754
John Zulauf36ef9282021-02-02 11:47:24 -07005755SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005756 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5757 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5758 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5759 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005760 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005761 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5762 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005763 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005764}
5765
John Zulauf4edde622021-02-15 08:54:50 -07005766SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5767 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5768 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5769 MakeEventsList(sync_state, eventCount, pEvents);
5770 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5771}
5772
John Zulauf610e28c2021-08-03 17:46:23 -06005773const char *const SyncOpWaitEvents::kIgnored = "Wait operation is ignored for this event.";
5774
John Zulaufd5115702021-01-18 12:34:33 -07005775bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005776 bool skip = false;
5777 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005778 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005779
John Zulauf610e28c2021-08-03 17:46:23 -06005780 // This is only interesting at record and not replay (Execute/Submit) time.
John Zulauf4edde622021-02-15 08:54:50 -07005781 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5782 const auto &barrier_set = barriers_[barrier_set_index];
5783 if (barrier_set.single_exec_scope) {
5784 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5785 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5786 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5787 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5788 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5789 } else {
5790 const auto &barriers = barrier_set.memory_barriers;
5791 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5792 const auto &barrier = barriers[barrier_index];
5793 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5794 const std::string vuid =
5795 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5796 skip =
5797 sync_state.LogInfo(command_buffer_handle, vuid,
5798 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5799 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5800 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5801 }
5802 }
5803 }
5804 }
John Zulaufd5115702021-01-18 12:34:33 -07005805 }
5806
John Zulauf610e28c2021-08-03 17:46:23 -06005807 // The rest is common to record time and replay time.
5808 skip |= DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
5809 return skip;
5810}
5811
5812bool SyncOpWaitEvents::DoValidate(const CommandBufferAccessContext &cb_context, const ResourceUsageTag base_tag) const {
5813 bool skip = false;
5814 const auto &sync_state = cb_context.GetSyncState();
5815
Jeremy Gebben40a22942020-12-22 14:22:06 -07005816 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005817 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005818 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005819 const auto *events_context = cb_context.GetCurrentEventsContext();
5820 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005821 size_t barrier_set_index = 0;
5822 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
John Zulauf78394fc2021-07-12 15:41:40 -06005823 for (const auto &event : events_) {
5824 const auto *sync_event = events_context->Get(event.get());
5825 const auto &barrier_set = barriers_[barrier_set_index];
5826 if (!sync_event) {
5827 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5828 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5829 // new validation error... wait without previously submitted set event...
5830 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives at *record time*
John Zulauf4edde622021-02-15 08:54:50 -07005831 barrier_set_index += barrier_set_incr;
John Zulauf78394fc2021-07-12 15:41:40 -06005832 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005833 }
John Zulauf610e28c2021-08-03 17:46:23 -06005834
5835 // For replay calls, don't revalidate "same command buffer" events
5836 if (sync_event->last_command_tag > base_tag) continue;
5837
John Zulauf78394fc2021-07-12 15:41:40 -06005838 const auto event_handle = sync_event->event->event();
5839 // TODO add "destroyed" checks
5840
John Zulauf78b1f892021-09-20 15:02:09 -06005841 if (sync_event->first_scope_set) {
5842 // Only accumulate barrier and event stages if there is a pending set in the current context
5843 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5844 event_stage_masks |= sync_event->scope.mask_param;
5845 }
5846
John Zulauf78394fc2021-07-12 15:41:40 -06005847 const auto &src_exec_scope = barrier_set.src_exec_scope;
John Zulauf78b1f892021-09-20 15:02:09 -06005848
John Zulauf78394fc2021-07-12 15:41:40 -06005849 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5850 if (ignore_reason) {
5851 switch (ignore_reason) {
5852 case SyncEventState::ResetWaitRace:
5853 case SyncEventState::Reset2WaitRace: {
5854 // Four permuations of Reset and Wait calls...
5855 const char *vuid =
5856 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5857 if (ignore_reason == SyncEventState::Reset2WaitRace) {
Tony-LunarG279601c2021-11-16 10:50:51 -07005858 vuid = (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2-event-03831"
5859 : "VUID-vkCmdResetEvent2-event-03832";
John Zulauf78394fc2021-07-12 15:41:40 -06005860 }
5861 const char *const message =
5862 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5863 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5864 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
John Zulauf610e28c2021-08-03 17:46:23 -06005865 CommandTypeString(sync_event->last_command), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005866 break;
5867 }
5868 case SyncEventState::SetRace: {
5869 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5870 // this event
5871 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5872 const char *const message =
5873 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5874 const char *const reason = "First synchronization scope is undefined.";
5875 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5876 sync_state.report_data->FormatHandle(event_handle).c_str(),
John Zulauf610e28c2021-08-03 17:46:23 -06005877 CommandTypeString(sync_event->last_command), reason, kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005878 break;
5879 }
5880 case SyncEventState::MissingStageBits: {
5881 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5882 // Issue error message that event waited for is not in wait events scope
5883 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5884 const char *const message = "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5885 ". Bits missing from srcStageMask %s. %s";
5886 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5887 sync_state.report_data->FormatHandle(event_handle).c_str(),
5888 sync_event->scope.mask_param, src_exec_scope.mask_param,
John Zulauf610e28c2021-08-03 17:46:23 -06005889 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005890 break;
5891 }
5892 case SyncEventState::SetVsWait2: {
Tony-LunarG279601c2021-11-16 10:50:51 -07005893 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2-pEvents-03837",
John Zulauf78394fc2021-07-12 15:41:40 -06005894 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5895 sync_state.report_data->FormatHandle(event_handle).c_str(),
5896 CommandTypeString(sync_event->last_command));
5897 break;
5898 }
5899 default:
5900 assert(ignore_reason == SyncEventState::NotIgnored);
5901 }
5902 } else if (barrier_set.image_memory_barriers.size()) {
5903 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5904 const auto *context = cb_context.GetCurrentAccessContext();
5905 assert(context);
5906 for (const auto &image_memory_barrier : image_memory_barriers) {
5907 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5908 const auto *image_state = image_memory_barrier.image.get();
5909 if (!image_state) continue;
5910 const auto &subresource_range = image_memory_barrier.range;
5911 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5912 const auto hazard =
5913 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5914 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5915 if (hazard.hazard) {
5916 skip |= sync_state.LogError(image_state->image(), string_SyncHazardVUID(hazard.hazard),
5917 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5918 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
5919 sync_state.report_data->FormatHandle(image_state->image()).c_str(),
5920 cb_context.FormatUsage(hazard).c_str());
5921 break;
5922 }
5923 }
5924 }
5925 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5926 // 03839
5927 barrier_set_index += barrier_set_incr;
5928 }
John Zulaufd5115702021-01-18 12:34:33 -07005929
5930 // Note that we can't check for HOST in pEvents as we don't track that set event type
John Zulauf4edde622021-02-15 08:54:50 -07005931 const auto extra_stage_bits = (barrier_mask_params & ~VK_PIPELINE_STAGE_2_HOST_BIT_KHR) & ~event_stage_masks;
John Zulaufd5115702021-01-18 12:34:33 -07005932 if (extra_stage_bits) {
5933 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005934 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5935 const char *const vuid =
Tony-LunarG279601c2021-11-16 10:50:51 -07005936 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005937 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005938 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulauf610e28c2021-08-03 17:46:23 -06005939 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005940 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005941 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005942 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005943 " vkCmdSetEvent may be in previously submitted command buffer.");
5944 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005945 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005946 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005947 }
5948 }
5949 return skip;
5950}
5951
5952struct SyncOpWaitEventsFunctorFactory {
5953 using BarrierOpFunctor = WaitEventBarrierOp;
5954 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5955 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5956 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5957 using BufferRange = EventSimpleRangeGenerator;
5958 using ImageRange = EventImageRangeGenerator;
5959 using GlobalRange = EventSimpleRangeGenerator;
5960
5961 // Need to restrict to only valid exec and access scope for this event
5962 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5963 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005964 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005965 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5966 return barrier;
5967 }
5968 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5969 auto barrier = RestrictToEvent(barrier_arg);
5970 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5971 }
John Zulauf14940722021-04-12 15:19:02 -06005972 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005973 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5974 }
5975 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5976 auto barrier = RestrictToEvent(barrier_arg);
5977 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5978 }
5979
5980 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5981 const AccessAddressType address_type = GetAccessAddressType(buffer);
5982 const auto base_address = ResourceBaseAddress(buffer);
5983 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5984 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5985 return filtered_range_gen;
5986 }
John Zulauf110413c2021-03-20 05:38:38 -06005987 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07005988 if (!SimpleBinding(image)) return ImageRange();
5989 const auto address_type = GetAccessAddressType(image);
5990 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005991 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005992 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5993
5994 return filtered_range_gen;
5995 }
5996 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5997 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5998 }
5999 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
6000 SyncEventState *sync_event;
6001};
6002
John Zulauf8eda1562021-04-13 17:06:41 -06006003ResourceUsageTag SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006004 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07006005 auto *access_context = cb_context->GetCurrentAccessContext();
6006 assert(access_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006007 if (!access_context) return tag;
John Zulauf669dfd52021-01-27 17:15:28 -07006008 auto *events_context = cb_context->GetCurrentEventsContext();
6009 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006010 if (!events_context) return tag;
John Zulaufd5115702021-01-18 12:34:33 -07006011
John Zulauf610e28c2021-08-03 17:46:23 -06006012 DoRecord(tag, access_context, events_context);
6013 return tag;
6014}
6015
6016void SyncOpWaitEvents::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07006017 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
6018 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
6019 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
6020 access_context->ResolvePreviousAccesses();
6021
John Zulauf4edde622021-02-15 08:54:50 -07006022 size_t barrier_set_index = 0;
6023 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
6024 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07006025 for (auto &event_shared : events_) {
6026 if (!event_shared.get()) continue;
6027 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07006028
John Zulauf4edde622021-02-15 08:54:50 -07006029 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006030 sync_event->last_command_tag = tag;
John Zulaufd5115702021-01-18 12:34:33 -07006031
John Zulauf4edde622021-02-15 08:54:50 -07006032 const auto &barrier_set = barriers_[barrier_set_index];
6033 const auto &dst = barrier_set.dst_exec_scope;
6034 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07006035 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
6036 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
6037 // of the barriers is maintained.
6038 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07006039 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
6040 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
6041 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07006042
6043 // Apply the global barrier to the event itself (for race condition tracking)
6044 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
6045 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6046 sync_event->barriers |= dst.exec_scope;
6047 } else {
6048 // We ignored this wait, so we don't have any effective synchronization barriers for it.
6049 sync_event->barriers = 0U;
6050 }
John Zulauf4edde622021-02-15 08:54:50 -07006051 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07006052 }
6053
6054 // Apply the pending barriers
6055 ResolvePendingBarrierFunctor apply_pending_action(tag);
6056 access_context->ApplyToContext(apply_pending_action);
6057}
6058
John Zulauf8eda1562021-04-13 17:06:41 -06006059bool SyncOpWaitEvents::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006060 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
6061 return DoValidate(*active_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006062}
6063
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006064bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6065 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
6066 bool skip = false;
6067 const auto *cb_access_context = GetAccessContext(commandBuffer);
6068 assert(cb_access_context);
6069 if (!cb_access_context) return skip;
6070
6071 const auto *context = cb_access_context->GetCurrentAccessContext();
6072 assert(context);
6073 if (!context) return skip;
6074
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006075 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006076
6077 if (dst_buffer) {
6078 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6079 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
6080 if (hazard.hazard) {
6081 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
6082 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
6083 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
John Zulauf14940722021-04-12 15:19:02 -06006084 cb_access_context->FormatUsage(hazard).c_str());
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006085 }
6086 }
6087 return skip;
6088}
6089
John Zulauf669dfd52021-01-27 17:15:28 -07006090void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07006091 events_.reserve(event_count);
6092 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006093 events_.emplace_back(sync_state.Get<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07006094 }
6095}
John Zulauf6ce24372021-01-30 05:56:25 -07006096
John Zulauf36ef9282021-02-02 11:47:24 -07006097SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07006098 VkPipelineStageFlags2KHR stageMask)
Jeremy Gebben9f537102021-10-05 16:37:12 -06006099 : SyncOpBase(cmd), event_(sync_state.Get<EVENT_STATE>(event)), exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006100
John Zulauf1bf30522021-09-03 15:39:06 -06006101bool SyncOpResetEvent::Validate(const CommandBufferAccessContext& cb_context) const {
6102 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6103}
6104
6105bool SyncOpResetEvent::DoValidate(const CommandBufferAccessContext & cb_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006106 auto *events_context = cb_context.GetCurrentEventsContext();
6107 assert(events_context);
6108 bool skip = false;
6109 if (!events_context) return skip;
6110
6111 const auto &sync_state = cb_context.GetSyncState();
6112 const auto *sync_event = events_context->Get(event_);
6113 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6114
John Zulauf1bf30522021-09-03 15:39:06 -06006115 if (sync_event->last_command_tag > base_tag) return skip; // if we validated this in recording of the secondary, don't repeat
6116
John Zulauf6ce24372021-01-30 05:56:25 -07006117 const char *const set_wait =
6118 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6119 "hazards.";
6120 const char *message = set_wait; // Only one message this call.
6121 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
6122 const char *vuid = nullptr;
6123 switch (sync_event->last_command) {
6124 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006125 case CMD_SETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006126 case CMD_SETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006127 // Needs a barrier between set and reset
6128 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
6129 break;
John Zulauf4edde622021-02-15 08:54:50 -07006130 case CMD_WAITEVENTS:
Tony-LunarG1364cf52021-11-17 16:10:11 -07006131 case CMD_WAITEVENTS2:
John Zulauf4edde622021-02-15 08:54:50 -07006132 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07006133 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
6134 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
6135 break;
6136 }
6137 default:
6138 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07006139 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
6140 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07006141 break;
6142 }
6143 if (vuid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006144 skip |= sync_state.LogError(event_->event(), vuid, message, CmdName(),
6145 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006146 CommandTypeString(sync_event->last_command));
6147 }
6148 }
6149 return skip;
6150}
6151
John Zulauf8eda1562021-04-13 17:06:41 -06006152ResourceUsageTag SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
6153 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006154 auto *events_context = cb_context->GetCurrentEventsContext();
6155 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006156 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006157
6158 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06006159 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006160
6161 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07006162 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006163 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006164 sync_event->unsynchronized_set = CMD_NONE;
6165 sync_event->ResetFirstScope();
6166 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06006167
6168 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006169}
6170
John Zulauf8eda1562021-04-13 17:06:41 -06006171bool SyncOpResetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006172 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf1bf30522021-09-03 15:39:06 -06006173 return DoValidate(*active_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006174}
6175
John Zulauf4fa68462021-04-26 21:04:22 -06006176void SyncOpResetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006177
John Zulauf36ef9282021-02-02 11:47:24 -07006178SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07006179 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07006180 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006181 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006182 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
6183 dep_info_() {}
6184
6185SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
6186 const VkDependencyInfoKHR &dep_info)
6187 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006188 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006189 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
Tony-LunarG273f32f2021-09-28 08:56:30 -06006190 dep_info_(new safe_VkDependencyInfo(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006191
6192bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf610e28c2021-08-03 17:46:23 -06006193 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6194}
6195bool SyncOpSetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6196 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
6197 assert(active_context);
6198 return DoValidate(*active_context, base_tag);
6199}
6200
6201bool SyncOpSetEvent::DoValidate(const CommandBufferAccessContext &cb_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006202 bool skip = false;
6203
6204 const auto &sync_state = cb_context.GetSyncState();
6205 auto *events_context = cb_context.GetCurrentEventsContext();
6206 assert(events_context);
6207 if (!events_context) return skip;
6208
6209 const auto *sync_event = events_context->Get(event_);
6210 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6211
John Zulauf610e28c2021-08-03 17:46:23 -06006212 if (sync_event->last_command_tag >= base_tag) return skip; // for replay we don't want to revalidate internal "last commmand"
6213
John Zulauf6ce24372021-01-30 05:56:25 -07006214 const char *const reset_set =
6215 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6216 "hazards.";
6217 const char *const wait =
6218 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
6219
6220 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07006221 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07006222 const char *message = nullptr;
6223 switch (sync_event->last_command) {
6224 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006225 case CMD_RESETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006226 case CMD_RESETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006227 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07006228 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07006229 message = reset_set;
6230 break;
6231 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006232 case CMD_SETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006233 case CMD_SETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006234 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07006235 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07006236 message = reset_set;
6237 break;
6238 case CMD_WAITEVENTS:
Tony-LunarG1364cf52021-11-17 16:10:11 -07006239 case CMD_WAITEVENTS2:
John Zulauf4edde622021-02-15 08:54:50 -07006240 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006241 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07006242 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07006243 message = wait;
6244 break;
6245 default:
6246 // The only other valid last command that wasn't one.
6247 assert(sync_event->last_command == CMD_NONE);
6248 break;
6249 }
John Zulauf4edde622021-02-15 08:54:50 -07006250 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07006251 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07006252 std::string vuid("SYNC-");
6253 vuid.append(CmdName()).append(vuid_stem);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006254 skip |= sync_state.LogError(event_->event(), vuid.c_str(), message, CmdName(),
6255 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006256 CommandTypeString(sync_event->last_command));
6257 }
6258 }
6259
6260 return skip;
6261}
6262
John Zulauf8eda1562021-04-13 17:06:41 -06006263ResourceUsageTag SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006264 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006265 auto *events_context = cb_context->GetCurrentEventsContext();
6266 auto *access_context = cb_context->GetCurrentAccessContext();
6267 assert(events_context);
John Zulauf610e28c2021-08-03 17:46:23 -06006268 if (access_context && events_context) {
6269 DoRecord(tag, access_context, events_context);
6270 }
6271 return tag;
6272}
John Zulauf6ce24372021-01-30 05:56:25 -07006273
John Zulauf610e28c2021-08-03 17:46:23 -06006274void SyncOpSetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006275 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf610e28c2021-08-03 17:46:23 -06006276 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006277
6278 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
6279 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
6280 // any issues caused by naive scope setting here.
6281
6282 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
6283 // Given:
6284 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
6285 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
6286
6287 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
6288 sync_event->unsynchronized_set = sync_event->last_command;
6289 sync_event->ResetFirstScope();
John Zulauf78b1f892021-09-20 15:02:09 -06006290 } else if (!sync_event->first_scope_set) {
John Zulauf6ce24372021-01-30 05:56:25 -07006291 // We only set the scope if there isn't one
6292 sync_event->scope = src_exec_scope_;
6293
6294 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
6295 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
6296 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
6297 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
6298 }
6299 };
6300 access_context->ForAll(set_scope);
6301 sync_event->unsynchronized_set = CMD_NONE;
John Zulauf78b1f892021-09-20 15:02:09 -06006302 sync_event->first_scope_set = true;
John Zulauf6ce24372021-01-30 05:56:25 -07006303 sync_event->first_scope_tag = tag;
6304 }
John Zulauf4edde622021-02-15 08:54:50 -07006305 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
6306 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006307 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006308 sync_event->barriers = 0U;
6309}
John Zulauf64ffe552021-02-06 10:25:07 -07006310
6311SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
6312 const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07006313 const VkSubpassBeginInfo *pSubpassBeginInfo)
6314 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006315 if (pRenderPassBegin) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006316 rp_state_ = sync_state.Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
John Zulauf64ffe552021-02-06 10:25:07 -07006317 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006318 auto fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07006319 if (fb_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006320 shared_attachments_ = sync_state.GetAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
John Zulauf64ffe552021-02-06 10:25:07 -07006321 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
6322 // Note that this a safe to presist as long as shared_attachments is not cleared
6323 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08006324 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07006325 attachments_.emplace_back(attachment.get());
6326 }
6327 }
6328 if (pSubpassBeginInfo) {
6329 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
6330 }
6331 }
6332}
6333
6334bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6335 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
6336 bool skip = false;
6337
6338 assert(rp_state_.get());
6339 if (nullptr == rp_state_.get()) return skip;
6340 auto &rp_state = *rp_state_.get();
6341
6342 const uint32_t subpass = 0;
6343
6344 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
6345 // hasn't happened yet)
6346 const std::vector<AccessContext> empty_context_vector;
6347 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
6348 cb_context.GetCurrentAccessContext());
6349
6350 // Validate attachment operations
6351 if (attachments_.size() == 0) return skip;
6352 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07006353
6354 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
6355 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
6356 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
6357 // operations (though it's currently a messy approach)
6358 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
6359 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006360
6361 // Validate load operations if there were no layout transition hazards
6362 if (!skip) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07006363 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kCurrentCommandTag);
6364 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006365 }
6366
6367 return skip;
6368}
6369
John Zulauf8eda1562021-04-13 17:06:41 -06006370ResourceUsageTag SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
6371 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf64ffe552021-02-06 10:25:07 -07006372 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
6373 assert(rp_state_.get());
John Zulauf8eda1562021-04-13 17:06:41 -06006374 if (nullptr == rp_state_.get()) return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006375 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006376
6377 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006378}
6379
John Zulauf8eda1562021-04-13 17:06:41 -06006380bool SyncOpBeginRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006381 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006382 return false;
6383}
6384
John Zulauf4fa68462021-04-26 21:04:22 -06006385void SyncOpBeginRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
6386}
John Zulauf8eda1562021-04-13 17:06:41 -06006387
John Zulauf64ffe552021-02-06 10:25:07 -07006388SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07006389 const VkSubpassEndInfo *pSubpassEndInfo)
6390 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006391 if (pSubpassBeginInfo) {
6392 subpass_begin_info_.initialize(pSubpassBeginInfo);
6393 }
6394 if (pSubpassEndInfo) {
6395 subpass_end_info_.initialize(pSubpassEndInfo);
6396 }
6397}
6398
6399bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
6400 bool skip = false;
6401 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6402 if (!renderpass_context) return skip;
6403
6404 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
6405 return skip;
6406}
6407
John Zulauf8eda1562021-04-13 17:06:41 -06006408ResourceUsageTag SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006409 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06006410 // TODO PHASE2 Need to fix renderpass tagging/segregation of barrier and access operations for QueueSubmit time validation
6411 auto prev_tag = cb_context->NextCommandTag(cmd_);
6412 auto next_tag = cb_context->NextSubcommandTag(cmd_);
6413
6414 cb_context->RecordNextSubpass(prev_tag, next_tag);
6415 // TODO PHASE2 This needs to be the tag of the barrier operations
6416 return prev_tag;
6417}
6418
6419bool SyncOpNextSubpass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006420 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006421 return false;
John Zulauf64ffe552021-02-06 10:25:07 -07006422}
6423
sfricke-samsung85584a72021-09-30 21:43:38 -07006424SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo)
6425 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006426 if (pSubpassEndInfo) {
6427 subpass_end_info_.initialize(pSubpassEndInfo);
6428 }
6429}
6430
John Zulauf4fa68462021-04-26 21:04:22 -06006431void SyncOpNextSubpass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006432
John Zulauf64ffe552021-02-06 10:25:07 -07006433bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6434 bool skip = false;
6435 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6436
6437 if (!renderpass_context) return skip;
6438 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
6439 return skip;
6440}
6441
John Zulauf8eda1562021-04-13 17:06:41 -06006442ResourceUsageTag SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006443 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06006444 const auto tag = cb_context->NextCommandTag(cmd_);
6445 cb_context->RecordEndRenderPass(tag);
6446 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006447}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006448
John Zulauf8eda1562021-04-13 17:06:41 -06006449bool SyncOpEndRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006450 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006451 return false;
6452}
6453
John Zulauf4fa68462021-04-26 21:04:22 -06006454void SyncOpEndRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006455
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006456void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6457 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
6458 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
6459 auto *cb_access_context = GetAccessContext(commandBuffer);
6460 assert(cb_access_context);
6461 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
6462 auto *context = cb_access_context->GetCurrentAccessContext();
6463 assert(context);
6464
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006465 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006466
6467 if (dst_buffer) {
6468 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6469 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
6470 }
6471}
John Zulaufd05c5842021-03-26 11:32:16 -06006472
John Zulaufae842002021-04-15 18:20:55 -06006473bool SyncValidator::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6474 const VkCommandBuffer *pCommandBuffers) const {
6475 bool skip = StateTracker::PreCallValidateCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
6476 const char *func_name = "vkCmdExecuteCommands";
6477 const auto *cb_context = GetAccessContext(commandBuffer);
6478 assert(cb_context);
John Zulauf4fa68462021-04-26 21:04:22 -06006479
6480 // Heavyweight, but we need a proxy copy of the active command buffer access context
6481 CommandBufferAccessContext proxy_cb_context(*cb_context, CommandBufferAccessContext::AsProxyContext());
John Zulaufae842002021-04-15 18:20:55 -06006482
6483 // Make working copies of the access and events contexts
John Zulauf4fa68462021-04-26 21:04:22 -06006484 proxy_cb_context.NextCommandTag(CMD_EXECUTECOMMANDS);
John Zulaufae842002021-04-15 18:20:55 -06006485
6486 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
John Zulauf4fa68462021-04-26 21:04:22 -06006487 proxy_cb_context.NextSubcommandTag(CMD_EXECUTECOMMANDS);
John Zulaufae842002021-04-15 18:20:55 -06006488 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6489 if (!recorded_cb_context) continue;
John Zulauf4fa68462021-04-26 21:04:22 -06006490
6491 const auto *recorded_context = recorded_cb_context->GetCurrentAccessContext();
6492 assert(recorded_context);
6493 skip |= recorded_cb_context->ValidateFirstUse(&proxy_cb_context, func_name, cb_index);
6494
6495 // The barriers have already been applied in ValidatFirstUse
6496 ResourceUsageRange tag_range = proxy_cb_context.ImportRecordedAccessLog(*recorded_cb_context);
6497 proxy_cb_context.ResolveRecordedContext(*recorded_context, tag_range.begin);
John Zulaufae842002021-04-15 18:20:55 -06006498 }
6499
John Zulaufae842002021-04-15 18:20:55 -06006500 return skip;
6501}
6502
6503void SyncValidator::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6504 const VkCommandBuffer *pCommandBuffers) {
6505 StateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
John Zulauf4fa68462021-04-26 21:04:22 -06006506 auto *cb_context = GetAccessContext(commandBuffer);
6507 assert(cb_context);
6508 cb_context->NextCommandTag(CMD_EXECUTECOMMANDS);
6509 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
6510 cb_context->NextSubcommandTag(CMD_EXECUTECOMMANDS);
6511 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6512 if (!recorded_cb_context) continue;
6513 cb_context->RecordExecutedCommandBuffer(*recorded_cb_context, CMD_EXECUTECOMMANDS);
6514 }
John Zulaufae842002021-04-15 18:20:55 -06006515}
6516
John Zulaufd0ec59f2021-03-13 14:25:08 -07006517AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
6518 : view_(view), view_mask_(), gen_store_() {
6519 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
6520 const IMAGE_STATE &image_state = *view_->image_state.get();
6521 const auto base_address = ResourceBaseAddress(image_state);
6522 const auto *encoder = image_state.fragment_encoder.get();
6523 if (!encoder) return;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06006524 // Get offset and extent for the view, accounting for possible depth slicing
6525 const VkOffset3D zero_offset = view->GetOffset();
6526 const VkExtent3D &image_extent = view->GetExtent();
John Zulaufd0ec59f2021-03-13 14:25:08 -07006527 // Intentional copy
6528 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
6529 view_mask_ = subres_range.aspectMask;
6530 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
6531 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6532
6533 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
6534 if (depth && (depth != view_mask_)) {
6535 subres_range.aspectMask = depth;
6536 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6537 }
6538 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
6539 if (stencil && (stencil != view_mask_)) {
6540 subres_range.aspectMask = stencil;
6541 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6542 }
6543}
6544
6545const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
6546 const ImageRangeGen *got = nullptr;
6547 switch (gen_type) {
6548 case kViewSubresource:
6549 got = &gen_store_[kViewSubresource];
6550 break;
6551 case kRenderArea:
6552 got = &gen_store_[kRenderArea];
6553 break;
6554 case kDepthOnlyRenderArea:
6555 got =
6556 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
6557 break;
6558 case kStencilOnlyRenderArea:
6559 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
6560 : &gen_store_[Gen::kStencilOnlyRenderArea];
6561 break;
6562 default:
6563 assert(got);
6564 }
6565 return got;
6566}
6567
6568AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
6569 assert(IsValid());
6570 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
6571 if (depth_op) {
6572 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
6573 if (stencil_op) {
6574 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6575 return kRenderArea;
6576 }
6577 return kDepthOnlyRenderArea;
6578 }
6579 if (stencil_op) {
6580 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6581 return kStencilOnlyRenderArea;
6582 }
6583
6584 assert(depth_op || stencil_op);
6585 return kRenderArea;
6586}
6587
6588AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }
John Zulauf8eda1562021-04-13 17:06:41 -06006589
6590void SyncEventsContext::ApplyBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
6591 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
6592 for (auto &event_pair : map_) {
6593 assert(event_pair.second); // Shouldn't be storing empty
6594 auto &sync_event = *event_pair.second;
6595 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
6596 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
6597 sync_event.barriers |= dst.exec_scope;
6598 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6599 }
6600 }
6601}