blob: 31958dad8df3c65116fbbd5a438d66d57ee326d9 [file] [log] [blame]
John Zulaufab7756b2020-12-29 16:10:16 -07001/* Copyright (c) 2019-2021 The Khronos Group Inc.
2 * Copyright (c) 2019-2021 Valve Corporation
3 * Copyright (c) 2019-2021 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;
1849 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1850 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1851
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001852 for (const auto &stage_state : pipe->stage_state) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06001853 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->create_info.graphics.pRasterizationState &&
1854 pipe->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001855 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001856 }
locke-lunarg61870c22020-06-09 14:51:50 -06001857 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001858 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set;
locke-lunarg61870c22020-06-09 14:51:50 -06001859 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001860 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001861 const auto descriptor_type = binding_it.GetType();
1862 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1863 auto array_idx = 0;
1864
1865 if (binding_it.IsVariableDescriptorCount()) {
1866 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1867 }
1868 SyncStageAccessIndex sync_index =
1869 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1870
1871 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1872 uint32_t index = i - index_range.start;
1873 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1874 switch (descriptor->GetClass()) {
1875 case DescriptorClass::ImageSampler:
1876 case DescriptorClass::Image: {
1877 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001878 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001879 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001880 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1881 img_view_state = image_sampler_descriptor->GetImageViewState();
1882 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001883 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001884 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1885 img_view_state = image_descriptor->GetImageViewState();
1886 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001887 }
1888 if (!img_view_state) continue;
John Zulauf361fb532020-07-22 10:45:39 -06001889 HazardResult hazard;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06001890 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
1891 // Descriptors, so we do not have to worry about depth slicing here.
1892 // See: VUID 00343
1893 assert(!img_view_state->IsDepthSliced());
John Zulauf110413c2021-03-20 05:38:38 -06001894 const IMAGE_STATE *img_state = img_view_state->image_state.get();
John Zulauf361fb532020-07-22 10:45:39 -06001895 const auto &subresource_range = img_view_state->normalized_subresource_range;
John Zulauf110413c2021-03-20 05:38:38 -06001896
1897 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1898 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1899 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
John Zulauf361fb532020-07-22 10:45:39 -06001900 // Input attachments are subject to raster ordering rules
1901 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001902 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001903 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001904 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range);
John Zulauf361fb532020-07-22 10:45:39 -06001905 }
John Zulauf110413c2021-03-20 05:38:38 -06001906
John Zulauf33fc1d52020-07-17 11:01:10 -06001907 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001908 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001909 img_view_state->image_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001910 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1911 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001912 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001913 sync_state_->report_data->FormatHandle(img_view_state->image_view()).c_str(),
1914 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1915 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001916 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1917 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001918 set_binding.first.binding, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001919 }
1920 break;
1921 }
1922 case DescriptorClass::TexelBuffer: {
1923 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1924 if (!buf_view_state) continue;
1925 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001926 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001927 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001928 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001929 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001930 buf_view_state->buffer_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001931 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1932 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001933 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view()).c_str(),
1934 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1935 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001936 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001937 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001938 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001939 }
1940 break;
1941 }
1942 case DescriptorClass::GeneralBuffer: {
1943 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1944 auto buf_state = buffer_descriptor->GetBufferState();
1945 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001946 const ResourceAccessRange range =
1947 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001948 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001949 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001950 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001951 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001952 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1953 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001954 sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
1955 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1956 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001957 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001958 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001959 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001960 }
1961 break;
1962 }
1963 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1964 default:
1965 break;
1966 }
1967 }
1968 }
1969 }
1970 return skip;
1971}
1972
1973void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
John Zulauf14940722021-04-12 15:19:02 -06001974 const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001975 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001976 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001977 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001978 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001979 return;
1980 }
1981
1982 using DescriptorClass = cvdescriptorset::DescriptorClass;
1983 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1984 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1985 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1986 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 Gebben7fc88a22021-08-25 13:30:45 -06001994 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set;
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: {
2012 const IMAGE_VIEW_STATE *img_view_state = nullptr;
2013 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
2014 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
2015 } else {
2016 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
2017 }
2018 if (!img_view_state) continue;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06002019 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
2020 // Descriptors, so we do not have to worry about depth slicing here.
2021 // See: VUID 00343
2022 assert(!img_view_state->IsDepthSliced());
locke-lunarg61870c22020-06-09 14:51:50 -06002023 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002024 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
John Zulauf110413c2021-03-20 05:38:38 -06002025 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
2026 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
2027 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kRaster,
2028 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002029 } else {
John Zulauf110413c2021-03-20 05:38:38 -06002030 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kNonAttachment,
2031 img_view_state->normalized_subresource_range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002032 }
locke-lunarg61870c22020-06-09 14:51:50 -06002033 break;
2034 }
2035 case DescriptorClass::TexelBuffer: {
2036 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
2037 if (!buf_view_state) continue;
2038 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002039 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07002040 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002041 break;
2042 }
2043 case DescriptorClass::GeneralBuffer: {
2044 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
2045 auto buf_state = buffer_descriptor->GetBufferState();
2046 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002047 const ResourceAccessRange range =
2048 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07002049 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002050 break;
2051 }
2052 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2053 default:
2054 break;
2055 }
2056 }
2057 }
2058 }
2059}
2060
2061bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2062 bool skip = false;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002063 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002064 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002065 return skip;
2066 }
2067
2068 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2069 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002070 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002071
2072 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002073 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002074 if (binding_description.binding < binding_buffers_size) {
2075 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002076 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002077
locke-lunarg1ae57d62020-11-18 10:49:19 -07002078 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002079 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2080 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002081 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002082 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002083 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002084 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
2085 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
2086 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002087 }
2088 }
2089 }
2090 return skip;
2091}
2092
John Zulauf14940722021-04-12 15:19:02 -06002093void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002094 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002095 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002096 return;
2097 }
2098 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2099 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002100 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002101
2102 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002103 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002104 if (binding_description.binding < binding_buffers_size) {
2105 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002106 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002107
locke-lunarg1ae57d62020-11-18 10:49:19 -07002108 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002109 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2110 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002111 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2112 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002113 }
2114 }
2115}
2116
2117bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2118 bool skip = false;
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002119 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->Destroyed()) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002120 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002121 }
locke-lunarg61870c22020-06-09 14:51:50 -06002122
locke-lunarg1ae57d62020-11-18 10:49:19 -07002123 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002124 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002125 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2126 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002127 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002128 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002129 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002130 index_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
2131 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer()).c_str(),
2132 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002133 }
2134
2135 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2136 // We will detect more accurate range in the future.
2137 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2138 return skip;
2139}
2140
John Zulauf14940722021-04-12 15:19:02 -06002141void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag tag) {
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002142 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 -06002143
locke-lunarg1ae57d62020-11-18 10:49:19 -07002144 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002145 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002146 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2147 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002148 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002149
2150 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2151 // We will detect more accurate range in the future.
2152 RecordDrawVertex(UINT32_MAX, 0, tag);
2153}
2154
2155bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002156 bool skip = false;
2157 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002158 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002159 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002160}
2161
John Zulauf14940722021-04-12 15:19:02 -06002162void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002163 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002164 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002165 }
locke-lunarg61870c22020-06-09 14:51:50 -06002166}
2167
John Zulauf64ffe552021-02-06 10:25:07 -07002168void CommandBufferAccessContext::RecordBeginRenderPass(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2169 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
John Zulauf14940722021-04-12 15:19:02 -06002170 const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002171 // Create an access context the current renderpass.
John Zulauf64ffe552021-02-06 10:25:07 -07002172 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002173 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf64ffe552021-02-06 10:25:07 -07002174 current_renderpass_context_->RecordBeginRenderPass(tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002175 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002176}
2177
John Zulauf8eda1562021-04-13 17:06:41 -06002178void CommandBufferAccessContext::RecordNextSubpass(ResourceUsageTag prev_tag, ResourceUsageTag next_tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002179 assert(current_renderpass_context_);
John Zulauf64ffe552021-02-06 10:25:07 -07002180 current_renderpass_context_->RecordNextSubpass(prev_tag, next_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002181 current_context_ = &current_renderpass_context_->CurrentContext();
2182}
2183
John Zulauf8eda1562021-04-13 17:06:41 -06002184void CommandBufferAccessContext::RecordEndRenderPass(const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002185 assert(current_renderpass_context_);
2186 if (!current_renderpass_context_) return;
2187
John Zulauf8eda1562021-04-13 17:06:41 -06002188 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002189 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002190 current_renderpass_context_ = nullptr;
2191}
2192
John Zulauf4a6105a2020-11-17 15:11:05 -07002193void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2194 // Erase is okay with the key not being
John Zulauf669dfd52021-01-27 17:15:28 -07002195 const auto *event_state = sync_state_->Get<EVENT_STATE>(event);
2196 if (event_state) {
2197 GetCurrentEventsContext()->Destroy(event_state);
John Zulaufd5115702021-01-18 12:34:33 -07002198 }
2199}
2200
John Zulaufae842002021-04-15 18:20:55 -06002201// The is the recorded cb context
John Zulauf4fa68462021-04-26 21:04:22 -06002202bool CommandBufferAccessContext::ValidateFirstUse(CommandBufferAccessContext *proxy_context, const char *func_name,
2203 uint32_t index) const {
2204 assert(proxy_context);
2205 auto *events_context = proxy_context->GetCurrentEventsContext();
2206 auto *access_context = proxy_context->GetCurrentAccessContext();
2207 const ResourceUsageTag base_tag = proxy_context->GetTagLimit();
John Zulaufae842002021-04-15 18:20:55 -06002208 bool skip = false;
2209 ResourceUsageRange tag_range = {0, 0};
2210 const AccessContext *recorded_context = GetCurrentAccessContext();
2211 assert(recorded_context);
2212 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002213 auto log_msg = [this](const HazardResult &hazard, const CommandBufferAccessContext &active_context, const char *func_name,
John Zulaufae842002021-04-15 18:20:55 -06002214 uint32_t index) {
2215 const auto cb_handle = active_context.cb_state_->commandBuffer();
2216 const auto recorded_handle = cb_state_->commandBuffer();
John Zulauf4fa68462021-04-26 21:04:22 -06002217 const auto *report_data = sync_state_->report_data;
John Zulaufae842002021-04-15 18:20:55 -06002218 return sync_state_->LogError(cb_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf4fa68462021-04-26 21:04:22 -06002219 "%s: Hazard %s for entry %" PRIu32 ", %s, Recorded access info %s. Access info %s.", func_name,
2220 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(recorded_handle).c_str(),
2221 FormatUsage(*hazard.recorded_access).c_str(), active_context.FormatUsage(hazard).c_str());
John Zulaufae842002021-04-15 18:20:55 -06002222 };
2223 for (const auto &sync_op : sync_ops_) {
John Zulauf4fa68462021-04-26 21:04:22 -06002224 // we update the range to any include layout transition first use writes,
2225 // as they are stored along with the source scope (as effective barrier) when recorded
2226 tag_range.end = sync_op.tag + 1;
John Zulauf610e28c2021-08-03 17:46:23 -06002227 skip |= sync_op.sync_op->ReplayValidate(sync_op.tag, *this, base_tag, proxy_context);
John Zulauf4fa68462021-04-26 21:04:22 -06002228
John Zulaufae842002021-04-15 18:20:55 -06002229 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context);
2230 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002231 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002232 }
2233 // NOTE: Add call to replay validate here when we add support for syncop with non-trivial replay
John Zulauf4fa68462021-04-26 21:04:22 -06002234 // Record the barrier into the proxy context.
2235 sync_op.sync_op->DoRecord(base_tag + sync_op.tag, access_context, events_context);
2236 tag_range.begin = tag_range.end;
John Zulaufae842002021-04-15 18:20:55 -06002237 }
2238
2239 // and anything after the last syncop
John Zulaufae842002021-04-15 18:20:55 -06002240 tag_range.end = ResourceUsageRecord::kMaxIndex;
2241 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context);
2242 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002243 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002244 }
2245
2246 return skip;
2247}
2248
John Zulauf4fa68462021-04-26 21:04:22 -06002249void CommandBufferAccessContext::RecordExecutedCommandBuffer(const CommandBufferAccessContext &recorded_cb_context, CMD_TYPE cmd) {
2250 auto *events_context = GetCurrentEventsContext();
2251 auto *access_context = GetCurrentAccessContext();
2252 const AccessContext *recorded_context = recorded_cb_context.GetCurrentAccessContext();
2253 assert(recorded_context);
2254
2255 // Just run through the barriers ignoring the usage from the recorded context, as Resolve will overwrite outdated state
2256 const ResourceUsageTag base_tag = GetTagLimit();
2257 for (const auto &sync_op : recorded_cb_context.sync_ops_) {
2258 // we update the range to any include layout transition first use writes,
2259 // as they are stored along with the source scope (as effective barrier) when recorded
2260 sync_op.sync_op->DoRecord(base_tag + sync_op.tag, access_context, events_context);
2261 }
2262
2263 ResourceUsageRange tag_range = ImportRecordedAccessLog(recorded_cb_context);
2264 assert(base_tag == tag_range.begin); // to ensure the to offset calculation agree
2265 ResolveRecordedContext(*recorded_context, tag_range.begin);
2266}
2267
2268void CommandBufferAccessContext::ResolveRecordedContext(const AccessContext &recorded_context, ResourceUsageTag offset) {
2269 auto tag_offset = [offset](ResourceAccessState *access) { access->OffsetTag(offset); };
2270
2271 auto *access_context = GetCurrentAccessContext();
2272 for (auto address_type : kAddressTypes) {
2273 recorded_context.ResolveAccessRange(address_type, kFullRange, tag_offset, &access_context->GetAccessStateMap(address_type),
2274 nullptr, false);
2275 }
2276}
2277
2278ResourceUsageRange CommandBufferAccessContext::ImportRecordedAccessLog(const CommandBufferAccessContext &recorded_context) {
2279 // The execution references ensure lifespan for the referenced child CB's...
2280 ResourceUsageRange tag_range(GetTagLimit(), 0);
John Zulauf3c2a0b32021-07-14 11:14:52 -06002281 cbs_referenced_.emplace(recorded_context.cb_state_);
John Zulauf4fa68462021-04-26 21:04:22 -06002282 access_log_.insert(access_log_.end(), recorded_context.access_log_.cbegin(), recorded_context.access_log_.end());
2283 tag_range.end = access_log_.size();
2284 return tag_range;
2285}
2286
John Zulaufae842002021-04-15 18:20:55 -06002287class HazardDetectFirstUse {
2288 public:
2289 HazardDetectFirstUse(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range)
2290 : recorded_use_(recorded_use), tag_range_(tag_range) {}
2291 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
2292 return pos->second.DetectHazard(recorded_use_, tag_range_);
2293 }
2294 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
2295 return pos->second.DetectAsyncHazard(recorded_use_, tag_range_, start_tag);
2296 }
2297
2298 private:
2299 const ResourceAccessState &recorded_use_;
2300 const ResourceUsageRange &tag_range_;
2301};
2302
2303// This is called with the *recorded* command buffers access context, with the *active* access context pass in, againsts which
2304// hazards will be detected
2305HazardResult AccessContext::DetectFirstUseHazard(const ResourceUsageRange &tag_range, const AccessContext &access_context) const {
2306 HazardResult hazard;
2307 for (const auto address_type : kAddressTypes) {
2308 const auto &recorded_access_map = GetAccessStateMap(address_type);
2309 for (const auto &recorded_access : recorded_access_map) {
2310 // Cull any entries not in the current tag range
2311 if (!recorded_access.second.FirstAccessInTagRange(tag_range)) continue;
2312 HazardDetectFirstUse detector(recorded_access.second, tag_range);
2313 hazard = access_context.DetectHazard(address_type, detector, recorded_access.first, DetectOptions::kDetectAll);
2314 if (hazard.hazard) break;
2315 }
2316 }
2317
2318 return hazard;
2319}
2320
John Zulauf64ffe552021-02-06 10:25:07 -07002321bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
John Zulauffaea0ee2021-01-14 14:01:32 -07002322 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002323 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002324 const auto &sync_state = ex_context.GetSyncState();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002325 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002326 if (!pipe) {
2327 return skip;
2328 }
2329
2330 const auto &create_info = pipe->create_info.graphics;
2331 if (create_info.pRasterizationState && create_info.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002332 return skip;
2333 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002334 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002335 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg37047832020-06-12 13:44:45 -06002336
John Zulauf1a224292020-06-30 14:52:13 -06002337 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002338 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002339 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2340 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002341 if (location >= subpass.colorAttachmentCount ||
2342 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002343 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002344 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002345 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2346 if (!view_gen.IsValid()) continue;
2347 HazardResult hazard =
2348 current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
2349 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment);
locke-lunarg96dc9632020-06-10 17:22:18 -06002350 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002351 const VkImageView view_handle = view_gen.GetViewState()->image_view();
John Zulaufd0ec59f2021-03-13 14:25:08 -07002352 skip |= sync_state.LogError(view_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002353 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002354 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002355 sync_state.report_data->FormatHandle(view_handle).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002356 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002357 location, ex_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002358 }
2359 }
2360 }
locke-lunarg37047832020-06-12 13:44:45 -06002361
2362 // PHASE1 TODO: Add layout based read/vs. write selection.
2363 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002364 const uint32_t depth_stencil_attachment =
Jeremy Gebben11af9792021-08-20 10:20:09 -06002365 GetSubpassDepthStencilAttachmentIndex(pipe->create_info.graphics.pDepthStencilState, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002366
2367 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2368 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2369 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002370 bool depth_write = false, stencil_write = false;
2371
2372 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002373 if (!FormatIsStencilOnly(view_state.create_info.format) && create_info.pDepthStencilState->depthTestEnable &&
2374 create_info.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002375 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2376 depth_write = true;
2377 }
2378 // PHASE1 TODO: It needs to check if stencil is writable.
2379 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2380 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2381 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002382 if (!FormatIsDepthOnly(view_state.create_info.format) && create_info.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002383 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2384 stencil_write = true;
2385 }
2386
2387 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2388 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002389 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2390 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2391 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002392 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002393 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002394 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002395 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002396 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002397 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2398 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002399 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002400 }
2401 }
2402 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002403 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2404 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2405 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002406 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002407 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002408 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002409 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002410 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002411 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2412 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002413 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002414 }
locke-lunarg61870c22020-06-09 14:51:50 -06002415 }
2416 }
2417 return skip;
2418}
2419
John Zulauf14940722021-04-12 15:19:02 -06002420void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002421 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002422 if (!pipe) {
2423 return;
2424 }
2425
2426 const auto &create_info = pipe->create_info.graphics;
2427 if (create_info.pRasterizationState && create_info.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002428 return;
2429 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002430 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002431 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002432
John Zulauf1a224292020-06-30 14:52:13 -06002433 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002434 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002435 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2436 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002437 if (location >= subpass.colorAttachmentCount ||
2438 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002439 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002440 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002441 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2442 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2443 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2444 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002445 }
2446 }
locke-lunarg37047832020-06-12 13:44:45 -06002447
2448 // PHASE1 TODO: Add layout based read/vs. write selection.
2449 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002450 const uint32_t depth_stencil_attachment =
Jeremy Gebben11af9792021-08-20 10:20:09 -06002451 GetSubpassDepthStencilAttachmentIndex(create_info.pDepthStencilState, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002452 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2453 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2454 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002455 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002456 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2457 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002458
2459 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002460 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) && create_info.pDepthStencilState->depthTestEnable &&
2461 create_info.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002462 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2463 depth_write = true;
2464 }
2465 // PHASE1 TODO: It needs to check if stencil is writable.
2466 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2467 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2468 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002469 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) && create_info.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002470 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2471 stencil_write = true;
2472 }
2473
John Zulaufd0ec59f2021-03-13 14:25:08 -07002474 if (depth_write || stencil_write) {
2475 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2476 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2477 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2478 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002479 }
locke-lunarg61870c22020-06-09 14:51:50 -06002480 }
2481}
2482
John Zulauf64ffe552021-02-06 10:25:07 -07002483bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002484 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002485 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002486 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002487 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002488 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002489 func_name);
2490
John Zulauf355e49b2020-04-24 15:11:15 -06002491 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002492 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002493 skip |=
2494 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002495 if (!skip) {
2496 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2497 // on a copy of the (empty) next context.
2498 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2499 AccessContext temp_context(next_context);
2500 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002501 skip |=
2502 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002503 }
John Zulauf7635de32020-05-29 17:14:15 -06002504 return skip;
2505}
John Zulauf64ffe552021-02-06 10:25:07 -07002506bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002507 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002508 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002509 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002510 current_subpass_);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002511 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_,
2512
2513 attachment_views_, func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002514 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002515 return skip;
2516}
2517
John Zulauf64ffe552021-02-06 10:25:07 -07002518AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002519 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002520}
2521
John Zulauf64ffe552021-02-06 10:25:07 -07002522bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2523 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002524 bool skip = false;
2525
John Zulauf7635de32020-05-29 17:14:15 -06002526 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2527 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2528 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2529 // to apply and only copy then, if this proves a hot spot.
2530 std::unique_ptr<AccessContext> proxy_for_current;
2531
John Zulauf355e49b2020-04-24 15:11:15 -06002532 // Validate the "finalLayout" transitions to external
2533 // Get them from where there we're hidding in the extra entry.
2534 const auto &final_transitions = rp_state_->subpass_transitions.back();
2535 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002536 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002537 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2538 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002539 auto *context = trackback.context;
2540
2541 if (transition.prev_pass == current_subpass_) {
2542 if (!proxy_for_current) {
2543 // 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 -07002544 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002545 }
2546 context = proxy_for_current.get();
2547 }
2548
John Zulaufa0a98292020-09-18 09:30:10 -06002549 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2550 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002551 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002552 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002553 skip |= ex_context.GetSyncState().LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002554 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07002555 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2556 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2557 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2558 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002559 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002560 }
2561 }
2562 return skip;
2563}
2564
John Zulauf14940722021-04-12 15:19:02 -06002565void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002566 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002567 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002568}
2569
John Zulauf14940722021-04-12 15:19:02 -06002570void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002571 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2572 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002573
2574 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2575 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002576 const AttachmentViewGen &view_gen = attachment_views_[i];
2577 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002578
2579 const auto &ci = attachment_ci[i];
2580 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002581 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002582 const bool is_color = !(has_depth || has_stencil);
2583
2584 if (is_color) {
John Zulauf57261402021-08-13 11:32:06 -06002585 const SyncStageAccessIndex load_op = ColorLoadUsage(ci.loadOp);
2586 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2587 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, load_op,
2588 SyncOrdering::kColorAttachment, tag);
2589 }
John Zulauf1507ee42020-05-18 11:33:09 -06002590 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002591 if (has_depth) {
John Zulauf57261402021-08-13 11:32:06 -06002592 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.loadOp);
2593 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2594 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_op,
2595 SyncOrdering::kDepthStencilAttachment, tag);
2596 }
John Zulauf1507ee42020-05-18 11:33:09 -06002597 }
2598 if (has_stencil) {
John Zulauf57261402021-08-13 11:32:06 -06002599 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.stencilLoadOp);
2600 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2601 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, load_op,
2602 SyncOrdering::kDepthStencilAttachment, tag);
2603 }
John Zulauf1507ee42020-05-18 11:33:09 -06002604 }
2605 }
2606 }
2607 }
2608}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002609AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2610 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2611 AttachmentViewGenVector view_gens;
2612 VkExtent3D extent = CastTo3D(render_area.extent);
2613 VkOffset3D offset = CastTo3D(render_area.offset);
2614 view_gens.reserve(attachment_views.size());
2615 for (const auto *view : attachment_views) {
2616 view_gens.emplace_back(view, offset, extent);
2617 }
2618 return view_gens;
2619}
John Zulauf64ffe552021-02-06 10:25:07 -07002620RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2621 VkQueueFlags queue_flags,
2622 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2623 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002624 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002625 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002626 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002627 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002628 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002629 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002630 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002631}
John Zulauf14940722021-04-12 15:19:02 -06002632void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag tag) {
John Zulauf64ffe552021-02-06 10:25:07 -07002633 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002634 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002635 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002636 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002637}
John Zulauf1507ee42020-05-18 11:33:09 -06002638
John Zulauf14940722021-04-12 15:19:02 -06002639void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag prev_subpass_tag, const ResourceUsageTag next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002640 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulaufd0ec59f2021-03-13 14:25:08 -07002641 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
2642 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002643
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002644 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2645 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002646 current_subpass_++;
2647 assert(current_subpass_ < subpass_contexts_.size());
John Zulauffaea0ee2021-01-14 14:01:32 -07002648 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2649 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002650 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002651}
2652
John Zulauf14940722021-04-12 15:19:02 -06002653void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002654 // Add the resolve and store accesses
John Zulaufd0ec59f2021-03-13 14:25:08 -07002655 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, tag);
2656 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002657
John Zulauf355e49b2020-04-24 15:11:15 -06002658 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002659 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002660
2661 // Add the "finalLayout" transitions to external
2662 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002663 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2664 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2665 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002666 const auto &final_transitions = rp_state_->subpass_transitions.back();
2667 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002668 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002669 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002670 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002671 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002672 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002673 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002674 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002675 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002676 }
2677}
2678
Jeremy Gebben40a22942020-12-22 14:22:06 -07002679SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002680 SyncExecScope result;
2681 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002682 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2683 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002684 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2685 return result;
2686}
2687
Jeremy Gebben40a22942020-12-22 14:22:06 -07002688SyncExecScope SyncExecScope::MakeDst(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::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002693 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2694 return result;
2695}
2696
2697SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002698 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002699 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002700 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002701 dst_access_scope = 0;
2702}
2703
2704template <typename Barrier>
2705SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002706 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002707 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002708 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002709 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2710}
2711
2712SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002713 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2714 if (barrier) {
2715 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002716 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002717 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002718
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002719 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002720 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002721 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2722
2723 } else {
2724 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.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, subpass.srcAccessMask);
2727
2728 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.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, subpass.dstAccessMask);
2731 }
2732}
2733
2734template <typename Barrier>
2735SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2736 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2737 src_exec_scope = src.exec_scope;
2738 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2739
2740 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002741 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002742 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002743}
2744
John Zulaufb02c1eb2020-10-06 16:33:36 -06002745// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2746void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2747 for (const auto &barrier : barriers) {
2748 ApplyBarrier(barrier, layout_transition);
2749 }
2750}
2751
John Zulauf89311b42020-09-29 16:28:47 -06002752// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2753// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2754// lazily, s.t. no previous access reports should need layout transitions.
John Zulauf14940722021-04-12 15:19:02 -06002755void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06002756 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002757 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002758 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002759 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002760 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002761 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002762 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002763}
John Zulauf9cb530d2019-09-30 14:14:10 -06002764HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2765 HazardResult hazard;
2766 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002767 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002768 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002769 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002770 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002771 }
2772 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002773 // Write operation:
2774 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2775 // If reads exists -- test only against them because either:
2776 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2777 // * 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
2778 // the current write happens after the reads, so just test the write against the reades
2779 // Otherwise test against last_write
2780 //
2781 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002782 if (last_reads.size()) {
2783 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002784 if (IsReadHazard(usage_stage, read_access)) {
2785 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2786 break;
2787 }
2788 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002789 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002790 // Write-After-Write check -- if we have a previous write to test against
2791 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002792 }
2793 }
2794 return hazard;
2795}
2796
John Zulauf4fa68462021-04-26 21:04:22 -06002797HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering ordering_rule) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002798 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf4fa68462021-04-26 21:04:22 -06002799 return DetectHazard(usage_index, ordering);
2800}
2801
2802HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const OrderingBarrier &ordering) const {
John Zulauf69133422020-05-20 14:55:53 -06002803 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2804 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002805 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002806 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002807 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2808 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002809 if (IsRead(usage_bit)) {
2810 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2811 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2812 if (is_raw_hazard) {
2813 // NOTE: we know last_write is non-zero
2814 // See if the ordering rules save us from the simple RAW check above
2815 // First check to see if the current usage is covered by the ordering rules
2816 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2817 const bool usage_is_ordered =
2818 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2819 if (usage_is_ordered) {
2820 // Now see of the most recent write (or a subsequent read) are ordered
2821 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2822 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002823 }
2824 }
John Zulauf4285ee92020-09-23 10:20:52 -06002825 if (is_raw_hazard) {
2826 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2827 }
John Zulauf5c628d02021-05-04 15:46:36 -06002828 } else if (usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2829 // For Image layout transitions, the barrier represents the first synchronization/access scope of the layout transition
2830 return DetectBarrierHazard(usage_index, ordering.exec_scope, ordering.access_scope);
John Zulauf361fb532020-07-22 10:45:39 -06002831 } else {
2832 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002833 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002834 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002835 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002836 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002837 if (usage_write_is_ordered) {
2838 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2839 ordered_stages = GetOrderedStages(ordering);
2840 }
2841 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2842 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002843 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002844 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2845 if (IsReadHazard(usage_stage, read_access)) {
2846 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2847 break;
2848 }
John Zulaufd14743a2020-07-03 09:42:39 -06002849 }
2850 }
John Zulauf4285ee92020-09-23 10:20:52 -06002851 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002852 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002853 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002854 }
John Zulauf69133422020-05-20 14:55:53 -06002855 }
2856 }
2857 return hazard;
2858}
2859
John Zulaufae842002021-04-15 18:20:55 -06002860HazardResult ResourceAccessState::DetectHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range) const {
2861 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002862 using Size = FirstAccesses::size_type;
2863 const auto &recorded_accesses = recorded_use.first_accesses_;
2864 Size count = recorded_accesses.size();
2865 if (count) {
2866 const auto &last_access = recorded_accesses.back();
2867 bool do_write_last = IsWrite(last_access.usage_index);
2868 if (do_write_last) --count;
John Zulaufae842002021-04-15 18:20:55 -06002869
John Zulauf4fa68462021-04-26 21:04:22 -06002870 for (Size i = 0; i < count; ++count) {
2871 const auto &first = recorded_accesses[i];
2872 // Skip and quit logic
2873 if (first.tag < tag_range.begin) continue;
2874 if (first.tag >= tag_range.end) {
2875 do_write_last = false; // ignore last since we know it can't be in tag_range
2876 break;
2877 }
2878
2879 hazard = DetectHazard(first.usage_index, first.ordering_rule);
2880 if (hazard.hazard) {
2881 hazard.AddRecordedAccess(first);
2882 break;
2883 }
2884 }
2885
2886 if (do_write_last && tag_range.includes(last_access.tag)) {
2887 // Writes are a bit special... both for the "most recent" access logic, and layout transition specific logic
2888 OrderingBarrier barrier = GetOrderingRules(last_access.ordering_rule);
2889 if (last_access.usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2890 // Or in the layout first access scope as a barrier... IFF the usage is an ILT
2891 // this was saved off in the "apply barriers" logic to simplify ILT access checks as they straddle
2892 // the barrier that applies them
2893 barrier |= recorded_use.first_write_layout_ordering_;
2894 }
2895 // Any read stages present in the recorded context (this) are most recent to the write, and thus mask those stages in
2896 // the active context
2897 if (recorded_use.first_read_stages_) {
2898 // we need to ignore the first use read stage in the active context (so we add them to the ordering rule),
2899 // reads in the active context are not "most recent" as all recorded context operations are *after* them
2900 // This supresses only RAW checks for stages present in the recorded context, but not those only present in the
2901 // active context.
2902 barrier.exec_scope |= recorded_use.first_read_stages_;
2903 // if there are any first use reads, we suppress WAW by injecting the active context write in the ordering rule
2904 barrier.access_scope |= FlagBit(last_access.usage_index);
2905 }
2906 hazard = DetectHazard(last_access.usage_index, barrier);
2907 if (hazard.hazard) {
2908 hazard.AddRecordedAccess(last_access);
2909 }
2910 }
John Zulaufae842002021-04-15 18:20:55 -06002911 }
2912 return hazard;
2913}
2914
John Zulauf2f952d22020-02-10 11:34:51 -07002915// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf14940722021-04-12 15:19:02 -06002916HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002917 HazardResult hazard;
2918 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002919 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2920 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2921 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002922 if (IsRead(usage)) {
John Zulauf14940722021-04-12 15:19:02 -06002923 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002924 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002925 }
2926 } else {
John Zulauf14940722021-04-12 15:19:02 -06002927 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002928 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002929 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002930 // 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 -07002931 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06002932 if (read_access.tag >= start_tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002933 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002934 break;
2935 }
2936 }
John Zulauf2f952d22020-02-10 11:34:51 -07002937 }
2938 }
2939 return hazard;
2940}
2941
John Zulaufae842002021-04-15 18:20:55 -06002942HazardResult ResourceAccessState::DetectAsyncHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
2943 ResourceUsageTag start_tag) const {
2944 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002945 for (const auto &first : recorded_use.first_accesses_) {
John Zulaufae842002021-04-15 18:20:55 -06002946 // Skip and quit logic
2947 if (first.tag < tag_range.begin) continue;
2948 if (first.tag >= tag_range.end) break;
John Zulaufae842002021-04-15 18:20:55 -06002949
2950 hazard = DetectAsyncHazard(first.usage_index, start_tag);
John Zulauf4fa68462021-04-26 21:04:22 -06002951 if (hazard.hazard) {
2952 hazard.AddRecordedAccess(first);
2953 break;
2954 }
John Zulaufae842002021-04-15 18:20:55 -06002955 }
2956 return hazard;
2957}
2958
Jeremy Gebben40a22942020-12-22 14:22:06 -07002959HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002960 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002961 // Only supporting image layout transitions for now
2962 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2963 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002964 // only test for WAW if there no intervening read operations.
2965 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002966 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002967 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002968 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002969 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002970 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002971 break;
2972 }
2973 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002974 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2975 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2976 }
2977
2978 return hazard;
2979}
2980
Jeremy Gebben40a22942020-12-22 14:22:06 -07002981HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07002982 const SyncStageAccessFlags &src_access_scope,
John Zulauf14940722021-04-12 15:19:02 -06002983 const ResourceUsageTag event_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002984 // Only supporting image layout transitions for now
2985 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2986 HazardResult hazard;
2987 // only test for WAW if there no intervening read operations.
2988 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2989
John Zulaufab7756b2020-12-29 16:10:16 -07002990 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002991 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
2992 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07002993 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06002994 if (read_access.tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002995 // The read is in the events first synchronization scope, so we use a barrier hazard check
2996 // If the read stage is not in the src sync scope
2997 // *AND* not execution chained with an existing sync barrier (that's the or)
2998 // then the barrier access is unsafe (R/W after R)
2999 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3000 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3001 break;
3002 }
3003 } else {
3004 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3005 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3006 }
3007 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003008 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003009 // 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 -06003010 if (write_tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003011 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3012 // So do a normal barrier hazard check
3013 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3014 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3015 }
3016 } else {
3017 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003018 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3019 }
John Zulaufd14743a2020-07-03 09:42:39 -06003020 }
John Zulauf361fb532020-07-22 10:45:39 -06003021
John Zulauf0cb5be22020-01-23 12:18:22 -07003022 return hazard;
3023}
3024
John Zulauf5f13a792020-03-10 07:31:21 -06003025// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3026// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3027// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3028void ResourceAccessState::Resolve(const ResourceAccessState &other) {
John Zulauf14940722021-04-12 15:19:02 -06003029 if (write_tag < other.write_tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003030 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3031 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003032 *this = other;
John Zulauf14940722021-04-12 15:19:02 -06003033 } else if (other.write_tag == write_tag) {
3034 // 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 -06003035 // dependency chaining logic or any stage expansion)
3036 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003037 pending_write_barriers |= other.pending_write_barriers;
3038 pending_layout_transition |= other.pending_layout_transition;
3039 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf4fa68462021-04-26 21:04:22 -06003040 pending_layout_ordering_ |= other.pending_layout_ordering_;
John Zulauf5f13a792020-03-10 07:31:21 -06003041
John Zulaufd14743a2020-07-03 09:42:39 -06003042 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07003043 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06003044 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07003045 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003046 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003047 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003048 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003049 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3050 // but we should wait on profiling data for that.
3051 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003052 auto &my_read = last_reads[my_read_index];
3053 if (other_read.stage == my_read.stage) {
John Zulauf14940722021-04-12 15:19:02 -06003054 if (my_read.tag < other_read.tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003055 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003056 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003057 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003058 my_read.pending_dep_chain = other_read.pending_dep_chain;
3059 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3060 // May require tracking more than one access per stage.
3061 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003062 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06003063 // Since I'm overwriting the fragement stage read, also update the input attachment info
3064 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003065 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003066 }
John Zulauf14940722021-04-12 15:19:02 -06003067 } else if (other_read.tag == my_read.tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06003068 // The read tags match so merge the barriers
3069 my_read.barriers |= other_read.barriers;
3070 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003071 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003072
John Zulauf5f13a792020-03-10 07:31:21 -06003073 break;
3074 }
3075 }
3076 } else {
3077 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07003078 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06003079 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003080 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003081 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003082 }
John Zulauf5f13a792020-03-10 07:31:21 -06003083 }
3084 }
John Zulauf361fb532020-07-22 10:45:39 -06003085 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003086 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3087 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07003088
3089 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
3090 // of the copy and other into this using the update first logic.
3091 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
3092 // of the other first_accesses... )
3093 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
3094 FirstAccesses firsts(std::move(first_accesses_));
3095 first_accesses_.clear();
3096 first_read_stages_ = 0U;
3097 auto a = firsts.begin();
3098 auto a_end = firsts.end();
3099 for (auto &b : other.first_accesses_) {
John Zulauf14940722021-04-12 15:19:02 -06003100 // TODO: Determine whether some tag offset will be needed for PHASE II
3101 while ((a != a_end) && (a->tag < b.tag)) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003102 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3103 ++a;
3104 }
3105 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
3106 }
3107 for (; a != a_end; ++a) {
3108 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3109 }
3110 }
John Zulauf5f13a792020-03-10 07:31:21 -06003111}
3112
John Zulauf14940722021-04-12 15:19:02 -06003113void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003114 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3115 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003116 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003117 // Mulitple outstanding reads may be of interest and do dependency chains independently
3118 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3119 const auto usage_stage = PipelineStageBit(usage_index);
3120 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003121 for (auto &read_access : last_reads) {
3122 if (read_access.stage == usage_stage) {
3123 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003124 break;
3125 }
3126 }
3127 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07003128 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003129 last_read_stages |= usage_stage;
3130 }
John Zulauf4285ee92020-09-23 10:20:52 -06003131
3132 // 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 -07003133 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003134 // TODO Revisit re: multiple reads for a given stage
3135 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003136 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003137 } else {
3138 // Assume write
3139 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003140 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003141 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003142 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06003143}
John Zulauf5f13a792020-03-10 07:31:21 -06003144
John Zulauf89311b42020-09-29 16:28:47 -06003145// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3146// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3147// We can overwrite them as *this* write is now after them.
3148//
3149// 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 -06003150void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003151 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06003152 last_read_stages = 0;
3153 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003154 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003155
3156 write_barriers = 0;
3157 write_dependency_chain = 0;
3158 write_tag = tag;
3159 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003160}
3161
John Zulauf89311b42020-09-29 16:28:47 -06003162// Apply the memory barrier without updating the existing barriers. The execution barrier
3163// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3164// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3165// replace the current write barriers or add to them, so accumulate to pending as well.
3166void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3167 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3168 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003169 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3170 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3171 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3172 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07003173 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003174 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003175 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003176 if (layout_transition) {
3177 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3178 }
John Zulaufa0a98292020-09-18 09:30:10 -06003179 }
John Zulauf89311b42020-09-29 16:28:47 -06003180 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3181 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003182
John Zulauf89311b42020-09-29 16:28:47 -06003183 if (!pending_layout_transition) {
3184 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3185 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003186 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06003187 // 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 -07003188 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
3189 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003190 }
3191 }
John Zulaufa0a98292020-09-18 09:30:10 -06003192 }
John Zulaufa0a98292020-09-18 09:30:10 -06003193}
3194
John Zulauf4a6105a2020-11-17 15:11:05 -07003195// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3196// changes the "chaining" state, but to keep barriers independent. See discussion above.
John Zulauf14940722021-04-12 15:19:02 -06003197void ResourceAccessState::ApplyBarrier(const ResourceUsageTag scope_tag, const SyncBarrier &barrier, bool layout_transition) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003198 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3199 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3200 // in order to know if it's in the excecution scope
3201 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3202 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3203 // errors w.r.t. "most recent" accesses.
John Zulauf14940722021-04-12 15:19:02 -06003204 if (layout_transition || ((write_tag < scope_tag) && (barrier.src_access_scope & last_write).any())) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003205 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003206 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003207 if (layout_transition) {
3208 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3209 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003210 }
3211 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3212 pending_layout_transition |= layout_transition;
3213
3214 if (!pending_layout_transition) {
3215 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3216 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003217 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003218 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3219 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3220 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3221 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3222 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3223 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3224 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulauf14940722021-04-12 15:19:02 -06003225 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 -07003226 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003227 }
3228 }
3229 }
3230}
John Zulauf14940722021-04-12 15:19:02 -06003231void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag tag) {
John Zulauf89311b42020-09-29 16:28:47 -06003232 if (pending_layout_transition) {
John Zulauf4fa68462021-04-26 21:04:22 -06003233 // 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 -06003234 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07003235 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf4fa68462021-04-26 21:04:22 -06003236 TouchupFirstForLayoutTransition(tag, pending_layout_ordering_);
3237 pending_layout_ordering_ = OrderingBarrier();
John Zulauf89311b42020-09-29 16:28:47 -06003238 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003239 }
John Zulauf89311b42020-09-29 16:28:47 -06003240
3241 // Apply the accumulate execution barriers (and thus update chaining information)
John Zulauf4fa68462021-04-26 21:04:22 -06003242 // for layout transition, last_reads is reset by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003243 for (auto &read_access : last_reads) {
3244 read_access.barriers |= read_access.pending_dep_chain;
3245 read_execution_barriers |= read_access.barriers;
3246 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003247 }
3248
3249 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3250 write_dependency_chain |= pending_write_dep_chain;
3251 write_barriers |= pending_write_barriers;
3252 pending_write_dep_chain = 0;
3253 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003254}
3255
John Zulaufae842002021-04-15 18:20:55 -06003256bool ResourceAccessState::FirstAccessInTagRange(const ResourceUsageRange &tag_range) const {
3257 if (!first_accesses_.size()) return false;
3258 const ResourceUsageRange first_access_range = {first_accesses_.front().tag, first_accesses_.back().tag + 1};
3259 return tag_range.intersects(first_access_range);
3260}
3261
John Zulauf59e25072020-07-17 10:55:21 -06003262// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07003263VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
3264 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003265
John Zulaufab7756b2020-12-29 16:10:16 -07003266 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003267 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003268 barriers = read_access.barriers;
3269 break;
John Zulauf59e25072020-07-17 10:55:21 -06003270 }
3271 }
John Zulauf4285ee92020-09-23 10:20:52 -06003272
John Zulauf59e25072020-07-17 10:55:21 -06003273 return barriers;
3274}
3275
Jeremy Gebben40a22942020-12-22 14:22:06 -07003276inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003277 assert(IsRead(usage));
3278 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3279 // * the previous reads are not hazards, and thus last_write must be visible and available to
3280 // any reads that happen after.
3281 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3282 // 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 -07003283 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003284}
3285
Jeremy Gebben40a22942020-12-22 14:22:06 -07003286VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003287 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07003288 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06003289 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003290 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003291 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003292 // 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 -07003293 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06003294 }
3295
3296 return ordered_stages;
3297}
3298
John Zulauf14940722021-04-12 15:19:02 -06003299void ResourceAccessState::UpdateFirst(const ResourceUsageTag tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003300 // Only record until we record a write.
3301 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003302 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003303 if (0 == (usage_stage & first_read_stages_)) {
3304 // If this is a read we haven't seen or a write, record.
John Zulauf4fa68462021-04-26 21:04:22 -06003305 // We always need to know what stages were found prior to write
John Zulauffaea0ee2021-01-14 14:01:32 -07003306 first_read_stages_ |= usage_stage;
John Zulauf4fa68462021-04-26 21:04:22 -06003307 if (0 == (read_execution_barriers & usage_stage)) {
3308 // If this stage isn't masked then we add it (since writes map to usage_stage 0, this also records writes)
3309 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3310 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003311 }
3312 }
3313}
3314
John Zulauf4fa68462021-04-26 21:04:22 -06003315void ResourceAccessState::TouchupFirstForLayoutTransition(ResourceUsageTag tag, const OrderingBarrier &layout_ordering) {
3316 // Only call this after recording an image layout transition
3317 assert(first_accesses_.size());
3318 if (first_accesses_.back().tag == tag) {
3319 // If this layout transition is the the first write, add the additional ordering rules that guard the ILT
3320 assert(first_accesses_.back().usage_index = SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3321 first_write_layout_ordering_ = layout_ordering;
3322 }
3323}
3324
John Zulaufd1f85d42020-04-15 12:23:15 -06003325void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003326 auto *access_context = GetAccessContextNoInsert(command_buffer);
3327 if (access_context) {
3328 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003329 }
3330}
3331
John Zulaufd1f85d42020-04-15 12:23:15 -06003332void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3333 auto access_found = cb_access_state.find(command_buffer);
3334 if (access_found != cb_access_state.end()) {
3335 access_found->second->Reset();
John Zulauf4fa68462021-04-26 21:04:22 -06003336 access_found->second->MarkDestroyed();
John Zulaufd1f85d42020-04-15 12:23:15 -06003337 cb_access_state.erase(access_found);
3338 }
3339}
3340
John Zulauf9cb530d2019-09-30 14:14:10 -06003341bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3342 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3343 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003344 const auto *cb_context = GetAccessContext(commandBuffer);
3345 assert(cb_context);
3346 if (!cb_context) return skip;
3347 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003348
John Zulauf3d84f1b2020-03-09 13:33:25 -06003349 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003350 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003351 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003352
3353 for (uint32_t region = 0; region < regionCount; region++) {
3354 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003355 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003356 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003357 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003358 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003359 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003360 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003361 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003362 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003363 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003364 }
John Zulauf16adfc92020-04-08 10:28:33 -06003365 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003366 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003367 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003368 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003369 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003370 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003371 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003372 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003373 }
3374 }
3375 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003376 }
3377 return skip;
3378}
3379
3380void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3381 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003382 auto *cb_context = GetAccessContext(commandBuffer);
3383 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003384 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003385 auto *context = cb_context->GetCurrentAccessContext();
3386
John Zulauf9cb530d2019-09-30 14:14:10 -06003387 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003388 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003389
3390 for (uint32_t region = 0; region < regionCount; region++) {
3391 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003392 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003393 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003394 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003395 }
John Zulauf16adfc92020-04-08 10:28:33 -06003396 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003397 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003398 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003399 }
3400 }
3401}
3402
John Zulauf4a6105a2020-11-17 15:11:05 -07003403void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3404 // Clear out events from the command buffer contexts
3405 for (auto &cb_context : cb_access_state) {
3406 cb_context.second->RecordDestroyEvent(event);
3407 }
3408}
3409
Jeff Leger178b1e52020-10-05 12:22:23 -04003410bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3411 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3412 bool skip = false;
3413 const auto *cb_context = GetAccessContext(commandBuffer);
3414 assert(cb_context);
3415 if (!cb_context) return skip;
3416 const auto *context = cb_context->GetCurrentAccessContext();
3417
3418 // If we have no previous accesses, we have no hazards
3419 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3420 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3421
3422 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3423 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3424 if (src_buffer) {
3425 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003426 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003427 if (hazard.hazard) {
3428 // TODO -- add tag information to log msg when useful.
3429 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3430 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3431 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003432 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003433 }
3434 }
3435 if (dst_buffer && !skip) {
3436 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003437 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003438 if (hazard.hazard) {
3439 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3440 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3441 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003442 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003443 }
3444 }
3445 if (skip) break;
3446 }
3447 return skip;
3448}
3449
3450void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3451 auto *cb_context = GetAccessContext(commandBuffer);
3452 assert(cb_context);
3453 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3454 auto *context = cb_context->GetCurrentAccessContext();
3455
3456 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3457 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3458
3459 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3460 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3461 if (src_buffer) {
3462 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003463 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003464 }
3465 if (dst_buffer) {
3466 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003467 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003468 }
3469 }
3470}
3471
John Zulauf5c5e88d2019-12-26 11:22:02 -07003472bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3473 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3474 const VkImageCopy *pRegions) const {
3475 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003476 const auto *cb_access_context = GetAccessContext(commandBuffer);
3477 assert(cb_access_context);
3478 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003479
John Zulauf3d84f1b2020-03-09 13:33:25 -06003480 const auto *context = cb_access_context->GetCurrentAccessContext();
3481 assert(context);
3482 if (!context) return skip;
3483
3484 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3485 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003486 for (uint32_t region = 0; region < regionCount; region++) {
3487 const auto &copy_region = pRegions[region];
3488 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003489 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003490 copy_region.srcOffset, copy_region.extent);
3491 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003492 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003493 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003494 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003495 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003496 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003497 }
3498
3499 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003500 VkExtent3D dst_copy_extent =
3501 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003502 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003503 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003504 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003505 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003506 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003507 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003508 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003509 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003510 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003511 }
3512 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003513
John Zulauf5c5e88d2019-12-26 11:22:02 -07003514 return skip;
3515}
3516
3517void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3518 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3519 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003520 auto *cb_access_context = GetAccessContext(commandBuffer);
3521 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003522 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003523 auto *context = cb_access_context->GetCurrentAccessContext();
3524 assert(context);
3525
John Zulauf5c5e88d2019-12-26 11:22:02 -07003526 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003527 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003528
3529 for (uint32_t region = 0; region < regionCount; region++) {
3530 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003531 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003532 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003533 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003534 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003535 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003536 VkExtent3D dst_copy_extent =
3537 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003538 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003539 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003540 }
3541 }
3542}
3543
Jeff Leger178b1e52020-10-05 12:22:23 -04003544bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3545 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3546 bool skip = false;
3547 const auto *cb_access_context = GetAccessContext(commandBuffer);
3548 assert(cb_access_context);
3549 if (!cb_access_context) return skip;
3550
3551 const auto *context = cb_access_context->GetCurrentAccessContext();
3552 assert(context);
3553 if (!context) return skip;
3554
3555 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3556 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3557 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3558 const auto &copy_region = pCopyImageInfo->pRegions[region];
3559 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003560 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003561 copy_region.srcOffset, copy_region.extent);
3562 if (hazard.hazard) {
3563 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3564 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3565 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003566 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003567 }
3568 }
3569
3570 if (dst_image) {
3571 VkExtent3D dst_copy_extent =
3572 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003573 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003574 copy_region.dstOffset, dst_copy_extent);
3575 if (hazard.hazard) {
3576 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3577 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3578 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003579 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003580 }
3581 if (skip) break;
3582 }
3583 }
3584
3585 return skip;
3586}
3587
3588void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3589 auto *cb_access_context = GetAccessContext(commandBuffer);
3590 assert(cb_access_context);
3591 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3592 auto *context = cb_access_context->GetCurrentAccessContext();
3593 assert(context);
3594
3595 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3596 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3597
3598 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3599 const auto &copy_region = pCopyImageInfo->pRegions[region];
3600 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003601 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003602 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003603 }
3604 if (dst_image) {
3605 VkExtent3D dst_copy_extent =
3606 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003607 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003608 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003609 }
3610 }
3611}
3612
John Zulauf9cb530d2019-09-30 14:14:10 -06003613bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3614 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3615 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3616 uint32_t bufferMemoryBarrierCount,
3617 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3618 uint32_t imageMemoryBarrierCount,
3619 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3620 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003621 const auto *cb_access_context = GetAccessContext(commandBuffer);
3622 assert(cb_access_context);
3623 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003624
John Zulauf36ef9282021-02-02 11:47:24 -07003625 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3626 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3627 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3628 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003629 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003630 return skip;
3631}
3632
3633void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3634 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3635 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3636 uint32_t bufferMemoryBarrierCount,
3637 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3638 uint32_t imageMemoryBarrierCount,
3639 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003640 auto *cb_access_context = GetAccessContext(commandBuffer);
3641 assert(cb_access_context);
3642 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003643
John Zulauf1bf30522021-09-03 15:39:06 -06003644 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(),
3645 srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount,
3646 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
3647 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06003648}
3649
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003650bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3651 const VkDependencyInfoKHR *pDependencyInfo) const {
3652 bool skip = false;
3653 const auto *cb_access_context = GetAccessContext(commandBuffer);
3654 assert(cb_access_context);
3655 if (!cb_access_context) return skip;
3656
3657 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3658 skip = pipeline_barrier.Validate(*cb_access_context);
3659 return skip;
3660}
3661
3662void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3663 auto *cb_access_context = GetAccessContext(commandBuffer);
3664 assert(cb_access_context);
3665 if (!cb_access_context) return;
3666
John Zulauf1bf30522021-09-03 15:39:06 -06003667 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(),
3668 *pDependencyInfo);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003669}
3670
John Zulauf9cb530d2019-09-30 14:14:10 -06003671void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3672 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3673 // The state tracker sets up the device state
3674 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3675
John Zulauf5f13a792020-03-10 07:31:21 -06003676 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3677 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003678 // TODO: Find a good way to do this hooklessly.
3679 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3680 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3681 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3682
John Zulaufd1f85d42020-04-15 12:23:15 -06003683 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3684 sync_device_state->ResetCommandBufferCallback(command_buffer);
3685 });
3686 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3687 sync_device_state->FreeCommandBufferCallback(command_buffer);
3688 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003689}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003690
John Zulauf355e49b2020-04-24 15:11:15 -06003691bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003692 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003693 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003694 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003695 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003696 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003697 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003698 }
John Zulauf355e49b2020-04-24 15:11:15 -06003699 return skip;
3700}
3701
3702bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3703 VkSubpassContents contents) const {
3704 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003705 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003706 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003707 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003708 return skip;
3709}
3710
3711bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003712 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003713 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003714 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003715 return skip;
3716}
3717
3718bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3719 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003720 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003721 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003722 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003723 return skip;
3724}
3725
John Zulauf3d84f1b2020-03-09 13:33:25 -06003726void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3727 VkResult result) {
3728 // The state tracker sets up the command buffer state
3729 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3730
3731 // Create/initialize the structure that trackers accesses at the command buffer scope.
3732 auto cb_access_context = GetAccessContext(commandBuffer);
3733 assert(cb_access_context);
3734 cb_access_context->Reset();
3735}
3736
3737void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003738 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003739 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003740 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003741 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003742 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003743 }
3744}
3745
3746void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3747 VkSubpassContents contents) {
3748 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003749 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003750 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003751 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003752}
3753
3754void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3755 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3756 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003757 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003758}
3759
3760void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3761 const VkRenderPassBeginInfo *pRenderPassBegin,
3762 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3763 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003764 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003765}
3766
Mike Schuchardt2df08912020-12-15 16:28:09 -08003767bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003768 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003769 bool skip = false;
3770
3771 auto cb_context = GetAccessContext(commandBuffer);
3772 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003773 if (!cb_context) return skip;
sfricke-samsung85584a72021-09-30 21:43:38 -07003774 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003775 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003776}
3777
3778bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3779 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003780 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003781 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003782 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003783 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3784 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003785 return skip;
3786}
3787
Mike Schuchardt2df08912020-12-15 16:28:09 -08003788bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3789 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003790 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003791 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003792 return skip;
3793}
3794
3795bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3796 const VkSubpassEndInfo *pSubpassEndInfo) const {
3797 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003798 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003799 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003800}
3801
3802void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003803 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003804 auto cb_context = GetAccessContext(commandBuffer);
3805 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003806 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003807
sfricke-samsung85584a72021-09-30 21:43:38 -07003808 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003809 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003810}
3811
3812void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3813 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, 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 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003817}
3818
3819void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3820 const VkSubpassEndInfo *pSubpassEndInfo) {
3821 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003822 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003823}
3824
3825void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3826 const VkSubpassEndInfo *pSubpassEndInfo) {
3827 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003828 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003829}
3830
sfricke-samsung85584a72021-09-30 21:43:38 -07003831bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3832 CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003833 bool skip = false;
3834
3835 auto cb_context = GetAccessContext(commandBuffer);
3836 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003837 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003838
sfricke-samsung85584a72021-09-30 21:43:38 -07003839 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003840 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003841 return skip;
3842}
3843
3844bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3845 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003846 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003847 return skip;
3848}
3849
Mike Schuchardt2df08912020-12-15 16:28:09 -08003850bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003851 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003852 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003853 return skip;
3854}
3855
3856bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003857 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003858 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003859 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003860 return skip;
3861}
3862
sfricke-samsung85584a72021-09-30 21:43:38 -07003863void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003864 // Resolve the all subpass contexts to the command buffer contexts
3865 auto cb_context = GetAccessContext(commandBuffer);
3866 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003867 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003868
sfricke-samsung85584a72021-09-30 21:43:38 -07003869 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003870 sync_op.Record(cb_context);
3871 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003872}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003873
John Zulauf33fc1d52020-07-17 11:01:10 -06003874// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3875// updates to a resource which do not conflict at the byte level.
3876// TODO: Revisit this rule to see if it needs to be tighter or looser
3877// TODO: Add programatic control over suppression heuristics
3878bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3879 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3880}
3881
John Zulauf3d84f1b2020-03-09 13:33:25 -06003882void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003883 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003884 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003885}
3886
3887void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003888 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003889 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003890}
3891
3892void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003893 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf5a1a5382020-06-22 17:23:25 -06003894 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003895}
locke-lunarga19c71d2020-03-02 18:17:04 -07003896
Jeff Leger178b1e52020-10-05 12:22:23 -04003897template <typename BufferImageCopyRegionType>
3898bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3899 VkImageLayout dstImageLayout, uint32_t regionCount,
3900 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003901 bool skip = false;
3902 const auto *cb_access_context = GetAccessContext(commandBuffer);
3903 assert(cb_access_context);
3904 if (!cb_access_context) return skip;
3905
Jeff Leger178b1e52020-10-05 12:22:23 -04003906 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3907 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3908
locke-lunarga19c71d2020-03-02 18:17:04 -07003909 const auto *context = cb_access_context->GetCurrentAccessContext();
3910 assert(context);
3911 if (!context) return skip;
3912
3913 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003914 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3915
3916 for (uint32_t region = 0; region < regionCount; region++) {
3917 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003918 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003919 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003920 if (src_buffer) {
3921 ResourceAccessRange src_range =
3922 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003923 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07003924 if (hazard.hazard) {
3925 // PHASE1 TODO -- add tag information to log msg when useful.
3926 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3927 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3928 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003929 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003930 }
3931 }
3932
Jeremy Gebben40a22942020-12-22 14:22:06 -07003933 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07003934 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003935 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003936 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003937 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003938 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003939 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003940 }
3941 if (skip) break;
3942 }
3943 if (skip) break;
3944 }
3945 return skip;
3946}
3947
Jeff Leger178b1e52020-10-05 12:22:23 -04003948bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3949 VkImageLayout dstImageLayout, uint32_t regionCount,
3950 const VkBufferImageCopy *pRegions) const {
3951 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3952 COPY_COMMAND_VERSION_1);
3953}
3954
3955bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3956 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3957 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3958 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3959 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3960}
3961
3962template <typename BufferImageCopyRegionType>
3963void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3964 VkImageLayout dstImageLayout, uint32_t regionCount,
3965 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003966 auto *cb_access_context = GetAccessContext(commandBuffer);
3967 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003968
3969 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3970 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3971
3972 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003973 auto *context = cb_access_context->GetCurrentAccessContext();
3974 assert(context);
3975
3976 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003977 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003978
3979 for (uint32_t region = 0; region < regionCount; region++) {
3980 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07003981 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003982 if (src_buffer) {
3983 ResourceAccessRange src_range =
3984 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003985 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003986 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07003987 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003988 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003989 }
3990 }
3991}
3992
Jeff Leger178b1e52020-10-05 12:22:23 -04003993void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3994 VkImageLayout dstImageLayout, uint32_t regionCount,
3995 const VkBufferImageCopy *pRegions) {
3996 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3997 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3998}
3999
4000void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4001 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4002 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4003 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4004 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4005 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4006}
4007
4008template <typename BufferImageCopyRegionType>
4009bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4010 VkBuffer dstBuffer, uint32_t regionCount,
4011 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004012 bool skip = false;
4013 const auto *cb_access_context = GetAccessContext(commandBuffer);
4014 assert(cb_access_context);
4015 if (!cb_access_context) return skip;
4016
Jeff Leger178b1e52020-10-05 12:22:23 -04004017 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4018 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
4019
locke-lunarga19c71d2020-03-02 18:17:04 -07004020 const auto *context = cb_access_context->GetCurrentAccessContext();
4021 assert(context);
4022 if (!context) return skip;
4023
4024 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4025 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004026 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
locke-lunarga19c71d2020-03-02 18:17:04 -07004027 for (uint32_t region = 0; region < regionCount; region++) {
4028 const auto &copy_region = pRegions[region];
4029 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004030 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004031 copy_region.imageOffset, copy_region.imageExtent);
4032 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004033 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004034 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004035 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004036 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004037 }
John Zulauf477700e2021-01-06 11:41:49 -07004038 if (dst_mem) {
4039 ResourceAccessRange dst_range =
4040 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004041 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07004042 if (hazard.hazard) {
4043 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4044 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4045 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004046 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07004047 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004048 }
4049 }
4050 if (skip) break;
4051 }
4052 return skip;
4053}
4054
Jeff Leger178b1e52020-10-05 12:22:23 -04004055bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4056 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4057 const VkBufferImageCopy *pRegions) const {
4058 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
4059 COPY_COMMAND_VERSION_1);
4060}
4061
4062bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4063 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4064 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4065 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4066 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4067}
4068
4069template <typename BufferImageCopyRegionType>
4070void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4071 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
4072 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004073 auto *cb_access_context = GetAccessContext(commandBuffer);
4074 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004075
4076 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4077 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
4078
4079 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004080 auto *context = cb_access_context->GetCurrentAccessContext();
4081 assert(context);
4082
4083 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004084 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004085 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06004086 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004087
4088 for (uint32_t region = 0; region < regionCount; region++) {
4089 const auto &copy_region = pRegions[region];
4090 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004091 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004092 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004093 if (dst_buffer) {
4094 ResourceAccessRange dst_range =
4095 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004096 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004097 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004098 }
4099 }
4100}
4101
Jeff Leger178b1e52020-10-05 12:22:23 -04004102void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4103 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4104 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
4105 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4106}
4107
4108void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4109 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4110 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4111 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4112 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4113 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4114}
4115
4116template <typename RegionType>
4117bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4118 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4119 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004120 bool skip = false;
4121 const auto *cb_access_context = GetAccessContext(commandBuffer);
4122 assert(cb_access_context);
4123 if (!cb_access_context) return skip;
4124
4125 const auto *context = cb_access_context->GetCurrentAccessContext();
4126 assert(context);
4127 if (!context) return skip;
4128
4129 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4130 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4131
4132 for (uint32_t region = 0; region < regionCount; region++) {
4133 const auto &blit_region = pRegions[region];
4134 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004135 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4136 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4137 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4138 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4139 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4140 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004141 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004142 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004143 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004144 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004145 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004146 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004147 }
4148 }
4149
4150 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004151 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4152 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4153 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4154 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4155 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4156 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004157 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004158 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004159 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004160 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004161 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004162 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004163 }
4164 if (skip) break;
4165 }
4166 }
4167
4168 return skip;
4169}
4170
Jeff Leger178b1e52020-10-05 12:22:23 -04004171bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4172 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4173 const VkImageBlit *pRegions, VkFilter filter) const {
4174 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4175 "vkCmdBlitImage");
4176}
4177
4178bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4179 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4180 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4181 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4182 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4183}
4184
4185template <typename RegionType>
4186void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4187 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4188 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004189 auto *cb_access_context = GetAccessContext(commandBuffer);
4190 assert(cb_access_context);
4191 auto *context = cb_access_context->GetCurrentAccessContext();
4192 assert(context);
4193
4194 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004195 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004196
4197 for (uint32_t region = 0; region < regionCount; region++) {
4198 const auto &blit_region = pRegions[region];
4199 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004200 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4201 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4202 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4203 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4204 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4205 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004206 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004207 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004208 }
4209 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004210 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4211 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4212 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4213 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4214 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4215 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004216 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004217 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004218 }
4219 }
4220}
locke-lunarg36ba2592020-04-03 09:42:04 -06004221
Jeff Leger178b1e52020-10-05 12:22:23 -04004222void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4223 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4224 const VkImageBlit *pRegions, VkFilter filter) {
4225 auto *cb_access_context = GetAccessContext(commandBuffer);
4226 assert(cb_access_context);
4227 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4228 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4229 pRegions, filter);
4230 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4231}
4232
4233void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4234 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4235 auto *cb_access_context = GetAccessContext(commandBuffer);
4236 assert(cb_access_context);
4237 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4238 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4239 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4240 pBlitImageInfo->filter, tag);
4241}
4242
John Zulauffaea0ee2021-01-14 14:01:32 -07004243bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4244 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
4245 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
4246 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004247 bool skip = false;
4248 if (drawCount == 0) return skip;
4249
4250 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4251 VkDeviceSize size = struct_size;
4252 if (drawCount == 1 || stride == size) {
4253 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004254 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004255 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4256 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004257 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004258 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004259 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004260 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004261 }
4262 } else {
4263 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004264 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004265 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4266 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004267 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004268 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4269 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004270 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004271 break;
4272 }
4273 }
4274 }
4275 return skip;
4276}
4277
John Zulauf14940722021-04-12 15:19:02 -06004278void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag tag, const VkDeviceSize struct_size,
locke-lunarg61870c22020-06-09 14:51:50 -06004279 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4280 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06004281 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4282 VkDeviceSize size = struct_size;
4283 if (drawCount == 1 || stride == size) {
4284 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004285 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004286 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004287 } else {
4288 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004289 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004290 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4291 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004292 }
4293 }
4294}
4295
John Zulauffaea0ee2021-01-14 14:01:32 -07004296bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4297 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4298 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004299 bool skip = false;
4300
4301 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004302 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004303 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4304 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004305 skip |= LogError(count_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004306 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004307 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004308 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004309 }
4310 return skip;
4311}
4312
John Zulauf14940722021-04-12 15:19:02 -06004313void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06004314 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004315 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004316 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004317}
4318
locke-lunarg36ba2592020-04-03 09:42:04 -06004319bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004320 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004321 const auto *cb_access_context = GetAccessContext(commandBuffer);
4322 assert(cb_access_context);
4323 if (!cb_access_context) return skip;
4324
locke-lunarg61870c22020-06-09 14:51:50 -06004325 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004326 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004327}
4328
4329void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004330 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004331 auto *cb_access_context = GetAccessContext(commandBuffer);
4332 assert(cb_access_context);
4333 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004334
locke-lunarg61870c22020-06-09 14:51:50 -06004335 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004336}
locke-lunarge1a67022020-04-29 00:15:36 -06004337
4338bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004339 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004340 const auto *cb_access_context = GetAccessContext(commandBuffer);
4341 assert(cb_access_context);
4342 if (!cb_access_context) return skip;
4343
4344 const auto *context = cb_access_context->GetCurrentAccessContext();
4345 assert(context);
4346 if (!context) return skip;
4347
locke-lunarg61870c22020-06-09 14:51:50 -06004348 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004349 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4350 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004351 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004352}
4353
4354void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004355 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004356 auto *cb_access_context = GetAccessContext(commandBuffer);
4357 assert(cb_access_context);
4358 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4359 auto *context = cb_access_context->GetCurrentAccessContext();
4360 assert(context);
4361
locke-lunarg61870c22020-06-09 14:51:50 -06004362 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4363 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004364}
4365
4366bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4367 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004368 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004369 const auto *cb_access_context = GetAccessContext(commandBuffer);
4370 assert(cb_access_context);
4371 if (!cb_access_context) return skip;
4372
locke-lunarg61870c22020-06-09 14:51:50 -06004373 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4374 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4375 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004376 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004377}
4378
4379void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4380 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004381 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004382 auto *cb_access_context = GetAccessContext(commandBuffer);
4383 assert(cb_access_context);
4384 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004385
locke-lunarg61870c22020-06-09 14:51:50 -06004386 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4387 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4388 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004389}
4390
4391bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4392 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004393 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004394 const auto *cb_access_context = GetAccessContext(commandBuffer);
4395 assert(cb_access_context);
4396 if (!cb_access_context) return skip;
4397
locke-lunarg61870c22020-06-09 14:51:50 -06004398 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4399 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4400 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004401 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004402}
4403
4404void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4405 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004406 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004407 auto *cb_access_context = GetAccessContext(commandBuffer);
4408 assert(cb_access_context);
4409 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004410
locke-lunarg61870c22020-06-09 14:51:50 -06004411 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4412 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4413 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004414}
4415
4416bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4417 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004418 bool skip = false;
4419 if (drawCount == 0) return skip;
4420
locke-lunargff255f92020-05-13 18:53:52 -06004421 const auto *cb_access_context = GetAccessContext(commandBuffer);
4422 assert(cb_access_context);
4423 if (!cb_access_context) return skip;
4424
4425 const auto *context = cb_access_context->GetCurrentAccessContext();
4426 assert(context);
4427 if (!context) return skip;
4428
locke-lunarg61870c22020-06-09 14:51:50 -06004429 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4430 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004431 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4432 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004433
4434 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4435 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4436 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004437 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004438 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004439}
4440
4441void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4442 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004443 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004444 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004445 auto *cb_access_context = GetAccessContext(commandBuffer);
4446 assert(cb_access_context);
4447 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4448 auto *context = cb_access_context->GetCurrentAccessContext();
4449 assert(context);
4450
locke-lunarg61870c22020-06-09 14:51:50 -06004451 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4452 cb_access_context->RecordDrawSubpassAttachment(tag);
4453 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004454
4455 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4456 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4457 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004458 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004459}
4460
4461bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4462 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004463 bool skip = false;
4464 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004465 const auto *cb_access_context = GetAccessContext(commandBuffer);
4466 assert(cb_access_context);
4467 if (!cb_access_context) return skip;
4468
4469 const auto *context = cb_access_context->GetCurrentAccessContext();
4470 assert(context);
4471 if (!context) return skip;
4472
locke-lunarg61870c22020-06-09 14:51:50 -06004473 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4474 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004475 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4476 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004477
4478 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4479 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4480 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004481 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004482 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004483}
4484
4485void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4486 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004487 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004488 auto *cb_access_context = GetAccessContext(commandBuffer);
4489 assert(cb_access_context);
4490 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4491 auto *context = cb_access_context->GetCurrentAccessContext();
4492 assert(context);
4493
locke-lunarg61870c22020-06-09 14:51:50 -06004494 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4495 cb_access_context->RecordDrawSubpassAttachment(tag);
4496 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004497
4498 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4499 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4500 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004501 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004502}
4503
4504bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4505 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4506 uint32_t stride, const char *function) const {
4507 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004508 const auto *cb_access_context = GetAccessContext(commandBuffer);
4509 assert(cb_access_context);
4510 if (!cb_access_context) return skip;
4511
4512 const auto *context = cb_access_context->GetCurrentAccessContext();
4513 assert(context);
4514 if (!context) return skip;
4515
locke-lunarg61870c22020-06-09 14:51:50 -06004516 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4517 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004518 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4519 maxDrawCount, stride, function);
4520 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004521
4522 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4523 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4524 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004525 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004526 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004527}
4528
4529bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4530 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4531 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004532 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4533 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004534}
4535
sfricke-samsung85584a72021-09-30 21:43:38 -07004536void SyncValidator::RecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4537 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4538 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004539 auto *cb_access_context = GetAccessContext(commandBuffer);
4540 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004541 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004542 auto *context = cb_access_context->GetCurrentAccessContext();
4543 assert(context);
4544
locke-lunarg61870c22020-06-09 14:51:50 -06004545 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4546 cb_access_context->RecordDrawSubpassAttachment(tag);
4547 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4548 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004549
4550 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4551 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4552 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004553 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004554}
4555
sfricke-samsung85584a72021-09-30 21:43:38 -07004556void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4557 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4558 uint32_t stride) {
4559 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4560 stride);
4561 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4562 CMD_DRAWINDIRECTCOUNT);
4563}
locke-lunarge1a67022020-04-29 00:15:36 -06004564bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4565 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4566 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004567 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4568 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004569}
4570
4571void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4572 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4573 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004574 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4575 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004576 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4577 CMD_DRAWINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004578}
4579
4580bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4581 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4582 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004583 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4584 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004585}
4586
4587void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4588 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4589 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004590 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4591 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004592 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4593 CMD_DRAWINDIRECTCOUNTAMD);
locke-lunargff255f92020-05-13 18:53:52 -06004594}
4595
4596bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4597 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4598 uint32_t stride, const char *function) const {
4599 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004600 const auto *cb_access_context = GetAccessContext(commandBuffer);
4601 assert(cb_access_context);
4602 if (!cb_access_context) return skip;
4603
4604 const auto *context = cb_access_context->GetCurrentAccessContext();
4605 assert(context);
4606 if (!context) return skip;
4607
locke-lunarg61870c22020-06-09 14:51:50 -06004608 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4609 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004610 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4611 offset, maxDrawCount, stride, function);
4612 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004613
4614 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4615 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4616 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004617 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004618 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004619}
4620
4621bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4622 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4623 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004624 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4625 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004626}
4627
sfricke-samsung85584a72021-09-30 21:43:38 -07004628void SyncValidator::RecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4629 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4630 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004631 auto *cb_access_context = GetAccessContext(commandBuffer);
4632 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004633 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004634 auto *context = cb_access_context->GetCurrentAccessContext();
4635 assert(context);
4636
locke-lunarg61870c22020-06-09 14:51:50 -06004637 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4638 cb_access_context->RecordDrawSubpassAttachment(tag);
4639 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4640 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004641
4642 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4643 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004644 // We will update the index and vertex buffer in SubmitQueue in the future.
4645 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004646}
4647
sfricke-samsung85584a72021-09-30 21:43:38 -07004648void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4649 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4650 uint32_t maxDrawCount, uint32_t stride) {
4651 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4652 maxDrawCount, stride);
4653 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4654 CMD_DRAWINDEXEDINDIRECTCOUNT);
4655}
4656
locke-lunarge1a67022020-04-29 00:15:36 -06004657bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4658 VkDeviceSize offset, VkBuffer countBuffer,
4659 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4660 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004661 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4662 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004663}
4664
4665void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4666 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4667 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004668 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4669 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004670 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4671 CMD_DRAWINDEXEDINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004672}
4673
4674bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4675 VkDeviceSize offset, VkBuffer countBuffer,
4676 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4677 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004678 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4679 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004680}
4681
4682void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4683 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4684 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004685 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4686 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004687 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4688 CMD_DRAWINDEXEDINDIRECTCOUNTAMD);
locke-lunarge1a67022020-04-29 00:15:36 -06004689}
4690
4691bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4692 const VkClearColorValue *pColor, uint32_t rangeCount,
4693 const VkImageSubresourceRange *pRanges) const {
4694 bool skip = false;
4695 const auto *cb_access_context = GetAccessContext(commandBuffer);
4696 assert(cb_access_context);
4697 if (!cb_access_context) return skip;
4698
4699 const auto *context = cb_access_context->GetCurrentAccessContext();
4700 assert(context);
4701 if (!context) return skip;
4702
4703 const auto *image_state = Get<IMAGE_STATE>(image);
4704
4705 for (uint32_t index = 0; index < rangeCount; index++) {
4706 const auto &range = pRanges[index];
4707 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004708 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004709 if (hazard.hazard) {
4710 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004711 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004712 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004713 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004714 }
4715 }
4716 }
4717 return skip;
4718}
4719
4720void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4721 const VkClearColorValue *pColor, uint32_t rangeCount,
4722 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004723 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004724 auto *cb_access_context = GetAccessContext(commandBuffer);
4725 assert(cb_access_context);
4726 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4727 auto *context = cb_access_context->GetCurrentAccessContext();
4728 assert(context);
4729
4730 const auto *image_state = Get<IMAGE_STATE>(image);
4731
4732 for (uint32_t index = 0; index < rangeCount; index++) {
4733 const auto &range = pRanges[index];
4734 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004735 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004736 }
4737 }
4738}
4739
4740bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4741 VkImageLayout imageLayout,
4742 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4743 const VkImageSubresourceRange *pRanges) const {
4744 bool skip = false;
4745 const auto *cb_access_context = GetAccessContext(commandBuffer);
4746 assert(cb_access_context);
4747 if (!cb_access_context) return skip;
4748
4749 const auto *context = cb_access_context->GetCurrentAccessContext();
4750 assert(context);
4751 if (!context) return skip;
4752
4753 const auto *image_state = Get<IMAGE_STATE>(image);
4754
4755 for (uint32_t index = 0; index < rangeCount; index++) {
4756 const auto &range = pRanges[index];
4757 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004758 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004759 if (hazard.hazard) {
4760 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004761 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004762 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004763 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004764 }
4765 }
4766 }
4767 return skip;
4768}
4769
4770void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4771 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4772 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004773 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004774 auto *cb_access_context = GetAccessContext(commandBuffer);
4775 assert(cb_access_context);
4776 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4777 auto *context = cb_access_context->GetCurrentAccessContext();
4778 assert(context);
4779
4780 const auto *image_state = Get<IMAGE_STATE>(image);
4781
4782 for (uint32_t index = 0; index < rangeCount; index++) {
4783 const auto &range = pRanges[index];
4784 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004785 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004786 }
4787 }
4788}
4789
4790bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4791 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4792 VkDeviceSize dstOffset, VkDeviceSize stride,
4793 VkQueryResultFlags flags) const {
4794 bool skip = false;
4795 const auto *cb_access_context = GetAccessContext(commandBuffer);
4796 assert(cb_access_context);
4797 if (!cb_access_context) return skip;
4798
4799 const auto *context = cb_access_context->GetCurrentAccessContext();
4800 assert(context);
4801 if (!context) return skip;
4802
4803 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4804
4805 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004806 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004807 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004808 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004809 skip |=
4810 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4811 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004812 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004813 }
4814 }
locke-lunargff255f92020-05-13 18:53:52 -06004815
4816 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004817 return skip;
4818}
4819
4820void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4821 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4822 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004823 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4824 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004825 auto *cb_access_context = GetAccessContext(commandBuffer);
4826 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004827 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004828 auto *context = cb_access_context->GetCurrentAccessContext();
4829 assert(context);
4830
4831 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4832
4833 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004834 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004835 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004836 }
locke-lunargff255f92020-05-13 18:53:52 -06004837
4838 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004839}
4840
4841bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4842 VkDeviceSize size, uint32_t data) const {
4843 bool skip = false;
4844 const auto *cb_access_context = GetAccessContext(commandBuffer);
4845 assert(cb_access_context);
4846 if (!cb_access_context) return skip;
4847
4848 const auto *context = cb_access_context->GetCurrentAccessContext();
4849 assert(context);
4850 if (!context) return skip;
4851
4852 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4853
4854 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004855 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004856 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004857 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004858 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004859 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004860 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004861 }
4862 }
4863 return skip;
4864}
4865
4866void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4867 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004868 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004869 auto *cb_access_context = GetAccessContext(commandBuffer);
4870 assert(cb_access_context);
4871 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4872 auto *context = cb_access_context->GetCurrentAccessContext();
4873 assert(context);
4874
4875 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4876
4877 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004878 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004879 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004880 }
4881}
4882
4883bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4884 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4885 const VkImageResolve *pRegions) const {
4886 bool skip = false;
4887 const auto *cb_access_context = GetAccessContext(commandBuffer);
4888 assert(cb_access_context);
4889 if (!cb_access_context) return skip;
4890
4891 const auto *context = cb_access_context->GetCurrentAccessContext();
4892 assert(context);
4893 if (!context) return skip;
4894
4895 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4896 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4897
4898 for (uint32_t region = 0; region < regionCount; region++) {
4899 const auto &resolve_region = pRegions[region];
4900 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004901 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004902 resolve_region.srcOffset, resolve_region.extent);
4903 if (hazard.hazard) {
4904 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004905 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004906 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004907 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004908 }
4909 }
4910
4911 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004912 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004913 resolve_region.dstOffset, resolve_region.extent);
4914 if (hazard.hazard) {
4915 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004916 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004917 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004918 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004919 }
4920 if (skip) break;
4921 }
4922 }
4923
4924 return skip;
4925}
4926
4927void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4928 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4929 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004930 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4931 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004932 auto *cb_access_context = GetAccessContext(commandBuffer);
4933 assert(cb_access_context);
4934 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4935 auto *context = cb_access_context->GetCurrentAccessContext();
4936 assert(context);
4937
4938 auto *src_image = Get<IMAGE_STATE>(srcImage);
4939 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4940
4941 for (uint32_t region = 0; region < regionCount; region++) {
4942 const auto &resolve_region = pRegions[region];
4943 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004944 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004945 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004946 }
4947 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004948 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004949 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004950 }
4951 }
4952}
4953
Jeff Leger178b1e52020-10-05 12:22:23 -04004954bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4955 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4956 bool skip = false;
4957 const auto *cb_access_context = GetAccessContext(commandBuffer);
4958 assert(cb_access_context);
4959 if (!cb_access_context) return skip;
4960
4961 const auto *context = cb_access_context->GetCurrentAccessContext();
4962 assert(context);
4963 if (!context) return skip;
4964
4965 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4966 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4967
4968 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4969 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4970 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004971 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004972 resolve_region.srcOffset, resolve_region.extent);
4973 if (hazard.hazard) {
4974 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4975 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4976 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004977 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004978 }
4979 }
4980
4981 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004982 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004983 resolve_region.dstOffset, resolve_region.extent);
4984 if (hazard.hazard) {
4985 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4986 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4987 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004988 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004989 }
4990 if (skip) break;
4991 }
4992 }
4993
4994 return skip;
4995}
4996
4997void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4998 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4999 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5000 auto *cb_access_context = GetAccessContext(commandBuffer);
5001 assert(cb_access_context);
5002 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
5003 auto *context = cb_access_context->GetCurrentAccessContext();
5004 assert(context);
5005
5006 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5007 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5008
5009 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5010 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5011 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005012 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005013 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005014 }
5015 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005016 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005017 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005018 }
5019 }
5020}
5021
locke-lunarge1a67022020-04-29 00:15:36 -06005022bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5023 VkDeviceSize dataSize, const void *pData) const {
5024 bool skip = false;
5025 const auto *cb_access_context = GetAccessContext(commandBuffer);
5026 assert(cb_access_context);
5027 if (!cb_access_context) return skip;
5028
5029 const auto *context = cb_access_context->GetCurrentAccessContext();
5030 assert(context);
5031 if (!context) return skip;
5032
5033 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5034
5035 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005036 // VK_WHOLE_SIZE not allowed
5037 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005038 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005039 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005040 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005041 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005042 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005043 }
5044 }
5045 return skip;
5046}
5047
5048void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5049 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005050 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005051 auto *cb_access_context = GetAccessContext(commandBuffer);
5052 assert(cb_access_context);
5053 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5054 auto *context = cb_access_context->GetCurrentAccessContext();
5055 assert(context);
5056
5057 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5058
5059 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005060 // VK_WHOLE_SIZE not allowed
5061 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005062 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005063 }
5064}
locke-lunargff255f92020-05-13 18:53:52 -06005065
5066bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5067 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5068 bool skip = false;
5069 const auto *cb_access_context = GetAccessContext(commandBuffer);
5070 assert(cb_access_context);
5071 if (!cb_access_context) return skip;
5072
5073 const auto *context = cb_access_context->GetCurrentAccessContext();
5074 assert(context);
5075 if (!context) return skip;
5076
5077 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5078
5079 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005080 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005081 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06005082 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005083 skip |=
5084 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5085 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005086 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005087 }
5088 }
5089 return skip;
5090}
5091
5092void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5093 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005094 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005095 auto *cb_access_context = GetAccessContext(commandBuffer);
5096 assert(cb_access_context);
5097 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5098 auto *context = cb_access_context->GetCurrentAccessContext();
5099 assert(context);
5100
5101 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5102
5103 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005104 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005105 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06005106 }
5107}
John Zulauf49beb112020-11-04 16:06:31 -07005108
5109bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5110 bool skip = false;
5111 const auto *cb_context = GetAccessContext(commandBuffer);
5112 assert(cb_context);
5113 if (!cb_context) return skip;
5114
John Zulauf36ef9282021-02-02 11:47:24 -07005115 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005116 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005117}
5118
5119void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5120 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5121 auto *cb_context = GetAccessContext(commandBuffer);
5122 assert(cb_context);
5123 if (!cb_context) return;
John Zulauf1bf30522021-09-03 15:39:06 -06005124 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005125}
5126
John Zulauf4edde622021-02-15 08:54:50 -07005127bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5128 const VkDependencyInfoKHR *pDependencyInfo) const {
5129 bool skip = false;
5130 const auto *cb_context = GetAccessContext(commandBuffer);
5131 assert(cb_context);
5132 if (!cb_context || !pDependencyInfo) return skip;
5133
5134 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5135 return set_event_op.Validate(*cb_context);
5136}
5137
5138void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5139 const VkDependencyInfoKHR *pDependencyInfo) {
5140 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
5141 auto *cb_context = GetAccessContext(commandBuffer);
5142 assert(cb_context);
5143 if (!cb_context || !pDependencyInfo) return;
5144
John Zulauf1bf30522021-09-03 15:39:06 -06005145 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
John Zulauf4edde622021-02-15 08:54:50 -07005146}
5147
John Zulauf49beb112020-11-04 16:06:31 -07005148bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5149 VkPipelineStageFlags stageMask) const {
5150 bool skip = false;
5151 const auto *cb_context = GetAccessContext(commandBuffer);
5152 assert(cb_context);
5153 if (!cb_context) return skip;
5154
John Zulauf36ef9282021-02-02 11:47:24 -07005155 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005156 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005157}
5158
5159void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5160 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5161 auto *cb_context = GetAccessContext(commandBuffer);
5162 assert(cb_context);
5163 if (!cb_context) return;
5164
John Zulauf1bf30522021-09-03 15:39:06 -06005165 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005166}
5167
John Zulauf4edde622021-02-15 08:54:50 -07005168bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5169 VkPipelineStageFlags2KHR stageMask) const {
5170 bool skip = false;
5171 const auto *cb_context = GetAccessContext(commandBuffer);
5172 assert(cb_context);
5173 if (!cb_context) return skip;
5174
5175 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
5176 return reset_event_op.Validate(*cb_context);
5177}
5178
5179void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5180 VkPipelineStageFlags2KHR stageMask) {
5181 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
5182 auto *cb_context = GetAccessContext(commandBuffer);
5183 assert(cb_context);
5184 if (!cb_context) return;
5185
John Zulauf1bf30522021-09-03 15:39:06 -06005186 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf4edde622021-02-15 08:54:50 -07005187}
5188
John Zulauf49beb112020-11-04 16:06:31 -07005189bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5190 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5191 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5192 uint32_t bufferMemoryBarrierCount,
5193 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5194 uint32_t imageMemoryBarrierCount,
5195 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5196 bool skip = false;
5197 const auto *cb_context = GetAccessContext(commandBuffer);
5198 assert(cb_context);
5199 if (!cb_context) return skip;
5200
John Zulauf36ef9282021-02-02 11:47:24 -07005201 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
5202 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
5203 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07005204 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005205}
5206
5207void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5208 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5209 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5210 uint32_t bufferMemoryBarrierCount,
5211 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5212 uint32_t imageMemoryBarrierCount,
5213 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5214 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5215 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5216 imageMemoryBarrierCount, pImageMemoryBarriers);
5217
5218 auto *cb_context = GetAccessContext(commandBuffer);
5219 assert(cb_context);
5220 if (!cb_context) return;
5221
John Zulauf1bf30522021-09-03 15:39:06 -06005222 cb_context->RecordSyncOp<SyncOpWaitEvents>(
John Zulauf610e28c2021-08-03 17:46:23 -06005223 CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
John Zulauf1bf30522021-09-03 15:39:06 -06005224 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf4a6105a2020-11-17 15:11:05 -07005225}
5226
John Zulauf4edde622021-02-15 08:54:50 -07005227bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5228 const VkDependencyInfoKHR *pDependencyInfos) const {
5229 bool skip = false;
5230 const auto *cb_context = GetAccessContext(commandBuffer);
5231 assert(cb_context);
5232 if (!cb_context) return skip;
5233
5234 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5235 skip |= wait_events_op.Validate(*cb_context);
5236 return skip;
5237}
5238
5239void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5240 const VkDependencyInfoKHR *pDependencyInfos) {
5241 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5242
5243 auto *cb_context = GetAccessContext(commandBuffer);
5244 assert(cb_context);
5245 if (!cb_context) return;
5246
John Zulauf1bf30522021-09-03 15:39:06 -06005247 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5248 pDependencyInfos);
John Zulauf4edde622021-02-15 08:54:50 -07005249}
5250
John Zulauf4a6105a2020-11-17 15:11:05 -07005251void SyncEventState::ResetFirstScope() {
5252 for (const auto address_type : kAddressTypes) {
5253 first_scope[static_cast<size_t>(address_type)].clear();
5254 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005255 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07005256}
5257
5258// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07005259SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005260 IgnoreReason reason = NotIgnored;
5261
John Zulauf4edde622021-02-15 08:54:50 -07005262 if ((CMD_WAITEVENTS2KHR == cmd) && (CMD_SETEVENT == last_command)) {
5263 reason = SetVsWait2;
5264 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
5265 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07005266 } else if (unsynchronized_set) {
5267 reason = SetRace;
5268 } else {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005269 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005270 if (missing_bits) reason = MissingStageBits;
5271 }
5272
5273 return reason;
5274}
5275
Jeremy Gebben40a22942020-12-22 14:22:06 -07005276bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005277 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5278 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5279 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005280}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005281
John Zulauf36ef9282021-02-02 11:47:24 -07005282SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5283 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5284 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005285 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5286 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5287 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07005288 : SyncOpBase(cmd), barriers_(1) {
5289 auto &barrier_set = barriers_[0];
5290 barrier_set.dependency_flags = dependencyFlags;
5291 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
5292 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005293 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07005294 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
5295 pMemoryBarriers);
5296 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5297 bufferMemoryBarrierCount, pBufferMemoryBarriers);
5298 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5299 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005300}
5301
John Zulauf4edde622021-02-15 08:54:50 -07005302SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
5303 const VkDependencyInfoKHR *dep_infos)
5304 : SyncOpBase(cmd), barriers_(event_count) {
5305 for (uint32_t i = 0; i < event_count; i++) {
5306 const auto &dep_info = dep_infos[i];
5307 auto &barrier_set = barriers_[i];
5308 barrier_set.dependency_flags = dep_info.dependencyFlags;
5309 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5310 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5311 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5312 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5313 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5314 dep_info.pMemoryBarriers);
5315 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5316 dep_info.pBufferMemoryBarriers);
5317 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5318 dep_info.pImageMemoryBarriers);
5319 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005320}
5321
John Zulauf36ef9282021-02-02 11:47:24 -07005322SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005323 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5324 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5325 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5326 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5327 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005328 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005329 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5330
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005331SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5332 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005333 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005334
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005335bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5336 bool skip = false;
5337 const auto *context = cb_context.GetCurrentAccessContext();
5338 assert(context);
5339 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005340 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5341
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005342 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005343 const auto &barrier_set = barriers_[0];
5344 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5345 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5346 const auto *image_state = image_barrier.image.get();
5347 if (!image_state) continue;
5348 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5349 if (hazard.hazard) {
5350 // PHASE1 TODO -- add tag information to log msg when useful.
5351 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005352 const auto image_handle = image_state->image();
John Zulauf6fdf3d02021-03-05 16:50:47 -07005353 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5354 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5355 string_SyncHazard(hazard.hazard), image_barrier.index,
5356 sync_state.report_data->FormatHandle(image_handle).c_str(),
5357 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005358 }
5359 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005360 return skip;
5361}
5362
John Zulaufd5115702021-01-18 12:34:33 -07005363struct SyncOpPipelineBarrierFunctorFactory {
5364 using BarrierOpFunctor = PipelineBarrierOp;
5365 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5366 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5367 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5368 using BufferRange = ResourceAccessRange;
5369 using ImageRange = subresource_adapter::ImageRangeGenerator;
5370 using GlobalRange = ResourceAccessRange;
5371
5372 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5373 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5374 }
John Zulauf14940722021-04-12 15:19:02 -06005375 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005376 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5377 }
5378 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5379 return GlobalBarrierOpFunctor(barrier, false);
5380 }
5381
5382 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5383 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5384 const auto base_address = ResourceBaseAddress(buffer);
5385 return (range + base_address);
5386 }
John Zulauf110413c2021-03-20 05:38:38 -06005387 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005388 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005389
5390 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005391 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005392 return range_gen;
5393 }
5394 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5395};
5396
5397template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005398void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005399 AccessContext *context) {
5400 for (const auto &barrier : barriers) {
5401 const auto *state = barrier.GetState();
5402 if (state) {
5403 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5404 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5405 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5406 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5407 }
5408 }
5409}
5410
5411template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005412void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005413 AccessContext *access_context) {
5414 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5415 for (const auto &barrier : barriers) {
5416 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5417 }
5418 for (const auto address_type : kAddressTypes) {
5419 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5420 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5421 }
5422}
5423
John Zulauf8eda1562021-04-13 17:06:41 -06005424ResourceUsageTag SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005425 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf8eda1562021-04-13 17:06:41 -06005426 auto *events_context = cb_context->GetCurrentEventsContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005427 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf4fa68462021-04-26 21:04:22 -06005428 DoRecord(tag, access_context, events_context);
5429 return tag;
5430}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005431
John Zulauf4fa68462021-04-26 21:04:22 -06005432void SyncOpPipelineBarrier::DoRecord(const ResourceUsageTag tag, AccessContext *access_context,
5433 SyncEventsContext *events_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06005434 SyncOpPipelineBarrierFunctorFactory factory;
John Zulauf4edde622021-02-15 08:54:50 -07005435 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5436 assert(barriers_.size() == 1);
5437 const auto &barrier_set = barriers_[0];
5438 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5439 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5440 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulauf4edde622021-02-15 08:54:50 -07005441 if (barrier_set.single_exec_scope) {
John Zulauf8eda1562021-04-13 17:06:41 -06005442 events_context->ApplyBarrier(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005443 } else {
5444 for (const auto &barrier : barrier_set.memory_barriers) {
John Zulauf8eda1562021-04-13 17:06:41 -06005445 events_context->ApplyBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005446 }
5447 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005448}
5449
John Zulauf8eda1562021-04-13 17:06:41 -06005450bool SyncOpPipelineBarrier::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06005451 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf4fa68462021-04-26 21:04:22 -06005452 // No Validation for replay, as the layout transition accesses are checked directly, and the src*Mask ordering is captured
5453 // with first access information.
John Zulauf8eda1562021-04-13 17:06:41 -06005454 return false;
5455}
5456
John Zulauf4edde622021-02-15 08:54:50 -07005457void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5458 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5459 const VkMemoryBarrier *barriers) {
5460 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005461 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005462 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005463 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005464 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005465 }
5466 if (0 == memory_barrier_count) {
5467 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005468 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005469 }
John Zulauf4edde622021-02-15 08:54:50 -07005470 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005471}
5472
John Zulauf4edde622021-02-15 08:54:50 -07005473void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5474 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5475 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5476 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005477 for (uint32_t index = 0; index < barrier_count; index++) {
5478 const auto &barrier = barriers[index];
5479 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5480 if (buffer) {
5481 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5482 const auto range = MakeRange(barrier.offset, barrier_size);
5483 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005484 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005485 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005486 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005487 }
5488 }
5489}
5490
John Zulauf4edde622021-02-15 08:54:50 -07005491void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
5492 uint32_t memory_barrier_count, const VkMemoryBarrier2KHR *barriers) {
5493 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005494 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005495 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005496 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5497 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5498 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005499 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005500 }
John Zulauf4edde622021-02-15 08:54:50 -07005501 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005502}
5503
John Zulauf4edde622021-02-15 08:54:50 -07005504void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5505 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5506 const VkBufferMemoryBarrier2KHR *barriers) {
5507 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005508 for (uint32_t index = 0; index < barrier_count; index++) {
5509 const auto &barrier = barriers[index];
5510 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5511 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5512 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5513 if (buffer) {
5514 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5515 const auto range = MakeRange(barrier.offset, barrier_size);
5516 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005517 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005518 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005519 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005520 }
5521 }
5522}
5523
John Zulauf4edde622021-02-15 08:54:50 -07005524void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5525 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5526 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5527 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005528 for (uint32_t index = 0; index < barrier_count; index++) {
5529 const auto &barrier = barriers[index];
5530 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5531 if (image) {
5532 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5533 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005534 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005535 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005536 image_memory_barriers.emplace_back();
5537 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005538 }
5539 }
5540}
John Zulaufd5115702021-01-18 12:34:33 -07005541
John Zulauf4edde622021-02-15 08:54:50 -07005542void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5543 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5544 const VkImageMemoryBarrier2KHR *barriers) {
5545 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005546 for (uint32_t index = 0; index < barrier_count; index++) {
5547 const auto &barrier = barriers[index];
5548 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5549 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5550 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5551 if (image) {
5552 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5553 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005554 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005555 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005556 image_memory_barriers.emplace_back();
5557 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005558 }
5559 }
5560}
5561
John Zulauf36ef9282021-02-02 11:47:24 -07005562SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005563 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5564 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5565 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5566 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005567 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005568 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5569 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005570 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005571}
5572
John Zulauf4edde622021-02-15 08:54:50 -07005573SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5574 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5575 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5576 MakeEventsList(sync_state, eventCount, pEvents);
5577 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5578}
5579
John Zulauf610e28c2021-08-03 17:46:23 -06005580const char *const SyncOpWaitEvents::kIgnored = "Wait operation is ignored for this event.";
5581
John Zulaufd5115702021-01-18 12:34:33 -07005582bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005583 bool skip = false;
5584 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005585 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005586
John Zulauf610e28c2021-08-03 17:46:23 -06005587 // This is only interesting at record and not replay (Execute/Submit) time.
John Zulauf4edde622021-02-15 08:54:50 -07005588 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5589 const auto &barrier_set = barriers_[barrier_set_index];
5590 if (barrier_set.single_exec_scope) {
5591 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5592 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5593 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5594 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5595 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5596 } else {
5597 const auto &barriers = barrier_set.memory_barriers;
5598 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5599 const auto &barrier = barriers[barrier_index];
5600 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5601 const std::string vuid =
5602 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5603 skip =
5604 sync_state.LogInfo(command_buffer_handle, vuid,
5605 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5606 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5607 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5608 }
5609 }
5610 }
5611 }
John Zulaufd5115702021-01-18 12:34:33 -07005612 }
5613
John Zulauf610e28c2021-08-03 17:46:23 -06005614 // The rest is common to record time and replay time.
5615 skip |= DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
5616 return skip;
5617}
5618
5619bool SyncOpWaitEvents::DoValidate(const CommandBufferAccessContext &cb_context, const ResourceUsageTag base_tag) const {
5620 bool skip = false;
5621 const auto &sync_state = cb_context.GetSyncState();
5622
Jeremy Gebben40a22942020-12-22 14:22:06 -07005623 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005624 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005625 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005626 const auto *events_context = cb_context.GetCurrentEventsContext();
5627 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005628 size_t barrier_set_index = 0;
5629 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
John Zulauf78394fc2021-07-12 15:41:40 -06005630 for (const auto &event : events_) {
5631 const auto *sync_event = events_context->Get(event.get());
5632 const auto &barrier_set = barriers_[barrier_set_index];
5633 if (!sync_event) {
5634 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5635 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5636 // new validation error... wait without previously submitted set event...
5637 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 -07005638 barrier_set_index += barrier_set_incr;
John Zulauf78394fc2021-07-12 15:41:40 -06005639 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005640 }
John Zulauf610e28c2021-08-03 17:46:23 -06005641
5642 // For replay calls, don't revalidate "same command buffer" events
5643 if (sync_event->last_command_tag > base_tag) continue;
5644
John Zulauf78394fc2021-07-12 15:41:40 -06005645 const auto event_handle = sync_event->event->event();
5646 // TODO add "destroyed" checks
5647
5648 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5649 const auto &src_exec_scope = barrier_set.src_exec_scope;
5650 event_stage_masks |= sync_event->scope.mask_param;
5651 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5652 if (ignore_reason) {
5653 switch (ignore_reason) {
5654 case SyncEventState::ResetWaitRace:
5655 case SyncEventState::Reset2WaitRace: {
5656 // Four permuations of Reset and Wait calls...
5657 const char *vuid =
5658 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5659 if (ignore_reason == SyncEventState::Reset2WaitRace) {
5660 vuid = (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2KHR-event-03831"
5661 : "VUID-vkCmdResetEvent2KHR-event-03832";
5662 }
5663 const char *const message =
5664 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5665 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5666 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
John Zulauf610e28c2021-08-03 17:46:23 -06005667 CommandTypeString(sync_event->last_command), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005668 break;
5669 }
5670 case SyncEventState::SetRace: {
5671 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5672 // this event
5673 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5674 const char *const message =
5675 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5676 const char *const reason = "First synchronization scope is undefined.";
5677 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5678 sync_state.report_data->FormatHandle(event_handle).c_str(),
John Zulauf610e28c2021-08-03 17:46:23 -06005679 CommandTypeString(sync_event->last_command), reason, kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005680 break;
5681 }
5682 case SyncEventState::MissingStageBits: {
5683 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5684 // Issue error message that event waited for is not in wait events scope
5685 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5686 const char *const message = "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5687 ". Bits missing from srcStageMask %s. %s";
5688 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5689 sync_state.report_data->FormatHandle(event_handle).c_str(),
5690 sync_event->scope.mask_param, src_exec_scope.mask_param,
John Zulauf610e28c2021-08-03 17:46:23 -06005691 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005692 break;
5693 }
5694 case SyncEventState::SetVsWait2: {
5695 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2KHR-pEvents-03837",
5696 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5697 sync_state.report_data->FormatHandle(event_handle).c_str(),
5698 CommandTypeString(sync_event->last_command));
5699 break;
5700 }
5701 default:
5702 assert(ignore_reason == SyncEventState::NotIgnored);
5703 }
5704 } else if (barrier_set.image_memory_barriers.size()) {
5705 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5706 const auto *context = cb_context.GetCurrentAccessContext();
5707 assert(context);
5708 for (const auto &image_memory_barrier : image_memory_barriers) {
5709 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5710 const auto *image_state = image_memory_barrier.image.get();
5711 if (!image_state) continue;
5712 const auto &subresource_range = image_memory_barrier.range;
5713 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5714 const auto hazard =
5715 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5716 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5717 if (hazard.hazard) {
5718 skip |= sync_state.LogError(image_state->image(), string_SyncHazardVUID(hazard.hazard),
5719 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5720 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
5721 sync_state.report_data->FormatHandle(image_state->image()).c_str(),
5722 cb_context.FormatUsage(hazard).c_str());
5723 break;
5724 }
5725 }
5726 }
5727 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5728 // 03839
5729 barrier_set_index += barrier_set_incr;
5730 }
John Zulaufd5115702021-01-18 12:34:33 -07005731
5732 // 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 -07005733 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 -07005734 if (extra_stage_bits) {
5735 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005736 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5737 const char *const vuid =
5738 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2KHR-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005739 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005740 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulauf610e28c2021-08-03 17:46:23 -06005741 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005742 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005743 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005744 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005745 " vkCmdSetEvent may be in previously submitted command buffer.");
5746 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005747 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005748 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005749 }
5750 }
5751 return skip;
5752}
5753
5754struct SyncOpWaitEventsFunctorFactory {
5755 using BarrierOpFunctor = WaitEventBarrierOp;
5756 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5757 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5758 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5759 using BufferRange = EventSimpleRangeGenerator;
5760 using ImageRange = EventImageRangeGenerator;
5761 using GlobalRange = EventSimpleRangeGenerator;
5762
5763 // Need to restrict to only valid exec and access scope for this event
5764 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5765 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005766 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005767 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5768 return barrier;
5769 }
5770 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5771 auto barrier = RestrictToEvent(barrier_arg);
5772 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5773 }
John Zulauf14940722021-04-12 15:19:02 -06005774 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005775 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5776 }
5777 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5778 auto barrier = RestrictToEvent(barrier_arg);
5779 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5780 }
5781
5782 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5783 const AccessAddressType address_type = GetAccessAddressType(buffer);
5784 const auto base_address = ResourceBaseAddress(buffer);
5785 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5786 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5787 return filtered_range_gen;
5788 }
John Zulauf110413c2021-03-20 05:38:38 -06005789 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07005790 if (!SimpleBinding(image)) return ImageRange();
5791 const auto address_type = GetAccessAddressType(image);
5792 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005793 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005794 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5795
5796 return filtered_range_gen;
5797 }
5798 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5799 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5800 }
5801 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5802 SyncEventState *sync_event;
5803};
5804
John Zulauf8eda1562021-04-13 17:06:41 -06005805ResourceUsageTag SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07005806 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07005807 auto *access_context = cb_context->GetCurrentAccessContext();
5808 assert(access_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005809 if (!access_context) return tag;
John Zulauf669dfd52021-01-27 17:15:28 -07005810 auto *events_context = cb_context->GetCurrentEventsContext();
5811 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005812 if (!events_context) return tag;
John Zulaufd5115702021-01-18 12:34:33 -07005813
John Zulauf610e28c2021-08-03 17:46:23 -06005814 DoRecord(tag, access_context, events_context);
5815 return tag;
5816}
5817
5818void SyncOpWaitEvents::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005819 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
5820 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
5821 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
5822 access_context->ResolvePreviousAccesses();
5823
John Zulauf4edde622021-02-15 08:54:50 -07005824 size_t barrier_set_index = 0;
5825 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5826 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07005827 for (auto &event_shared : events_) {
5828 if (!event_shared.get()) continue;
5829 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07005830
John Zulauf4edde622021-02-15 08:54:50 -07005831 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06005832 sync_event->last_command_tag = tag;
John Zulaufd5115702021-01-18 12:34:33 -07005833
John Zulauf4edde622021-02-15 08:54:50 -07005834 const auto &barrier_set = barriers_[barrier_set_index];
5835 const auto &dst = barrier_set.dst_exec_scope;
5836 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07005837 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
5838 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
5839 // of the barriers is maintained.
5840 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07005841 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5842 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5843 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07005844
5845 // Apply the global barrier to the event itself (for race condition tracking)
5846 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
5847 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
5848 sync_event->barriers |= dst.exec_scope;
5849 } else {
5850 // We ignored this wait, so we don't have any effective synchronization barriers for it.
5851 sync_event->barriers = 0U;
5852 }
John Zulauf4edde622021-02-15 08:54:50 -07005853 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005854 }
5855
5856 // Apply the pending barriers
5857 ResolvePendingBarrierFunctor apply_pending_action(tag);
5858 access_context->ApplyToContext(apply_pending_action);
5859}
5860
John Zulauf8eda1562021-04-13 17:06:41 -06005861bool SyncOpWaitEvents::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06005862 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
5863 return DoValidate(*active_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06005864}
5865
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005866bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5867 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5868 bool skip = false;
5869 const auto *cb_access_context = GetAccessContext(commandBuffer);
5870 assert(cb_access_context);
5871 if (!cb_access_context) return skip;
5872
5873 const auto *context = cb_access_context->GetCurrentAccessContext();
5874 assert(context);
5875 if (!context) return skip;
5876
5877 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5878
5879 if (dst_buffer) {
5880 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5881 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
5882 if (hazard.hazard) {
5883 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5884 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
5885 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
John Zulauf14940722021-04-12 15:19:02 -06005886 cb_access_context->FormatUsage(hazard).c_str());
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005887 }
5888 }
5889 return skip;
5890}
5891
John Zulauf669dfd52021-01-27 17:15:28 -07005892void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07005893 events_.reserve(event_count);
5894 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
John Zulauf669dfd52021-01-27 17:15:28 -07005895 events_.emplace_back(sync_state.GetShared<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07005896 }
5897}
John Zulauf6ce24372021-01-30 05:56:25 -07005898
John Zulauf36ef9282021-02-02 11:47:24 -07005899SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005900 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005901 : SyncOpBase(cmd),
5902 event_(sync_state.GetShared<EVENT_STATE>(event)),
5903 exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005904
John Zulauf1bf30522021-09-03 15:39:06 -06005905bool SyncOpResetEvent::Validate(const CommandBufferAccessContext& cb_context) const {
5906 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
5907}
5908
5909bool SyncOpResetEvent::DoValidate(const CommandBufferAccessContext & cb_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005910 auto *events_context = cb_context.GetCurrentEventsContext();
5911 assert(events_context);
5912 bool skip = false;
5913 if (!events_context) return skip;
5914
5915 const auto &sync_state = cb_context.GetSyncState();
5916 const auto *sync_event = events_context->Get(event_);
5917 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5918
John Zulauf1bf30522021-09-03 15:39:06 -06005919 if (sync_event->last_command_tag > base_tag) return skip; // if we validated this in recording of the secondary, don't repeat
5920
John Zulauf6ce24372021-01-30 05:56:25 -07005921 const char *const set_wait =
5922 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5923 "hazards.";
5924 const char *message = set_wait; // Only one message this call.
5925 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
5926 const char *vuid = nullptr;
5927 switch (sync_event->last_command) {
5928 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005929 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005930 // Needs a barrier between set and reset
5931 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
5932 break;
John Zulauf4edde622021-02-15 08:54:50 -07005933 case CMD_WAITEVENTS:
5934 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07005935 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
5936 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
5937 break;
5938 }
5939 default:
5940 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07005941 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
5942 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07005943 break;
5944 }
5945 if (vuid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005946 skip |= sync_state.LogError(event_->event(), vuid, message, CmdName(),
5947 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005948 CommandTypeString(sync_event->last_command));
5949 }
5950 }
5951 return skip;
5952}
5953
John Zulauf8eda1562021-04-13 17:06:41 -06005954ResourceUsageTag SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
5955 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07005956 auto *events_context = cb_context->GetCurrentEventsContext();
5957 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005958 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07005959
5960 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06005961 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07005962
5963 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07005964 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06005965 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07005966 sync_event->unsynchronized_set = CMD_NONE;
5967 sync_event->ResetFirstScope();
5968 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06005969
5970 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07005971}
5972
John Zulauf8eda1562021-04-13 17:06:41 -06005973bool SyncOpResetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06005974 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf1bf30522021-09-03 15:39:06 -06005975 return DoValidate(*active_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06005976}
5977
John Zulauf4fa68462021-04-26 21:04:22 -06005978void SyncOpResetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06005979
John Zulauf36ef9282021-02-02 11:47:24 -07005980SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005981 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005982 : SyncOpBase(cmd),
5983 event_(sync_state.GetShared<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07005984 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
5985 dep_info_() {}
5986
5987SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
5988 const VkDependencyInfoKHR &dep_info)
5989 : SyncOpBase(cmd),
5990 event_(sync_state.GetShared<EVENT_STATE>(event)),
5991 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
5992 dep_info_(new safe_VkDependencyInfoKHR(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005993
5994bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf610e28c2021-08-03 17:46:23 -06005995 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
5996}
5997bool SyncOpSetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5998 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
5999 assert(active_context);
6000 return DoValidate(*active_context, base_tag);
6001}
6002
6003bool SyncOpSetEvent::DoValidate(const CommandBufferAccessContext &cb_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006004 bool skip = false;
6005
6006 const auto &sync_state = cb_context.GetSyncState();
6007 auto *events_context = cb_context.GetCurrentEventsContext();
6008 assert(events_context);
6009 if (!events_context) return skip;
6010
6011 const auto *sync_event = events_context->Get(event_);
6012 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6013
John Zulauf610e28c2021-08-03 17:46:23 -06006014 if (sync_event->last_command_tag >= base_tag) return skip; // for replay we don't want to revalidate internal "last commmand"
6015
John Zulauf6ce24372021-01-30 05:56:25 -07006016 const char *const reset_set =
6017 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6018 "hazards.";
6019 const char *const wait =
6020 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
6021
6022 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07006023 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07006024 const char *message = nullptr;
6025 switch (sync_event->last_command) {
6026 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006027 case CMD_RESETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006028 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07006029 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07006030 message = reset_set;
6031 break;
6032 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006033 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006034 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07006035 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07006036 message = reset_set;
6037 break;
6038 case CMD_WAITEVENTS:
John Zulauf4edde622021-02-15 08:54:50 -07006039 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006040 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07006041 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07006042 message = wait;
6043 break;
6044 default:
6045 // The only other valid last command that wasn't one.
6046 assert(sync_event->last_command == CMD_NONE);
6047 break;
6048 }
John Zulauf4edde622021-02-15 08:54:50 -07006049 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07006050 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07006051 std::string vuid("SYNC-");
6052 vuid.append(CmdName()).append(vuid_stem);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006053 skip |= sync_state.LogError(event_->event(), vuid.c_str(), message, CmdName(),
6054 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006055 CommandTypeString(sync_event->last_command));
6056 }
6057 }
6058
6059 return skip;
6060}
6061
John Zulauf8eda1562021-04-13 17:06:41 -06006062ResourceUsageTag SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006063 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006064 auto *events_context = cb_context->GetCurrentEventsContext();
6065 auto *access_context = cb_context->GetCurrentAccessContext();
6066 assert(events_context);
John Zulauf610e28c2021-08-03 17:46:23 -06006067 if (access_context && events_context) {
6068 DoRecord(tag, access_context, events_context);
6069 }
6070 return tag;
6071}
John Zulauf6ce24372021-01-30 05:56:25 -07006072
John Zulauf610e28c2021-08-03 17:46:23 -06006073void SyncOpSetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006074 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf610e28c2021-08-03 17:46:23 -06006075 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006076
6077 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
6078 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
6079 // any issues caused by naive scope setting here.
6080
6081 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
6082 // Given:
6083 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
6084 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
6085
6086 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
6087 sync_event->unsynchronized_set = sync_event->last_command;
6088 sync_event->ResetFirstScope();
6089 } else if (sync_event->scope.exec_scope == 0) {
6090 // We only set the scope if there isn't one
6091 sync_event->scope = src_exec_scope_;
6092
6093 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
6094 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
6095 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
6096 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
6097 }
6098 };
6099 access_context->ForAll(set_scope);
6100 sync_event->unsynchronized_set = CMD_NONE;
6101 sync_event->first_scope_tag = tag;
6102 }
John Zulauf4edde622021-02-15 08:54:50 -07006103 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
6104 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006105 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006106 sync_event->barriers = 0U;
6107}
John Zulauf64ffe552021-02-06 10:25:07 -07006108
6109SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
6110 const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07006111 const VkSubpassBeginInfo *pSubpassBeginInfo)
6112 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006113 if (pRenderPassBegin) {
6114 rp_state_ = sync_state.GetShared<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
6115 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
6116 const auto *fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
6117 if (fb_state) {
6118 shared_attachments_ = sync_state.GetSharedAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
6119 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
6120 // Note that this a safe to presist as long as shared_attachments is not cleared
6121 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08006122 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07006123 attachments_.emplace_back(attachment.get());
6124 }
6125 }
6126 if (pSubpassBeginInfo) {
6127 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
6128 }
6129 }
6130}
6131
6132bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6133 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
6134 bool skip = false;
6135
6136 assert(rp_state_.get());
6137 if (nullptr == rp_state_.get()) return skip;
6138 auto &rp_state = *rp_state_.get();
6139
6140 const uint32_t subpass = 0;
6141
6142 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
6143 // hasn't happened yet)
6144 const std::vector<AccessContext> empty_context_vector;
6145 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
6146 cb_context.GetCurrentAccessContext());
6147
6148 // Validate attachment operations
6149 if (attachments_.size() == 0) return skip;
6150 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07006151
6152 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
6153 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
6154 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
6155 // operations (though it's currently a messy approach)
6156 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
6157 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006158
6159 // Validate load operations if there were no layout transition hazards
6160 if (!skip) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07006161 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kCurrentCommandTag);
6162 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006163 }
6164
6165 return skip;
6166}
6167
John Zulauf8eda1562021-04-13 17:06:41 -06006168ResourceUsageTag SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
6169 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf64ffe552021-02-06 10:25:07 -07006170 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
6171 assert(rp_state_.get());
John Zulauf8eda1562021-04-13 17:06:41 -06006172 if (nullptr == rp_state_.get()) return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006173 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006174
6175 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006176}
6177
John Zulauf8eda1562021-04-13 17:06:41 -06006178bool SyncOpBeginRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006179 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006180 return false;
6181}
6182
John Zulauf4fa68462021-04-26 21:04:22 -06006183void SyncOpBeginRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
6184}
John Zulauf8eda1562021-04-13 17:06:41 -06006185
John Zulauf64ffe552021-02-06 10:25:07 -07006186SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07006187 const VkSubpassEndInfo *pSubpassEndInfo)
6188 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006189 if (pSubpassBeginInfo) {
6190 subpass_begin_info_.initialize(pSubpassBeginInfo);
6191 }
6192 if (pSubpassEndInfo) {
6193 subpass_end_info_.initialize(pSubpassEndInfo);
6194 }
6195}
6196
6197bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
6198 bool skip = false;
6199 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6200 if (!renderpass_context) return skip;
6201
6202 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
6203 return skip;
6204}
6205
John Zulauf8eda1562021-04-13 17:06:41 -06006206ResourceUsageTag SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006207 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06006208 // TODO PHASE2 Need to fix renderpass tagging/segregation of barrier and access operations for QueueSubmit time validation
6209 auto prev_tag = cb_context->NextCommandTag(cmd_);
6210 auto next_tag = cb_context->NextSubcommandTag(cmd_);
6211
6212 cb_context->RecordNextSubpass(prev_tag, next_tag);
6213 // TODO PHASE2 This needs to be the tag of the barrier operations
6214 return prev_tag;
6215}
6216
6217bool SyncOpNextSubpass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006218 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006219 return false;
John Zulauf64ffe552021-02-06 10:25:07 -07006220}
6221
sfricke-samsung85584a72021-09-30 21:43:38 -07006222SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo)
6223 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006224 if (pSubpassEndInfo) {
6225 subpass_end_info_.initialize(pSubpassEndInfo);
6226 }
6227}
6228
John Zulauf4fa68462021-04-26 21:04:22 -06006229void SyncOpNextSubpass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006230
John Zulauf64ffe552021-02-06 10:25:07 -07006231bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6232 bool skip = false;
6233 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6234
6235 if (!renderpass_context) return skip;
6236 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
6237 return skip;
6238}
6239
John Zulauf8eda1562021-04-13 17:06:41 -06006240ResourceUsageTag SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006241 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06006242 const auto tag = cb_context->NextCommandTag(cmd_);
6243 cb_context->RecordEndRenderPass(tag);
6244 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006245}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006246
John Zulauf8eda1562021-04-13 17:06:41 -06006247bool SyncOpEndRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006248 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006249 return false;
6250}
6251
John Zulauf4fa68462021-04-26 21:04:22 -06006252void SyncOpEndRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006253
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006254void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6255 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
6256 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
6257 auto *cb_access_context = GetAccessContext(commandBuffer);
6258 assert(cb_access_context);
6259 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
6260 auto *context = cb_access_context->GetCurrentAccessContext();
6261 assert(context);
6262
6263 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
6264
6265 if (dst_buffer) {
6266 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6267 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
6268 }
6269}
John Zulaufd05c5842021-03-26 11:32:16 -06006270
John Zulaufae842002021-04-15 18:20:55 -06006271bool SyncValidator::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6272 const VkCommandBuffer *pCommandBuffers) const {
6273 bool skip = StateTracker::PreCallValidateCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
6274 const char *func_name = "vkCmdExecuteCommands";
6275 const auto *cb_context = GetAccessContext(commandBuffer);
6276 assert(cb_context);
John Zulauf4fa68462021-04-26 21:04:22 -06006277
6278 // Heavyweight, but we need a proxy copy of the active command buffer access context
6279 CommandBufferAccessContext proxy_cb_context(*cb_context, CommandBufferAccessContext::AsProxyContext());
John Zulaufae842002021-04-15 18:20:55 -06006280
6281 // Make working copies of the access and events contexts
John Zulauf4fa68462021-04-26 21:04:22 -06006282 proxy_cb_context.NextCommandTag(CMD_EXECUTECOMMANDS);
John Zulaufae842002021-04-15 18:20:55 -06006283
6284 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
John Zulauf4fa68462021-04-26 21:04:22 -06006285 proxy_cb_context.NextSubcommandTag(CMD_EXECUTECOMMANDS);
John Zulaufae842002021-04-15 18:20:55 -06006286 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6287 if (!recorded_cb_context) continue;
John Zulauf4fa68462021-04-26 21:04:22 -06006288
6289 const auto *recorded_context = recorded_cb_context->GetCurrentAccessContext();
6290 assert(recorded_context);
6291 skip |= recorded_cb_context->ValidateFirstUse(&proxy_cb_context, func_name, cb_index);
6292
6293 // The barriers have already been applied in ValidatFirstUse
6294 ResourceUsageRange tag_range = proxy_cb_context.ImportRecordedAccessLog(*recorded_cb_context);
6295 proxy_cb_context.ResolveRecordedContext(*recorded_context, tag_range.begin);
John Zulaufae842002021-04-15 18:20:55 -06006296 }
6297
John Zulaufae842002021-04-15 18:20:55 -06006298 return skip;
6299}
6300
6301void SyncValidator::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6302 const VkCommandBuffer *pCommandBuffers) {
6303 StateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
John Zulauf4fa68462021-04-26 21:04:22 -06006304 auto *cb_context = GetAccessContext(commandBuffer);
6305 assert(cb_context);
6306 cb_context->NextCommandTag(CMD_EXECUTECOMMANDS);
6307 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
6308 cb_context->NextSubcommandTag(CMD_EXECUTECOMMANDS);
6309 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6310 if (!recorded_cb_context) continue;
6311 cb_context->RecordExecutedCommandBuffer(*recorded_cb_context, CMD_EXECUTECOMMANDS);
6312 }
John Zulaufae842002021-04-15 18:20:55 -06006313}
6314
John Zulaufd0ec59f2021-03-13 14:25:08 -07006315AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
6316 : view_(view), view_mask_(), gen_store_() {
6317 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
6318 const IMAGE_STATE &image_state = *view_->image_state.get();
6319 const auto base_address = ResourceBaseAddress(image_state);
6320 const auto *encoder = image_state.fragment_encoder.get();
6321 if (!encoder) return;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06006322 // Get offset and extent for the view, accounting for possible depth slicing
6323 const VkOffset3D zero_offset = view->GetOffset();
6324 const VkExtent3D &image_extent = view->GetExtent();
John Zulaufd0ec59f2021-03-13 14:25:08 -07006325 // Intentional copy
6326 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
6327 view_mask_ = subres_range.aspectMask;
6328 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
6329 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6330
6331 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
6332 if (depth && (depth != view_mask_)) {
6333 subres_range.aspectMask = depth;
6334 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6335 }
6336 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
6337 if (stencil && (stencil != view_mask_)) {
6338 subres_range.aspectMask = stencil;
6339 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6340 }
6341}
6342
6343const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
6344 const ImageRangeGen *got = nullptr;
6345 switch (gen_type) {
6346 case kViewSubresource:
6347 got = &gen_store_[kViewSubresource];
6348 break;
6349 case kRenderArea:
6350 got = &gen_store_[kRenderArea];
6351 break;
6352 case kDepthOnlyRenderArea:
6353 got =
6354 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
6355 break;
6356 case kStencilOnlyRenderArea:
6357 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
6358 : &gen_store_[Gen::kStencilOnlyRenderArea];
6359 break;
6360 default:
6361 assert(got);
6362 }
6363 return got;
6364}
6365
6366AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
6367 assert(IsValid());
6368 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
6369 if (depth_op) {
6370 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
6371 if (stencil_op) {
6372 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6373 return kRenderArea;
6374 }
6375 return kDepthOnlyRenderArea;
6376 }
6377 if (stencil_op) {
6378 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6379 return kStencilOnlyRenderArea;
6380 }
6381
6382 assert(depth_op || stencil_op);
6383 return kRenderArea;
6384}
6385
6386AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }
John Zulauf8eda1562021-04-13 17:06:41 -06006387
6388void SyncEventsContext::ApplyBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
6389 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
6390 for (auto &event_pair : map_) {
6391 assert(event_pair.second); // Shouldn't be storing empty
6392 auto &sync_event = *event_pair.second;
6393 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
6394 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
6395 sync_event.barriers |= dst.exec_scope;
6396 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6397 }
6398 }
6399}