blob: 1feee5d340e7a3701054554f65b45eee7b38f0e5 [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;
2227
John Zulaufae842002021-04-15 18:20:55 -06002228 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context);
2229 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002230 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002231 }
2232 // NOTE: Add call to replay validate here when we add support for syncop with non-trivial replay
John Zulauf4fa68462021-04-26 21:04:22 -06002233 // Record the barrier into the proxy context.
2234 sync_op.sync_op->DoRecord(base_tag + sync_op.tag, access_context, events_context);
2235 tag_range.begin = tag_range.end;
John Zulaufae842002021-04-15 18:20:55 -06002236 }
2237
2238 // and anything after the last syncop
John Zulaufae842002021-04-15 18:20:55 -06002239 tag_range.end = ResourceUsageRecord::kMaxIndex;
2240 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context);
2241 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002242 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002243 }
2244
2245 return skip;
2246}
2247
John Zulauf4fa68462021-04-26 21:04:22 -06002248void CommandBufferAccessContext::RecordExecutedCommandBuffer(const CommandBufferAccessContext &recorded_cb_context, CMD_TYPE cmd) {
2249 auto *events_context = GetCurrentEventsContext();
2250 auto *access_context = GetCurrentAccessContext();
2251 const AccessContext *recorded_context = recorded_cb_context.GetCurrentAccessContext();
2252 assert(recorded_context);
2253
2254 // Just run through the barriers ignoring the usage from the recorded context, as Resolve will overwrite outdated state
2255 const ResourceUsageTag base_tag = GetTagLimit();
2256 for (const auto &sync_op : recorded_cb_context.sync_ops_) {
2257 // we update the range to any include layout transition first use writes,
2258 // as they are stored along with the source scope (as effective barrier) when recorded
2259 sync_op.sync_op->DoRecord(base_tag + sync_op.tag, access_context, events_context);
2260 }
2261
2262 ResourceUsageRange tag_range = ImportRecordedAccessLog(recorded_cb_context);
2263 assert(base_tag == tag_range.begin); // to ensure the to offset calculation agree
2264 ResolveRecordedContext(*recorded_context, tag_range.begin);
2265}
2266
2267void CommandBufferAccessContext::ResolveRecordedContext(const AccessContext &recorded_context, ResourceUsageTag offset) {
2268 auto tag_offset = [offset](ResourceAccessState *access) { access->OffsetTag(offset); };
2269
2270 auto *access_context = GetCurrentAccessContext();
2271 for (auto address_type : kAddressTypes) {
2272 recorded_context.ResolveAccessRange(address_type, kFullRange, tag_offset, &access_context->GetAccessStateMap(address_type),
2273 nullptr, false);
2274 }
2275}
2276
2277ResourceUsageRange CommandBufferAccessContext::ImportRecordedAccessLog(const CommandBufferAccessContext &recorded_context) {
2278 // The execution references ensure lifespan for the referenced child CB's...
2279 ResourceUsageRange tag_range(GetTagLimit(), 0);
John Zulauf3c2a0b32021-07-14 11:14:52 -06002280 cbs_referenced_.emplace(recorded_context.cb_state_);
John Zulauf4fa68462021-04-26 21:04:22 -06002281 access_log_.insert(access_log_.end(), recorded_context.access_log_.cbegin(), recorded_context.access_log_.end());
2282 tag_range.end = access_log_.size();
2283 return tag_range;
2284}
2285
John Zulaufae842002021-04-15 18:20:55 -06002286class HazardDetectFirstUse {
2287 public:
2288 HazardDetectFirstUse(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range)
2289 : recorded_use_(recorded_use), tag_range_(tag_range) {}
2290 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
2291 return pos->second.DetectHazard(recorded_use_, tag_range_);
2292 }
2293 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
2294 return pos->second.DetectAsyncHazard(recorded_use_, tag_range_, start_tag);
2295 }
2296
2297 private:
2298 const ResourceAccessState &recorded_use_;
2299 const ResourceUsageRange &tag_range_;
2300};
2301
2302// This is called with the *recorded* command buffers access context, with the *active* access context pass in, againsts which
2303// hazards will be detected
2304HazardResult AccessContext::DetectFirstUseHazard(const ResourceUsageRange &tag_range, const AccessContext &access_context) const {
2305 HazardResult hazard;
2306 for (const auto address_type : kAddressTypes) {
2307 const auto &recorded_access_map = GetAccessStateMap(address_type);
2308 for (const auto &recorded_access : recorded_access_map) {
2309 // Cull any entries not in the current tag range
2310 if (!recorded_access.second.FirstAccessInTagRange(tag_range)) continue;
2311 HazardDetectFirstUse detector(recorded_access.second, tag_range);
2312 hazard = access_context.DetectHazard(address_type, detector, recorded_access.first, DetectOptions::kDetectAll);
2313 if (hazard.hazard) break;
2314 }
2315 }
2316
2317 return hazard;
2318}
2319
John Zulauf64ffe552021-02-06 10:25:07 -07002320bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
John Zulauffaea0ee2021-01-14 14:01:32 -07002321 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002322 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002323 const auto &sync_state = ex_context.GetSyncState();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002324 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002325 if (!pipe) {
2326 return skip;
2327 }
2328
2329 const auto &create_info = pipe->create_info.graphics;
2330 if (create_info.pRasterizationState && create_info.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002331 return skip;
2332 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002333 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002334 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg37047832020-06-12 13:44:45 -06002335
John Zulauf1a224292020-06-30 14:52:13 -06002336 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002337 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002338 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2339 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002340 if (location >= subpass.colorAttachmentCount ||
2341 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002342 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002343 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002344 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2345 if (!view_gen.IsValid()) continue;
2346 HazardResult hazard =
2347 current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
2348 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment);
locke-lunarg96dc9632020-06-10 17:22:18 -06002349 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002350 const VkImageView view_handle = view_gen.GetViewState()->image_view();
John Zulaufd0ec59f2021-03-13 14:25:08 -07002351 skip |= sync_state.LogError(view_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002352 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002353 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002354 sync_state.report_data->FormatHandle(view_handle).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002355 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002356 location, ex_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002357 }
2358 }
2359 }
locke-lunarg37047832020-06-12 13:44:45 -06002360
2361 // PHASE1 TODO: Add layout based read/vs. write selection.
2362 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002363 const uint32_t depth_stencil_attachment =
Jeremy Gebben11af9792021-08-20 10:20:09 -06002364 GetSubpassDepthStencilAttachmentIndex(pipe->create_info.graphics.pDepthStencilState, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002365
2366 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2367 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2368 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002369 bool depth_write = false, stencil_write = false;
2370
2371 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002372 if (!FormatIsStencilOnly(view_state.create_info.format) && create_info.pDepthStencilState->depthTestEnable &&
2373 create_info.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002374 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2375 depth_write = true;
2376 }
2377 // PHASE1 TODO: It needs to check if stencil is writable.
2378 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2379 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2380 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002381 if (!FormatIsDepthOnly(view_state.create_info.format) && create_info.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002382 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2383 stencil_write = true;
2384 }
2385
2386 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2387 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002388 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2389 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2390 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002391 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002392 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002393 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002394 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002395 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002396 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2397 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002398 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002399 }
2400 }
2401 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002402 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2403 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2404 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002405 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002406 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002407 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002408 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002409 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002410 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2411 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002412 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002413 }
locke-lunarg61870c22020-06-09 14:51:50 -06002414 }
2415 }
2416 return skip;
2417}
2418
John Zulauf14940722021-04-12 15:19:02 -06002419void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002420 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002421 if (!pipe) {
2422 return;
2423 }
2424
2425 const auto &create_info = pipe->create_info.graphics;
2426 if (create_info.pRasterizationState && create_info.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002427 return;
2428 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002429 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002430 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002431
John Zulauf1a224292020-06-30 14:52:13 -06002432 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002433 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002434 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2435 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002436 if (location >= subpass.colorAttachmentCount ||
2437 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002438 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002439 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002440 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2441 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2442 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2443 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002444 }
2445 }
locke-lunarg37047832020-06-12 13:44:45 -06002446
2447 // PHASE1 TODO: Add layout based read/vs. write selection.
2448 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002449 const uint32_t depth_stencil_attachment =
Jeremy Gebben11af9792021-08-20 10:20:09 -06002450 GetSubpassDepthStencilAttachmentIndex(create_info.pDepthStencilState, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002451 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2452 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2453 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002454 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002455 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2456 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002457
2458 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002459 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) && create_info.pDepthStencilState->depthTestEnable &&
2460 create_info.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002461 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2462 depth_write = true;
2463 }
2464 // PHASE1 TODO: It needs to check if stencil is writable.
2465 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2466 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2467 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002468 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) && create_info.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002469 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2470 stencil_write = true;
2471 }
2472
John Zulaufd0ec59f2021-03-13 14:25:08 -07002473 if (depth_write || stencil_write) {
2474 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2475 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2476 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2477 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002478 }
locke-lunarg61870c22020-06-09 14:51:50 -06002479 }
2480}
2481
John Zulauf64ffe552021-02-06 10:25:07 -07002482bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002483 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002484 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002485 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002486 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002487 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002488 func_name);
2489
John Zulauf355e49b2020-04-24 15:11:15 -06002490 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002491 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002492 skip |=
2493 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002494 if (!skip) {
2495 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2496 // on a copy of the (empty) next context.
2497 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2498 AccessContext temp_context(next_context);
2499 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002500 skip |=
2501 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002502 }
John Zulauf7635de32020-05-29 17:14:15 -06002503 return skip;
2504}
John Zulauf64ffe552021-02-06 10:25:07 -07002505bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002506 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002507 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002508 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002509 current_subpass_);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002510 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_,
2511
2512 attachment_views_, func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002513 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002514 return skip;
2515}
2516
John Zulauf64ffe552021-02-06 10:25:07 -07002517AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002518 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002519}
2520
John Zulauf64ffe552021-02-06 10:25:07 -07002521bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2522 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002523 bool skip = false;
2524
John Zulauf7635de32020-05-29 17:14:15 -06002525 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2526 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2527 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2528 // to apply and only copy then, if this proves a hot spot.
2529 std::unique_ptr<AccessContext> proxy_for_current;
2530
John Zulauf355e49b2020-04-24 15:11:15 -06002531 // Validate the "finalLayout" transitions to external
2532 // Get them from where there we're hidding in the extra entry.
2533 const auto &final_transitions = rp_state_->subpass_transitions.back();
2534 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002535 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002536 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2537 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002538 auto *context = trackback.context;
2539
2540 if (transition.prev_pass == current_subpass_) {
2541 if (!proxy_for_current) {
2542 // 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 -07002543 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002544 }
2545 context = proxy_for_current.get();
2546 }
2547
John Zulaufa0a98292020-09-18 09:30:10 -06002548 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2549 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002550 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002551 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002552 skip |= ex_context.GetSyncState().LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002553 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07002554 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2555 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2556 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2557 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002558 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002559 }
2560 }
2561 return skip;
2562}
2563
John Zulauf14940722021-04-12 15:19:02 -06002564void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002565 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002566 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002567}
2568
John Zulauf14940722021-04-12 15:19:02 -06002569void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002570 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2571 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002572
2573 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2574 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002575 const AttachmentViewGen &view_gen = attachment_views_[i];
2576 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002577
2578 const auto &ci = attachment_ci[i];
2579 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002580 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002581 const bool is_color = !(has_depth || has_stencil);
2582
2583 if (is_color) {
John Zulauf57261402021-08-13 11:32:06 -06002584 const SyncStageAccessIndex load_op = ColorLoadUsage(ci.loadOp);
2585 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2586 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, load_op,
2587 SyncOrdering::kColorAttachment, tag);
2588 }
John Zulauf1507ee42020-05-18 11:33:09 -06002589 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002590 if (has_depth) {
John Zulauf57261402021-08-13 11:32:06 -06002591 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.loadOp);
2592 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2593 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_op,
2594 SyncOrdering::kDepthStencilAttachment, tag);
2595 }
John Zulauf1507ee42020-05-18 11:33:09 -06002596 }
2597 if (has_stencil) {
John Zulauf57261402021-08-13 11:32:06 -06002598 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.stencilLoadOp);
2599 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2600 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, load_op,
2601 SyncOrdering::kDepthStencilAttachment, tag);
2602 }
John Zulauf1507ee42020-05-18 11:33:09 -06002603 }
2604 }
2605 }
2606 }
2607}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002608AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2609 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2610 AttachmentViewGenVector view_gens;
2611 VkExtent3D extent = CastTo3D(render_area.extent);
2612 VkOffset3D offset = CastTo3D(render_area.offset);
2613 view_gens.reserve(attachment_views.size());
2614 for (const auto *view : attachment_views) {
2615 view_gens.emplace_back(view, offset, extent);
2616 }
2617 return view_gens;
2618}
John Zulauf64ffe552021-02-06 10:25:07 -07002619RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2620 VkQueueFlags queue_flags,
2621 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2622 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002623 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002624 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002625 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002626 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002627 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002628 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002629 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002630}
John Zulauf14940722021-04-12 15:19:02 -06002631void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag tag) {
John Zulauf64ffe552021-02-06 10:25:07 -07002632 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002633 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002634 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002635 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002636}
John Zulauf1507ee42020-05-18 11:33:09 -06002637
John Zulauf14940722021-04-12 15:19:02 -06002638void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag prev_subpass_tag, const ResourceUsageTag next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002639 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulaufd0ec59f2021-03-13 14:25:08 -07002640 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
2641 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002642
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002643 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2644 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002645 current_subpass_++;
2646 assert(current_subpass_ < subpass_contexts_.size());
John Zulauffaea0ee2021-01-14 14:01:32 -07002647 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2648 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002649 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002650}
2651
John Zulauf14940722021-04-12 15:19:02 -06002652void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002653 // Add the resolve and store accesses
John Zulaufd0ec59f2021-03-13 14:25:08 -07002654 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, tag);
2655 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002656
John Zulauf355e49b2020-04-24 15:11:15 -06002657 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002658 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002659
2660 // Add the "finalLayout" transitions to external
2661 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002662 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2663 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2664 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002665 const auto &final_transitions = rp_state_->subpass_transitions.back();
2666 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002667 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002668 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002669 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002670 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002671 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002672 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002673 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002674 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002675 }
2676}
2677
Jeremy Gebben40a22942020-12-22 14:22:06 -07002678SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002679 SyncExecScope result;
2680 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002681 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2682 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002683 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2684 return result;
2685}
2686
Jeremy Gebben40a22942020-12-22 14:22:06 -07002687SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002688 SyncExecScope result;
2689 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002690 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2691 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002692 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2693 return result;
2694}
2695
2696SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002697 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002698 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002699 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002700 dst_access_scope = 0;
2701}
2702
2703template <typename Barrier>
2704SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002705 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002706 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002707 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002708 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2709}
2710
2711SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002712 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2713 if (barrier) {
2714 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002715 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002716 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002717
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002718 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002719 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002720 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2721
2722 } else {
2723 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002724 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002725 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2726
2727 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002728 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002729 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2730 }
2731}
2732
2733template <typename Barrier>
2734SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2735 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2736 src_exec_scope = src.exec_scope;
2737 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2738
2739 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002740 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002741 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002742}
2743
John Zulaufb02c1eb2020-10-06 16:33:36 -06002744// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2745void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2746 for (const auto &barrier : barriers) {
2747 ApplyBarrier(barrier, layout_transition);
2748 }
2749}
2750
John Zulauf89311b42020-09-29 16:28:47 -06002751// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2752// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2753// lazily, s.t. no previous access reports should need layout transitions.
John Zulauf14940722021-04-12 15:19:02 -06002754void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06002755 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002756 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002757 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002758 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002759 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002760 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002761 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002762}
John Zulauf9cb530d2019-09-30 14:14:10 -06002763HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2764 HazardResult hazard;
2765 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002766 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002767 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002768 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002769 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002770 }
2771 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002772 // Write operation:
2773 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2774 // If reads exists -- test only against them because either:
2775 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2776 // * 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
2777 // the current write happens after the reads, so just test the write against the reades
2778 // Otherwise test against last_write
2779 //
2780 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002781 if (last_reads.size()) {
2782 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002783 if (IsReadHazard(usage_stage, read_access)) {
2784 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2785 break;
2786 }
2787 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002788 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002789 // Write-After-Write check -- if we have a previous write to test against
2790 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002791 }
2792 }
2793 return hazard;
2794}
2795
John Zulauf4fa68462021-04-26 21:04:22 -06002796HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering ordering_rule) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002797 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf4fa68462021-04-26 21:04:22 -06002798 return DetectHazard(usage_index, ordering);
2799}
2800
2801HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const OrderingBarrier &ordering) const {
John Zulauf69133422020-05-20 14:55:53 -06002802 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2803 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002804 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002805 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002806 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2807 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002808 if (IsRead(usage_bit)) {
2809 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2810 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2811 if (is_raw_hazard) {
2812 // NOTE: we know last_write is non-zero
2813 // See if the ordering rules save us from the simple RAW check above
2814 // First check to see if the current usage is covered by the ordering rules
2815 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2816 const bool usage_is_ordered =
2817 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2818 if (usage_is_ordered) {
2819 // Now see of the most recent write (or a subsequent read) are ordered
2820 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2821 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002822 }
2823 }
John Zulauf4285ee92020-09-23 10:20:52 -06002824 if (is_raw_hazard) {
2825 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2826 }
John Zulauf5c628d02021-05-04 15:46:36 -06002827 } else if (usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2828 // For Image layout transitions, the barrier represents the first synchronization/access scope of the layout transition
2829 return DetectBarrierHazard(usage_index, ordering.exec_scope, ordering.access_scope);
John Zulauf361fb532020-07-22 10:45:39 -06002830 } else {
2831 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002832 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002833 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002834 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002835 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002836 if (usage_write_is_ordered) {
2837 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2838 ordered_stages = GetOrderedStages(ordering);
2839 }
2840 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2841 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002842 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002843 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2844 if (IsReadHazard(usage_stage, read_access)) {
2845 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2846 break;
2847 }
John Zulaufd14743a2020-07-03 09:42:39 -06002848 }
2849 }
John Zulauf4285ee92020-09-23 10:20:52 -06002850 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002851 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002852 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002853 }
John Zulauf69133422020-05-20 14:55:53 -06002854 }
2855 }
2856 return hazard;
2857}
2858
John Zulaufae842002021-04-15 18:20:55 -06002859HazardResult ResourceAccessState::DetectHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range) const {
2860 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002861 using Size = FirstAccesses::size_type;
2862 const auto &recorded_accesses = recorded_use.first_accesses_;
2863 Size count = recorded_accesses.size();
2864 if (count) {
2865 const auto &last_access = recorded_accesses.back();
2866 bool do_write_last = IsWrite(last_access.usage_index);
2867 if (do_write_last) --count;
John Zulaufae842002021-04-15 18:20:55 -06002868
John Zulauf4fa68462021-04-26 21:04:22 -06002869 for (Size i = 0; i < count; ++count) {
2870 const auto &first = recorded_accesses[i];
2871 // Skip and quit logic
2872 if (first.tag < tag_range.begin) continue;
2873 if (first.tag >= tag_range.end) {
2874 do_write_last = false; // ignore last since we know it can't be in tag_range
2875 break;
2876 }
2877
2878 hazard = DetectHazard(first.usage_index, first.ordering_rule);
2879 if (hazard.hazard) {
2880 hazard.AddRecordedAccess(first);
2881 break;
2882 }
2883 }
2884
2885 if (do_write_last && tag_range.includes(last_access.tag)) {
2886 // Writes are a bit special... both for the "most recent" access logic, and layout transition specific logic
2887 OrderingBarrier barrier = GetOrderingRules(last_access.ordering_rule);
2888 if (last_access.usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2889 // Or in the layout first access scope as a barrier... IFF the usage is an ILT
2890 // this was saved off in the "apply barriers" logic to simplify ILT access checks as they straddle
2891 // the barrier that applies them
2892 barrier |= recorded_use.first_write_layout_ordering_;
2893 }
2894 // Any read stages present in the recorded context (this) are most recent to the write, and thus mask those stages in
2895 // the active context
2896 if (recorded_use.first_read_stages_) {
2897 // we need to ignore the first use read stage in the active context (so we add them to the ordering rule),
2898 // reads in the active context are not "most recent" as all recorded context operations are *after* them
2899 // This supresses only RAW checks for stages present in the recorded context, but not those only present in the
2900 // active context.
2901 barrier.exec_scope |= recorded_use.first_read_stages_;
2902 // if there are any first use reads, we suppress WAW by injecting the active context write in the ordering rule
2903 barrier.access_scope |= FlagBit(last_access.usage_index);
2904 }
2905 hazard = DetectHazard(last_access.usage_index, barrier);
2906 if (hazard.hazard) {
2907 hazard.AddRecordedAccess(last_access);
2908 }
2909 }
John Zulaufae842002021-04-15 18:20:55 -06002910 }
2911 return hazard;
2912}
2913
John Zulauf2f952d22020-02-10 11:34:51 -07002914// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf14940722021-04-12 15:19:02 -06002915HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002916 HazardResult hazard;
2917 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002918 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2919 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2920 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002921 if (IsRead(usage)) {
John Zulauf14940722021-04-12 15:19:02 -06002922 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002923 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002924 }
2925 } else {
John Zulauf14940722021-04-12 15:19:02 -06002926 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002927 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002928 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002929 // 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 -07002930 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06002931 if (read_access.tag >= start_tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002932 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002933 break;
2934 }
2935 }
John Zulauf2f952d22020-02-10 11:34:51 -07002936 }
2937 }
2938 return hazard;
2939}
2940
John Zulaufae842002021-04-15 18:20:55 -06002941HazardResult ResourceAccessState::DetectAsyncHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
2942 ResourceUsageTag start_tag) const {
2943 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002944 for (const auto &first : recorded_use.first_accesses_) {
John Zulaufae842002021-04-15 18:20:55 -06002945 // Skip and quit logic
2946 if (first.tag < tag_range.begin) continue;
2947 if (first.tag >= tag_range.end) break;
John Zulaufae842002021-04-15 18:20:55 -06002948
2949 hazard = DetectAsyncHazard(first.usage_index, start_tag);
John Zulauf4fa68462021-04-26 21:04:22 -06002950 if (hazard.hazard) {
2951 hazard.AddRecordedAccess(first);
2952 break;
2953 }
John Zulaufae842002021-04-15 18:20:55 -06002954 }
2955 return hazard;
2956}
2957
Jeremy Gebben40a22942020-12-22 14:22:06 -07002958HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002959 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002960 // Only supporting image layout transitions for now
2961 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2962 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002963 // only test for WAW if there no intervening read operations.
2964 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002965 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002966 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002967 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002968 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002969 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002970 break;
2971 }
2972 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002973 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2974 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2975 }
2976
2977 return hazard;
2978}
2979
Jeremy Gebben40a22942020-12-22 14:22:06 -07002980HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07002981 const SyncStageAccessFlags &src_access_scope,
John Zulauf14940722021-04-12 15:19:02 -06002982 const ResourceUsageTag event_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002983 // Only supporting image layout transitions for now
2984 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2985 HazardResult hazard;
2986 // only test for WAW if there no intervening read operations.
2987 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2988
John Zulaufab7756b2020-12-29 16:10:16 -07002989 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002990 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
2991 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07002992 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06002993 if (read_access.tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002994 // The read is in the events first synchronization scope, so we use a barrier hazard check
2995 // If the read stage is not in the src sync scope
2996 // *AND* not execution chained with an existing sync barrier (that's the or)
2997 // then the barrier access is unsafe (R/W after R)
2998 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
2999 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3000 break;
3001 }
3002 } else {
3003 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3004 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3005 }
3006 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003007 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003008 // 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 -06003009 if (write_tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003010 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3011 // So do a normal barrier hazard check
3012 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3013 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3014 }
3015 } else {
3016 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003017 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3018 }
John Zulaufd14743a2020-07-03 09:42:39 -06003019 }
John Zulauf361fb532020-07-22 10:45:39 -06003020
John Zulauf0cb5be22020-01-23 12:18:22 -07003021 return hazard;
3022}
3023
John Zulauf5f13a792020-03-10 07:31:21 -06003024// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3025// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3026// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3027void ResourceAccessState::Resolve(const ResourceAccessState &other) {
John Zulauf14940722021-04-12 15:19:02 -06003028 if (write_tag < other.write_tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003029 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3030 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003031 *this = other;
John Zulauf14940722021-04-12 15:19:02 -06003032 } else if (other.write_tag == write_tag) {
3033 // 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 -06003034 // dependency chaining logic or any stage expansion)
3035 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003036 pending_write_barriers |= other.pending_write_barriers;
3037 pending_layout_transition |= other.pending_layout_transition;
3038 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf4fa68462021-04-26 21:04:22 -06003039 pending_layout_ordering_ |= other.pending_layout_ordering_;
John Zulauf5f13a792020-03-10 07:31:21 -06003040
John Zulaufd14743a2020-07-03 09:42:39 -06003041 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07003042 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06003043 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07003044 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003045 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003046 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003047 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003048 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3049 // but we should wait on profiling data for that.
3050 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003051 auto &my_read = last_reads[my_read_index];
3052 if (other_read.stage == my_read.stage) {
John Zulauf14940722021-04-12 15:19:02 -06003053 if (my_read.tag < other_read.tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003054 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003055 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003056 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003057 my_read.pending_dep_chain = other_read.pending_dep_chain;
3058 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3059 // May require tracking more than one access per stage.
3060 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003061 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06003062 // Since I'm overwriting the fragement stage read, also update the input attachment info
3063 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003064 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003065 }
John Zulauf14940722021-04-12 15:19:02 -06003066 } else if (other_read.tag == my_read.tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06003067 // The read tags match so merge the barriers
3068 my_read.barriers |= other_read.barriers;
3069 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003070 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003071
John Zulauf5f13a792020-03-10 07:31:21 -06003072 break;
3073 }
3074 }
3075 } else {
3076 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07003077 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06003078 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003079 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003080 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003081 }
John Zulauf5f13a792020-03-10 07:31:21 -06003082 }
3083 }
John Zulauf361fb532020-07-22 10:45:39 -06003084 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003085 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3086 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07003087
3088 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
3089 // of the copy and other into this using the update first logic.
3090 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
3091 // of the other first_accesses... )
3092 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
3093 FirstAccesses firsts(std::move(first_accesses_));
3094 first_accesses_.clear();
3095 first_read_stages_ = 0U;
3096 auto a = firsts.begin();
3097 auto a_end = firsts.end();
3098 for (auto &b : other.first_accesses_) {
John Zulauf14940722021-04-12 15:19:02 -06003099 // TODO: Determine whether some tag offset will be needed for PHASE II
3100 while ((a != a_end) && (a->tag < b.tag)) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003101 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3102 ++a;
3103 }
3104 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
3105 }
3106 for (; a != a_end; ++a) {
3107 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3108 }
3109 }
John Zulauf5f13a792020-03-10 07:31:21 -06003110}
3111
John Zulauf14940722021-04-12 15:19:02 -06003112void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003113 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3114 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003115 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003116 // Mulitple outstanding reads may be of interest and do dependency chains independently
3117 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3118 const auto usage_stage = PipelineStageBit(usage_index);
3119 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003120 for (auto &read_access : last_reads) {
3121 if (read_access.stage == usage_stage) {
3122 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003123 break;
3124 }
3125 }
3126 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07003127 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003128 last_read_stages |= usage_stage;
3129 }
John Zulauf4285ee92020-09-23 10:20:52 -06003130
3131 // 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 -07003132 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003133 // TODO Revisit re: multiple reads for a given stage
3134 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003135 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003136 } else {
3137 // Assume write
3138 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003139 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003140 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003141 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06003142}
John Zulauf5f13a792020-03-10 07:31:21 -06003143
John Zulauf89311b42020-09-29 16:28:47 -06003144// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3145// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3146// We can overwrite them as *this* write is now after them.
3147//
3148// 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 -06003149void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003150 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06003151 last_read_stages = 0;
3152 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003153 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003154
3155 write_barriers = 0;
3156 write_dependency_chain = 0;
3157 write_tag = tag;
3158 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003159}
3160
John Zulauf89311b42020-09-29 16:28:47 -06003161// Apply the memory barrier without updating the existing barriers. The execution barrier
3162// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3163// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3164// replace the current write barriers or add to them, so accumulate to pending as well.
3165void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3166 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3167 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003168 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3169 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3170 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3171 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07003172 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003173 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003174 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003175 if (layout_transition) {
3176 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3177 }
John Zulaufa0a98292020-09-18 09:30:10 -06003178 }
John Zulauf89311b42020-09-29 16:28:47 -06003179 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3180 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003181
John Zulauf89311b42020-09-29 16:28:47 -06003182 if (!pending_layout_transition) {
3183 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3184 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003185 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06003186 // 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 -07003187 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
3188 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003189 }
3190 }
John Zulaufa0a98292020-09-18 09:30:10 -06003191 }
John Zulaufa0a98292020-09-18 09:30:10 -06003192}
3193
John Zulauf4a6105a2020-11-17 15:11:05 -07003194// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3195// changes the "chaining" state, but to keep barriers independent. See discussion above.
John Zulauf14940722021-04-12 15:19:02 -06003196void ResourceAccessState::ApplyBarrier(const ResourceUsageTag scope_tag, const SyncBarrier &barrier, bool layout_transition) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003197 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3198 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3199 // in order to know if it's in the excecution scope
3200 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3201 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3202 // errors w.r.t. "most recent" accesses.
John Zulauf14940722021-04-12 15:19:02 -06003203 if (layout_transition || ((write_tag < scope_tag) && (barrier.src_access_scope & last_write).any())) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003204 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003205 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003206 if (layout_transition) {
3207 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3208 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003209 }
3210 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3211 pending_layout_transition |= layout_transition;
3212
3213 if (!pending_layout_transition) {
3214 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3215 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003216 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003217 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3218 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3219 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3220 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3221 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3222 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3223 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulauf14940722021-04-12 15:19:02 -06003224 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 -07003225 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003226 }
3227 }
3228 }
3229}
John Zulauf14940722021-04-12 15:19:02 -06003230void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag tag) {
John Zulauf89311b42020-09-29 16:28:47 -06003231 if (pending_layout_transition) {
John Zulauf4fa68462021-04-26 21:04:22 -06003232 // 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 -06003233 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07003234 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf4fa68462021-04-26 21:04:22 -06003235 TouchupFirstForLayoutTransition(tag, pending_layout_ordering_);
3236 pending_layout_ordering_ = OrderingBarrier();
John Zulauf89311b42020-09-29 16:28:47 -06003237 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003238 }
John Zulauf89311b42020-09-29 16:28:47 -06003239
3240 // Apply the accumulate execution barriers (and thus update chaining information)
John Zulauf4fa68462021-04-26 21:04:22 -06003241 // for layout transition, last_reads is reset by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003242 for (auto &read_access : last_reads) {
3243 read_access.barriers |= read_access.pending_dep_chain;
3244 read_execution_barriers |= read_access.barriers;
3245 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003246 }
3247
3248 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3249 write_dependency_chain |= pending_write_dep_chain;
3250 write_barriers |= pending_write_barriers;
3251 pending_write_dep_chain = 0;
3252 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003253}
3254
John Zulaufae842002021-04-15 18:20:55 -06003255bool ResourceAccessState::FirstAccessInTagRange(const ResourceUsageRange &tag_range) const {
3256 if (!first_accesses_.size()) return false;
3257 const ResourceUsageRange first_access_range = {first_accesses_.front().tag, first_accesses_.back().tag + 1};
3258 return tag_range.intersects(first_access_range);
3259}
3260
John Zulauf59e25072020-07-17 10:55:21 -06003261// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07003262VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
3263 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003264
John Zulaufab7756b2020-12-29 16:10:16 -07003265 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003266 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003267 barriers = read_access.barriers;
3268 break;
John Zulauf59e25072020-07-17 10:55:21 -06003269 }
3270 }
John Zulauf4285ee92020-09-23 10:20:52 -06003271
John Zulauf59e25072020-07-17 10:55:21 -06003272 return barriers;
3273}
3274
Jeremy Gebben40a22942020-12-22 14:22:06 -07003275inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003276 assert(IsRead(usage));
3277 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3278 // * the previous reads are not hazards, and thus last_write must be visible and available to
3279 // any reads that happen after.
3280 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3281 // 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 -07003282 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003283}
3284
Jeremy Gebben40a22942020-12-22 14:22:06 -07003285VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003286 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07003287 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06003288 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003289 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003290 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003291 // 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 -07003292 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06003293 }
3294
3295 return ordered_stages;
3296}
3297
John Zulauf14940722021-04-12 15:19:02 -06003298void ResourceAccessState::UpdateFirst(const ResourceUsageTag tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003299 // Only record until we record a write.
3300 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003301 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003302 if (0 == (usage_stage & first_read_stages_)) {
3303 // If this is a read we haven't seen or a write, record.
John Zulauf4fa68462021-04-26 21:04:22 -06003304 // We always need to know what stages were found prior to write
John Zulauffaea0ee2021-01-14 14:01:32 -07003305 first_read_stages_ |= usage_stage;
John Zulauf4fa68462021-04-26 21:04:22 -06003306 if (0 == (read_execution_barriers & usage_stage)) {
3307 // If this stage isn't masked then we add it (since writes map to usage_stage 0, this also records writes)
3308 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3309 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003310 }
3311 }
3312}
3313
John Zulauf4fa68462021-04-26 21:04:22 -06003314void ResourceAccessState::TouchupFirstForLayoutTransition(ResourceUsageTag tag, const OrderingBarrier &layout_ordering) {
3315 // Only call this after recording an image layout transition
3316 assert(first_accesses_.size());
3317 if (first_accesses_.back().tag == tag) {
3318 // If this layout transition is the the first write, add the additional ordering rules that guard the ILT
3319 assert(first_accesses_.back().usage_index = SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3320 first_write_layout_ordering_ = layout_ordering;
3321 }
3322}
3323
John Zulaufd1f85d42020-04-15 12:23:15 -06003324void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003325 auto *access_context = GetAccessContextNoInsert(command_buffer);
3326 if (access_context) {
3327 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003328 }
3329}
3330
John Zulaufd1f85d42020-04-15 12:23:15 -06003331void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3332 auto access_found = cb_access_state.find(command_buffer);
3333 if (access_found != cb_access_state.end()) {
3334 access_found->second->Reset();
John Zulauf4fa68462021-04-26 21:04:22 -06003335 access_found->second->MarkDestroyed();
John Zulaufd1f85d42020-04-15 12:23:15 -06003336 cb_access_state.erase(access_found);
3337 }
3338}
3339
John Zulauf9cb530d2019-09-30 14:14:10 -06003340bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3341 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3342 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003343 const auto *cb_context = GetAccessContext(commandBuffer);
3344 assert(cb_context);
3345 if (!cb_context) return skip;
3346 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003347
John Zulauf3d84f1b2020-03-09 13:33:25 -06003348 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003349 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003350 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003351
3352 for (uint32_t region = 0; region < regionCount; region++) {
3353 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003354 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003355 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003356 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003357 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003358 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003359 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003360 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003361 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003362 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003363 }
John Zulauf16adfc92020-04-08 10:28:33 -06003364 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003365 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003366 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003367 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003368 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003369 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003370 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003371 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003372 }
3373 }
3374 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003375 }
3376 return skip;
3377}
3378
3379void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3380 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003381 auto *cb_context = GetAccessContext(commandBuffer);
3382 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003383 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003384 auto *context = cb_context->GetCurrentAccessContext();
3385
John Zulauf9cb530d2019-09-30 14:14:10 -06003386 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003387 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003388
3389 for (uint32_t region = 0; region < regionCount; region++) {
3390 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003391 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003392 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003393 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003394 }
John Zulauf16adfc92020-04-08 10:28:33 -06003395 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003396 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003397 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003398 }
3399 }
3400}
3401
John Zulauf4a6105a2020-11-17 15:11:05 -07003402void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3403 // Clear out events from the command buffer contexts
3404 for (auto &cb_context : cb_access_state) {
3405 cb_context.second->RecordDestroyEvent(event);
3406 }
3407}
3408
Jeff Leger178b1e52020-10-05 12:22:23 -04003409bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3410 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3411 bool skip = false;
3412 const auto *cb_context = GetAccessContext(commandBuffer);
3413 assert(cb_context);
3414 if (!cb_context) return skip;
3415 const auto *context = cb_context->GetCurrentAccessContext();
3416
3417 // If we have no previous accesses, we have no hazards
3418 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3419 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3420
3421 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3422 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3423 if (src_buffer) {
3424 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003425 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003426 if (hazard.hazard) {
3427 // TODO -- add tag information to log msg when useful.
3428 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3429 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3430 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003431 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003432 }
3433 }
3434 if (dst_buffer && !skip) {
3435 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003436 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003437 if (hazard.hazard) {
3438 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3439 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3440 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003441 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003442 }
3443 }
3444 if (skip) break;
3445 }
3446 return skip;
3447}
3448
3449void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3450 auto *cb_context = GetAccessContext(commandBuffer);
3451 assert(cb_context);
3452 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3453 auto *context = cb_context->GetCurrentAccessContext();
3454
3455 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3456 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3457
3458 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3459 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3460 if (src_buffer) {
3461 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003462 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003463 }
3464 if (dst_buffer) {
3465 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003466 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003467 }
3468 }
3469}
3470
John Zulauf5c5e88d2019-12-26 11:22:02 -07003471bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3472 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3473 const VkImageCopy *pRegions) const {
3474 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003475 const auto *cb_access_context = GetAccessContext(commandBuffer);
3476 assert(cb_access_context);
3477 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003478
John Zulauf3d84f1b2020-03-09 13:33:25 -06003479 const auto *context = cb_access_context->GetCurrentAccessContext();
3480 assert(context);
3481 if (!context) return skip;
3482
3483 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3484 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003485 for (uint32_t region = 0; region < regionCount; region++) {
3486 const auto &copy_region = pRegions[region];
3487 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003488 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003489 copy_region.srcOffset, copy_region.extent);
3490 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003491 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003492 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003493 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003494 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003495 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003496 }
3497
3498 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003499 VkExtent3D dst_copy_extent =
3500 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003501 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003502 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003503 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003504 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003505 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003506 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003507 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003508 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003509 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003510 }
3511 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003512
John Zulauf5c5e88d2019-12-26 11:22:02 -07003513 return skip;
3514}
3515
3516void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3517 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3518 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003519 auto *cb_access_context = GetAccessContext(commandBuffer);
3520 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003521 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003522 auto *context = cb_access_context->GetCurrentAccessContext();
3523 assert(context);
3524
John Zulauf5c5e88d2019-12-26 11:22:02 -07003525 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003526 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003527
3528 for (uint32_t region = 0; region < regionCount; region++) {
3529 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003530 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003531 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003532 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003533 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003534 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003535 VkExtent3D dst_copy_extent =
3536 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003537 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003538 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003539 }
3540 }
3541}
3542
Jeff Leger178b1e52020-10-05 12:22:23 -04003543bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3544 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3545 bool skip = false;
3546 const auto *cb_access_context = GetAccessContext(commandBuffer);
3547 assert(cb_access_context);
3548 if (!cb_access_context) return skip;
3549
3550 const auto *context = cb_access_context->GetCurrentAccessContext();
3551 assert(context);
3552 if (!context) return skip;
3553
3554 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3555 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3556 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3557 const auto &copy_region = pCopyImageInfo->pRegions[region];
3558 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003559 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003560 copy_region.srcOffset, copy_region.extent);
3561 if (hazard.hazard) {
3562 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3563 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3564 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003565 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003566 }
3567 }
3568
3569 if (dst_image) {
3570 VkExtent3D dst_copy_extent =
3571 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003572 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003573 copy_region.dstOffset, dst_copy_extent);
3574 if (hazard.hazard) {
3575 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3576 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3577 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003578 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003579 }
3580 if (skip) break;
3581 }
3582 }
3583
3584 return skip;
3585}
3586
3587void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3588 auto *cb_access_context = GetAccessContext(commandBuffer);
3589 assert(cb_access_context);
3590 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3591 auto *context = cb_access_context->GetCurrentAccessContext();
3592 assert(context);
3593
3594 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3595 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3596
3597 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3598 const auto &copy_region = pCopyImageInfo->pRegions[region];
3599 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003600 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003601 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003602 }
3603 if (dst_image) {
3604 VkExtent3D dst_copy_extent =
3605 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003606 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003607 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003608 }
3609 }
3610}
3611
John Zulauf9cb530d2019-09-30 14:14:10 -06003612bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3613 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3614 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3615 uint32_t bufferMemoryBarrierCount,
3616 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3617 uint32_t imageMemoryBarrierCount,
3618 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3619 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003620 const auto *cb_access_context = GetAccessContext(commandBuffer);
3621 assert(cb_access_context);
3622 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003623
John Zulauf36ef9282021-02-02 11:47:24 -07003624 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3625 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3626 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3627 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003628 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003629 return skip;
3630}
3631
3632void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3633 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3634 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3635 uint32_t bufferMemoryBarrierCount,
3636 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3637 uint32_t imageMemoryBarrierCount,
3638 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003639 auto *cb_access_context = GetAccessContext(commandBuffer);
3640 assert(cb_access_context);
3641 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003642
John Zulauf8eda1562021-04-13 17:06:41 -06003643 CommandBufferAccessContext::SyncOpPointer sync_op(
3644 new SyncOpPipelineBarrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask, dstStageMask,
3645 dependencyFlags, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
3646 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers));
3647 const auto tag = sync_op->Record(cb_access_context);
3648 cb_access_context->AddSyncOp(tag, std::move(sync_op));
John Zulauf9cb530d2019-09-30 14:14:10 -06003649}
3650
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003651bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3652 const VkDependencyInfoKHR *pDependencyInfo) const {
3653 bool skip = false;
3654 const auto *cb_access_context = GetAccessContext(commandBuffer);
3655 assert(cb_access_context);
3656 if (!cb_access_context) return skip;
3657
3658 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3659 skip = pipeline_barrier.Validate(*cb_access_context);
3660 return skip;
3661}
3662
3663void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3664 auto *cb_access_context = GetAccessContext(commandBuffer);
3665 assert(cb_access_context);
3666 if (!cb_access_context) return;
3667
John Zulauf8eda1562021-04-13 17:06:41 -06003668 CommandBufferAccessContext::SyncOpPointer sync_op(
3669 new SyncOpPipelineBarrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo));
3670 const auto tag = sync_op->Record(cb_access_context);
3671 cb_access_context->AddSyncOp(tag, std::move(sync_op));
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003672}
3673
John Zulauf9cb530d2019-09-30 14:14:10 -06003674void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3675 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3676 // The state tracker sets up the device state
3677 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3678
John Zulauf5f13a792020-03-10 07:31:21 -06003679 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3680 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003681 // TODO: Find a good way to do this hooklessly.
3682 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3683 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3684 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3685
John Zulaufd1f85d42020-04-15 12:23:15 -06003686 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3687 sync_device_state->ResetCommandBufferCallback(command_buffer);
3688 });
3689 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3690 sync_device_state->FreeCommandBufferCallback(command_buffer);
3691 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003692}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003693
John Zulauf355e49b2020-04-24 15:11:15 -06003694bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003695 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003696 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003697 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003698 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003699 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003700 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003701 }
John Zulauf355e49b2020-04-24 15:11:15 -06003702 return skip;
3703}
3704
3705bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3706 VkSubpassContents contents) const {
3707 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003708 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003709 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003710 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003711 return skip;
3712}
3713
3714bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003715 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003716 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003717 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003718 return skip;
3719}
3720
3721bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3722 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003723 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003724 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003725 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003726 return skip;
3727}
3728
John Zulauf3d84f1b2020-03-09 13:33:25 -06003729void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3730 VkResult result) {
3731 // The state tracker sets up the command buffer state
3732 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3733
3734 // Create/initialize the structure that trackers accesses at the command buffer scope.
3735 auto cb_access_context = GetAccessContext(commandBuffer);
3736 assert(cb_access_context);
3737 cb_access_context->Reset();
3738}
3739
3740void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003741 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003742 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003743 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003744 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003745 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003746 }
3747}
3748
3749void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3750 VkSubpassContents contents) {
3751 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003752 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003753 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003754 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003755}
3756
3757void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3758 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3759 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003760 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003761}
3762
3763void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3764 const VkRenderPassBeginInfo *pRenderPassBegin,
3765 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3766 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003767 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003768}
3769
Mike Schuchardt2df08912020-12-15 16:28:09 -08003770bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003771 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003772 bool skip = false;
3773
3774 auto cb_context = GetAccessContext(commandBuffer);
3775 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003776 if (!cb_context) return skip;
sfricke-samsung85584a72021-09-30 21:43:38 -07003777 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003778 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003779}
3780
3781bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3782 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003783 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003784 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003785 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003786 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3787 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003788 return skip;
3789}
3790
Mike Schuchardt2df08912020-12-15 16:28:09 -08003791bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3792 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003793 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003794 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003795 return skip;
3796}
3797
3798bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3799 const VkSubpassEndInfo *pSubpassEndInfo) const {
3800 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003801 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003802 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003803}
3804
3805void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003806 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003807 auto cb_context = GetAccessContext(commandBuffer);
3808 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003809 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003810
sfricke-samsung85584a72021-09-30 21:43:38 -07003811 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003812 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003813}
3814
3815void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3816 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003817 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003818 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003819 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003820}
3821
3822void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3823 const VkSubpassEndInfo *pSubpassEndInfo) {
3824 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003825 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003826}
3827
3828void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3829 const VkSubpassEndInfo *pSubpassEndInfo) {
3830 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003831 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003832}
3833
sfricke-samsung85584a72021-09-30 21:43:38 -07003834bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3835 CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003836 bool skip = false;
3837
3838 auto cb_context = GetAccessContext(commandBuffer);
3839 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003840 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003841
sfricke-samsung85584a72021-09-30 21:43:38 -07003842 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003843 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003844 return skip;
3845}
3846
3847bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3848 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003849 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003850 return skip;
3851}
3852
Mike Schuchardt2df08912020-12-15 16:28:09 -08003853bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003854 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003855 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003856 return skip;
3857}
3858
3859bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003860 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003861 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003862 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003863 return skip;
3864}
3865
sfricke-samsung85584a72021-09-30 21:43:38 -07003866void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003867 // Resolve the all subpass contexts to the command buffer contexts
3868 auto cb_context = GetAccessContext(commandBuffer);
3869 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003870 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003871
sfricke-samsung85584a72021-09-30 21:43:38 -07003872 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003873 sync_op.Record(cb_context);
3874 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003875}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003876
John Zulauf33fc1d52020-07-17 11:01:10 -06003877// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3878// updates to a resource which do not conflict at the byte level.
3879// TODO: Revisit this rule to see if it needs to be tighter or looser
3880// TODO: Add programatic control over suppression heuristics
3881bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3882 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3883}
3884
John Zulauf3d84f1b2020-03-09 13:33:25 -06003885void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003886 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003887 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003888}
3889
3890void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003891 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003892 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003893}
3894
3895void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003896 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf5a1a5382020-06-22 17:23:25 -06003897 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003898}
locke-lunarga19c71d2020-03-02 18:17:04 -07003899
Jeff Leger178b1e52020-10-05 12:22:23 -04003900template <typename BufferImageCopyRegionType>
3901bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3902 VkImageLayout dstImageLayout, uint32_t regionCount,
3903 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003904 bool skip = false;
3905 const auto *cb_access_context = GetAccessContext(commandBuffer);
3906 assert(cb_access_context);
3907 if (!cb_access_context) return skip;
3908
Jeff Leger178b1e52020-10-05 12:22:23 -04003909 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3910 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3911
locke-lunarga19c71d2020-03-02 18:17:04 -07003912 const auto *context = cb_access_context->GetCurrentAccessContext();
3913 assert(context);
3914 if (!context) return skip;
3915
3916 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003917 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3918
3919 for (uint32_t region = 0; region < regionCount; region++) {
3920 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003921 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003922 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003923 if (src_buffer) {
3924 ResourceAccessRange src_range =
3925 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003926 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07003927 if (hazard.hazard) {
3928 // PHASE1 TODO -- add tag information to log msg when useful.
3929 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3930 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3931 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003932 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003933 }
3934 }
3935
Jeremy Gebben40a22942020-12-22 14:22:06 -07003936 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07003937 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003938 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003939 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003940 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003941 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003942 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003943 }
3944 if (skip) break;
3945 }
3946 if (skip) break;
3947 }
3948 return skip;
3949}
3950
Jeff Leger178b1e52020-10-05 12:22:23 -04003951bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3952 VkImageLayout dstImageLayout, uint32_t regionCount,
3953 const VkBufferImageCopy *pRegions) const {
3954 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3955 COPY_COMMAND_VERSION_1);
3956}
3957
3958bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3959 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3960 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3961 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3962 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3963}
3964
3965template <typename BufferImageCopyRegionType>
3966void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3967 VkImageLayout dstImageLayout, uint32_t regionCount,
3968 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003969 auto *cb_access_context = GetAccessContext(commandBuffer);
3970 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003971
3972 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3973 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3974
3975 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003976 auto *context = cb_access_context->GetCurrentAccessContext();
3977 assert(context);
3978
3979 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003980 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003981
3982 for (uint32_t region = 0; region < regionCount; region++) {
3983 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07003984 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003985 if (src_buffer) {
3986 ResourceAccessRange src_range =
3987 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003988 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003989 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07003990 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003991 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003992 }
3993 }
3994}
3995
Jeff Leger178b1e52020-10-05 12:22:23 -04003996void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3997 VkImageLayout dstImageLayout, uint32_t regionCount,
3998 const VkBufferImageCopy *pRegions) {
3999 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
4000 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4001}
4002
4003void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4004 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4005 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4006 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4007 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4008 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4009}
4010
4011template <typename BufferImageCopyRegionType>
4012bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4013 VkBuffer dstBuffer, uint32_t regionCount,
4014 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004015 bool skip = false;
4016 const auto *cb_access_context = GetAccessContext(commandBuffer);
4017 assert(cb_access_context);
4018 if (!cb_access_context) return skip;
4019
Jeff Leger178b1e52020-10-05 12:22:23 -04004020 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4021 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
4022
locke-lunarga19c71d2020-03-02 18:17:04 -07004023 const auto *context = cb_access_context->GetCurrentAccessContext();
4024 assert(context);
4025 if (!context) return skip;
4026
4027 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4028 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004029 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
locke-lunarga19c71d2020-03-02 18:17:04 -07004030 for (uint32_t region = 0; region < regionCount; region++) {
4031 const auto &copy_region = pRegions[region];
4032 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004033 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004034 copy_region.imageOffset, copy_region.imageExtent);
4035 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004036 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004037 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004038 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004039 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004040 }
John Zulauf477700e2021-01-06 11:41:49 -07004041 if (dst_mem) {
4042 ResourceAccessRange dst_range =
4043 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004044 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07004045 if (hazard.hazard) {
4046 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4047 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4048 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004049 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07004050 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004051 }
4052 }
4053 if (skip) break;
4054 }
4055 return skip;
4056}
4057
Jeff Leger178b1e52020-10-05 12:22:23 -04004058bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4059 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4060 const VkBufferImageCopy *pRegions) const {
4061 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
4062 COPY_COMMAND_VERSION_1);
4063}
4064
4065bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4066 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4067 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4068 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4069 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4070}
4071
4072template <typename BufferImageCopyRegionType>
4073void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4074 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
4075 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004076 auto *cb_access_context = GetAccessContext(commandBuffer);
4077 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004078
4079 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4080 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
4081
4082 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004083 auto *context = cb_access_context->GetCurrentAccessContext();
4084 assert(context);
4085
4086 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004087 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004088 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06004089 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004090
4091 for (uint32_t region = 0; region < regionCount; region++) {
4092 const auto &copy_region = pRegions[region];
4093 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004094 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004095 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004096 if (dst_buffer) {
4097 ResourceAccessRange dst_range =
4098 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004099 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004100 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004101 }
4102 }
4103}
4104
Jeff Leger178b1e52020-10-05 12:22:23 -04004105void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4106 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4107 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
4108 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4109}
4110
4111void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4112 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4113 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4114 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4115 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4116 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4117}
4118
4119template <typename RegionType>
4120bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4121 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4122 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004123 bool skip = false;
4124 const auto *cb_access_context = GetAccessContext(commandBuffer);
4125 assert(cb_access_context);
4126 if (!cb_access_context) return skip;
4127
4128 const auto *context = cb_access_context->GetCurrentAccessContext();
4129 assert(context);
4130 if (!context) return skip;
4131
4132 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4133 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4134
4135 for (uint32_t region = 0; region < regionCount; region++) {
4136 const auto &blit_region = pRegions[region];
4137 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004138 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4139 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4140 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4141 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4142 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4143 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004144 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004145 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004146 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004147 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004148 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004149 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004150 }
4151 }
4152
4153 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004154 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4155 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4156 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4157 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4158 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4159 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004160 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004161 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004162 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004163 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004164 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004165 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004166 }
4167 if (skip) break;
4168 }
4169 }
4170
4171 return skip;
4172}
4173
Jeff Leger178b1e52020-10-05 12:22:23 -04004174bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4175 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4176 const VkImageBlit *pRegions, VkFilter filter) const {
4177 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4178 "vkCmdBlitImage");
4179}
4180
4181bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4182 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4183 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4184 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4185 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4186}
4187
4188template <typename RegionType>
4189void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4190 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4191 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004192 auto *cb_access_context = GetAccessContext(commandBuffer);
4193 assert(cb_access_context);
4194 auto *context = cb_access_context->GetCurrentAccessContext();
4195 assert(context);
4196
4197 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004198 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004199
4200 for (uint32_t region = 0; region < regionCount; region++) {
4201 const auto &blit_region = pRegions[region];
4202 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004203 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4204 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4205 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4206 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4207 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4208 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004209 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004210 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004211 }
4212 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004213 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4214 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4215 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4216 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4217 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4218 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004219 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004220 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004221 }
4222 }
4223}
locke-lunarg36ba2592020-04-03 09:42:04 -06004224
Jeff Leger178b1e52020-10-05 12:22:23 -04004225void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4226 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4227 const VkImageBlit *pRegions, VkFilter filter) {
4228 auto *cb_access_context = GetAccessContext(commandBuffer);
4229 assert(cb_access_context);
4230 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4231 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4232 pRegions, filter);
4233 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4234}
4235
4236void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4237 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4238 auto *cb_access_context = GetAccessContext(commandBuffer);
4239 assert(cb_access_context);
4240 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4241 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4242 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4243 pBlitImageInfo->filter, tag);
4244}
4245
John Zulauffaea0ee2021-01-14 14:01:32 -07004246bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4247 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
4248 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
4249 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004250 bool skip = false;
4251 if (drawCount == 0) return skip;
4252
4253 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4254 VkDeviceSize size = struct_size;
4255 if (drawCount == 1 || stride == size) {
4256 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004257 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004258 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4259 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004260 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004261 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004262 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004263 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004264 }
4265 } else {
4266 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004267 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004268 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4269 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004270 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004271 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4272 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004273 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004274 break;
4275 }
4276 }
4277 }
4278 return skip;
4279}
4280
John Zulauf14940722021-04-12 15:19:02 -06004281void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag tag, const VkDeviceSize struct_size,
locke-lunarg61870c22020-06-09 14:51:50 -06004282 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4283 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06004284 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4285 VkDeviceSize size = struct_size;
4286 if (drawCount == 1 || stride == size) {
4287 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004288 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004289 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004290 } else {
4291 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004292 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004293 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4294 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004295 }
4296 }
4297}
4298
John Zulauffaea0ee2021-01-14 14:01:32 -07004299bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4300 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4301 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004302 bool skip = false;
4303
4304 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004305 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004306 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4307 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004308 skip |= LogError(count_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004309 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004310 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004311 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004312 }
4313 return skip;
4314}
4315
John Zulauf14940722021-04-12 15:19:02 -06004316void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06004317 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004318 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004319 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004320}
4321
locke-lunarg36ba2592020-04-03 09:42:04 -06004322bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004323 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004324 const auto *cb_access_context = GetAccessContext(commandBuffer);
4325 assert(cb_access_context);
4326 if (!cb_access_context) return skip;
4327
locke-lunarg61870c22020-06-09 14:51:50 -06004328 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004329 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004330}
4331
4332void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004333 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004334 auto *cb_access_context = GetAccessContext(commandBuffer);
4335 assert(cb_access_context);
4336 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004337
locke-lunarg61870c22020-06-09 14:51:50 -06004338 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004339}
locke-lunarge1a67022020-04-29 00:15:36 -06004340
4341bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004342 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004343 const auto *cb_access_context = GetAccessContext(commandBuffer);
4344 assert(cb_access_context);
4345 if (!cb_access_context) return skip;
4346
4347 const auto *context = cb_access_context->GetCurrentAccessContext();
4348 assert(context);
4349 if (!context) return skip;
4350
locke-lunarg61870c22020-06-09 14:51:50 -06004351 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004352 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4353 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004354 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004355}
4356
4357void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004358 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004359 auto *cb_access_context = GetAccessContext(commandBuffer);
4360 assert(cb_access_context);
4361 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4362 auto *context = cb_access_context->GetCurrentAccessContext();
4363 assert(context);
4364
locke-lunarg61870c22020-06-09 14:51:50 -06004365 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4366 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004367}
4368
4369bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4370 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004371 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004372 const auto *cb_access_context = GetAccessContext(commandBuffer);
4373 assert(cb_access_context);
4374 if (!cb_access_context) return skip;
4375
locke-lunarg61870c22020-06-09 14:51:50 -06004376 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4377 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4378 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004379 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004380}
4381
4382void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4383 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004384 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004385 auto *cb_access_context = GetAccessContext(commandBuffer);
4386 assert(cb_access_context);
4387 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004388
locke-lunarg61870c22020-06-09 14:51:50 -06004389 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4390 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4391 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004392}
4393
4394bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4395 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004396 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004397 const auto *cb_access_context = GetAccessContext(commandBuffer);
4398 assert(cb_access_context);
4399 if (!cb_access_context) return skip;
4400
locke-lunarg61870c22020-06-09 14:51:50 -06004401 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4402 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4403 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004404 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004405}
4406
4407void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4408 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004409 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004410 auto *cb_access_context = GetAccessContext(commandBuffer);
4411 assert(cb_access_context);
4412 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004413
locke-lunarg61870c22020-06-09 14:51:50 -06004414 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4415 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4416 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004417}
4418
4419bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4420 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004421 bool skip = false;
4422 if (drawCount == 0) return skip;
4423
locke-lunargff255f92020-05-13 18:53:52 -06004424 const auto *cb_access_context = GetAccessContext(commandBuffer);
4425 assert(cb_access_context);
4426 if (!cb_access_context) return skip;
4427
4428 const auto *context = cb_access_context->GetCurrentAccessContext();
4429 assert(context);
4430 if (!context) return skip;
4431
locke-lunarg61870c22020-06-09 14:51:50 -06004432 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4433 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004434 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4435 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004436
4437 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4438 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4439 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004440 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004441 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004442}
4443
4444void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4445 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004446 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004447 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004448 auto *cb_access_context = GetAccessContext(commandBuffer);
4449 assert(cb_access_context);
4450 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4451 auto *context = cb_access_context->GetCurrentAccessContext();
4452 assert(context);
4453
locke-lunarg61870c22020-06-09 14:51:50 -06004454 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4455 cb_access_context->RecordDrawSubpassAttachment(tag);
4456 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004457
4458 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4459 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4460 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004461 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004462}
4463
4464bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4465 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004466 bool skip = false;
4467 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004468 const auto *cb_access_context = GetAccessContext(commandBuffer);
4469 assert(cb_access_context);
4470 if (!cb_access_context) return skip;
4471
4472 const auto *context = cb_access_context->GetCurrentAccessContext();
4473 assert(context);
4474 if (!context) return skip;
4475
locke-lunarg61870c22020-06-09 14:51:50 -06004476 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4477 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004478 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4479 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004480
4481 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4482 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4483 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004484 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004485 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004486}
4487
4488void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4489 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004490 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004491 auto *cb_access_context = GetAccessContext(commandBuffer);
4492 assert(cb_access_context);
4493 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4494 auto *context = cb_access_context->GetCurrentAccessContext();
4495 assert(context);
4496
locke-lunarg61870c22020-06-09 14:51:50 -06004497 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4498 cb_access_context->RecordDrawSubpassAttachment(tag);
4499 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004500
4501 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4502 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4503 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004504 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004505}
4506
4507bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4508 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4509 uint32_t stride, const char *function) const {
4510 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004511 const auto *cb_access_context = GetAccessContext(commandBuffer);
4512 assert(cb_access_context);
4513 if (!cb_access_context) return skip;
4514
4515 const auto *context = cb_access_context->GetCurrentAccessContext();
4516 assert(context);
4517 if (!context) return skip;
4518
locke-lunarg61870c22020-06-09 14:51:50 -06004519 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4520 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004521 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4522 maxDrawCount, stride, function);
4523 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004524
4525 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4526 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4527 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004528 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004529 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004530}
4531
4532bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4533 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4534 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004535 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4536 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004537}
4538
sfricke-samsung85584a72021-09-30 21:43:38 -07004539void SyncValidator::RecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4540 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4541 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004542 auto *cb_access_context = GetAccessContext(commandBuffer);
4543 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004544 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004545 auto *context = cb_access_context->GetCurrentAccessContext();
4546 assert(context);
4547
locke-lunarg61870c22020-06-09 14:51:50 -06004548 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4549 cb_access_context->RecordDrawSubpassAttachment(tag);
4550 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4551 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004552
4553 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4554 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4555 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004556 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004557}
4558
sfricke-samsung85584a72021-09-30 21:43:38 -07004559void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4560 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4561 uint32_t stride) {
4562 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4563 stride);
4564 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4565 CMD_DRAWINDIRECTCOUNT);
4566}
locke-lunarge1a67022020-04-29 00:15:36 -06004567bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4568 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4569 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004570 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4571 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004572}
4573
4574void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4575 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4576 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004577 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4578 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004579 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4580 CMD_DRAWINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004581}
4582
4583bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4584 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4585 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004586 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4587 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004588}
4589
4590void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4591 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4592 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004593 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4594 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004595 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4596 CMD_DRAWINDIRECTCOUNTAMD);
locke-lunargff255f92020-05-13 18:53:52 -06004597}
4598
4599bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4600 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4601 uint32_t stride, const char *function) const {
4602 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004603 const auto *cb_access_context = GetAccessContext(commandBuffer);
4604 assert(cb_access_context);
4605 if (!cb_access_context) return skip;
4606
4607 const auto *context = cb_access_context->GetCurrentAccessContext();
4608 assert(context);
4609 if (!context) return skip;
4610
locke-lunarg61870c22020-06-09 14:51:50 -06004611 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4612 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004613 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4614 offset, maxDrawCount, stride, function);
4615 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004616
4617 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4618 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4619 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004620 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004621 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004622}
4623
4624bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4625 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4626 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004627 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4628 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004629}
4630
sfricke-samsung85584a72021-09-30 21:43:38 -07004631void SyncValidator::RecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4632 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4633 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004634 auto *cb_access_context = GetAccessContext(commandBuffer);
4635 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004636 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004637 auto *context = cb_access_context->GetCurrentAccessContext();
4638 assert(context);
4639
locke-lunarg61870c22020-06-09 14:51:50 -06004640 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4641 cb_access_context->RecordDrawSubpassAttachment(tag);
4642 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4643 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004644
4645 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4646 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004647 // We will update the index and vertex buffer in SubmitQueue in the future.
4648 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004649}
4650
sfricke-samsung85584a72021-09-30 21:43:38 -07004651void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4652 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4653 uint32_t maxDrawCount, uint32_t stride) {
4654 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4655 maxDrawCount, stride);
4656 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4657 CMD_DRAWINDEXEDINDIRECTCOUNT);
4658}
4659
locke-lunarge1a67022020-04-29 00:15:36 -06004660bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4661 VkDeviceSize offset, VkBuffer countBuffer,
4662 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4663 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004664 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4665 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004666}
4667
4668void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4669 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4670 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004671 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4672 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004673 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4674 CMD_DRAWINDEXEDINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004675}
4676
4677bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4678 VkDeviceSize offset, VkBuffer countBuffer,
4679 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4680 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004681 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4682 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004683}
4684
4685void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4686 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4687 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004688 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4689 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004690 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4691 CMD_DRAWINDEXEDINDIRECTCOUNTAMD);
locke-lunarge1a67022020-04-29 00:15:36 -06004692}
4693
4694bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4695 const VkClearColorValue *pColor, uint32_t rangeCount,
4696 const VkImageSubresourceRange *pRanges) const {
4697 bool skip = false;
4698 const auto *cb_access_context = GetAccessContext(commandBuffer);
4699 assert(cb_access_context);
4700 if (!cb_access_context) return skip;
4701
4702 const auto *context = cb_access_context->GetCurrentAccessContext();
4703 assert(context);
4704 if (!context) return skip;
4705
4706 const auto *image_state = Get<IMAGE_STATE>(image);
4707
4708 for (uint32_t index = 0; index < rangeCount; index++) {
4709 const auto &range = pRanges[index];
4710 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004711 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004712 if (hazard.hazard) {
4713 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004714 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004715 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004716 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004717 }
4718 }
4719 }
4720 return skip;
4721}
4722
4723void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4724 const VkClearColorValue *pColor, uint32_t rangeCount,
4725 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004726 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004727 auto *cb_access_context = GetAccessContext(commandBuffer);
4728 assert(cb_access_context);
4729 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4730 auto *context = cb_access_context->GetCurrentAccessContext();
4731 assert(context);
4732
4733 const auto *image_state = Get<IMAGE_STATE>(image);
4734
4735 for (uint32_t index = 0; index < rangeCount; index++) {
4736 const auto &range = pRanges[index];
4737 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004738 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004739 }
4740 }
4741}
4742
4743bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4744 VkImageLayout imageLayout,
4745 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4746 const VkImageSubresourceRange *pRanges) const {
4747 bool skip = false;
4748 const auto *cb_access_context = GetAccessContext(commandBuffer);
4749 assert(cb_access_context);
4750 if (!cb_access_context) return skip;
4751
4752 const auto *context = cb_access_context->GetCurrentAccessContext();
4753 assert(context);
4754 if (!context) return skip;
4755
4756 const auto *image_state = Get<IMAGE_STATE>(image);
4757
4758 for (uint32_t index = 0; index < rangeCount; index++) {
4759 const auto &range = pRanges[index];
4760 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004761 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004762 if (hazard.hazard) {
4763 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004764 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004765 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004766 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004767 }
4768 }
4769 }
4770 return skip;
4771}
4772
4773void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4774 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4775 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004776 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004777 auto *cb_access_context = GetAccessContext(commandBuffer);
4778 assert(cb_access_context);
4779 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4780 auto *context = cb_access_context->GetCurrentAccessContext();
4781 assert(context);
4782
4783 const auto *image_state = Get<IMAGE_STATE>(image);
4784
4785 for (uint32_t index = 0; index < rangeCount; index++) {
4786 const auto &range = pRanges[index];
4787 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004788 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004789 }
4790 }
4791}
4792
4793bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4794 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4795 VkDeviceSize dstOffset, VkDeviceSize stride,
4796 VkQueryResultFlags flags) const {
4797 bool skip = false;
4798 const auto *cb_access_context = GetAccessContext(commandBuffer);
4799 assert(cb_access_context);
4800 if (!cb_access_context) return skip;
4801
4802 const auto *context = cb_access_context->GetCurrentAccessContext();
4803 assert(context);
4804 if (!context) return skip;
4805
4806 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4807
4808 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004809 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004810 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004811 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004812 skip |=
4813 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4814 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004815 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004816 }
4817 }
locke-lunargff255f92020-05-13 18:53:52 -06004818
4819 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004820 return skip;
4821}
4822
4823void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4824 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4825 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004826 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4827 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004828 auto *cb_access_context = GetAccessContext(commandBuffer);
4829 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004830 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004831 auto *context = cb_access_context->GetCurrentAccessContext();
4832 assert(context);
4833
4834 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4835
4836 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004837 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004838 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004839 }
locke-lunargff255f92020-05-13 18:53:52 -06004840
4841 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004842}
4843
4844bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4845 VkDeviceSize size, uint32_t data) const {
4846 bool skip = false;
4847 const auto *cb_access_context = GetAccessContext(commandBuffer);
4848 assert(cb_access_context);
4849 if (!cb_access_context) return skip;
4850
4851 const auto *context = cb_access_context->GetCurrentAccessContext();
4852 assert(context);
4853 if (!context) return skip;
4854
4855 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4856
4857 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004858 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004859 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004860 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004861 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004862 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004863 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004864 }
4865 }
4866 return skip;
4867}
4868
4869void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4870 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004871 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004872 auto *cb_access_context = GetAccessContext(commandBuffer);
4873 assert(cb_access_context);
4874 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4875 auto *context = cb_access_context->GetCurrentAccessContext();
4876 assert(context);
4877
4878 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4879
4880 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004881 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004882 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004883 }
4884}
4885
4886bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4887 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4888 const VkImageResolve *pRegions) const {
4889 bool skip = false;
4890 const auto *cb_access_context = GetAccessContext(commandBuffer);
4891 assert(cb_access_context);
4892 if (!cb_access_context) return skip;
4893
4894 const auto *context = cb_access_context->GetCurrentAccessContext();
4895 assert(context);
4896 if (!context) return skip;
4897
4898 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4899 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4900
4901 for (uint32_t region = 0; region < regionCount; region++) {
4902 const auto &resolve_region = pRegions[region];
4903 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004904 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004905 resolve_region.srcOffset, resolve_region.extent);
4906 if (hazard.hazard) {
4907 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004908 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004909 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004910 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004911 }
4912 }
4913
4914 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004915 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004916 resolve_region.dstOffset, resolve_region.extent);
4917 if (hazard.hazard) {
4918 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004919 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004920 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004921 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004922 }
4923 if (skip) break;
4924 }
4925 }
4926
4927 return skip;
4928}
4929
4930void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4931 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4932 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004933 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4934 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004935 auto *cb_access_context = GetAccessContext(commandBuffer);
4936 assert(cb_access_context);
4937 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4938 auto *context = cb_access_context->GetCurrentAccessContext();
4939 assert(context);
4940
4941 auto *src_image = Get<IMAGE_STATE>(srcImage);
4942 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4943
4944 for (uint32_t region = 0; region < regionCount; region++) {
4945 const auto &resolve_region = pRegions[region];
4946 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004947 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004948 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004949 }
4950 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004951 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004952 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004953 }
4954 }
4955}
4956
Jeff Leger178b1e52020-10-05 12:22:23 -04004957bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4958 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4959 bool skip = false;
4960 const auto *cb_access_context = GetAccessContext(commandBuffer);
4961 assert(cb_access_context);
4962 if (!cb_access_context) return skip;
4963
4964 const auto *context = cb_access_context->GetCurrentAccessContext();
4965 assert(context);
4966 if (!context) return skip;
4967
4968 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4969 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4970
4971 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4972 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4973 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004974 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004975 resolve_region.srcOffset, resolve_region.extent);
4976 if (hazard.hazard) {
4977 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4978 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4979 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004980 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004981 }
4982 }
4983
4984 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004985 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004986 resolve_region.dstOffset, resolve_region.extent);
4987 if (hazard.hazard) {
4988 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4989 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4990 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004991 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004992 }
4993 if (skip) break;
4994 }
4995 }
4996
4997 return skip;
4998}
4999
5000void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5001 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5002 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5003 auto *cb_access_context = GetAccessContext(commandBuffer);
5004 assert(cb_access_context);
5005 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
5006 auto *context = cb_access_context->GetCurrentAccessContext();
5007 assert(context);
5008
5009 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5010 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5011
5012 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5013 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5014 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005015 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005016 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005017 }
5018 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005019 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005020 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005021 }
5022 }
5023}
5024
locke-lunarge1a67022020-04-29 00:15:36 -06005025bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5026 VkDeviceSize dataSize, const void *pData) const {
5027 bool skip = false;
5028 const auto *cb_access_context = GetAccessContext(commandBuffer);
5029 assert(cb_access_context);
5030 if (!cb_access_context) return skip;
5031
5032 const auto *context = cb_access_context->GetCurrentAccessContext();
5033 assert(context);
5034 if (!context) return skip;
5035
5036 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5037
5038 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005039 // VK_WHOLE_SIZE not allowed
5040 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005041 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005042 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005043 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005044 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005045 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005046 }
5047 }
5048 return skip;
5049}
5050
5051void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5052 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005053 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005054 auto *cb_access_context = GetAccessContext(commandBuffer);
5055 assert(cb_access_context);
5056 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5057 auto *context = cb_access_context->GetCurrentAccessContext();
5058 assert(context);
5059
5060 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5061
5062 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005063 // VK_WHOLE_SIZE not allowed
5064 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005065 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005066 }
5067}
locke-lunargff255f92020-05-13 18:53:52 -06005068
5069bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5070 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5071 bool skip = false;
5072 const auto *cb_access_context = GetAccessContext(commandBuffer);
5073 assert(cb_access_context);
5074 if (!cb_access_context) return skip;
5075
5076 const auto *context = cb_access_context->GetCurrentAccessContext();
5077 assert(context);
5078 if (!context) return skip;
5079
5080 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5081
5082 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005083 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005084 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06005085 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005086 skip |=
5087 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5088 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005089 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005090 }
5091 }
5092 return skip;
5093}
5094
5095void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5096 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005097 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005098 auto *cb_access_context = GetAccessContext(commandBuffer);
5099 assert(cb_access_context);
5100 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5101 auto *context = cb_access_context->GetCurrentAccessContext();
5102 assert(context);
5103
5104 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5105
5106 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005107 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005108 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06005109 }
5110}
John Zulauf49beb112020-11-04 16:06:31 -07005111
5112bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5113 bool skip = false;
5114 const auto *cb_context = GetAccessContext(commandBuffer);
5115 assert(cb_context);
5116 if (!cb_context) return skip;
5117
John Zulauf36ef9282021-02-02 11:47:24 -07005118 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005119 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005120}
5121
5122void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5123 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5124 auto *cb_context = GetAccessContext(commandBuffer);
5125 assert(cb_context);
5126 if (!cb_context) return;
John Zulauf36ef9282021-02-02 11:47:24 -07005127 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
5128 set_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005129}
5130
John Zulauf4edde622021-02-15 08:54:50 -07005131bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5132 const VkDependencyInfoKHR *pDependencyInfo) const {
5133 bool skip = false;
5134 const auto *cb_context = GetAccessContext(commandBuffer);
5135 assert(cb_context);
5136 if (!cb_context || !pDependencyInfo) return skip;
5137
5138 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5139 return set_event_op.Validate(*cb_context);
5140}
5141
5142void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5143 const VkDependencyInfoKHR *pDependencyInfo) {
5144 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
5145 auto *cb_context = GetAccessContext(commandBuffer);
5146 assert(cb_context);
5147 if (!cb_context || !pDependencyInfo) return;
5148
5149 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5150 set_event_op.Record(cb_context);
5151}
5152
John Zulauf49beb112020-11-04 16:06:31 -07005153bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5154 VkPipelineStageFlags stageMask) const {
5155 bool skip = false;
5156 const auto *cb_context = GetAccessContext(commandBuffer);
5157 assert(cb_context);
5158 if (!cb_context) return skip;
5159
John Zulauf36ef9282021-02-02 11:47:24 -07005160 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005161 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005162}
5163
5164void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5165 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5166 auto *cb_context = GetAccessContext(commandBuffer);
5167 assert(cb_context);
5168 if (!cb_context) return;
5169
John Zulauf36ef9282021-02-02 11:47:24 -07005170 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
5171 reset_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005172}
5173
John Zulauf4edde622021-02-15 08:54:50 -07005174bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5175 VkPipelineStageFlags2KHR stageMask) const {
5176 bool skip = false;
5177 const auto *cb_context = GetAccessContext(commandBuffer);
5178 assert(cb_context);
5179 if (!cb_context) return skip;
5180
5181 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
5182 return reset_event_op.Validate(*cb_context);
5183}
5184
5185void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5186 VkPipelineStageFlags2KHR stageMask) {
5187 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
5188 auto *cb_context = GetAccessContext(commandBuffer);
5189 assert(cb_context);
5190 if (!cb_context) return;
5191
5192 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
5193 reset_event_op.Record(cb_context);
5194}
5195
John Zulauf49beb112020-11-04 16:06:31 -07005196bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5197 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5198 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5199 uint32_t bufferMemoryBarrierCount,
5200 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5201 uint32_t imageMemoryBarrierCount,
5202 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5203 bool skip = false;
5204 const auto *cb_context = GetAccessContext(commandBuffer);
5205 assert(cb_context);
5206 if (!cb_context) return skip;
5207
John Zulauf36ef9282021-02-02 11:47:24 -07005208 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
5209 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
5210 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07005211 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005212}
5213
5214void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5215 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5216 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5217 uint32_t bufferMemoryBarrierCount,
5218 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5219 uint32_t imageMemoryBarrierCount,
5220 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5221 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5222 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5223 imageMemoryBarrierCount, pImageMemoryBarriers);
5224
5225 auto *cb_context = GetAccessContext(commandBuffer);
5226 assert(cb_context);
5227 if (!cb_context) return;
5228
John Zulauf36ef9282021-02-02 11:47:24 -07005229 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
5230 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
5231 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf8eda1562021-04-13 17:06:41 -06005232 wait_events_op.Record(cb_context);
5233 return;
John Zulauf4a6105a2020-11-17 15:11:05 -07005234}
5235
John Zulauf4edde622021-02-15 08:54:50 -07005236bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5237 const VkDependencyInfoKHR *pDependencyInfos) const {
5238 bool skip = false;
5239 const auto *cb_context = GetAccessContext(commandBuffer);
5240 assert(cb_context);
5241 if (!cb_context) return skip;
5242
5243 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5244 skip |= wait_events_op.Validate(*cb_context);
5245 return skip;
5246}
5247
5248void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5249 const VkDependencyInfoKHR *pDependencyInfos) {
5250 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5251
5252 auto *cb_context = GetAccessContext(commandBuffer);
5253 assert(cb_context);
5254 if (!cb_context) return;
5255
5256 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5257 wait_events_op.Record(cb_context);
5258}
5259
John Zulauf4a6105a2020-11-17 15:11:05 -07005260void SyncEventState::ResetFirstScope() {
5261 for (const auto address_type : kAddressTypes) {
5262 first_scope[static_cast<size_t>(address_type)].clear();
5263 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005264 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07005265}
5266
5267// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07005268SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005269 IgnoreReason reason = NotIgnored;
5270
John Zulauf4edde622021-02-15 08:54:50 -07005271 if ((CMD_WAITEVENTS2KHR == cmd) && (CMD_SETEVENT == last_command)) {
5272 reason = SetVsWait2;
5273 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
5274 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07005275 } else if (unsynchronized_set) {
5276 reason = SetRace;
5277 } else {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005278 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005279 if (missing_bits) reason = MissingStageBits;
5280 }
5281
5282 return reason;
5283}
5284
Jeremy Gebben40a22942020-12-22 14:22:06 -07005285bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005286 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5287 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5288 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005289}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005290
John Zulauf36ef9282021-02-02 11:47:24 -07005291SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5292 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5293 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005294 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5295 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5296 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07005297 : SyncOpBase(cmd), barriers_(1) {
5298 auto &barrier_set = barriers_[0];
5299 barrier_set.dependency_flags = dependencyFlags;
5300 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
5301 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005302 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07005303 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
5304 pMemoryBarriers);
5305 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5306 bufferMemoryBarrierCount, pBufferMemoryBarriers);
5307 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5308 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005309}
5310
John Zulauf4edde622021-02-15 08:54:50 -07005311SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
5312 const VkDependencyInfoKHR *dep_infos)
5313 : SyncOpBase(cmd), barriers_(event_count) {
5314 for (uint32_t i = 0; i < event_count; i++) {
5315 const auto &dep_info = dep_infos[i];
5316 auto &barrier_set = barriers_[i];
5317 barrier_set.dependency_flags = dep_info.dependencyFlags;
5318 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5319 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5320 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5321 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5322 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5323 dep_info.pMemoryBarriers);
5324 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5325 dep_info.pBufferMemoryBarriers);
5326 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5327 dep_info.pImageMemoryBarriers);
5328 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005329}
5330
John Zulauf36ef9282021-02-02 11:47:24 -07005331SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005332 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5333 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5334 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5335 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5336 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005337 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005338 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5339
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005340SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5341 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005342 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005343
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005344bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5345 bool skip = false;
5346 const auto *context = cb_context.GetCurrentAccessContext();
5347 assert(context);
5348 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005349 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5350
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005351 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005352 const auto &barrier_set = barriers_[0];
5353 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5354 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5355 const auto *image_state = image_barrier.image.get();
5356 if (!image_state) continue;
5357 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5358 if (hazard.hazard) {
5359 // PHASE1 TODO -- add tag information to log msg when useful.
5360 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005361 const auto image_handle = image_state->image();
John Zulauf6fdf3d02021-03-05 16:50:47 -07005362 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5363 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5364 string_SyncHazard(hazard.hazard), image_barrier.index,
5365 sync_state.report_data->FormatHandle(image_handle).c_str(),
5366 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005367 }
5368 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005369 return skip;
5370}
5371
John Zulaufd5115702021-01-18 12:34:33 -07005372struct SyncOpPipelineBarrierFunctorFactory {
5373 using BarrierOpFunctor = PipelineBarrierOp;
5374 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5375 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5376 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5377 using BufferRange = ResourceAccessRange;
5378 using ImageRange = subresource_adapter::ImageRangeGenerator;
5379 using GlobalRange = ResourceAccessRange;
5380
5381 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5382 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5383 }
John Zulauf14940722021-04-12 15:19:02 -06005384 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005385 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5386 }
5387 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5388 return GlobalBarrierOpFunctor(barrier, false);
5389 }
5390
5391 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5392 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5393 const auto base_address = ResourceBaseAddress(buffer);
5394 return (range + base_address);
5395 }
John Zulauf110413c2021-03-20 05:38:38 -06005396 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005397 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005398
5399 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005400 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005401 return range_gen;
5402 }
5403 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5404};
5405
5406template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005407void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005408 AccessContext *context) {
5409 for (const auto &barrier : barriers) {
5410 const auto *state = barrier.GetState();
5411 if (state) {
5412 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5413 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5414 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5415 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5416 }
5417 }
5418}
5419
5420template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005421void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005422 AccessContext *access_context) {
5423 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5424 for (const auto &barrier : barriers) {
5425 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5426 }
5427 for (const auto address_type : kAddressTypes) {
5428 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5429 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5430 }
5431}
5432
John Zulauf8eda1562021-04-13 17:06:41 -06005433ResourceUsageTag SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005434 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf8eda1562021-04-13 17:06:41 -06005435 auto *events_context = cb_context->GetCurrentEventsContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005436 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf4fa68462021-04-26 21:04:22 -06005437 DoRecord(tag, access_context, events_context);
5438 return tag;
5439}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005440
John Zulauf4fa68462021-04-26 21:04:22 -06005441void SyncOpPipelineBarrier::DoRecord(const ResourceUsageTag tag, AccessContext *access_context,
5442 SyncEventsContext *events_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06005443 SyncOpPipelineBarrierFunctorFactory factory;
John Zulauf4edde622021-02-15 08:54:50 -07005444 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5445 assert(barriers_.size() == 1);
5446 const auto &barrier_set = barriers_[0];
5447 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5448 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5449 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulauf4edde622021-02-15 08:54:50 -07005450 if (barrier_set.single_exec_scope) {
John Zulauf8eda1562021-04-13 17:06:41 -06005451 events_context->ApplyBarrier(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005452 } else {
5453 for (const auto &barrier : barrier_set.memory_barriers) {
John Zulauf8eda1562021-04-13 17:06:41 -06005454 events_context->ApplyBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005455 }
5456 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005457}
5458
John Zulauf8eda1562021-04-13 17:06:41 -06005459bool SyncOpPipelineBarrier::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5460 CommandBufferAccessContext *active_context) const {
John Zulauf4fa68462021-04-26 21:04:22 -06005461 // No Validation for replay, as the layout transition accesses are checked directly, and the src*Mask ordering is captured
5462 // with first access information.
John Zulauf8eda1562021-04-13 17:06:41 -06005463 return false;
5464}
5465
John Zulauf8eda1562021-04-13 17:06:41 -06005466
John Zulauf4edde622021-02-15 08:54:50 -07005467void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5468 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5469 const VkMemoryBarrier *barriers) {
5470 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005471 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005472 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005473 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005474 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005475 }
5476 if (0 == memory_barrier_count) {
5477 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005478 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005479 }
John Zulauf4edde622021-02-15 08:54:50 -07005480 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005481}
5482
John Zulauf4edde622021-02-15 08:54:50 -07005483void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5484 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5485 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5486 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005487 for (uint32_t index = 0; index < barrier_count; index++) {
5488 const auto &barrier = barriers[index];
5489 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5490 if (buffer) {
5491 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5492 const auto range = MakeRange(barrier.offset, barrier_size);
5493 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005494 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005495 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005496 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005497 }
5498 }
5499}
5500
John Zulauf4edde622021-02-15 08:54:50 -07005501void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
5502 uint32_t memory_barrier_count, const VkMemoryBarrier2KHR *barriers) {
5503 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005504 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005505 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005506 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5507 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5508 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005509 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005510 }
John Zulauf4edde622021-02-15 08:54:50 -07005511 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005512}
5513
John Zulauf4edde622021-02-15 08:54:50 -07005514void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5515 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5516 const VkBufferMemoryBarrier2KHR *barriers) {
5517 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005518 for (uint32_t index = 0; index < barrier_count; index++) {
5519 const auto &barrier = barriers[index];
5520 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5521 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5522 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5523 if (buffer) {
5524 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5525 const auto range = MakeRange(barrier.offset, barrier_size);
5526 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005527 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005528 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005529 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005530 }
5531 }
5532}
5533
John Zulauf4edde622021-02-15 08:54:50 -07005534void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5535 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5536 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5537 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005538 for (uint32_t index = 0; index < barrier_count; index++) {
5539 const auto &barrier = barriers[index];
5540 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5541 if (image) {
5542 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5543 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005544 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005545 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005546 image_memory_barriers.emplace_back();
5547 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005548 }
5549 }
5550}
John Zulaufd5115702021-01-18 12:34:33 -07005551
John Zulauf4edde622021-02-15 08:54:50 -07005552void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5553 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5554 const VkImageMemoryBarrier2KHR *barriers) {
5555 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005556 for (uint32_t index = 0; index < barrier_count; index++) {
5557 const auto &barrier = barriers[index];
5558 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5559 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5560 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5561 if (image) {
5562 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5563 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005564 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005565 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005566 image_memory_barriers.emplace_back();
5567 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005568 }
5569 }
5570}
5571
John Zulauf36ef9282021-02-02 11:47:24 -07005572SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005573 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5574 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5575 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5576 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005577 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005578 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5579 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005580 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005581}
5582
John Zulauf4edde622021-02-15 08:54:50 -07005583SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5584 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5585 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5586 MakeEventsList(sync_state, eventCount, pEvents);
5587 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5588}
5589
John Zulaufd5115702021-01-18 12:34:33 -07005590bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005591 const char *const ignored = "Wait operation is ignored for this event.";
5592 bool skip = false;
5593 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005594 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005595
John Zulauf4edde622021-02-15 08:54:50 -07005596 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5597 const auto &barrier_set = barriers_[barrier_set_index];
5598 if (barrier_set.single_exec_scope) {
5599 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5600 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5601 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5602 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5603 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5604 } else {
5605 const auto &barriers = barrier_set.memory_barriers;
5606 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5607 const auto &barrier = barriers[barrier_index];
5608 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5609 const std::string vuid =
5610 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5611 skip =
5612 sync_state.LogInfo(command_buffer_handle, vuid,
5613 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5614 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5615 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5616 }
5617 }
5618 }
5619 }
John Zulaufd5115702021-01-18 12:34:33 -07005620 }
5621
Jeremy Gebben40a22942020-12-22 14:22:06 -07005622 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005623 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005624 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005625 const auto *events_context = cb_context.GetCurrentEventsContext();
5626 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005627 size_t barrier_set_index = 0;
5628 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
John Zulauf78394fc2021-07-12 15:41:40 -06005629 for (const auto &event : events_) {
5630 const auto *sync_event = events_context->Get(event.get());
5631 const auto &barrier_set = barriers_[barrier_set_index];
5632 if (!sync_event) {
5633 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5634 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5635 // new validation error... wait without previously submitted set event...
5636 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 -07005637 barrier_set_index += barrier_set_incr;
John Zulauf78394fc2021-07-12 15:41:40 -06005638 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005639 }
John Zulauf78394fc2021-07-12 15:41:40 -06005640 const auto event_handle = sync_event->event->event();
5641 // TODO add "destroyed" checks
5642
5643 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5644 const auto &src_exec_scope = barrier_set.src_exec_scope;
5645 event_stage_masks |= sync_event->scope.mask_param;
5646 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5647 if (ignore_reason) {
5648 switch (ignore_reason) {
5649 case SyncEventState::ResetWaitRace:
5650 case SyncEventState::Reset2WaitRace: {
5651 // Four permuations of Reset and Wait calls...
5652 const char *vuid =
5653 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5654 if (ignore_reason == SyncEventState::Reset2WaitRace) {
5655 vuid = (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2KHR-event-03831"
5656 : "VUID-vkCmdResetEvent2KHR-event-03832";
5657 }
5658 const char *const message =
5659 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5660 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5661 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
5662 CommandTypeString(sync_event->last_command), ignored);
5663 break;
5664 }
5665 case SyncEventState::SetRace: {
5666 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5667 // this event
5668 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5669 const char *const message =
5670 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5671 const char *const reason = "First synchronization scope is undefined.";
5672 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5673 sync_state.report_data->FormatHandle(event_handle).c_str(),
5674 CommandTypeString(sync_event->last_command), reason, ignored);
5675 break;
5676 }
5677 case SyncEventState::MissingStageBits: {
5678 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5679 // Issue error message that event waited for is not in wait events scope
5680 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5681 const char *const message = "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5682 ". Bits missing from srcStageMask %s. %s";
5683 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5684 sync_state.report_data->FormatHandle(event_handle).c_str(),
5685 sync_event->scope.mask_param, src_exec_scope.mask_param,
5686 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), ignored);
5687 break;
5688 }
5689 case SyncEventState::SetVsWait2: {
5690 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2KHR-pEvents-03837",
5691 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5692 sync_state.report_data->FormatHandle(event_handle).c_str(),
5693 CommandTypeString(sync_event->last_command));
5694 break;
5695 }
5696 default:
5697 assert(ignore_reason == SyncEventState::NotIgnored);
5698 }
5699 } else if (barrier_set.image_memory_barriers.size()) {
5700 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5701 const auto *context = cb_context.GetCurrentAccessContext();
5702 assert(context);
5703 for (const auto &image_memory_barrier : image_memory_barriers) {
5704 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5705 const auto *image_state = image_memory_barrier.image.get();
5706 if (!image_state) continue;
5707 const auto &subresource_range = image_memory_barrier.range;
5708 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5709 const auto hazard =
5710 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5711 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5712 if (hazard.hazard) {
5713 skip |= sync_state.LogError(image_state->image(), string_SyncHazardVUID(hazard.hazard),
5714 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5715 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
5716 sync_state.report_data->FormatHandle(image_state->image()).c_str(),
5717 cb_context.FormatUsage(hazard).c_str());
5718 break;
5719 }
5720 }
5721 }
5722 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5723 // 03839
5724 barrier_set_index += barrier_set_incr;
5725 }
John Zulaufd5115702021-01-18 12:34:33 -07005726
5727 // 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 -07005728 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 -07005729 if (extra_stage_bits) {
5730 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005731 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5732 const char *const vuid =
5733 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2KHR-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005734 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005735 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulaufd5115702021-01-18 12:34:33 -07005736 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005737 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005738 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005739 " vkCmdSetEvent may be in previously submitted command buffer.");
5740 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005741 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005742 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005743 }
5744 }
5745 return skip;
5746}
5747
5748struct SyncOpWaitEventsFunctorFactory {
5749 using BarrierOpFunctor = WaitEventBarrierOp;
5750 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5751 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5752 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5753 using BufferRange = EventSimpleRangeGenerator;
5754 using ImageRange = EventImageRangeGenerator;
5755 using GlobalRange = EventSimpleRangeGenerator;
5756
5757 // Need to restrict to only valid exec and access scope for this event
5758 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5759 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005760 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005761 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5762 return barrier;
5763 }
5764 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5765 auto barrier = RestrictToEvent(barrier_arg);
5766 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5767 }
John Zulauf14940722021-04-12 15:19:02 -06005768 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005769 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5770 }
5771 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5772 auto barrier = RestrictToEvent(barrier_arg);
5773 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5774 }
5775
5776 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5777 const AccessAddressType address_type = GetAccessAddressType(buffer);
5778 const auto base_address = ResourceBaseAddress(buffer);
5779 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5780 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5781 return filtered_range_gen;
5782 }
John Zulauf110413c2021-03-20 05:38:38 -06005783 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07005784 if (!SimpleBinding(image)) return ImageRange();
5785 const auto address_type = GetAccessAddressType(image);
5786 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005787 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005788 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5789
5790 return filtered_range_gen;
5791 }
5792 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5793 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5794 }
5795 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5796 SyncEventState *sync_event;
5797};
5798
John Zulauf8eda1562021-04-13 17:06:41 -06005799ResourceUsageTag SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07005800 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07005801 auto *access_context = cb_context->GetCurrentAccessContext();
5802 assert(access_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005803 if (!access_context) return tag;
John Zulauf669dfd52021-01-27 17:15:28 -07005804 auto *events_context = cb_context->GetCurrentEventsContext();
5805 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005806 if (!events_context) return tag;
John Zulaufd5115702021-01-18 12:34:33 -07005807
5808 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
5809 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
5810 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
5811 access_context->ResolvePreviousAccesses();
5812
John Zulaufd5115702021-01-18 12:34:33 -07005813 // TODO... this needs change the SyncEventContext it's using depending on whether this is replay... the recorded
5814 // sync_event will be in the recorded context, but we need to update the sync_events in the current context....
John Zulauf4edde622021-02-15 08:54:50 -07005815 size_t barrier_set_index = 0;
5816 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5817 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07005818 for (auto &event_shared : events_) {
5819 if (!event_shared.get()) continue;
5820 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07005821
John Zulauf4edde622021-02-15 08:54:50 -07005822 sync_event->last_command = cmd_;
John Zulaufd5115702021-01-18 12:34:33 -07005823
John Zulauf4edde622021-02-15 08:54:50 -07005824 const auto &barrier_set = barriers_[barrier_set_index];
5825 const auto &dst = barrier_set.dst_exec_scope;
5826 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07005827 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
5828 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
5829 // of the barriers is maintained.
5830 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07005831 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5832 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5833 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07005834
5835 // Apply the global barrier to the event itself (for race condition tracking)
5836 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
5837 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
5838 sync_event->barriers |= dst.exec_scope;
5839 } else {
5840 // We ignored this wait, so we don't have any effective synchronization barriers for it.
5841 sync_event->barriers = 0U;
5842 }
John Zulauf4edde622021-02-15 08:54:50 -07005843 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005844 }
5845
5846 // Apply the pending barriers
5847 ResolvePendingBarrierFunctor apply_pending_action(tag);
5848 access_context->ApplyToContext(apply_pending_action);
John Zulauf8eda1562021-04-13 17:06:41 -06005849
5850 return tag;
John Zulaufd5115702021-01-18 12:34:33 -07005851}
5852
John Zulauf8eda1562021-04-13 17:06:41 -06005853bool SyncOpWaitEvents::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5854 CommandBufferAccessContext *active_context) const {
5855 return false;
5856}
5857
John Zulauf4fa68462021-04-26 21:04:22 -06005858void SyncOpWaitEvents::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06005859
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005860bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5861 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5862 bool skip = false;
5863 const auto *cb_access_context = GetAccessContext(commandBuffer);
5864 assert(cb_access_context);
5865 if (!cb_access_context) return skip;
5866
5867 const auto *context = cb_access_context->GetCurrentAccessContext();
5868 assert(context);
5869 if (!context) return skip;
5870
5871 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5872
5873 if (dst_buffer) {
5874 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5875 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
5876 if (hazard.hazard) {
5877 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5878 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
5879 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
John Zulauf14940722021-04-12 15:19:02 -06005880 cb_access_context->FormatUsage(hazard).c_str());
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005881 }
5882 }
5883 return skip;
5884}
5885
John Zulauf669dfd52021-01-27 17:15:28 -07005886void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07005887 events_.reserve(event_count);
5888 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
John Zulauf669dfd52021-01-27 17:15:28 -07005889 events_.emplace_back(sync_state.GetShared<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07005890 }
5891}
John Zulauf6ce24372021-01-30 05:56:25 -07005892
John Zulauf36ef9282021-02-02 11:47:24 -07005893SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005894 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005895 : SyncOpBase(cmd),
5896 event_(sync_state.GetShared<EVENT_STATE>(event)),
5897 exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005898
5899bool SyncOpResetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005900 auto *events_context = cb_context.GetCurrentEventsContext();
5901 assert(events_context);
5902 bool skip = false;
5903 if (!events_context) return skip;
5904
5905 const auto &sync_state = cb_context.GetSyncState();
5906 const auto *sync_event = events_context->Get(event_);
5907 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5908
5909 const char *const set_wait =
5910 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5911 "hazards.";
5912 const char *message = set_wait; // Only one message this call.
5913 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
5914 const char *vuid = nullptr;
5915 switch (sync_event->last_command) {
5916 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005917 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005918 // Needs a barrier between set and reset
5919 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
5920 break;
John Zulauf4edde622021-02-15 08:54:50 -07005921 case CMD_WAITEVENTS:
5922 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07005923 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
5924 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
5925 break;
5926 }
5927 default:
5928 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07005929 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
5930 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07005931 break;
5932 }
5933 if (vuid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005934 skip |= sync_state.LogError(event_->event(), vuid, message, CmdName(),
5935 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005936 CommandTypeString(sync_event->last_command));
5937 }
5938 }
5939 return skip;
5940}
5941
John Zulauf8eda1562021-04-13 17:06:41 -06005942ResourceUsageTag SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
5943 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07005944 auto *events_context = cb_context->GetCurrentEventsContext();
5945 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005946 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07005947
5948 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06005949 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07005950
5951 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07005952 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005953 sync_event->unsynchronized_set = CMD_NONE;
5954 sync_event->ResetFirstScope();
5955 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06005956
5957 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07005958}
5959
John Zulauf8eda1562021-04-13 17:06:41 -06005960bool SyncOpResetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5961 CommandBufferAccessContext *active_context) const {
5962 return false;
5963}
5964
John Zulauf4fa68462021-04-26 21:04:22 -06005965void SyncOpResetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06005966
John Zulauf36ef9282021-02-02 11:47:24 -07005967SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005968 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005969 : SyncOpBase(cmd),
5970 event_(sync_state.GetShared<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07005971 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
5972 dep_info_() {}
5973
5974SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
5975 const VkDependencyInfoKHR &dep_info)
5976 : SyncOpBase(cmd),
5977 event_(sync_state.GetShared<EVENT_STATE>(event)),
5978 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
5979 dep_info_(new safe_VkDependencyInfoKHR(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005980
5981bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
5982 // I'll put this here just in case we need to pass this in for future extension support
John Zulauf6ce24372021-01-30 05:56:25 -07005983 bool skip = false;
5984
5985 const auto &sync_state = cb_context.GetSyncState();
5986 auto *events_context = cb_context.GetCurrentEventsContext();
5987 assert(events_context);
5988 if (!events_context) return skip;
5989
5990 const auto *sync_event = events_context->Get(event_);
5991 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5992
5993 const char *const reset_set =
5994 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5995 "hazards.";
5996 const char *const wait =
5997 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
5998
5999 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07006000 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07006001 const char *message = nullptr;
6002 switch (sync_event->last_command) {
6003 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006004 case CMD_RESETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006005 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07006006 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07006007 message = reset_set;
6008 break;
6009 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006010 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006011 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07006012 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07006013 message = reset_set;
6014 break;
6015 case CMD_WAITEVENTS:
John Zulauf4edde622021-02-15 08:54:50 -07006016 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006017 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07006018 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07006019 message = wait;
6020 break;
6021 default:
6022 // The only other valid last command that wasn't one.
6023 assert(sync_event->last_command == CMD_NONE);
6024 break;
6025 }
John Zulauf4edde622021-02-15 08:54:50 -07006026 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07006027 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07006028 std::string vuid("SYNC-");
6029 vuid.append(CmdName()).append(vuid_stem);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006030 skip |= sync_state.LogError(event_->event(), vuid.c_str(), message, CmdName(),
6031 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006032 CommandTypeString(sync_event->last_command));
6033 }
6034 }
6035
6036 return skip;
6037}
6038
John Zulauf8eda1562021-04-13 17:06:41 -06006039ResourceUsageTag SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006040 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006041 auto *events_context = cb_context->GetCurrentEventsContext();
6042 auto *access_context = cb_context->GetCurrentAccessContext();
6043 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006044 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006045
6046 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06006047 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006048
6049 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
6050 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
6051 // any issues caused by naive scope setting here.
6052
6053 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
6054 // Given:
6055 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
6056 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
6057
6058 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
6059 sync_event->unsynchronized_set = sync_event->last_command;
6060 sync_event->ResetFirstScope();
6061 } else if (sync_event->scope.exec_scope == 0) {
6062 // We only set the scope if there isn't one
6063 sync_event->scope = src_exec_scope_;
6064
6065 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
6066 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
6067 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
6068 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
6069 }
6070 };
6071 access_context->ForAll(set_scope);
6072 sync_event->unsynchronized_set = CMD_NONE;
6073 sync_event->first_scope_tag = tag;
6074 }
John Zulauf4edde622021-02-15 08:54:50 -07006075 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
6076 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07006077 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06006078
6079 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006080}
John Zulauf64ffe552021-02-06 10:25:07 -07006081
John Zulauf8eda1562021-04-13 17:06:41 -06006082bool SyncOpSetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6083 CommandBufferAccessContext *active_context) const {
6084 return false;
6085}
6086
John Zulauf4fa68462021-04-26 21:04:22 -06006087void SyncOpSetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006088
John Zulauf64ffe552021-02-06 10:25:07 -07006089SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
6090 const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07006091 const VkSubpassBeginInfo *pSubpassBeginInfo)
6092 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006093 if (pRenderPassBegin) {
6094 rp_state_ = sync_state.GetShared<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
6095 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
6096 const auto *fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
6097 if (fb_state) {
6098 shared_attachments_ = sync_state.GetSharedAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
6099 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
6100 // Note that this a safe to presist as long as shared_attachments is not cleared
6101 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08006102 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07006103 attachments_.emplace_back(attachment.get());
6104 }
6105 }
6106 if (pSubpassBeginInfo) {
6107 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
6108 }
6109 }
6110}
6111
6112bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6113 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
6114 bool skip = false;
6115
6116 assert(rp_state_.get());
6117 if (nullptr == rp_state_.get()) return skip;
6118 auto &rp_state = *rp_state_.get();
6119
6120 const uint32_t subpass = 0;
6121
6122 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
6123 // hasn't happened yet)
6124 const std::vector<AccessContext> empty_context_vector;
6125 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
6126 cb_context.GetCurrentAccessContext());
6127
6128 // Validate attachment operations
6129 if (attachments_.size() == 0) return skip;
6130 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07006131
6132 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
6133 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
6134 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
6135 // operations (though it's currently a messy approach)
6136 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
6137 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006138
6139 // Validate load operations if there were no layout transition hazards
6140 if (!skip) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07006141 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kCurrentCommandTag);
6142 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006143 }
6144
6145 return skip;
6146}
6147
John Zulauf8eda1562021-04-13 17:06:41 -06006148ResourceUsageTag SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
6149 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf64ffe552021-02-06 10:25:07 -07006150 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
6151 assert(rp_state_.get());
John Zulauf8eda1562021-04-13 17:06:41 -06006152 if (nullptr == rp_state_.get()) return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006153 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006154
6155 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006156}
6157
John Zulauf8eda1562021-04-13 17:06:41 -06006158bool SyncOpBeginRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6159 CommandBufferAccessContext *active_context) const {
6160 return false;
6161}
6162
John Zulauf4fa68462021-04-26 21:04:22 -06006163void SyncOpBeginRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
6164}
John Zulauf8eda1562021-04-13 17:06:41 -06006165
John Zulauf64ffe552021-02-06 10:25:07 -07006166SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07006167 const VkSubpassEndInfo *pSubpassEndInfo)
6168 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006169 if (pSubpassBeginInfo) {
6170 subpass_begin_info_.initialize(pSubpassBeginInfo);
6171 }
6172 if (pSubpassEndInfo) {
6173 subpass_end_info_.initialize(pSubpassEndInfo);
6174 }
6175}
6176
6177bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
6178 bool skip = false;
6179 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6180 if (!renderpass_context) return skip;
6181
6182 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
6183 return skip;
6184}
6185
John Zulauf8eda1562021-04-13 17:06:41 -06006186ResourceUsageTag SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006187 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06006188 // TODO PHASE2 Need to fix renderpass tagging/segregation of barrier and access operations for QueueSubmit time validation
6189 auto prev_tag = cb_context->NextCommandTag(cmd_);
6190 auto next_tag = cb_context->NextSubcommandTag(cmd_);
6191
6192 cb_context->RecordNextSubpass(prev_tag, next_tag);
6193 // TODO PHASE2 This needs to be the tag of the barrier operations
6194 return prev_tag;
6195}
6196
6197bool SyncOpNextSubpass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6198 CommandBufferAccessContext *active_context) const {
6199 return false;
John Zulauf64ffe552021-02-06 10:25:07 -07006200}
6201
sfricke-samsung85584a72021-09-30 21:43:38 -07006202SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo)
6203 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006204 if (pSubpassEndInfo) {
6205 subpass_end_info_.initialize(pSubpassEndInfo);
6206 }
6207}
6208
John Zulauf4fa68462021-04-26 21:04:22 -06006209void SyncOpNextSubpass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006210
John Zulauf64ffe552021-02-06 10:25:07 -07006211bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6212 bool skip = false;
6213 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6214
6215 if (!renderpass_context) return skip;
6216 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
6217 return skip;
6218}
6219
John Zulauf8eda1562021-04-13 17:06:41 -06006220ResourceUsageTag SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006221 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06006222 const auto tag = cb_context->NextCommandTag(cmd_);
6223 cb_context->RecordEndRenderPass(tag);
6224 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006225}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006226
John Zulauf8eda1562021-04-13 17:06:41 -06006227bool SyncOpEndRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6228 CommandBufferAccessContext *active_context) const {
6229 return false;
6230}
6231
John Zulauf4fa68462021-04-26 21:04:22 -06006232void SyncOpEndRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006233
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006234void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6235 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
6236 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
6237 auto *cb_access_context = GetAccessContext(commandBuffer);
6238 assert(cb_access_context);
6239 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
6240 auto *context = cb_access_context->GetCurrentAccessContext();
6241 assert(context);
6242
6243 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
6244
6245 if (dst_buffer) {
6246 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6247 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
6248 }
6249}
John Zulaufd05c5842021-03-26 11:32:16 -06006250
John Zulaufae842002021-04-15 18:20:55 -06006251bool SyncValidator::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6252 const VkCommandBuffer *pCommandBuffers) const {
6253 bool skip = StateTracker::PreCallValidateCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
6254 const char *func_name = "vkCmdExecuteCommands";
6255 const auto *cb_context = GetAccessContext(commandBuffer);
6256 assert(cb_context);
John Zulauf4fa68462021-04-26 21:04:22 -06006257
6258 // Heavyweight, but we need a proxy copy of the active command buffer access context
6259 CommandBufferAccessContext proxy_cb_context(*cb_context, CommandBufferAccessContext::AsProxyContext());
John Zulaufae842002021-04-15 18:20:55 -06006260
6261 // Make working copies of the access and events contexts
John Zulauf4fa68462021-04-26 21:04:22 -06006262 proxy_cb_context.NextCommandTag(CMD_EXECUTECOMMANDS);
John Zulaufae842002021-04-15 18:20:55 -06006263
6264 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
John Zulauf4fa68462021-04-26 21:04:22 -06006265 proxy_cb_context.NextSubcommandTag(CMD_EXECUTECOMMANDS);
John Zulaufae842002021-04-15 18:20:55 -06006266 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6267 if (!recorded_cb_context) continue;
John Zulauf4fa68462021-04-26 21:04:22 -06006268
6269 const auto *recorded_context = recorded_cb_context->GetCurrentAccessContext();
6270 assert(recorded_context);
6271 skip |= recorded_cb_context->ValidateFirstUse(&proxy_cb_context, func_name, cb_index);
6272
6273 // The barriers have already been applied in ValidatFirstUse
6274 ResourceUsageRange tag_range = proxy_cb_context.ImportRecordedAccessLog(*recorded_cb_context);
6275 proxy_cb_context.ResolveRecordedContext(*recorded_context, tag_range.begin);
John Zulaufae842002021-04-15 18:20:55 -06006276 }
6277
John Zulaufae842002021-04-15 18:20:55 -06006278 return skip;
6279}
6280
6281void SyncValidator::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6282 const VkCommandBuffer *pCommandBuffers) {
6283 StateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
John Zulauf4fa68462021-04-26 21:04:22 -06006284 auto *cb_context = GetAccessContext(commandBuffer);
6285 assert(cb_context);
6286 cb_context->NextCommandTag(CMD_EXECUTECOMMANDS);
6287 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
6288 cb_context->NextSubcommandTag(CMD_EXECUTECOMMANDS);
6289 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6290 if (!recorded_cb_context) continue;
6291 cb_context->RecordExecutedCommandBuffer(*recorded_cb_context, CMD_EXECUTECOMMANDS);
6292 }
John Zulaufae842002021-04-15 18:20:55 -06006293}
6294
John Zulaufd0ec59f2021-03-13 14:25:08 -07006295AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
6296 : view_(view), view_mask_(), gen_store_() {
6297 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
6298 const IMAGE_STATE &image_state = *view_->image_state.get();
6299 const auto base_address = ResourceBaseAddress(image_state);
6300 const auto *encoder = image_state.fragment_encoder.get();
6301 if (!encoder) return;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06006302 // Get offset and extent for the view, accounting for possible depth slicing
6303 const VkOffset3D zero_offset = view->GetOffset();
6304 const VkExtent3D &image_extent = view->GetExtent();
John Zulaufd0ec59f2021-03-13 14:25:08 -07006305 // Intentional copy
6306 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
6307 view_mask_ = subres_range.aspectMask;
6308 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
6309 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6310
6311 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
6312 if (depth && (depth != view_mask_)) {
6313 subres_range.aspectMask = depth;
6314 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6315 }
6316 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
6317 if (stencil && (stencil != view_mask_)) {
6318 subres_range.aspectMask = stencil;
6319 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6320 }
6321}
6322
6323const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
6324 const ImageRangeGen *got = nullptr;
6325 switch (gen_type) {
6326 case kViewSubresource:
6327 got = &gen_store_[kViewSubresource];
6328 break;
6329 case kRenderArea:
6330 got = &gen_store_[kRenderArea];
6331 break;
6332 case kDepthOnlyRenderArea:
6333 got =
6334 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
6335 break;
6336 case kStencilOnlyRenderArea:
6337 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
6338 : &gen_store_[Gen::kStencilOnlyRenderArea];
6339 break;
6340 default:
6341 assert(got);
6342 }
6343 return got;
6344}
6345
6346AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
6347 assert(IsValid());
6348 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
6349 if (depth_op) {
6350 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
6351 if (stencil_op) {
6352 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6353 return kRenderArea;
6354 }
6355 return kDepthOnlyRenderArea;
6356 }
6357 if (stencil_op) {
6358 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6359 return kStencilOnlyRenderArea;
6360 }
6361
6362 assert(depth_op || stencil_op);
6363 return kRenderArea;
6364}
6365
6366AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }
John Zulauf8eda1562021-04-13 17:06:41 -06006367
6368void SyncEventsContext::ApplyBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
6369 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
6370 for (auto &event_pair : map_) {
6371 assert(event_pair.second); // Shouldn't be storing empty
6372 auto &sync_event = *event_pair.second;
6373 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
6374 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
6375 sync_event.barriers |= dst.exec_scope;
6376 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6377 }
6378 }
6379}