blob: 98dbdeb963aca66baf869db920755f7a3eb244ea [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;
ziga-lunarg31a3e772022-03-22 11:48:46 +01002496 if (next_subpass >= subpass_contexts_.size()) {
2497 return skip;
2498 }
John Zulauf1507ee42020-05-18 11:33:09 -06002499 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002500 skip |=
2501 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002502 if (!skip) {
2503 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2504 // on a copy of the (empty) next context.
2505 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2506 AccessContext temp_context(next_context);
2507 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002508 skip |=
2509 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002510 }
John Zulauf7635de32020-05-29 17:14:15 -06002511 return skip;
2512}
John Zulauf64ffe552021-02-06 10:25:07 -07002513bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002514 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002515 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002516 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002517 current_subpass_);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002518 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_,
2519
2520 attachment_views_, func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002521 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002522 return skip;
2523}
2524
John Zulauf64ffe552021-02-06 10:25:07 -07002525AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002526 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002527}
2528
John Zulauf64ffe552021-02-06 10:25:07 -07002529bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2530 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002531 bool skip = false;
2532
John Zulauf7635de32020-05-29 17:14:15 -06002533 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2534 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2535 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2536 // to apply and only copy then, if this proves a hot spot.
2537 std::unique_ptr<AccessContext> proxy_for_current;
2538
John Zulauf355e49b2020-04-24 15:11:15 -06002539 // Validate the "finalLayout" transitions to external
2540 // Get them from where there we're hidding in the extra entry.
2541 const auto &final_transitions = rp_state_->subpass_transitions.back();
2542 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002543 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002544 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2545 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002546 auto *context = trackback.context;
2547
2548 if (transition.prev_pass == current_subpass_) {
2549 if (!proxy_for_current) {
2550 // 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 -07002551 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002552 }
2553 context = proxy_for_current.get();
2554 }
2555
John Zulaufa0a98292020-09-18 09:30:10 -06002556 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2557 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002558 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002559 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002560 skip |= ex_context.GetSyncState().LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002561 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07002562 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2563 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2564 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2565 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002566 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002567 }
2568 }
2569 return skip;
2570}
2571
John Zulauf14940722021-04-12 15:19:02 -06002572void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002573 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002574 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002575}
2576
John Zulauf14940722021-04-12 15:19:02 -06002577void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002578 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2579 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002580
2581 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2582 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002583 const AttachmentViewGen &view_gen = attachment_views_[i];
2584 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002585
2586 const auto &ci = attachment_ci[i];
2587 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002588 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002589 const bool is_color = !(has_depth || has_stencil);
2590
2591 if (is_color) {
John Zulauf57261402021-08-13 11:32:06 -06002592 const SyncStageAccessIndex load_op = ColorLoadUsage(ci.loadOp);
2593 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2594 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, load_op,
2595 SyncOrdering::kColorAttachment, tag);
2596 }
John Zulauf1507ee42020-05-18 11:33:09 -06002597 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002598 if (has_depth) {
John Zulauf57261402021-08-13 11:32:06 -06002599 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.loadOp);
2600 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2601 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_op,
2602 SyncOrdering::kDepthStencilAttachment, tag);
2603 }
John Zulauf1507ee42020-05-18 11:33:09 -06002604 }
2605 if (has_stencil) {
John Zulauf57261402021-08-13 11:32:06 -06002606 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.stencilLoadOp);
2607 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2608 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, load_op,
2609 SyncOrdering::kDepthStencilAttachment, tag);
2610 }
John Zulauf1507ee42020-05-18 11:33:09 -06002611 }
2612 }
2613 }
2614 }
2615}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002616AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2617 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2618 AttachmentViewGenVector view_gens;
2619 VkExtent3D extent = CastTo3D(render_area.extent);
2620 VkOffset3D offset = CastTo3D(render_area.offset);
2621 view_gens.reserve(attachment_views.size());
2622 for (const auto *view : attachment_views) {
2623 view_gens.emplace_back(view, offset, extent);
2624 }
2625 return view_gens;
2626}
John Zulauf64ffe552021-02-06 10:25:07 -07002627RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2628 VkQueueFlags queue_flags,
2629 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2630 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002631 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002632 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002633 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002634 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002635 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002636 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002637 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002638}
John Zulauf14940722021-04-12 15:19:02 -06002639void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag tag) {
John Zulauf64ffe552021-02-06 10:25:07 -07002640 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002641 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002642 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002643 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002644}
John Zulauf1507ee42020-05-18 11:33:09 -06002645
John Zulauf14940722021-04-12 15:19:02 -06002646void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag prev_subpass_tag, const ResourceUsageTag next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002647 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulaufd0ec59f2021-03-13 14:25:08 -07002648 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
2649 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002650
ziga-lunarg31a3e772022-03-22 11:48:46 +01002651 if (current_subpass_ + 1 >= subpass_contexts_.size()) {
2652 return;
2653 }
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002654 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2655 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002656 current_subpass_++;
John Zulauffaea0ee2021-01-14 14:01:32 -07002657 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2658 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002659 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002660}
2661
John Zulauf14940722021-04-12 15:19:02 -06002662void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002663 // Add the resolve and store accesses
John Zulaufd0ec59f2021-03-13 14:25:08 -07002664 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, tag);
2665 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002666
John Zulauf355e49b2020-04-24 15:11:15 -06002667 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002668 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002669
2670 // Add the "finalLayout" transitions to external
2671 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002672 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2673 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2674 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002675 const auto &final_transitions = rp_state_->subpass_transitions.back();
2676 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002677 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002678 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002679 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002680 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002681 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002682 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002683 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002684 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002685 }
2686}
2687
Jeremy Gebben40a22942020-12-22 14:22:06 -07002688SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002689 SyncExecScope result;
2690 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002691 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2692 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002693 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2694 return result;
2695}
2696
Jeremy Gebben40a22942020-12-22 14:22:06 -07002697SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002698 SyncExecScope result;
2699 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002700 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2701 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002702 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2703 return result;
2704}
2705
2706SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002707 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002708 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002709 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002710 dst_access_scope = 0;
2711}
2712
2713template <typename Barrier>
2714SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002715 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002716 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002717 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002718 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2719}
2720
2721SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002722 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2723 if (barrier) {
2724 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002725 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002726 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002727
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002728 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002729 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002730 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2731
2732 } else {
2733 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002734 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002735 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2736
2737 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002738 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002739 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2740 }
2741}
2742
2743template <typename Barrier>
2744SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2745 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2746 src_exec_scope = src.exec_scope;
2747 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2748
2749 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002750 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002751 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002752}
2753
John Zulaufb02c1eb2020-10-06 16:33:36 -06002754// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2755void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2756 for (const auto &barrier : barriers) {
2757 ApplyBarrier(barrier, layout_transition);
2758 }
2759}
2760
John Zulauf89311b42020-09-29 16:28:47 -06002761// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2762// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2763// lazily, s.t. no previous access reports should need layout transitions.
John Zulauf14940722021-04-12 15:19:02 -06002764void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06002765 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002766 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002767 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002768 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002769 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002770 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002771 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002772}
John Zulauf9cb530d2019-09-30 14:14:10 -06002773HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2774 HazardResult hazard;
2775 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002776 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002777 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002778 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002779 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002780 }
2781 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002782 // Write operation:
2783 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2784 // If reads exists -- test only against them because either:
2785 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2786 // * 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
2787 // the current write happens after the reads, so just test the write against the reades
2788 // Otherwise test against last_write
2789 //
2790 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002791 if (last_reads.size()) {
2792 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002793 if (IsReadHazard(usage_stage, read_access)) {
2794 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2795 break;
2796 }
2797 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002798 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002799 // Write-After-Write check -- if we have a previous write to test against
2800 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002801 }
2802 }
2803 return hazard;
2804}
2805
John Zulauf4fa68462021-04-26 21:04:22 -06002806HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering ordering_rule) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002807 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf4fa68462021-04-26 21:04:22 -06002808 return DetectHazard(usage_index, ordering);
2809}
2810
2811HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const OrderingBarrier &ordering) const {
John Zulauf69133422020-05-20 14:55:53 -06002812 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2813 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002814 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002815 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002816 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2817 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002818 if (IsRead(usage_bit)) {
2819 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2820 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2821 if (is_raw_hazard) {
2822 // NOTE: we know last_write is non-zero
2823 // See if the ordering rules save us from the simple RAW check above
2824 // First check to see if the current usage is covered by the ordering rules
2825 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2826 const bool usage_is_ordered =
2827 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2828 if (usage_is_ordered) {
2829 // Now see of the most recent write (or a subsequent read) are ordered
2830 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2831 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002832 }
2833 }
John Zulauf4285ee92020-09-23 10:20:52 -06002834 if (is_raw_hazard) {
2835 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2836 }
John Zulauf5c628d02021-05-04 15:46:36 -06002837 } else if (usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2838 // For Image layout transitions, the barrier represents the first synchronization/access scope of the layout transition
2839 return DetectBarrierHazard(usage_index, ordering.exec_scope, ordering.access_scope);
John Zulauf361fb532020-07-22 10:45:39 -06002840 } else {
2841 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002842 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002843 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002844 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002845 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002846 if (usage_write_is_ordered) {
2847 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2848 ordered_stages = GetOrderedStages(ordering);
2849 }
2850 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2851 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002852 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002853 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2854 if (IsReadHazard(usage_stage, read_access)) {
2855 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2856 break;
2857 }
John Zulaufd14743a2020-07-03 09:42:39 -06002858 }
2859 }
John Zulauf2a344ca2021-09-09 17:07:19 -06002860 } else if (last_write.any() && !(last_write_is_ordered && usage_write_is_ordered)) {
2861 bool ilt_ilt_hazard = false;
2862 if ((usage_index == SYNC_IMAGE_LAYOUT_TRANSITION) && (usage_bit == last_write)) {
2863 // ILT after ILT is a special case where we check the 2nd access scope of the first ILT against the first access
2864 // scope of the second ILT, which has been passed (smuggled?) in the ordering barrier
2865 ilt_ilt_hazard = !(write_barriers & ordering.access_scope).any();
2866 }
2867 if (ilt_ilt_hazard || IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002868 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002869 }
John Zulauf69133422020-05-20 14:55:53 -06002870 }
2871 }
2872 return hazard;
2873}
2874
John Zulaufae842002021-04-15 18:20:55 -06002875HazardResult ResourceAccessState::DetectHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range) const {
2876 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002877 using Size = FirstAccesses::size_type;
2878 const auto &recorded_accesses = recorded_use.first_accesses_;
2879 Size count = recorded_accesses.size();
2880 if (count) {
2881 const auto &last_access = recorded_accesses.back();
2882 bool do_write_last = IsWrite(last_access.usage_index);
2883 if (do_write_last) --count;
John Zulaufae842002021-04-15 18:20:55 -06002884
John Zulauf4fa68462021-04-26 21:04:22 -06002885 for (Size i = 0; i < count; ++count) {
2886 const auto &first = recorded_accesses[i];
2887 // Skip and quit logic
2888 if (first.tag < tag_range.begin) continue;
2889 if (first.tag >= tag_range.end) {
2890 do_write_last = false; // ignore last since we know it can't be in tag_range
2891 break;
2892 }
2893
2894 hazard = DetectHazard(first.usage_index, first.ordering_rule);
2895 if (hazard.hazard) {
2896 hazard.AddRecordedAccess(first);
2897 break;
2898 }
2899 }
2900
2901 if (do_write_last && tag_range.includes(last_access.tag)) {
2902 // Writes are a bit special... both for the "most recent" access logic, and layout transition specific logic
2903 OrderingBarrier barrier = GetOrderingRules(last_access.ordering_rule);
2904 if (last_access.usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2905 // Or in the layout first access scope as a barrier... IFF the usage is an ILT
2906 // this was saved off in the "apply barriers" logic to simplify ILT access checks as they straddle
2907 // the barrier that applies them
2908 barrier |= recorded_use.first_write_layout_ordering_;
2909 }
2910 // Any read stages present in the recorded context (this) are most recent to the write, and thus mask those stages in
2911 // the active context
2912 if (recorded_use.first_read_stages_) {
2913 // we need to ignore the first use read stage in the active context (so we add them to the ordering rule),
2914 // reads in the active context are not "most recent" as all recorded context operations are *after* them
2915 // This supresses only RAW checks for stages present in the recorded context, but not those only present in the
2916 // active context.
2917 barrier.exec_scope |= recorded_use.first_read_stages_;
2918 // if there are any first use reads, we suppress WAW by injecting the active context write in the ordering rule
2919 barrier.access_scope |= FlagBit(last_access.usage_index);
2920 }
2921 hazard = DetectHazard(last_access.usage_index, barrier);
2922 if (hazard.hazard) {
2923 hazard.AddRecordedAccess(last_access);
2924 }
2925 }
John Zulaufae842002021-04-15 18:20:55 -06002926 }
2927 return hazard;
2928}
2929
John Zulauf2f952d22020-02-10 11:34:51 -07002930// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf14940722021-04-12 15:19:02 -06002931HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002932 HazardResult hazard;
2933 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002934 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2935 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2936 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002937 if (IsRead(usage)) {
John Zulauf14940722021-04-12 15:19:02 -06002938 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002939 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002940 }
2941 } else {
John Zulauf14940722021-04-12 15:19:02 -06002942 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002943 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002944 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002945 // 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 -07002946 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06002947 if (read_access.tag >= start_tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002948 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002949 break;
2950 }
2951 }
John Zulauf2f952d22020-02-10 11:34:51 -07002952 }
2953 }
2954 return hazard;
2955}
2956
John Zulaufae842002021-04-15 18:20:55 -06002957HazardResult ResourceAccessState::DetectAsyncHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
2958 ResourceUsageTag start_tag) const {
2959 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002960 for (const auto &first : recorded_use.first_accesses_) {
John Zulaufae842002021-04-15 18:20:55 -06002961 // Skip and quit logic
2962 if (first.tag < tag_range.begin) continue;
2963 if (first.tag >= tag_range.end) break;
John Zulaufae842002021-04-15 18:20:55 -06002964
2965 hazard = DetectAsyncHazard(first.usage_index, start_tag);
John Zulauf4fa68462021-04-26 21:04:22 -06002966 if (hazard.hazard) {
2967 hazard.AddRecordedAccess(first);
2968 break;
2969 }
John Zulaufae842002021-04-15 18:20:55 -06002970 }
2971 return hazard;
2972}
2973
Jeremy Gebben40a22942020-12-22 14:22:06 -07002974HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002975 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002976 // Only supporting image layout transitions for now
2977 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2978 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002979 // only test for WAW if there no intervening read operations.
2980 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002981 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002982 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002983 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002984 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002985 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002986 break;
2987 }
2988 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002989 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2990 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2991 }
2992
2993 return hazard;
2994}
2995
Jeremy Gebben40a22942020-12-22 14:22:06 -07002996HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07002997 const SyncStageAccessFlags &src_access_scope,
John Zulauf14940722021-04-12 15:19:02 -06002998 const ResourceUsageTag event_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002999 // Only supporting image layout transitions for now
3000 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3001 HazardResult hazard;
3002 // only test for WAW if there no intervening read operations.
3003 // See DetectHazard(SyncStagetAccessIndex) above for more details.
3004
John Zulaufab7756b2020-12-29 16:10:16 -07003005 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003006 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
3007 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07003008 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06003009 if (read_access.tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003010 // The read is in the events first synchronization scope, so we use a barrier hazard check
3011 // If the read stage is not in the src sync scope
3012 // *AND* not execution chained with an existing sync barrier (that's the or)
3013 // then the barrier access is unsafe (R/W after R)
3014 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3015 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3016 break;
3017 }
3018 } else {
3019 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3020 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3021 }
3022 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003023 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003024 // 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 -06003025 if (write_tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003026 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3027 // So do a normal barrier hazard check
3028 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3029 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3030 }
3031 } else {
3032 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003033 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3034 }
John Zulaufd14743a2020-07-03 09:42:39 -06003035 }
John Zulauf361fb532020-07-22 10:45:39 -06003036
John Zulauf0cb5be22020-01-23 12:18:22 -07003037 return hazard;
3038}
3039
John Zulauf5f13a792020-03-10 07:31:21 -06003040// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3041// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3042// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3043void ResourceAccessState::Resolve(const ResourceAccessState &other) {
John Zulauf14940722021-04-12 15:19:02 -06003044 if (write_tag < other.write_tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003045 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3046 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003047 *this = other;
John Zulauf14940722021-04-12 15:19:02 -06003048 } else if (other.write_tag == write_tag) {
3049 // 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 -06003050 // dependency chaining logic or any stage expansion)
3051 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003052 pending_write_barriers |= other.pending_write_barriers;
3053 pending_layout_transition |= other.pending_layout_transition;
3054 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf4fa68462021-04-26 21:04:22 -06003055 pending_layout_ordering_ |= other.pending_layout_ordering_;
John Zulauf5f13a792020-03-10 07:31:21 -06003056
John Zulaufd14743a2020-07-03 09:42:39 -06003057 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07003058 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06003059 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07003060 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003061 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003062 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003063 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003064 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3065 // but we should wait on profiling data for that.
3066 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003067 auto &my_read = last_reads[my_read_index];
3068 if (other_read.stage == my_read.stage) {
John Zulauf14940722021-04-12 15:19:02 -06003069 if (my_read.tag < other_read.tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003070 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003071 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003072 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003073 my_read.pending_dep_chain = other_read.pending_dep_chain;
3074 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3075 // May require tracking more than one access per stage.
3076 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003077 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06003078 // Since I'm overwriting the fragement stage read, also update the input attachment info
3079 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003080 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003081 }
John Zulauf14940722021-04-12 15:19:02 -06003082 } else if (other_read.tag == my_read.tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06003083 // The read tags match so merge the barriers
3084 my_read.barriers |= other_read.barriers;
3085 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003086 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003087
John Zulauf5f13a792020-03-10 07:31:21 -06003088 break;
3089 }
3090 }
3091 } else {
3092 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07003093 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06003094 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003095 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003096 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003097 }
John Zulauf5f13a792020-03-10 07:31:21 -06003098 }
3099 }
John Zulauf361fb532020-07-22 10:45:39 -06003100 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003101 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3102 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07003103
3104 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
3105 // of the copy and other into this using the update first logic.
3106 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
3107 // of the other first_accesses... )
3108 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
3109 FirstAccesses firsts(std::move(first_accesses_));
3110 first_accesses_.clear();
3111 first_read_stages_ = 0U;
3112 auto a = firsts.begin();
3113 auto a_end = firsts.end();
3114 for (auto &b : other.first_accesses_) {
John Zulauf14940722021-04-12 15:19:02 -06003115 // TODO: Determine whether some tag offset will be needed for PHASE II
3116 while ((a != a_end) && (a->tag < b.tag)) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003117 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3118 ++a;
3119 }
3120 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
3121 }
3122 for (; a != a_end; ++a) {
3123 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3124 }
3125 }
John Zulauf5f13a792020-03-10 07:31:21 -06003126}
3127
John Zulauf14940722021-04-12 15:19:02 -06003128void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003129 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3130 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003131 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003132 // Mulitple outstanding reads may be of interest and do dependency chains independently
3133 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3134 const auto usage_stage = PipelineStageBit(usage_index);
3135 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003136 for (auto &read_access : last_reads) {
3137 if (read_access.stage == usage_stage) {
3138 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003139 break;
3140 }
3141 }
3142 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07003143 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003144 last_read_stages |= usage_stage;
3145 }
John Zulauf4285ee92020-09-23 10:20:52 -06003146
3147 // 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 -07003148 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003149 // TODO Revisit re: multiple reads for a given stage
3150 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003151 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003152 } else {
3153 // Assume write
3154 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003155 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003156 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003157 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06003158}
John Zulauf5f13a792020-03-10 07:31:21 -06003159
John Zulauf89311b42020-09-29 16:28:47 -06003160// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3161// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3162// We can overwrite them as *this* write is now after them.
3163//
3164// 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 -06003165void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003166 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06003167 last_read_stages = 0;
3168 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003169 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003170
3171 write_barriers = 0;
3172 write_dependency_chain = 0;
3173 write_tag = tag;
3174 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003175}
3176
John Zulauf89311b42020-09-29 16:28:47 -06003177// Apply the memory barrier without updating the existing barriers. The execution barrier
3178// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3179// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3180// replace the current write barriers or add to them, so accumulate to pending as well.
3181void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3182 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3183 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003184 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3185 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3186 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3187 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07003188 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003189 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003190 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003191 if (layout_transition) {
3192 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3193 }
John Zulaufa0a98292020-09-18 09:30:10 -06003194 }
John Zulauf89311b42020-09-29 16:28:47 -06003195 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3196 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003197
John Zulauf89311b42020-09-29 16:28:47 -06003198 if (!pending_layout_transition) {
3199 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3200 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003201 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06003202 // 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 -07003203 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
3204 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003205 }
3206 }
John Zulaufa0a98292020-09-18 09:30:10 -06003207 }
John Zulaufa0a98292020-09-18 09:30:10 -06003208}
3209
John Zulauf4a6105a2020-11-17 15:11:05 -07003210// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3211// changes the "chaining" state, but to keep barriers independent. See discussion above.
John Zulauf14940722021-04-12 15:19:02 -06003212void ResourceAccessState::ApplyBarrier(const ResourceUsageTag scope_tag, const SyncBarrier &barrier, bool layout_transition) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003213 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3214 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3215 // in order to know if it's in the excecution scope
3216 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3217 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3218 // errors w.r.t. "most recent" accesses.
John Zulauf14940722021-04-12 15:19:02 -06003219 if (layout_transition || ((write_tag < scope_tag) && (barrier.src_access_scope & last_write).any())) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003220 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003221 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003222 if (layout_transition) {
3223 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3224 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003225 }
3226 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3227 pending_layout_transition |= layout_transition;
3228
3229 if (!pending_layout_transition) {
3230 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3231 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003232 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003233 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3234 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3235 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3236 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3237 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3238 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3239 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulauf14940722021-04-12 15:19:02 -06003240 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 -07003241 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003242 }
3243 }
3244 }
3245}
John Zulauf14940722021-04-12 15:19:02 -06003246void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag tag) {
John Zulauf89311b42020-09-29 16:28:47 -06003247 if (pending_layout_transition) {
John Zulauf4fa68462021-04-26 21:04:22 -06003248 // 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 -06003249 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07003250 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf4fa68462021-04-26 21:04:22 -06003251 TouchupFirstForLayoutTransition(tag, pending_layout_ordering_);
3252 pending_layout_ordering_ = OrderingBarrier();
John Zulauf89311b42020-09-29 16:28:47 -06003253 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003254 }
John Zulauf89311b42020-09-29 16:28:47 -06003255
3256 // Apply the accumulate execution barriers (and thus update chaining information)
John Zulauf4fa68462021-04-26 21:04:22 -06003257 // for layout transition, last_reads is reset by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003258 for (auto &read_access : last_reads) {
3259 read_access.barriers |= read_access.pending_dep_chain;
3260 read_execution_barriers |= read_access.barriers;
3261 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003262 }
3263
3264 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3265 write_dependency_chain |= pending_write_dep_chain;
3266 write_barriers |= pending_write_barriers;
3267 pending_write_dep_chain = 0;
3268 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003269}
3270
John Zulaufae842002021-04-15 18:20:55 -06003271bool ResourceAccessState::FirstAccessInTagRange(const ResourceUsageRange &tag_range) const {
3272 if (!first_accesses_.size()) return false;
3273 const ResourceUsageRange first_access_range = {first_accesses_.front().tag, first_accesses_.back().tag + 1};
3274 return tag_range.intersects(first_access_range);
3275}
3276
John Zulauf59e25072020-07-17 10:55:21 -06003277// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07003278VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
3279 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003280
John Zulaufab7756b2020-12-29 16:10:16 -07003281 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003282 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003283 barriers = read_access.barriers;
3284 break;
John Zulauf59e25072020-07-17 10:55:21 -06003285 }
3286 }
John Zulauf4285ee92020-09-23 10:20:52 -06003287
John Zulauf59e25072020-07-17 10:55:21 -06003288 return barriers;
3289}
3290
Jeremy Gebben40a22942020-12-22 14:22:06 -07003291inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003292 assert(IsRead(usage));
3293 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3294 // * the previous reads are not hazards, and thus last_write must be visible and available to
3295 // any reads that happen after.
3296 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3297 // 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 -07003298 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003299}
3300
Jeremy Gebben40a22942020-12-22 14:22:06 -07003301VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003302 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07003303 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06003304 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003305 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003306 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003307 // 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 -07003308 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06003309 }
3310
3311 return ordered_stages;
3312}
3313
John Zulauf14940722021-04-12 15:19:02 -06003314void ResourceAccessState::UpdateFirst(const ResourceUsageTag tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003315 // Only record until we record a write.
3316 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003317 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003318 if (0 == (usage_stage & first_read_stages_)) {
3319 // If this is a read we haven't seen or a write, record.
John Zulauf4fa68462021-04-26 21:04:22 -06003320 // We always need to know what stages were found prior to write
John Zulauffaea0ee2021-01-14 14:01:32 -07003321 first_read_stages_ |= usage_stage;
John Zulauf4fa68462021-04-26 21:04:22 -06003322 if (0 == (read_execution_barriers & usage_stage)) {
3323 // If this stage isn't masked then we add it (since writes map to usage_stage 0, this also records writes)
3324 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3325 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003326 }
3327 }
3328}
3329
John Zulauf4fa68462021-04-26 21:04:22 -06003330void ResourceAccessState::TouchupFirstForLayoutTransition(ResourceUsageTag tag, const OrderingBarrier &layout_ordering) {
3331 // Only call this after recording an image layout transition
3332 assert(first_accesses_.size());
3333 if (first_accesses_.back().tag == tag) {
3334 // 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 +02003335 assert(first_accesses_.back().usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
John Zulauf4fa68462021-04-26 21:04:22 -06003336 first_write_layout_ordering_ = layout_ordering;
3337 }
3338}
3339
John Zulaufd1f85d42020-04-15 12:23:15 -06003340void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003341 auto *access_context = GetAccessContextNoInsert(command_buffer);
3342 if (access_context) {
3343 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003344 }
3345}
3346
John Zulaufd1f85d42020-04-15 12:23:15 -06003347void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3348 auto access_found = cb_access_state.find(command_buffer);
3349 if (access_found != cb_access_state.end()) {
3350 access_found->second->Reset();
John Zulauf4fa68462021-04-26 21:04:22 -06003351 access_found->second->MarkDestroyed();
John Zulaufd1f85d42020-04-15 12:23:15 -06003352 cb_access_state.erase(access_found);
3353 }
3354}
3355
John Zulauf9cb530d2019-09-30 14:14:10 -06003356bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3357 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3358 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003359 const auto *cb_context = GetAccessContext(commandBuffer);
3360 assert(cb_context);
3361 if (!cb_context) return skip;
3362 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003363
John Zulauf3d84f1b2020-03-09 13:33:25 -06003364 // If we have no previous accesses, we have no hazards
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003365 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3366 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003367
3368 for (uint32_t region = 0; region < regionCount; region++) {
3369 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003370 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003371 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003372 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003373 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003374 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003375 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003376 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003377 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003378 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003379 }
John Zulauf16adfc92020-04-08 10:28:33 -06003380 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003381 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003382 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003383 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003384 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003385 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003386 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003387 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003388 }
3389 }
3390 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003391 }
3392 return skip;
3393}
3394
3395void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3396 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003397 auto *cb_context = GetAccessContext(commandBuffer);
3398 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003399 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003400 auto *context = cb_context->GetCurrentAccessContext();
3401
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003402 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3403 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003404
3405 for (uint32_t region = 0; region < regionCount; region++) {
3406 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003407 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003408 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003409 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003410 }
John Zulauf16adfc92020-04-08 10:28:33 -06003411 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003412 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003413 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003414 }
3415 }
3416}
3417
John Zulauf4a6105a2020-11-17 15:11:05 -07003418void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3419 // Clear out events from the command buffer contexts
3420 for (auto &cb_context : cb_access_state) {
3421 cb_context.second->RecordDestroyEvent(event);
3422 }
3423}
3424
Tony-LunarGef035472021-11-02 10:23:33 -06003425bool SyncValidator::ValidateCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos,
3426 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04003427 bool skip = false;
3428 const auto *cb_context = GetAccessContext(commandBuffer);
3429 assert(cb_context);
3430 if (!cb_context) return skip;
3431 const auto *context = cb_context->GetCurrentAccessContext();
Tony-LunarGef035472021-11-02 10:23:33 -06003432 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003433
3434 // If we have no previous accesses, we have no hazards
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003435 auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3436 auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003437
3438 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3439 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3440 if (src_buffer) {
3441 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003442 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003443 if (hazard.hazard) {
3444 // TODO -- add tag information to log msg when useful.
3445 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
Tony-LunarGef035472021-11-02 10:23:33 -06003446 "%s(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003447 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003448 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003449 }
3450 }
3451 if (dst_buffer && !skip) {
3452 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003453 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003454 if (hazard.hazard) {
3455 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
Tony-LunarGef035472021-11-02 10:23:33 -06003456 "%s(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003457 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003458 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003459 }
3460 }
3461 if (skip) break;
3462 }
3463 return skip;
3464}
3465
Tony-LunarGef035472021-11-02 10:23:33 -06003466bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3467 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3468 return ValidateCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2KHR);
3469}
3470
3471bool SyncValidator::PreCallValidateCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos) const {
3472 return ValidateCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2);
3473}
3474
3475void SyncValidator::RecordCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos, CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04003476 auto *cb_context = GetAccessContext(commandBuffer);
3477 assert(cb_context);
Tony-LunarGef035472021-11-02 10:23:33 -06003478 const auto tag = cb_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003479 auto *context = cb_context->GetCurrentAccessContext();
3480
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003481 auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3482 auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003483
3484 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3485 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3486 if (src_buffer) {
3487 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003488 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003489 }
3490 if (dst_buffer) {
3491 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003492 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003493 }
3494 }
3495}
3496
Tony-LunarGef035472021-11-02 10:23:33 -06003497void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3498 RecordCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2KHR);
3499}
3500
3501void SyncValidator::PreCallRecordCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos) {
3502 RecordCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2);
3503}
3504
John Zulauf5c5e88d2019-12-26 11:22:02 -07003505bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3506 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3507 const VkImageCopy *pRegions) const {
3508 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003509 const auto *cb_access_context = GetAccessContext(commandBuffer);
3510 assert(cb_access_context);
3511 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003512
John Zulauf3d84f1b2020-03-09 13:33:25 -06003513 const auto *context = cb_access_context->GetCurrentAccessContext();
3514 assert(context);
3515 if (!context) return skip;
3516
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003517 auto src_image = Get<IMAGE_STATE>(srcImage);
3518 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003519 for (uint32_t region = 0; region < regionCount; region++) {
3520 const auto &copy_region = pRegions[region];
3521 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003522 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003523 copy_region.srcOffset, copy_region.extent);
3524 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003525 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003526 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003527 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003528 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003529 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003530 }
3531
3532 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003533 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
ziga-lunarg73746512022-03-23 23:08:17 +01003534 copy_region.dstOffset, copy_region.extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003535 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003536 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003537 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003538 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003539 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003540 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003541 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003542 }
3543 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003544
John Zulauf5c5e88d2019-12-26 11:22:02 -07003545 return skip;
3546}
3547
3548void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3549 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3550 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003551 auto *cb_access_context = GetAccessContext(commandBuffer);
3552 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003553 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003554 auto *context = cb_access_context->GetCurrentAccessContext();
3555 assert(context);
3556
Jeremy Gebben9f537102021-10-05 16:37:12 -06003557 auto src_image = Get<IMAGE_STATE>(srcImage);
3558 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003559
3560 for (uint32_t region = 0; region < regionCount; region++) {
3561 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003562 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003563 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003564 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003565 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003566 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003567 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
ziga-lunarg73746512022-03-23 23:08:17 +01003568 copy_region.dstSubresource, copy_region.dstOffset, copy_region.extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003569 }
3570 }
3571}
3572
Tony-LunarGb61514a2021-11-02 12:36:51 -06003573bool SyncValidator::ValidateCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo,
3574 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04003575 bool skip = false;
3576 const auto *cb_access_context = GetAccessContext(commandBuffer);
3577 assert(cb_access_context);
3578 if (!cb_access_context) return skip;
3579
3580 const auto *context = cb_access_context->GetCurrentAccessContext();
3581 assert(context);
3582 if (!context) return skip;
3583
Tony-LunarGb61514a2021-11-02 12:36:51 -06003584 const char *func_name = CommandTypeString(cmd_type);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003585 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3586 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Tony-LunarGb61514a2021-11-02 12:36:51 -06003587
Jeff Leger178b1e52020-10-05 12:22:23 -04003588 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3589 const auto &copy_region = pCopyImageInfo->pRegions[region];
3590 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003591 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003592 copy_region.srcOffset, copy_region.extent);
3593 if (hazard.hazard) {
3594 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
sfricke-samsung71f04e32022-03-16 01:21:21 -05003595 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003596 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003597 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003598 }
3599 }
3600
3601 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003602 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
ziga-lunarg73746512022-03-23 23:08:17 +01003603 copy_region.dstOffset, copy_region.extent);
Jeff Leger178b1e52020-10-05 12:22:23 -04003604 if (hazard.hazard) {
3605 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
sfricke-samsung71f04e32022-03-16 01:21:21 -05003606 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003607 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003608 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003609 }
3610 if (skip) break;
3611 }
3612 }
3613
3614 return skip;
3615}
3616
Tony-LunarGb61514a2021-11-02 12:36:51 -06003617bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3618 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3619 return ValidateCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2KHR);
3620}
3621
3622bool SyncValidator::PreCallValidateCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo) const {
3623 return ValidateCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2);
3624}
3625
3626void SyncValidator::RecordCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo, CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04003627 auto *cb_access_context = GetAccessContext(commandBuffer);
3628 assert(cb_access_context);
Tony-LunarGb61514a2021-11-02 12:36:51 -06003629 const auto tag = cb_access_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003630 auto *context = cb_access_context->GetCurrentAccessContext();
3631 assert(context);
3632
Jeremy Gebben9f537102021-10-05 16:37:12 -06003633 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3634 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04003635
3636 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3637 const auto &copy_region = pCopyImageInfo->pRegions[region];
3638 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003639 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003640 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003641 }
3642 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003643 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
ziga-lunarg73746512022-03-23 23:08:17 +01003644 copy_region.dstSubresource, copy_region.dstOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003645 }
3646 }
3647}
3648
Tony-LunarGb61514a2021-11-02 12:36:51 -06003649void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3650 RecordCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2KHR);
3651}
3652
3653void SyncValidator::PreCallRecordCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo) {
3654 RecordCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2);
3655}
3656
John Zulauf9cb530d2019-09-30 14:14:10 -06003657bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3658 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3659 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3660 uint32_t bufferMemoryBarrierCount,
3661 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3662 uint32_t imageMemoryBarrierCount,
3663 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3664 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003665 const auto *cb_access_context = GetAccessContext(commandBuffer);
3666 assert(cb_access_context);
3667 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003668
John Zulauf36ef9282021-02-02 11:47:24 -07003669 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3670 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3671 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3672 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003673 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003674 return skip;
3675}
3676
3677void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3678 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3679 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3680 uint32_t bufferMemoryBarrierCount,
3681 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3682 uint32_t imageMemoryBarrierCount,
3683 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003684 auto *cb_access_context = GetAccessContext(commandBuffer);
3685 assert(cb_access_context);
3686 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003687
John Zulauf1bf30522021-09-03 15:39:06 -06003688 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(),
3689 srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount,
3690 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
3691 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06003692}
3693
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003694bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3695 const VkDependencyInfoKHR *pDependencyInfo) const {
3696 bool skip = false;
3697 const auto *cb_access_context = GetAccessContext(commandBuffer);
3698 assert(cb_access_context);
3699 if (!cb_access_context) return skip;
3700
3701 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3702 skip = pipeline_barrier.Validate(*cb_access_context);
3703 return skip;
3704}
3705
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07003706bool SyncValidator::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer,
3707 const VkDependencyInfo *pDependencyInfo) const {
3708 bool skip = false;
3709 const auto *cb_access_context = GetAccessContext(commandBuffer);
3710 assert(cb_access_context);
3711 if (!cb_access_context) return skip;
3712
3713 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3714 skip = pipeline_barrier.Validate(*cb_access_context);
3715 return skip;
3716}
3717
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003718void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3719 auto *cb_access_context = GetAccessContext(commandBuffer);
3720 assert(cb_access_context);
3721 if (!cb_access_context) return;
3722
John Zulauf1bf30522021-09-03 15:39:06 -06003723 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(),
3724 *pDependencyInfo);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003725}
3726
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07003727void SyncValidator::PreCallRecordCmdPipelineBarrier2(VkCommandBuffer commandBuffer, const VkDependencyInfo *pDependencyInfo) {
3728 auto *cb_access_context = GetAccessContext(commandBuffer);
3729 assert(cb_access_context);
3730 if (!cb_access_context) return;
3731
3732 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2, *this, cb_access_context->GetQueueFlags(),
3733 *pDependencyInfo);
3734}
3735
John Zulauf9cb530d2019-09-30 14:14:10 -06003736void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3737 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3738 // The state tracker sets up the device state
3739 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3740
John Zulauf5f13a792020-03-10 07:31:21 -06003741 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3742 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003743 // TODO: Find a good way to do this hooklessly.
3744 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3745 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3746 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3747
John Zulaufd1f85d42020-04-15 12:23:15 -06003748 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3749 sync_device_state->ResetCommandBufferCallback(command_buffer);
3750 });
3751 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3752 sync_device_state->FreeCommandBufferCallback(command_buffer);
3753 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003754}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003755
John Zulauf355e49b2020-04-24 15:11:15 -06003756bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003757 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003758 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003759 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003760 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003761 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003762 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003763 }
John Zulauf355e49b2020-04-24 15:11:15 -06003764 return skip;
3765}
3766
3767bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3768 VkSubpassContents contents) const {
3769 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003770 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003771 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003772 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003773 return skip;
3774}
3775
3776bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003777 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003778 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003779 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003780 return skip;
3781}
3782
3783bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3784 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003785 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003786 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003787 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003788 return skip;
3789}
3790
John Zulauf3d84f1b2020-03-09 13:33:25 -06003791void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3792 VkResult result) {
3793 // The state tracker sets up the command buffer state
3794 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3795
3796 // Create/initialize the structure that trackers accesses at the command buffer scope.
3797 auto cb_access_context = GetAccessContext(commandBuffer);
3798 assert(cb_access_context);
3799 cb_access_context->Reset();
3800}
3801
3802void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003803 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003804 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003805 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003806 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003807 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003808 }
3809}
3810
3811void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3812 VkSubpassContents contents) {
3813 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003814 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003815 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003816 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003817}
3818
3819void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3820 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3821 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003822 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003823}
3824
3825void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3826 const VkRenderPassBeginInfo *pRenderPassBegin,
3827 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3828 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003829 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003830}
3831
Mike Schuchardt2df08912020-12-15 16:28:09 -08003832bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003833 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003834 bool skip = false;
3835
3836 auto cb_context = GetAccessContext(commandBuffer);
3837 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003838 if (!cb_context) return skip;
sfricke-samsung85584a72021-09-30 21:43:38 -07003839 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003840 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003841}
3842
3843bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3844 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003845 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003846 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003847 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003848 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3849 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003850 return skip;
3851}
3852
Mike Schuchardt2df08912020-12-15 16:28:09 -08003853bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3854 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003855 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003856 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003857 return skip;
3858}
3859
3860bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3861 const VkSubpassEndInfo *pSubpassEndInfo) const {
3862 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003863 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003864 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003865}
3866
3867void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003868 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003869 auto cb_context = GetAccessContext(commandBuffer);
3870 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003871 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003872
sfricke-samsung85584a72021-09-30 21:43:38 -07003873 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003874 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003875}
3876
3877void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3878 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003879 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003880 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003881 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003882}
3883
3884void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3885 const VkSubpassEndInfo *pSubpassEndInfo) {
3886 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003887 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003888}
3889
3890void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3891 const VkSubpassEndInfo *pSubpassEndInfo) {
3892 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003893 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003894}
3895
sfricke-samsung85584a72021-09-30 21:43:38 -07003896bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3897 CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003898 bool skip = false;
3899
3900 auto cb_context = GetAccessContext(commandBuffer);
3901 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003902 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003903
sfricke-samsung85584a72021-09-30 21:43:38 -07003904 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003905 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003906 return skip;
3907}
3908
3909bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3910 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003911 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003912 return skip;
3913}
3914
Mike Schuchardt2df08912020-12-15 16:28:09 -08003915bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003916 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003917 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003918 return skip;
3919}
3920
3921bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003922 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003923 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003924 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003925 return skip;
3926}
3927
sfricke-samsung85584a72021-09-30 21:43:38 -07003928void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003929 // Resolve the all subpass contexts to the command buffer contexts
3930 auto cb_context = GetAccessContext(commandBuffer);
3931 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003932 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003933
sfricke-samsung85584a72021-09-30 21:43:38 -07003934 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003935 sync_op.Record(cb_context);
3936 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003937}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003938
John Zulauf33fc1d52020-07-17 11:01:10 -06003939// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3940// updates to a resource which do not conflict at the byte level.
3941// TODO: Revisit this rule to see if it needs to be tighter or looser
3942// TODO: Add programatic control over suppression heuristics
3943bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3944 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3945}
3946
John Zulauf3d84f1b2020-03-09 13:33:25 -06003947void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003948 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003949 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003950}
3951
3952void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003953 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003954 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003955}
3956
3957void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003958 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf5a1a5382020-06-22 17:23:25 -06003959 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003960}
locke-lunarga19c71d2020-03-02 18:17:04 -07003961
sfricke-samsung71f04e32022-03-16 01:21:21 -05003962template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04003963bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
sfricke-samsung71f04e32022-03-16 01:21:21 -05003964 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions,
3965 CMD_TYPE cmd_type) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003966 bool skip = false;
3967 const auto *cb_access_context = GetAccessContext(commandBuffer);
3968 assert(cb_access_context);
3969 if (!cb_access_context) return skip;
3970
Tony Barbour845d29b2021-11-09 11:43:14 -07003971 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003972
locke-lunarga19c71d2020-03-02 18:17:04 -07003973 const auto *context = cb_access_context->GetCurrentAccessContext();
3974 assert(context);
3975 if (!context) return skip;
3976
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003977 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3978 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003979
3980 for (uint32_t region = 0; region < regionCount; region++) {
3981 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003982 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003983 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003984 if (src_buffer) {
3985 ResourceAccessRange src_range =
3986 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003987 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07003988 if (hazard.hazard) {
3989 // PHASE1 TODO -- add tag information to log msg when useful.
3990 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3991 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3992 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003993 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003994 }
3995 }
3996
Jeremy Gebben40a22942020-12-22 14:22:06 -07003997 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07003998 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003999 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004000 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004001 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004002 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004003 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004004 }
4005 if (skip) break;
4006 }
4007 if (skip) break;
4008 }
4009 return skip;
4010}
4011
Jeff Leger178b1e52020-10-05 12:22:23 -04004012bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4013 VkImageLayout dstImageLayout, uint32_t regionCount,
4014 const VkBufferImageCopy *pRegions) const {
4015 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
Tony Barbour845d29b2021-11-09 11:43:14 -07004016 CMD_COPYBUFFERTOIMAGE);
Jeff Leger178b1e52020-10-05 12:22:23 -04004017}
4018
4019bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4020 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
4021 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4022 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
Tony Barbour845d29b2021-11-09 11:43:14 -07004023 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2KHR);
4024}
4025
4026bool SyncValidator::PreCallValidateCmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
4027 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) const {
4028 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4029 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4030 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004031}
4032
sfricke-samsung71f04e32022-03-16 01:21:21 -05004033template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004034void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004035 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions,
4036 CMD_TYPE cmd_type) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004037 auto *cb_access_context = GetAccessContext(commandBuffer);
4038 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004039
Jeff Leger178b1e52020-10-05 12:22:23 -04004040 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004041 auto *context = cb_access_context->GetCurrentAccessContext();
4042 assert(context);
4043
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004044 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
4045 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004046
4047 for (uint32_t region = 0; region < regionCount; region++) {
4048 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07004049 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07004050 if (src_buffer) {
4051 ResourceAccessRange src_range =
4052 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004053 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004054 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07004055 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004056 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004057 }
4058 }
4059}
4060
Jeff Leger178b1e52020-10-05 12:22:23 -04004061void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4062 VkImageLayout dstImageLayout, uint32_t regionCount,
4063 const VkBufferImageCopy *pRegions) {
4064 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
Tony Barbour845d29b2021-11-09 11:43:14 -07004065 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, CMD_COPYBUFFERTOIMAGE);
Jeff Leger178b1e52020-10-05 12:22:23 -04004066}
4067
4068void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4069 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4070 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4071 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4072 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
Tony Barbour845d29b2021-11-09 11:43:14 -07004073 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2KHR);
4074}
4075
4076void SyncValidator::PreCallRecordCmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
4077 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) {
4078 StateTracker::PreCallRecordCmdCopyBufferToImage2(commandBuffer, pCopyBufferToImageInfo);
4079 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4080 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4081 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004082}
4083
sfricke-samsung71f04e32022-03-16 01:21:21 -05004084template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004085bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004086 VkBuffer dstBuffer, uint32_t regionCount, const RegionType *pRegions,
4087 CMD_TYPE cmd_type) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004088 bool skip = false;
4089 const auto *cb_access_context = GetAccessContext(commandBuffer);
4090 assert(cb_access_context);
4091 if (!cb_access_context) return skip;
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004092 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04004093
locke-lunarga19c71d2020-03-02 18:17:04 -07004094 const auto *context = cb_access_context->GetCurrentAccessContext();
4095 assert(context);
4096 if (!context) return skip;
4097
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004098 auto src_image = Get<IMAGE_STATE>(srcImage);
4099 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004100 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
locke-lunarga19c71d2020-03-02 18:17:04 -07004101 for (uint32_t region = 0; region < regionCount; region++) {
4102 const auto &copy_region = pRegions[region];
4103 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004104 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004105 copy_region.imageOffset, copy_region.imageExtent);
4106 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004107 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004108 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004109 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004110 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004111 }
John Zulauf477700e2021-01-06 11:41:49 -07004112 if (dst_mem) {
4113 ResourceAccessRange dst_range =
4114 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004115 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07004116 if (hazard.hazard) {
4117 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4118 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4119 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004120 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07004121 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004122 }
4123 }
4124 if (skip) break;
4125 }
4126 return skip;
4127}
4128
Jeff Leger178b1e52020-10-05 12:22:23 -04004129bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4130 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4131 const VkBufferImageCopy *pRegions) const {
4132 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004133 CMD_COPYIMAGETOBUFFER);
Jeff Leger178b1e52020-10-05 12:22:23 -04004134}
4135
4136bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4137 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4138 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4139 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004140 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2KHR);
4141}
4142
4143bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
4144 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) const {
4145 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4146 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4147 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004148}
4149
sfricke-samsung71f04e32022-03-16 01:21:21 -05004150template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004151void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004152 VkBuffer dstBuffer, uint32_t regionCount, const RegionType *pRegions,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004153 CMD_TYPE cmd_type) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004154 auto *cb_access_context = GetAccessContext(commandBuffer);
4155 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004156
Jeff Leger178b1e52020-10-05 12:22:23 -04004157 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004158 auto *context = cb_access_context->GetCurrentAccessContext();
4159 assert(context);
4160
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004161 auto src_image = Get<IMAGE_STATE>(srcImage);
Jeremy Gebben9f537102021-10-05 16:37:12 -06004162 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004163 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06004164 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004165
4166 for (uint32_t region = 0; region < regionCount; region++) {
4167 const auto &copy_region = pRegions[region];
4168 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004169 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004170 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004171 if (dst_buffer) {
4172 ResourceAccessRange dst_range =
4173 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004174 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004175 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004176 }
4177 }
4178}
4179
Jeff Leger178b1e52020-10-05 12:22:23 -04004180void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4181 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4182 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004183 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, CMD_COPYIMAGETOBUFFER);
Jeff Leger178b1e52020-10-05 12:22:23 -04004184}
4185
4186void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4187 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4188 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4189 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4190 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004191 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2KHR);
4192}
4193
4194void SyncValidator::PreCallRecordCmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
4195 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) {
4196 StateTracker::PreCallRecordCmdCopyImageToBuffer2(commandBuffer, pCopyImageToBufferInfo);
4197 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4198 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4199 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004200}
4201
4202template <typename RegionType>
4203bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4204 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4205 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004206 bool skip = false;
4207 const auto *cb_access_context = GetAccessContext(commandBuffer);
4208 assert(cb_access_context);
4209 if (!cb_access_context) return skip;
4210
4211 const auto *context = cb_access_context->GetCurrentAccessContext();
4212 assert(context);
4213 if (!context) return skip;
4214
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004215 auto src_image = Get<IMAGE_STATE>(srcImage);
4216 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004217
4218 for (uint32_t region = 0; region < regionCount; region++) {
4219 const auto &blit_region = pRegions[region];
4220 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004221 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4222 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4223 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4224 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4225 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4226 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004227 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004228 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004229 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004230 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004231 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004232 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004233 }
4234 }
4235
4236 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004237 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4238 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4239 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4240 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4241 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4242 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004243 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004244 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004245 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004246 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004247 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004248 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004249 }
4250 if (skip) break;
4251 }
4252 }
4253
4254 return skip;
4255}
4256
Jeff Leger178b1e52020-10-05 12:22:23 -04004257bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4258 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4259 const VkImageBlit *pRegions, VkFilter filter) const {
4260 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4261 "vkCmdBlitImage");
4262}
4263
4264bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4265 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4266 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4267 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4268 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4269}
4270
Tony-LunarG542ae912021-11-04 16:06:44 -06004271bool SyncValidator::PreCallValidateCmdBlitImage2(VkCommandBuffer commandBuffer,
4272 const VkBlitImageInfo2 *pBlitImageInfo) const {
4273 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4274 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4275 pBlitImageInfo->filter, "vkCmdBlitImage2");
4276}
4277
Jeff Leger178b1e52020-10-05 12:22:23 -04004278template <typename RegionType>
4279void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4280 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4281 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004282 auto *cb_access_context = GetAccessContext(commandBuffer);
4283 assert(cb_access_context);
4284 auto *context = cb_access_context->GetCurrentAccessContext();
4285 assert(context);
4286
Jeremy Gebben9f537102021-10-05 16:37:12 -06004287 auto src_image = Get<IMAGE_STATE>(srcImage);
4288 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004289
4290 for (uint32_t region = 0; region < regionCount; region++) {
4291 const auto &blit_region = pRegions[region];
4292 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004293 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4294 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4295 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4296 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4297 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4298 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004299 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004300 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004301 }
4302 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004303 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4304 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4305 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4306 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4307 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4308 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004309 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004310 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004311 }
4312 }
4313}
locke-lunarg36ba2592020-04-03 09:42:04 -06004314
Jeff Leger178b1e52020-10-05 12:22:23 -04004315void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4316 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4317 const VkImageBlit *pRegions, VkFilter filter) {
4318 auto *cb_access_context = GetAccessContext(commandBuffer);
4319 assert(cb_access_context);
4320 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4321 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4322 pRegions, filter);
4323 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4324}
4325
4326void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4327 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4328 auto *cb_access_context = GetAccessContext(commandBuffer);
4329 assert(cb_access_context);
4330 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4331 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4332 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4333 pBlitImageInfo->filter, tag);
4334}
4335
Tony-LunarG542ae912021-11-04 16:06:44 -06004336void SyncValidator::PreCallRecordCmdBlitImage2(VkCommandBuffer commandBuffer, const VkBlitImageInfo2 *pBlitImageInfo) {
4337 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4338 auto *cb_access_context = GetAccessContext(commandBuffer);
4339 assert(cb_access_context);
4340 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2);
4341 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4342 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4343 pBlitImageInfo->filter, tag);
4344}
4345
John Zulauffaea0ee2021-01-14 14:01:32 -07004346bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4347 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
4348 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
4349 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004350 bool skip = false;
4351 if (drawCount == 0) return skip;
4352
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004353 auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004354 VkDeviceSize size = struct_size;
4355 if (drawCount == 1 || stride == size) {
4356 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004357 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004358 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4359 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004360 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004361 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004362 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004363 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004364 }
4365 } else {
4366 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004367 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004368 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4369 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004370 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004371 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4372 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004373 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004374 break;
4375 }
4376 }
4377 }
4378 return skip;
4379}
4380
John Zulauf14940722021-04-12 15:19:02 -06004381void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag tag, const VkDeviceSize struct_size,
locke-lunarg61870c22020-06-09 14:51:50 -06004382 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4383 uint32_t stride) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004384 auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004385 VkDeviceSize size = struct_size;
4386 if (drawCount == 1 || stride == size) {
4387 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004388 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004389 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004390 } else {
4391 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004392 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004393 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4394 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004395 }
4396 }
4397}
4398
John Zulauffaea0ee2021-01-14 14:01:32 -07004399bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4400 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4401 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004402 bool skip = false;
4403
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004404 auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004405 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004406 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4407 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004408 skip |= LogError(count_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004409 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004410 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004411 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004412 }
4413 return skip;
4414}
4415
John Zulauf14940722021-04-12 15:19:02 -06004416void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag tag, VkBuffer buffer, VkDeviceSize offset) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004417 auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004418 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004419 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004420}
4421
locke-lunarg36ba2592020-04-03 09:42:04 -06004422bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004423 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004424 const auto *cb_access_context = GetAccessContext(commandBuffer);
4425 assert(cb_access_context);
4426 if (!cb_access_context) return skip;
4427
locke-lunarg61870c22020-06-09 14:51:50 -06004428 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004429 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004430}
4431
4432void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004433 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004434 auto *cb_access_context = GetAccessContext(commandBuffer);
4435 assert(cb_access_context);
4436 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004437
locke-lunarg61870c22020-06-09 14:51:50 -06004438 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004439}
locke-lunarge1a67022020-04-29 00:15:36 -06004440
4441bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004442 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004443 const auto *cb_access_context = GetAccessContext(commandBuffer);
4444 assert(cb_access_context);
4445 if (!cb_access_context) return skip;
4446
4447 const auto *context = cb_access_context->GetCurrentAccessContext();
4448 assert(context);
4449 if (!context) return skip;
4450
locke-lunarg61870c22020-06-09 14:51:50 -06004451 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004452 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4453 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004454 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004455}
4456
4457void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004458 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004459 auto *cb_access_context = GetAccessContext(commandBuffer);
4460 assert(cb_access_context);
4461 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4462 auto *context = cb_access_context->GetCurrentAccessContext();
4463 assert(context);
4464
locke-lunarg61870c22020-06-09 14:51:50 -06004465 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4466 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004467}
4468
4469bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4470 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004471 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004472 const auto *cb_access_context = GetAccessContext(commandBuffer);
4473 assert(cb_access_context);
4474 if (!cb_access_context) return skip;
4475
locke-lunarg61870c22020-06-09 14:51:50 -06004476 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4477 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4478 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004479 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004480}
4481
4482void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4483 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004484 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004485 auto *cb_access_context = GetAccessContext(commandBuffer);
4486 assert(cb_access_context);
4487 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004488
locke-lunarg61870c22020-06-09 14:51:50 -06004489 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4490 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4491 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004492}
4493
4494bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4495 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004496 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004497 const auto *cb_access_context = GetAccessContext(commandBuffer);
4498 assert(cb_access_context);
4499 if (!cb_access_context) return skip;
4500
locke-lunarg61870c22020-06-09 14:51:50 -06004501 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4502 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4503 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004504 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004505}
4506
4507void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4508 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004509 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004510 auto *cb_access_context = GetAccessContext(commandBuffer);
4511 assert(cb_access_context);
4512 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004513
locke-lunarg61870c22020-06-09 14:51:50 -06004514 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4515 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4516 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004517}
4518
4519bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4520 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004521 bool skip = false;
4522 if (drawCount == 0) return skip;
4523
locke-lunargff255f92020-05-13 18:53:52 -06004524 const auto *cb_access_context = GetAccessContext(commandBuffer);
4525 assert(cb_access_context);
4526 if (!cb_access_context) return skip;
4527
4528 const auto *context = cb_access_context->GetCurrentAccessContext();
4529 assert(context);
4530 if (!context) return skip;
4531
locke-lunarg61870c22020-06-09 14:51:50 -06004532 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4533 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004534 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4535 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004536
4537 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4538 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4539 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004540 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004541 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004542}
4543
4544void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4545 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004546 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004547 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004548 auto *cb_access_context = GetAccessContext(commandBuffer);
4549 assert(cb_access_context);
4550 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4551 auto *context = cb_access_context->GetCurrentAccessContext();
4552 assert(context);
4553
locke-lunarg61870c22020-06-09 14:51:50 -06004554 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4555 cb_access_context->RecordDrawSubpassAttachment(tag);
4556 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004557
4558 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4559 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4560 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004561 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004562}
4563
4564bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4565 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004566 bool skip = false;
4567 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004568 const auto *cb_access_context = GetAccessContext(commandBuffer);
4569 assert(cb_access_context);
4570 if (!cb_access_context) return skip;
4571
4572 const auto *context = cb_access_context->GetCurrentAccessContext();
4573 assert(context);
4574 if (!context) return skip;
4575
locke-lunarg61870c22020-06-09 14:51:50 -06004576 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4577 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004578 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4579 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004580
4581 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4582 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4583 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004584 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004585 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004586}
4587
4588void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4589 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004590 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004591 auto *cb_access_context = GetAccessContext(commandBuffer);
4592 assert(cb_access_context);
4593 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4594 auto *context = cb_access_context->GetCurrentAccessContext();
4595 assert(context);
4596
locke-lunarg61870c22020-06-09 14:51:50 -06004597 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4598 cb_access_context->RecordDrawSubpassAttachment(tag);
4599 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004600
4601 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4602 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4603 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004604 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004605}
4606
4607bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4608 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4609 uint32_t stride, const char *function) const {
4610 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004611 const auto *cb_access_context = GetAccessContext(commandBuffer);
4612 assert(cb_access_context);
4613 if (!cb_access_context) return skip;
4614
4615 const auto *context = cb_access_context->GetCurrentAccessContext();
4616 assert(context);
4617 if (!context) return skip;
4618
locke-lunarg61870c22020-06-09 14:51:50 -06004619 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4620 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004621 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4622 maxDrawCount, stride, function);
4623 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004624
4625 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4626 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4627 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004628 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004629 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004630}
4631
4632bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4633 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4634 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004635 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4636 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004637}
4638
sfricke-samsung85584a72021-09-30 21:43:38 -07004639void SyncValidator::RecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4640 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4641 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004642 auto *cb_access_context = GetAccessContext(commandBuffer);
4643 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004644 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004645 auto *context = cb_access_context->GetCurrentAccessContext();
4646 assert(context);
4647
locke-lunarg61870c22020-06-09 14:51:50 -06004648 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4649 cb_access_context->RecordDrawSubpassAttachment(tag);
4650 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4651 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004652
4653 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4654 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4655 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004656 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004657}
4658
sfricke-samsung85584a72021-09-30 21:43:38 -07004659void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4660 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4661 uint32_t stride) {
4662 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4663 stride);
4664 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4665 CMD_DRAWINDIRECTCOUNT);
4666}
locke-lunarge1a67022020-04-29 00:15:36 -06004667bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4668 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4669 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004670 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4671 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004672}
4673
4674void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4675 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4676 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004677 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4678 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004679 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4680 CMD_DRAWINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004681}
4682
4683bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4684 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4685 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004686 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4687 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004688}
4689
4690void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4691 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4692 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004693 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4694 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004695 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4696 CMD_DRAWINDIRECTCOUNTAMD);
locke-lunargff255f92020-05-13 18:53:52 -06004697}
4698
4699bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4700 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4701 uint32_t stride, const char *function) const {
4702 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004703 const auto *cb_access_context = GetAccessContext(commandBuffer);
4704 assert(cb_access_context);
4705 if (!cb_access_context) return skip;
4706
4707 const auto *context = cb_access_context->GetCurrentAccessContext();
4708 assert(context);
4709 if (!context) return skip;
4710
locke-lunarg61870c22020-06-09 14:51:50 -06004711 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4712 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004713 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4714 offset, maxDrawCount, stride, function);
4715 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004716
4717 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4718 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4719 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004720 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004721 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004722}
4723
4724bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4725 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4726 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004727 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4728 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004729}
4730
sfricke-samsung85584a72021-09-30 21:43:38 -07004731void SyncValidator::RecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4732 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4733 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004734 auto *cb_access_context = GetAccessContext(commandBuffer);
4735 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004736 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004737 auto *context = cb_access_context->GetCurrentAccessContext();
4738 assert(context);
4739
locke-lunarg61870c22020-06-09 14:51:50 -06004740 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4741 cb_access_context->RecordDrawSubpassAttachment(tag);
4742 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4743 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004744
4745 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4746 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004747 // We will update the index and vertex buffer in SubmitQueue in the future.
4748 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004749}
4750
sfricke-samsung85584a72021-09-30 21:43:38 -07004751void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4752 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4753 uint32_t maxDrawCount, uint32_t stride) {
4754 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4755 maxDrawCount, stride);
4756 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4757 CMD_DRAWINDEXEDINDIRECTCOUNT);
4758}
4759
locke-lunarge1a67022020-04-29 00:15:36 -06004760bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4761 VkDeviceSize offset, VkBuffer countBuffer,
4762 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4763 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004764 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4765 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004766}
4767
4768void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4769 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4770 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004771 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4772 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004773 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4774 CMD_DRAWINDEXEDINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004775}
4776
4777bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4778 VkDeviceSize offset, VkBuffer countBuffer,
4779 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4780 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004781 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4782 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004783}
4784
4785void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4786 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4787 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004788 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4789 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004790 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4791 CMD_DRAWINDEXEDINDIRECTCOUNTAMD);
locke-lunarge1a67022020-04-29 00:15:36 -06004792}
4793
4794bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4795 const VkClearColorValue *pColor, uint32_t rangeCount,
4796 const VkImageSubresourceRange *pRanges) const {
4797 bool skip = false;
4798 const auto *cb_access_context = GetAccessContext(commandBuffer);
4799 assert(cb_access_context);
4800 if (!cb_access_context) return skip;
4801
4802 const auto *context = cb_access_context->GetCurrentAccessContext();
4803 assert(context);
4804 if (!context) return skip;
4805
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004806 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004807
4808 for (uint32_t index = 0; index < rangeCount; index++) {
4809 const auto &range = pRanges[index];
4810 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004811 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004812 if (hazard.hazard) {
4813 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004814 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004815 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004816 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004817 }
4818 }
4819 }
4820 return skip;
4821}
4822
4823void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4824 const VkClearColorValue *pColor, uint32_t rangeCount,
4825 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004826 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004827 auto *cb_access_context = GetAccessContext(commandBuffer);
4828 assert(cb_access_context);
4829 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4830 auto *context = cb_access_context->GetCurrentAccessContext();
4831 assert(context);
4832
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004833 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004834
4835 for (uint32_t index = 0; index < rangeCount; index++) {
4836 const auto &range = pRanges[index];
4837 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004838 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004839 }
4840 }
4841}
4842
4843bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4844 VkImageLayout imageLayout,
4845 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4846 const VkImageSubresourceRange *pRanges) const {
4847 bool skip = false;
4848 const auto *cb_access_context = GetAccessContext(commandBuffer);
4849 assert(cb_access_context);
4850 if (!cb_access_context) return skip;
4851
4852 const auto *context = cb_access_context->GetCurrentAccessContext();
4853 assert(context);
4854 if (!context) return skip;
4855
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004856 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004857
4858 for (uint32_t index = 0; index < rangeCount; index++) {
4859 const auto &range = pRanges[index];
4860 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004861 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004862 if (hazard.hazard) {
4863 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004864 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004865 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004866 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004867 }
4868 }
4869 }
4870 return skip;
4871}
4872
4873void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4874 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4875 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004876 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004877 auto *cb_access_context = GetAccessContext(commandBuffer);
4878 assert(cb_access_context);
4879 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4880 auto *context = cb_access_context->GetCurrentAccessContext();
4881 assert(context);
4882
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004883 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004884
4885 for (uint32_t index = 0; index < rangeCount; index++) {
4886 const auto &range = pRanges[index];
4887 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004888 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004889 }
4890 }
4891}
4892
4893bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4894 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4895 VkDeviceSize dstOffset, VkDeviceSize stride,
4896 VkQueryResultFlags flags) const {
4897 bool skip = false;
4898 const auto *cb_access_context = GetAccessContext(commandBuffer);
4899 assert(cb_access_context);
4900 if (!cb_access_context) return skip;
4901
4902 const auto *context = cb_access_context->GetCurrentAccessContext();
4903 assert(context);
4904 if (!context) return skip;
4905
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004906 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004907
4908 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004909 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004910 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004911 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004912 skip |=
4913 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4914 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004915 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004916 }
4917 }
locke-lunargff255f92020-05-13 18:53:52 -06004918
4919 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004920 return skip;
4921}
4922
4923void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4924 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4925 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004926 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4927 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004928 auto *cb_access_context = GetAccessContext(commandBuffer);
4929 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004930 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004931 auto *context = cb_access_context->GetCurrentAccessContext();
4932 assert(context);
4933
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004934 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004935
4936 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004937 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004938 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004939 }
locke-lunargff255f92020-05-13 18:53:52 -06004940
4941 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004942}
4943
4944bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4945 VkDeviceSize size, uint32_t data) const {
4946 bool skip = false;
4947 const auto *cb_access_context = GetAccessContext(commandBuffer);
4948 assert(cb_access_context);
4949 if (!cb_access_context) return skip;
4950
4951 const auto *context = cb_access_context->GetCurrentAccessContext();
4952 assert(context);
4953 if (!context) return skip;
4954
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004955 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004956
4957 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004958 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004959 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004960 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004961 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004962 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004963 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004964 }
4965 }
4966 return skip;
4967}
4968
4969void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4970 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004971 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004972 auto *cb_access_context = GetAccessContext(commandBuffer);
4973 assert(cb_access_context);
4974 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4975 auto *context = cb_access_context->GetCurrentAccessContext();
4976 assert(context);
4977
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004978 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004979
4980 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004981 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004982 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004983 }
4984}
4985
4986bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4987 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4988 const VkImageResolve *pRegions) const {
4989 bool skip = false;
4990 const auto *cb_access_context = GetAccessContext(commandBuffer);
4991 assert(cb_access_context);
4992 if (!cb_access_context) return skip;
4993
4994 const auto *context = cb_access_context->GetCurrentAccessContext();
4995 assert(context);
4996 if (!context) return skip;
4997
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004998 auto src_image = Get<IMAGE_STATE>(srcImage);
4999 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06005000
5001 for (uint32_t region = 0; region < regionCount; region++) {
5002 const auto &resolve_region = pRegions[region];
5003 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005004 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06005005 resolve_region.srcOffset, resolve_region.extent);
5006 if (hazard.hazard) {
5007 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005008 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005009 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07005010 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005011 }
5012 }
5013
5014 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005015 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06005016 resolve_region.dstOffset, resolve_region.extent);
5017 if (hazard.hazard) {
5018 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005019 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005020 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07005021 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005022 }
5023 if (skip) break;
5024 }
5025 }
5026
5027 return skip;
5028}
5029
5030void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5031 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5032 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005033 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
5034 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06005035 auto *cb_access_context = GetAccessContext(commandBuffer);
5036 assert(cb_access_context);
5037 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
5038 auto *context = cb_access_context->GetCurrentAccessContext();
5039 assert(context);
5040
Jeremy Gebben9f537102021-10-05 16:37:12 -06005041 auto src_image = Get<IMAGE_STATE>(srcImage);
5042 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06005043
5044 for (uint32_t region = 0; region < regionCount; region++) {
5045 const auto &resolve_region = pRegions[region];
5046 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005047 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005048 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005049 }
5050 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005051 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005052 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005053 }
5054 }
5055}
5056
Tony-LunarG562fc102021-11-12 13:58:35 -07005057bool SyncValidator::ValidateCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo,
5058 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04005059 bool skip = false;
5060 const auto *cb_access_context = GetAccessContext(commandBuffer);
5061 assert(cb_access_context);
5062 if (!cb_access_context) return skip;
5063
5064 const auto *context = cb_access_context->GetCurrentAccessContext();
5065 assert(context);
5066 if (!context) return skip;
5067
Tony-LunarG562fc102021-11-12 13:58:35 -07005068 const char *func_name = CommandTypeString(cmd_type);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005069 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5070 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005071
5072 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5073 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5074 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005075 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04005076 resolve_region.srcOffset, resolve_region.extent);
5077 if (hazard.hazard) {
5078 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
Tony-LunarG562fc102021-11-12 13:58:35 -07005079 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04005080 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07005081 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04005082 }
5083 }
5084
5085 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005086 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04005087 resolve_region.dstOffset, resolve_region.extent);
5088 if (hazard.hazard) {
5089 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
Tony-LunarG562fc102021-11-12 13:58:35 -07005090 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04005091 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07005092 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04005093 }
5094 if (skip) break;
5095 }
5096 }
5097
5098 return skip;
5099}
5100
Tony-LunarG562fc102021-11-12 13:58:35 -07005101bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5102 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
5103 return ValidateCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2KHR);
5104}
5105
5106bool SyncValidator::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer,
5107 const VkResolveImageInfo2 *pResolveImageInfo) const {
5108 return ValidateCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2);
5109}
5110
5111void SyncValidator::RecordCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo,
5112 CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04005113 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5114 auto *cb_access_context = GetAccessContext(commandBuffer);
5115 assert(cb_access_context);
Tony-LunarG562fc102021-11-12 13:58:35 -07005116 const auto tag = cb_access_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04005117 auto *context = cb_access_context->GetCurrentAccessContext();
5118 assert(context);
5119
Jeremy Gebben9f537102021-10-05 16:37:12 -06005120 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5121 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005122
5123 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5124 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5125 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005126 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005127 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005128 }
5129 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005130 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005131 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005132 }
5133 }
5134}
5135
Tony-LunarG562fc102021-11-12 13:58:35 -07005136void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5137 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5138 RecordCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2KHR);
5139}
5140
5141void SyncValidator::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2 *pResolveImageInfo) {
5142 RecordCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2);
5143}
5144
locke-lunarge1a67022020-04-29 00:15:36 -06005145bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5146 VkDeviceSize dataSize, const void *pData) const {
5147 bool skip = false;
5148 const auto *cb_access_context = GetAccessContext(commandBuffer);
5149 assert(cb_access_context);
5150 if (!cb_access_context) return skip;
5151
5152 const auto *context = cb_access_context->GetCurrentAccessContext();
5153 assert(context);
5154 if (!context) return skip;
5155
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005156 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005157
5158 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005159 // VK_WHOLE_SIZE not allowed
5160 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005161 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005162 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005163 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005164 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005165 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005166 }
5167 }
5168 return skip;
5169}
5170
5171void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5172 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005173 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005174 auto *cb_access_context = GetAccessContext(commandBuffer);
5175 assert(cb_access_context);
5176 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5177 auto *context = cb_access_context->GetCurrentAccessContext();
5178 assert(context);
5179
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005180 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005181
5182 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005183 // VK_WHOLE_SIZE not allowed
5184 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005185 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005186 }
5187}
locke-lunargff255f92020-05-13 18:53:52 -06005188
5189bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5190 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5191 bool skip = false;
5192 const auto *cb_access_context = GetAccessContext(commandBuffer);
5193 assert(cb_access_context);
5194 if (!cb_access_context) return skip;
5195
5196 const auto *context = cb_access_context->GetCurrentAccessContext();
5197 assert(context);
5198 if (!context) return skip;
5199
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005200 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005201
5202 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005203 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005204 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06005205 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005206 skip |=
5207 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5208 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005209 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005210 }
5211 }
5212 return skip;
5213}
5214
5215void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5216 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005217 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005218 auto *cb_access_context = GetAccessContext(commandBuffer);
5219 assert(cb_access_context);
5220 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5221 auto *context = cb_access_context->GetCurrentAccessContext();
5222 assert(context);
5223
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005224 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005225
5226 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005227 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005228 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06005229 }
5230}
John Zulauf49beb112020-11-04 16:06:31 -07005231
5232bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5233 bool skip = false;
5234 const auto *cb_context = GetAccessContext(commandBuffer);
5235 assert(cb_context);
5236 if (!cb_context) return skip;
5237
John Zulauf36ef9282021-02-02 11:47:24 -07005238 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005239 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005240}
5241
5242void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5243 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5244 auto *cb_context = GetAccessContext(commandBuffer);
5245 assert(cb_context);
5246 if (!cb_context) return;
John Zulauf1bf30522021-09-03 15:39:06 -06005247 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005248}
5249
John Zulauf4edde622021-02-15 08:54:50 -07005250bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5251 const VkDependencyInfoKHR *pDependencyInfo) const {
5252 bool skip = false;
5253 const auto *cb_context = GetAccessContext(commandBuffer);
5254 assert(cb_context);
5255 if (!cb_context || !pDependencyInfo) return skip;
5256
5257 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5258 return set_event_op.Validate(*cb_context);
5259}
5260
Tony-LunarGc43525f2021-11-15 16:12:38 -07005261bool SyncValidator::PreCallValidateCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5262 const VkDependencyInfo *pDependencyInfo) const {
5263 bool skip = false;
5264 const auto *cb_context = GetAccessContext(commandBuffer);
5265 assert(cb_context);
5266 if (!cb_context || !pDependencyInfo) return skip;
5267
5268 SyncOpSetEvent set_event_op(CMD_SETEVENT2, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5269 return set_event_op.Validate(*cb_context);
5270}
5271
John Zulauf4edde622021-02-15 08:54:50 -07005272void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5273 const VkDependencyInfoKHR *pDependencyInfo) {
5274 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
5275 auto *cb_context = GetAccessContext(commandBuffer);
5276 assert(cb_context);
5277 if (!cb_context || !pDependencyInfo) return;
5278
John Zulauf1bf30522021-09-03 15:39:06 -06005279 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
John Zulauf4edde622021-02-15 08:54:50 -07005280}
5281
Tony-LunarGc43525f2021-11-15 16:12:38 -07005282void SyncValidator::PostCallRecordCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5283 const VkDependencyInfo *pDependencyInfo) {
5284 StateTracker::PostCallRecordCmdSetEvent2(commandBuffer, event, pDependencyInfo);
5285 auto *cb_context = GetAccessContext(commandBuffer);
5286 assert(cb_context);
5287 if (!cb_context || !pDependencyInfo) return;
5288
5289 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5290}
5291
John Zulauf49beb112020-11-04 16:06:31 -07005292bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5293 VkPipelineStageFlags stageMask) const {
5294 bool skip = false;
5295 const auto *cb_context = GetAccessContext(commandBuffer);
5296 assert(cb_context);
5297 if (!cb_context) return skip;
5298
John Zulauf36ef9282021-02-02 11:47:24 -07005299 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005300 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005301}
5302
5303void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5304 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5305 auto *cb_context = GetAccessContext(commandBuffer);
5306 assert(cb_context);
5307 if (!cb_context) return;
5308
John Zulauf1bf30522021-09-03 15:39:06 -06005309 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005310}
5311
John Zulauf4edde622021-02-15 08:54:50 -07005312bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5313 VkPipelineStageFlags2KHR stageMask) const {
5314 bool skip = false;
5315 const auto *cb_context = GetAccessContext(commandBuffer);
5316 assert(cb_context);
5317 if (!cb_context) return skip;
5318
5319 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
5320 return reset_event_op.Validate(*cb_context);
5321}
5322
Tony-LunarGa2662db2021-11-16 07:26:24 -07005323bool SyncValidator::PreCallValidateCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5324 VkPipelineStageFlags2 stageMask) const {
5325 bool skip = false;
5326 const auto *cb_context = GetAccessContext(commandBuffer);
5327 assert(cb_context);
5328 if (!cb_context) return skip;
5329
5330 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2, *this, cb_context->GetQueueFlags(), event, stageMask);
5331 return reset_event_op.Validate(*cb_context);
5332}
5333
John Zulauf4edde622021-02-15 08:54:50 -07005334void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5335 VkPipelineStageFlags2KHR stageMask) {
5336 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
5337 auto *cb_context = GetAccessContext(commandBuffer);
5338 assert(cb_context);
5339 if (!cb_context) return;
5340
John Zulauf1bf30522021-09-03 15:39:06 -06005341 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf4edde622021-02-15 08:54:50 -07005342}
5343
Tony-LunarGa2662db2021-11-16 07:26:24 -07005344void SyncValidator::PostCallRecordCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags2 stageMask) {
5345 StateTracker::PostCallRecordCmdResetEvent2(commandBuffer, event, stageMask);
5346 auto *cb_context = GetAccessContext(commandBuffer);
5347 assert(cb_context);
5348 if (!cb_context) return;
5349
5350 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2, *this, cb_context->GetQueueFlags(), event, stageMask);
5351}
5352
John Zulauf49beb112020-11-04 16:06:31 -07005353bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5354 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5355 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5356 uint32_t bufferMemoryBarrierCount,
5357 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5358 uint32_t imageMemoryBarrierCount,
5359 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5360 bool skip = false;
5361 const auto *cb_context = GetAccessContext(commandBuffer);
5362 assert(cb_context);
5363 if (!cb_context) return skip;
5364
John Zulauf36ef9282021-02-02 11:47:24 -07005365 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
5366 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
5367 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07005368 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005369}
5370
5371void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5372 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5373 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5374 uint32_t bufferMemoryBarrierCount,
5375 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5376 uint32_t imageMemoryBarrierCount,
5377 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5378 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5379 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5380 imageMemoryBarrierCount, pImageMemoryBarriers);
5381
5382 auto *cb_context = GetAccessContext(commandBuffer);
5383 assert(cb_context);
5384 if (!cb_context) return;
5385
John Zulauf1bf30522021-09-03 15:39:06 -06005386 cb_context->RecordSyncOp<SyncOpWaitEvents>(
John Zulauf610e28c2021-08-03 17:46:23 -06005387 CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
John Zulauf1bf30522021-09-03 15:39:06 -06005388 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf4a6105a2020-11-17 15:11:05 -07005389}
5390
John Zulauf4edde622021-02-15 08:54:50 -07005391bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5392 const VkDependencyInfoKHR *pDependencyInfos) const {
5393 bool skip = false;
5394 const auto *cb_context = GetAccessContext(commandBuffer);
5395 assert(cb_context);
5396 if (!cb_context) return skip;
5397
5398 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5399 skip |= wait_events_op.Validate(*cb_context);
5400 return skip;
5401}
5402
5403void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5404 const VkDependencyInfoKHR *pDependencyInfos) {
5405 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5406
5407 auto *cb_context = GetAccessContext(commandBuffer);
5408 assert(cb_context);
5409 if (!cb_context) return;
5410
John Zulauf1bf30522021-09-03 15:39:06 -06005411 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5412 pDependencyInfos);
John Zulauf4edde622021-02-15 08:54:50 -07005413}
5414
Tony-LunarG1364cf52021-11-17 16:10:11 -07005415bool SyncValidator::PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5416 const VkDependencyInfo *pDependencyInfos) const {
5417 bool skip = false;
5418 const auto *cb_context = GetAccessContext(commandBuffer);
5419 assert(cb_context);
5420 if (!cb_context) return skip;
5421
5422 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5423 skip |= wait_events_op.Validate(*cb_context);
5424 return skip;
5425}
5426
5427void SyncValidator::PostCallRecordCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5428 const VkDependencyInfo *pDependencyInfos) {
5429 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5430
5431 auto *cb_context = GetAccessContext(commandBuffer);
5432 assert(cb_context);
5433 if (!cb_context) return;
5434
5435 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5436 pDependencyInfos);
5437}
5438
John Zulauf4a6105a2020-11-17 15:11:05 -07005439void SyncEventState::ResetFirstScope() {
5440 for (const auto address_type : kAddressTypes) {
5441 first_scope[static_cast<size_t>(address_type)].clear();
5442 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005443 scope = SyncExecScope();
John Zulauf78b1f892021-09-20 15:02:09 -06005444 first_scope_set = false;
5445 first_scope_tag = 0;
John Zulauf4a6105a2020-11-17 15:11:05 -07005446}
5447
5448// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07005449SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005450 IgnoreReason reason = NotIgnored;
5451
Tony-LunarG1364cf52021-11-17 16:10:11 -07005452 if ((CMD_WAITEVENTS2KHR == cmd || CMD_WAITEVENTS2 == cmd) && (CMD_SETEVENT == last_command)) {
John Zulauf4edde622021-02-15 08:54:50 -07005453 reason = SetVsWait2;
5454 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
5455 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07005456 } else if (unsynchronized_set) {
5457 reason = SetRace;
John Zulauf78b1f892021-09-20 15:02:09 -06005458 } else if (first_scope_set) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005459 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005460 if (missing_bits) reason = MissingStageBits;
5461 }
5462
5463 return reason;
5464}
5465
Jeremy Gebben40a22942020-12-22 14:22:06 -07005466bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005467 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5468 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5469 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005470}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005471
John Zulauf36ef9282021-02-02 11:47:24 -07005472SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5473 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5474 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005475 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5476 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5477 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07005478 : SyncOpBase(cmd), barriers_(1) {
5479 auto &barrier_set = barriers_[0];
5480 barrier_set.dependency_flags = dependencyFlags;
5481 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
5482 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005483 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07005484 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
5485 pMemoryBarriers);
5486 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5487 bufferMemoryBarrierCount, pBufferMemoryBarriers);
5488 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5489 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005490}
5491
John Zulauf4edde622021-02-15 08:54:50 -07005492SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
5493 const VkDependencyInfoKHR *dep_infos)
5494 : SyncOpBase(cmd), barriers_(event_count) {
5495 for (uint32_t i = 0; i < event_count; i++) {
5496 const auto &dep_info = dep_infos[i];
5497 auto &barrier_set = barriers_[i];
5498 barrier_set.dependency_flags = dep_info.dependencyFlags;
5499 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5500 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5501 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5502 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5503 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5504 dep_info.pMemoryBarriers);
5505 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5506 dep_info.pBufferMemoryBarriers);
5507 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5508 dep_info.pImageMemoryBarriers);
5509 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005510}
5511
John Zulauf36ef9282021-02-02 11:47:24 -07005512SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005513 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5514 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5515 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5516 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5517 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005518 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005519 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5520
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005521SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5522 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005523 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005524
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005525bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5526 bool skip = false;
5527 const auto *context = cb_context.GetCurrentAccessContext();
5528 assert(context);
5529 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005530 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5531
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005532 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005533 const auto &barrier_set = barriers_[0];
5534 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5535 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5536 const auto *image_state = image_barrier.image.get();
5537 if (!image_state) continue;
5538 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5539 if (hazard.hazard) {
5540 // PHASE1 TODO -- add tag information to log msg when useful.
5541 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005542 const auto image_handle = image_state->image();
John Zulauf6fdf3d02021-03-05 16:50:47 -07005543 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5544 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5545 string_SyncHazard(hazard.hazard), image_barrier.index,
5546 sync_state.report_data->FormatHandle(image_handle).c_str(),
5547 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005548 }
5549 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005550 return skip;
5551}
5552
John Zulaufd5115702021-01-18 12:34:33 -07005553struct SyncOpPipelineBarrierFunctorFactory {
5554 using BarrierOpFunctor = PipelineBarrierOp;
5555 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5556 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5557 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5558 using BufferRange = ResourceAccessRange;
5559 using ImageRange = subresource_adapter::ImageRangeGenerator;
5560 using GlobalRange = ResourceAccessRange;
5561
5562 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5563 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5564 }
John Zulauf14940722021-04-12 15:19:02 -06005565 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005566 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5567 }
5568 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5569 return GlobalBarrierOpFunctor(barrier, false);
5570 }
5571
5572 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5573 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5574 const auto base_address = ResourceBaseAddress(buffer);
5575 return (range + base_address);
5576 }
John Zulauf110413c2021-03-20 05:38:38 -06005577 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005578 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005579
5580 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005581 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005582 return range_gen;
5583 }
5584 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5585};
5586
5587template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005588void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005589 AccessContext *context) {
5590 for (const auto &barrier : barriers) {
5591 const auto *state = barrier.GetState();
5592 if (state) {
5593 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5594 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5595 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5596 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5597 }
5598 }
5599}
5600
5601template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005602void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005603 AccessContext *access_context) {
5604 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5605 for (const auto &barrier : barriers) {
5606 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5607 }
5608 for (const auto address_type : kAddressTypes) {
5609 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5610 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5611 }
5612}
5613
John Zulauf8eda1562021-04-13 17:06:41 -06005614ResourceUsageTag SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005615 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf8eda1562021-04-13 17:06:41 -06005616 auto *events_context = cb_context->GetCurrentEventsContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005617 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf4fa68462021-04-26 21:04:22 -06005618 DoRecord(tag, access_context, events_context);
5619 return tag;
5620}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005621
John Zulauf4fa68462021-04-26 21:04:22 -06005622void SyncOpPipelineBarrier::DoRecord(const ResourceUsageTag tag, AccessContext *access_context,
5623 SyncEventsContext *events_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06005624 SyncOpPipelineBarrierFunctorFactory factory;
John Zulauf4edde622021-02-15 08:54:50 -07005625 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5626 assert(barriers_.size() == 1);
5627 const auto &barrier_set = barriers_[0];
5628 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5629 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5630 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulauf4edde622021-02-15 08:54:50 -07005631 if (barrier_set.single_exec_scope) {
John Zulauf8eda1562021-04-13 17:06:41 -06005632 events_context->ApplyBarrier(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005633 } else {
5634 for (const auto &barrier : barrier_set.memory_barriers) {
John Zulauf8eda1562021-04-13 17:06:41 -06005635 events_context->ApplyBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005636 }
5637 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005638}
5639
John Zulauf8eda1562021-04-13 17:06:41 -06005640bool SyncOpPipelineBarrier::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06005641 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf4fa68462021-04-26 21:04:22 -06005642 // No Validation for replay, as the layout transition accesses are checked directly, and the src*Mask ordering is captured
5643 // with first access information.
John Zulauf8eda1562021-04-13 17:06:41 -06005644 return false;
5645}
5646
John Zulauf4edde622021-02-15 08:54:50 -07005647void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5648 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5649 const VkMemoryBarrier *barriers) {
5650 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005651 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005652 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005653 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005654 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005655 }
5656 if (0 == memory_barrier_count) {
5657 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005658 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005659 }
John Zulauf4edde622021-02-15 08:54:50 -07005660 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005661}
5662
John Zulauf4edde622021-02-15 08:54:50 -07005663void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5664 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5665 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5666 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005667 for (uint32_t index = 0; index < barrier_count; index++) {
5668 const auto &barrier = barriers[index];
Jeremy Gebben9f537102021-10-05 16:37:12 -06005669 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005670 if (buffer) {
5671 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5672 const auto range = MakeRange(barrier.offset, barrier_size);
5673 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005674 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005675 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005676 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005677 }
5678 }
5679}
5680
John Zulauf4edde622021-02-15 08:54:50 -07005681void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005682 uint32_t memory_barrier_count, const VkMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005683 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005684 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005685 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005686 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5687 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5688 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005689 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005690 }
John Zulauf4edde622021-02-15 08:54:50 -07005691 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005692}
5693
John Zulauf4edde622021-02-15 08:54:50 -07005694void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5695 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005696 const VkBufferMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005697 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005698 for (uint32_t index = 0; index < barrier_count; index++) {
5699 const auto &barrier = barriers[index];
5700 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5701 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9f537102021-10-05 16:37:12 -06005702 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005703 if (buffer) {
5704 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5705 const auto range = MakeRange(barrier.offset, barrier_size);
5706 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005707 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005708 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005709 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005710 }
5711 }
5712}
5713
John Zulauf4edde622021-02-15 08:54:50 -07005714void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5715 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5716 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5717 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005718 for (uint32_t index = 0; index < barrier_count; index++) {
5719 const auto &barrier = barriers[index];
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005720 auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005721 if (image) {
5722 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5723 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005724 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005725 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005726 image_memory_barriers.emplace_back();
5727 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005728 }
5729 }
5730}
John Zulaufd5115702021-01-18 12:34:33 -07005731
John Zulauf4edde622021-02-15 08:54:50 -07005732void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5733 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005734 const VkImageMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005735 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005736 for (uint32_t index = 0; index < barrier_count; index++) {
5737 const auto &barrier = barriers[index];
5738 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5739 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005740 auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005741 if (image) {
5742 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5743 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005744 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005745 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005746 image_memory_barriers.emplace_back();
5747 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005748 }
5749 }
5750}
5751
John Zulauf36ef9282021-02-02 11:47:24 -07005752SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005753 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5754 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5755 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5756 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005757 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005758 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5759 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005760 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005761}
5762
John Zulauf4edde622021-02-15 08:54:50 -07005763SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5764 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5765 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5766 MakeEventsList(sync_state, eventCount, pEvents);
5767 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5768}
5769
John Zulauf610e28c2021-08-03 17:46:23 -06005770const char *const SyncOpWaitEvents::kIgnored = "Wait operation is ignored for this event.";
5771
John Zulaufd5115702021-01-18 12:34:33 -07005772bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005773 bool skip = false;
5774 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005775 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005776
John Zulauf610e28c2021-08-03 17:46:23 -06005777 // This is only interesting at record and not replay (Execute/Submit) time.
John Zulauf4edde622021-02-15 08:54:50 -07005778 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5779 const auto &barrier_set = barriers_[barrier_set_index];
5780 if (barrier_set.single_exec_scope) {
5781 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5782 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5783 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5784 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5785 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5786 } else {
5787 const auto &barriers = barrier_set.memory_barriers;
5788 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5789 const auto &barrier = barriers[barrier_index];
5790 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5791 const std::string vuid =
5792 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5793 skip =
5794 sync_state.LogInfo(command_buffer_handle, vuid,
5795 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5796 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5797 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5798 }
5799 }
5800 }
5801 }
John Zulaufd5115702021-01-18 12:34:33 -07005802 }
5803
John Zulauf610e28c2021-08-03 17:46:23 -06005804 // The rest is common to record time and replay time.
5805 skip |= DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
5806 return skip;
5807}
5808
5809bool SyncOpWaitEvents::DoValidate(const CommandBufferAccessContext &cb_context, const ResourceUsageTag base_tag) const {
5810 bool skip = false;
5811 const auto &sync_state = cb_context.GetSyncState();
5812
Jeremy Gebben40a22942020-12-22 14:22:06 -07005813 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005814 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005815 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005816 const auto *events_context = cb_context.GetCurrentEventsContext();
5817 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005818 size_t barrier_set_index = 0;
5819 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
John Zulauf78394fc2021-07-12 15:41:40 -06005820 for (const auto &event : events_) {
5821 const auto *sync_event = events_context->Get(event.get());
5822 const auto &barrier_set = barriers_[barrier_set_index];
5823 if (!sync_event) {
5824 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5825 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5826 // new validation error... wait without previously submitted set event...
5827 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 -07005828 barrier_set_index += barrier_set_incr;
John Zulauf78394fc2021-07-12 15:41:40 -06005829 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005830 }
John Zulauf610e28c2021-08-03 17:46:23 -06005831
5832 // For replay calls, don't revalidate "same command buffer" events
5833 if (sync_event->last_command_tag > base_tag) continue;
5834
John Zulauf78394fc2021-07-12 15:41:40 -06005835 const auto event_handle = sync_event->event->event();
5836 // TODO add "destroyed" checks
5837
John Zulauf78b1f892021-09-20 15:02:09 -06005838 if (sync_event->first_scope_set) {
5839 // Only accumulate barrier and event stages if there is a pending set in the current context
5840 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5841 event_stage_masks |= sync_event->scope.mask_param;
5842 }
5843
John Zulauf78394fc2021-07-12 15:41:40 -06005844 const auto &src_exec_scope = barrier_set.src_exec_scope;
John Zulauf78b1f892021-09-20 15:02:09 -06005845
John Zulauf78394fc2021-07-12 15:41:40 -06005846 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5847 if (ignore_reason) {
5848 switch (ignore_reason) {
5849 case SyncEventState::ResetWaitRace:
5850 case SyncEventState::Reset2WaitRace: {
5851 // Four permuations of Reset and Wait calls...
5852 const char *vuid =
5853 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5854 if (ignore_reason == SyncEventState::Reset2WaitRace) {
Tony-LunarG279601c2021-11-16 10:50:51 -07005855 vuid = (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2-event-03831"
5856 : "VUID-vkCmdResetEvent2-event-03832";
John Zulauf78394fc2021-07-12 15:41:40 -06005857 }
5858 const char *const message =
5859 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5860 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5861 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
John Zulauf610e28c2021-08-03 17:46:23 -06005862 CommandTypeString(sync_event->last_command), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005863 break;
5864 }
5865 case SyncEventState::SetRace: {
5866 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5867 // this event
5868 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5869 const char *const message =
5870 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5871 const char *const reason = "First synchronization scope is undefined.";
5872 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5873 sync_state.report_data->FormatHandle(event_handle).c_str(),
John Zulauf610e28c2021-08-03 17:46:23 -06005874 CommandTypeString(sync_event->last_command), reason, kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005875 break;
5876 }
5877 case SyncEventState::MissingStageBits: {
5878 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5879 // Issue error message that event waited for is not in wait events scope
5880 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5881 const char *const message = "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5882 ". Bits missing from srcStageMask %s. %s";
5883 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5884 sync_state.report_data->FormatHandle(event_handle).c_str(),
5885 sync_event->scope.mask_param, src_exec_scope.mask_param,
John Zulauf610e28c2021-08-03 17:46:23 -06005886 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005887 break;
5888 }
5889 case SyncEventState::SetVsWait2: {
Tony-LunarG279601c2021-11-16 10:50:51 -07005890 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2-pEvents-03837",
John Zulauf78394fc2021-07-12 15:41:40 -06005891 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5892 sync_state.report_data->FormatHandle(event_handle).c_str(),
5893 CommandTypeString(sync_event->last_command));
5894 break;
5895 }
5896 default:
5897 assert(ignore_reason == SyncEventState::NotIgnored);
5898 }
5899 } else if (barrier_set.image_memory_barriers.size()) {
5900 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5901 const auto *context = cb_context.GetCurrentAccessContext();
5902 assert(context);
5903 for (const auto &image_memory_barrier : image_memory_barriers) {
5904 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5905 const auto *image_state = image_memory_barrier.image.get();
5906 if (!image_state) continue;
5907 const auto &subresource_range = image_memory_barrier.range;
5908 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5909 const auto hazard =
5910 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5911 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5912 if (hazard.hazard) {
5913 skip |= sync_state.LogError(image_state->image(), string_SyncHazardVUID(hazard.hazard),
5914 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5915 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
5916 sync_state.report_data->FormatHandle(image_state->image()).c_str(),
5917 cb_context.FormatUsage(hazard).c_str());
5918 break;
5919 }
5920 }
5921 }
5922 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5923 // 03839
5924 barrier_set_index += barrier_set_incr;
5925 }
John Zulaufd5115702021-01-18 12:34:33 -07005926
5927 // 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 -07005928 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 -07005929 if (extra_stage_bits) {
5930 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005931 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5932 const char *const vuid =
Tony-LunarG279601c2021-11-16 10:50:51 -07005933 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005934 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005935 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulauf610e28c2021-08-03 17:46:23 -06005936 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005937 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005938 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005939 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005940 " vkCmdSetEvent may be in previously submitted command buffer.");
5941 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005942 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005943 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005944 }
5945 }
5946 return skip;
5947}
5948
5949struct SyncOpWaitEventsFunctorFactory {
5950 using BarrierOpFunctor = WaitEventBarrierOp;
5951 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5952 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5953 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5954 using BufferRange = EventSimpleRangeGenerator;
5955 using ImageRange = EventImageRangeGenerator;
5956 using GlobalRange = EventSimpleRangeGenerator;
5957
5958 // Need to restrict to only valid exec and access scope for this event
5959 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5960 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005961 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005962 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5963 return barrier;
5964 }
5965 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5966 auto barrier = RestrictToEvent(barrier_arg);
5967 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5968 }
John Zulauf14940722021-04-12 15:19:02 -06005969 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005970 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5971 }
5972 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5973 auto barrier = RestrictToEvent(barrier_arg);
5974 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5975 }
5976
5977 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5978 const AccessAddressType address_type = GetAccessAddressType(buffer);
5979 const auto base_address = ResourceBaseAddress(buffer);
5980 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5981 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5982 return filtered_range_gen;
5983 }
John Zulauf110413c2021-03-20 05:38:38 -06005984 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07005985 if (!SimpleBinding(image)) return ImageRange();
5986 const auto address_type = GetAccessAddressType(image);
5987 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005988 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005989 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5990
5991 return filtered_range_gen;
5992 }
5993 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5994 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5995 }
5996 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5997 SyncEventState *sync_event;
5998};
5999
John Zulauf8eda1562021-04-13 17:06:41 -06006000ResourceUsageTag SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006001 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07006002 auto *access_context = cb_context->GetCurrentAccessContext();
6003 assert(access_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006004 if (!access_context) return tag;
John Zulauf669dfd52021-01-27 17:15:28 -07006005 auto *events_context = cb_context->GetCurrentEventsContext();
6006 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006007 if (!events_context) return tag;
John Zulaufd5115702021-01-18 12:34:33 -07006008
John Zulauf610e28c2021-08-03 17:46:23 -06006009 DoRecord(tag, access_context, events_context);
6010 return tag;
6011}
6012
6013void SyncOpWaitEvents::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07006014 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
6015 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
6016 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
6017 access_context->ResolvePreviousAccesses();
6018
John Zulauf4edde622021-02-15 08:54:50 -07006019 size_t barrier_set_index = 0;
6020 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
6021 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07006022 for (auto &event_shared : events_) {
6023 if (!event_shared.get()) continue;
6024 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07006025
John Zulauf4edde622021-02-15 08:54:50 -07006026 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006027 sync_event->last_command_tag = tag;
John Zulaufd5115702021-01-18 12:34:33 -07006028
John Zulauf4edde622021-02-15 08:54:50 -07006029 const auto &barrier_set = barriers_[barrier_set_index];
6030 const auto &dst = barrier_set.dst_exec_scope;
6031 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07006032 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
6033 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
6034 // of the barriers is maintained.
6035 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07006036 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
6037 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
6038 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07006039
6040 // Apply the global barrier to the event itself (for race condition tracking)
6041 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
6042 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6043 sync_event->barriers |= dst.exec_scope;
6044 } else {
6045 // We ignored this wait, so we don't have any effective synchronization barriers for it.
6046 sync_event->barriers = 0U;
6047 }
John Zulauf4edde622021-02-15 08:54:50 -07006048 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07006049 }
6050
6051 // Apply the pending barriers
6052 ResolvePendingBarrierFunctor apply_pending_action(tag);
6053 access_context->ApplyToContext(apply_pending_action);
6054}
6055
John Zulauf8eda1562021-04-13 17:06:41 -06006056bool SyncOpWaitEvents::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006057 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
6058 return DoValidate(*active_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006059}
6060
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006061bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6062 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
6063 bool skip = false;
6064 const auto *cb_access_context = GetAccessContext(commandBuffer);
6065 assert(cb_access_context);
6066 if (!cb_access_context) return skip;
6067
6068 const auto *context = cb_access_context->GetCurrentAccessContext();
6069 assert(context);
6070 if (!context) return skip;
6071
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006072 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006073
6074 if (dst_buffer) {
6075 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6076 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
6077 if (hazard.hazard) {
6078 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
6079 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
6080 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
John Zulauf14940722021-04-12 15:19:02 -06006081 cb_access_context->FormatUsage(hazard).c_str());
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006082 }
6083 }
6084 return skip;
6085}
6086
John Zulauf669dfd52021-01-27 17:15:28 -07006087void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07006088 events_.reserve(event_count);
6089 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006090 events_.emplace_back(sync_state.Get<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07006091 }
6092}
John Zulauf6ce24372021-01-30 05:56:25 -07006093
John Zulauf36ef9282021-02-02 11:47:24 -07006094SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07006095 VkPipelineStageFlags2KHR stageMask)
Jeremy Gebben9f537102021-10-05 16:37:12 -06006096 : SyncOpBase(cmd), event_(sync_state.Get<EVENT_STATE>(event)), exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006097
John Zulauf1bf30522021-09-03 15:39:06 -06006098bool SyncOpResetEvent::Validate(const CommandBufferAccessContext& cb_context) const {
6099 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6100}
6101
6102bool SyncOpResetEvent::DoValidate(const CommandBufferAccessContext & cb_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006103 auto *events_context = cb_context.GetCurrentEventsContext();
6104 assert(events_context);
6105 bool skip = false;
6106 if (!events_context) return skip;
6107
6108 const auto &sync_state = cb_context.GetSyncState();
6109 const auto *sync_event = events_context->Get(event_);
6110 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6111
John Zulauf1bf30522021-09-03 15:39:06 -06006112 if (sync_event->last_command_tag > base_tag) return skip; // if we validated this in recording of the secondary, don't repeat
6113
John Zulauf6ce24372021-01-30 05:56:25 -07006114 const char *const set_wait =
6115 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6116 "hazards.";
6117 const char *message = set_wait; // Only one message this call.
6118 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
6119 const char *vuid = nullptr;
6120 switch (sync_event->last_command) {
6121 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006122 case CMD_SETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006123 case CMD_SETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006124 // Needs a barrier between set and reset
6125 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
6126 break;
John Zulauf4edde622021-02-15 08:54:50 -07006127 case CMD_WAITEVENTS:
Tony-LunarG1364cf52021-11-17 16:10:11 -07006128 case CMD_WAITEVENTS2:
John Zulauf4edde622021-02-15 08:54:50 -07006129 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07006130 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
6131 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
6132 break;
6133 }
6134 default:
6135 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07006136 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
6137 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07006138 break;
6139 }
6140 if (vuid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006141 skip |= sync_state.LogError(event_->event(), vuid, message, CmdName(),
6142 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006143 CommandTypeString(sync_event->last_command));
6144 }
6145 }
6146 return skip;
6147}
6148
John Zulauf8eda1562021-04-13 17:06:41 -06006149ResourceUsageTag SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
6150 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006151 auto *events_context = cb_context->GetCurrentEventsContext();
6152 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006153 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006154
6155 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06006156 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006157
6158 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07006159 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006160 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006161 sync_event->unsynchronized_set = CMD_NONE;
6162 sync_event->ResetFirstScope();
6163 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06006164
6165 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006166}
6167
John Zulauf8eda1562021-04-13 17:06:41 -06006168bool SyncOpResetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006169 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf1bf30522021-09-03 15:39:06 -06006170 return DoValidate(*active_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006171}
6172
John Zulauf4fa68462021-04-26 21:04:22 -06006173void SyncOpResetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006174
John Zulauf36ef9282021-02-02 11:47:24 -07006175SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07006176 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07006177 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006178 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006179 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
6180 dep_info_() {}
6181
6182SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
6183 const VkDependencyInfoKHR &dep_info)
6184 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006185 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006186 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
Tony-LunarG273f32f2021-09-28 08:56:30 -06006187 dep_info_(new safe_VkDependencyInfo(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006188
6189bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf610e28c2021-08-03 17:46:23 -06006190 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6191}
6192bool SyncOpSetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6193 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
6194 assert(active_context);
6195 return DoValidate(*active_context, base_tag);
6196}
6197
6198bool SyncOpSetEvent::DoValidate(const CommandBufferAccessContext &cb_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006199 bool skip = false;
6200
6201 const auto &sync_state = cb_context.GetSyncState();
6202 auto *events_context = cb_context.GetCurrentEventsContext();
6203 assert(events_context);
6204 if (!events_context) return skip;
6205
6206 const auto *sync_event = events_context->Get(event_);
6207 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6208
John Zulauf610e28c2021-08-03 17:46:23 -06006209 if (sync_event->last_command_tag >= base_tag) return skip; // for replay we don't want to revalidate internal "last commmand"
6210
John Zulauf6ce24372021-01-30 05:56:25 -07006211 const char *const reset_set =
6212 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6213 "hazards.";
6214 const char *const wait =
6215 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
6216
6217 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07006218 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07006219 const char *message = nullptr;
6220 switch (sync_event->last_command) {
6221 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006222 case CMD_RESETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006223 case CMD_RESETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006224 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07006225 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07006226 message = reset_set;
6227 break;
6228 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006229 case CMD_SETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006230 case CMD_SETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006231 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07006232 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07006233 message = reset_set;
6234 break;
6235 case CMD_WAITEVENTS:
Tony-LunarG1364cf52021-11-17 16:10:11 -07006236 case CMD_WAITEVENTS2:
John Zulauf4edde622021-02-15 08:54:50 -07006237 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006238 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07006239 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07006240 message = wait;
6241 break;
6242 default:
6243 // The only other valid last command that wasn't one.
6244 assert(sync_event->last_command == CMD_NONE);
6245 break;
6246 }
John Zulauf4edde622021-02-15 08:54:50 -07006247 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07006248 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07006249 std::string vuid("SYNC-");
6250 vuid.append(CmdName()).append(vuid_stem);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006251 skip |= sync_state.LogError(event_->event(), vuid.c_str(), message, CmdName(),
6252 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006253 CommandTypeString(sync_event->last_command));
6254 }
6255 }
6256
6257 return skip;
6258}
6259
John Zulauf8eda1562021-04-13 17:06:41 -06006260ResourceUsageTag SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006261 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006262 auto *events_context = cb_context->GetCurrentEventsContext();
6263 auto *access_context = cb_context->GetCurrentAccessContext();
6264 assert(events_context);
John Zulauf610e28c2021-08-03 17:46:23 -06006265 if (access_context && events_context) {
6266 DoRecord(tag, access_context, events_context);
6267 }
6268 return tag;
6269}
John Zulauf6ce24372021-01-30 05:56:25 -07006270
John Zulauf610e28c2021-08-03 17:46:23 -06006271void SyncOpSetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006272 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf610e28c2021-08-03 17:46:23 -06006273 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006274
6275 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
6276 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
6277 // any issues caused by naive scope setting here.
6278
6279 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
6280 // Given:
6281 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
6282 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
6283
6284 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
6285 sync_event->unsynchronized_set = sync_event->last_command;
6286 sync_event->ResetFirstScope();
John Zulauf78b1f892021-09-20 15:02:09 -06006287 } else if (!sync_event->first_scope_set) {
John Zulauf6ce24372021-01-30 05:56:25 -07006288 // We only set the scope if there isn't one
6289 sync_event->scope = src_exec_scope_;
6290
6291 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
6292 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
6293 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
6294 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
6295 }
6296 };
6297 access_context->ForAll(set_scope);
6298 sync_event->unsynchronized_set = CMD_NONE;
John Zulauf78b1f892021-09-20 15:02:09 -06006299 sync_event->first_scope_set = true;
John Zulauf6ce24372021-01-30 05:56:25 -07006300 sync_event->first_scope_tag = tag;
6301 }
John Zulauf4edde622021-02-15 08:54:50 -07006302 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
6303 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006304 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006305 sync_event->barriers = 0U;
6306}
John Zulauf64ffe552021-02-06 10:25:07 -07006307
6308SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
6309 const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07006310 const VkSubpassBeginInfo *pSubpassBeginInfo)
6311 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006312 if (pRenderPassBegin) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006313 rp_state_ = sync_state.Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
John Zulauf64ffe552021-02-06 10:25:07 -07006314 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006315 auto fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07006316 if (fb_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006317 shared_attachments_ = sync_state.GetAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
John Zulauf64ffe552021-02-06 10:25:07 -07006318 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
6319 // Note that this a safe to presist as long as shared_attachments is not cleared
6320 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08006321 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07006322 attachments_.emplace_back(attachment.get());
6323 }
6324 }
6325 if (pSubpassBeginInfo) {
6326 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
6327 }
6328 }
6329}
6330
6331bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6332 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
6333 bool skip = false;
6334
6335 assert(rp_state_.get());
6336 if (nullptr == rp_state_.get()) return skip;
6337 auto &rp_state = *rp_state_.get();
6338
6339 const uint32_t subpass = 0;
6340
6341 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
6342 // hasn't happened yet)
6343 const std::vector<AccessContext> empty_context_vector;
6344 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
6345 cb_context.GetCurrentAccessContext());
6346
6347 // Validate attachment operations
6348 if (attachments_.size() == 0) return skip;
6349 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07006350
6351 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
6352 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
6353 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
6354 // operations (though it's currently a messy approach)
6355 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
6356 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006357
6358 // Validate load operations if there were no layout transition hazards
6359 if (!skip) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07006360 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kCurrentCommandTag);
6361 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006362 }
6363
6364 return skip;
6365}
6366
John Zulauf8eda1562021-04-13 17:06:41 -06006367ResourceUsageTag SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
6368 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf64ffe552021-02-06 10:25:07 -07006369 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
6370 assert(rp_state_.get());
John Zulauf8eda1562021-04-13 17:06:41 -06006371 if (nullptr == rp_state_.get()) return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006372 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006373
6374 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006375}
6376
John Zulauf8eda1562021-04-13 17:06:41 -06006377bool SyncOpBeginRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006378 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006379 return false;
6380}
6381
John Zulauf4fa68462021-04-26 21:04:22 -06006382void SyncOpBeginRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
6383}
John Zulauf8eda1562021-04-13 17:06:41 -06006384
John Zulauf64ffe552021-02-06 10:25:07 -07006385SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07006386 const VkSubpassEndInfo *pSubpassEndInfo)
6387 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006388 if (pSubpassBeginInfo) {
6389 subpass_begin_info_.initialize(pSubpassBeginInfo);
6390 }
6391 if (pSubpassEndInfo) {
6392 subpass_end_info_.initialize(pSubpassEndInfo);
6393 }
6394}
6395
6396bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
6397 bool skip = false;
6398 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6399 if (!renderpass_context) return skip;
6400
6401 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
6402 return skip;
6403}
6404
John Zulauf8eda1562021-04-13 17:06:41 -06006405ResourceUsageTag SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006406 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06006407 // TODO PHASE2 Need to fix renderpass tagging/segregation of barrier and access operations for QueueSubmit time validation
6408 auto prev_tag = cb_context->NextCommandTag(cmd_);
6409 auto next_tag = cb_context->NextSubcommandTag(cmd_);
6410
6411 cb_context->RecordNextSubpass(prev_tag, next_tag);
6412 // TODO PHASE2 This needs to be the tag of the barrier operations
6413 return prev_tag;
6414}
6415
6416bool SyncOpNextSubpass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006417 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006418 return false;
John Zulauf64ffe552021-02-06 10:25:07 -07006419}
6420
sfricke-samsung85584a72021-09-30 21:43:38 -07006421SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo)
6422 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006423 if (pSubpassEndInfo) {
6424 subpass_end_info_.initialize(pSubpassEndInfo);
6425 }
6426}
6427
John Zulauf4fa68462021-04-26 21:04:22 -06006428void SyncOpNextSubpass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006429
John Zulauf64ffe552021-02-06 10:25:07 -07006430bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6431 bool skip = false;
6432 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6433
6434 if (!renderpass_context) return skip;
6435 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
6436 return skip;
6437}
6438
John Zulauf8eda1562021-04-13 17:06:41 -06006439ResourceUsageTag SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006440 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06006441 const auto tag = cb_context->NextCommandTag(cmd_);
6442 cb_context->RecordEndRenderPass(tag);
6443 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006444}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006445
John Zulauf8eda1562021-04-13 17:06:41 -06006446bool SyncOpEndRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006447 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006448 return false;
6449}
6450
John Zulauf4fa68462021-04-26 21:04:22 -06006451void SyncOpEndRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006452
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006453void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6454 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
6455 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
6456 auto *cb_access_context = GetAccessContext(commandBuffer);
6457 assert(cb_access_context);
6458 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
6459 auto *context = cb_access_context->GetCurrentAccessContext();
6460 assert(context);
6461
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006462 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006463
6464 if (dst_buffer) {
6465 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6466 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
6467 }
6468}
John Zulaufd05c5842021-03-26 11:32:16 -06006469
John Zulaufae842002021-04-15 18:20:55 -06006470bool SyncValidator::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6471 const VkCommandBuffer *pCommandBuffers) const {
6472 bool skip = StateTracker::PreCallValidateCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
6473 const char *func_name = "vkCmdExecuteCommands";
6474 const auto *cb_context = GetAccessContext(commandBuffer);
6475 assert(cb_context);
John Zulauf4fa68462021-04-26 21:04:22 -06006476
6477 // Heavyweight, but we need a proxy copy of the active command buffer access context
6478 CommandBufferAccessContext proxy_cb_context(*cb_context, CommandBufferAccessContext::AsProxyContext());
John Zulaufae842002021-04-15 18:20:55 -06006479
6480 // Make working copies of the access and events contexts
John Zulauf4fa68462021-04-26 21:04:22 -06006481 proxy_cb_context.NextCommandTag(CMD_EXECUTECOMMANDS);
John Zulaufae842002021-04-15 18:20:55 -06006482
6483 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
John Zulauf4fa68462021-04-26 21:04:22 -06006484 proxy_cb_context.NextSubcommandTag(CMD_EXECUTECOMMANDS);
John Zulaufae842002021-04-15 18:20:55 -06006485 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6486 if (!recorded_cb_context) continue;
John Zulauf4fa68462021-04-26 21:04:22 -06006487
6488 const auto *recorded_context = recorded_cb_context->GetCurrentAccessContext();
6489 assert(recorded_context);
6490 skip |= recorded_cb_context->ValidateFirstUse(&proxy_cb_context, func_name, cb_index);
6491
6492 // The barriers have already been applied in ValidatFirstUse
6493 ResourceUsageRange tag_range = proxy_cb_context.ImportRecordedAccessLog(*recorded_cb_context);
6494 proxy_cb_context.ResolveRecordedContext(*recorded_context, tag_range.begin);
John Zulaufae842002021-04-15 18:20:55 -06006495 }
6496
John Zulaufae842002021-04-15 18:20:55 -06006497 return skip;
6498}
6499
6500void SyncValidator::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6501 const VkCommandBuffer *pCommandBuffers) {
6502 StateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
John Zulauf4fa68462021-04-26 21:04:22 -06006503 auto *cb_context = GetAccessContext(commandBuffer);
6504 assert(cb_context);
6505 cb_context->NextCommandTag(CMD_EXECUTECOMMANDS);
6506 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
6507 cb_context->NextSubcommandTag(CMD_EXECUTECOMMANDS);
6508 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6509 if (!recorded_cb_context) continue;
6510 cb_context->RecordExecutedCommandBuffer(*recorded_cb_context, CMD_EXECUTECOMMANDS);
6511 }
John Zulaufae842002021-04-15 18:20:55 -06006512}
6513
John Zulaufd0ec59f2021-03-13 14:25:08 -07006514AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
6515 : view_(view), view_mask_(), gen_store_() {
6516 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
6517 const IMAGE_STATE &image_state = *view_->image_state.get();
6518 const auto base_address = ResourceBaseAddress(image_state);
6519 const auto *encoder = image_state.fragment_encoder.get();
6520 if (!encoder) return;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06006521 // Get offset and extent for the view, accounting for possible depth slicing
6522 const VkOffset3D zero_offset = view->GetOffset();
6523 const VkExtent3D &image_extent = view->GetExtent();
John Zulaufd0ec59f2021-03-13 14:25:08 -07006524 // Intentional copy
6525 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
6526 view_mask_ = subres_range.aspectMask;
6527 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
6528 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6529
6530 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
6531 if (depth && (depth != view_mask_)) {
6532 subres_range.aspectMask = depth;
6533 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6534 }
6535 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
6536 if (stencil && (stencil != view_mask_)) {
6537 subres_range.aspectMask = stencil;
6538 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6539 }
6540}
6541
6542const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
6543 const ImageRangeGen *got = nullptr;
6544 switch (gen_type) {
6545 case kViewSubresource:
6546 got = &gen_store_[kViewSubresource];
6547 break;
6548 case kRenderArea:
6549 got = &gen_store_[kRenderArea];
6550 break;
6551 case kDepthOnlyRenderArea:
6552 got =
6553 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
6554 break;
6555 case kStencilOnlyRenderArea:
6556 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
6557 : &gen_store_[Gen::kStencilOnlyRenderArea];
6558 break;
6559 default:
6560 assert(got);
6561 }
6562 return got;
6563}
6564
6565AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
6566 assert(IsValid());
6567 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
6568 if (depth_op) {
6569 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
6570 if (stencil_op) {
6571 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6572 return kRenderArea;
6573 }
6574 return kDepthOnlyRenderArea;
6575 }
6576 if (stencil_op) {
6577 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6578 return kStencilOnlyRenderArea;
6579 }
6580
6581 assert(depth_op || stencil_op);
6582 return kRenderArea;
6583}
6584
6585AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }
John Zulauf8eda1562021-04-13 17:06:41 -06006586
6587void SyncEventsContext::ApplyBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
6588 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
6589 for (auto &event_pair : map_) {
6590 assert(event_pair.second); // Shouldn't be storing empty
6591 auto &sync_event = *event_pair.second;
6592 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
6593 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
6594 sync_event.barriers |= dst.exec_scope;
6595 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6596 }
6597 }
6598}