blob: 596873c11259b11ab94e4e037bcbd953e212357d [file] [log] [blame]
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001/* Copyright (c) 2019-2022 The Khronos Group Inc.
2 * Copyright (c) 2019-2022 Valve Corporation
3 * Copyright (c) 2019-2022 LunarG, Inc.
John Zulauf9cb530d2019-09-30 14:14:10 -06004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: John Zulauf <jzulauf@lunarg.com>
John Zulaufab7756b2020-12-29 16:10:16 -070018 * Author: Locke Lin <locke@lunarg.com>
19 * Author: Jeremy Gebben <jeremyg@lunarg.com>
John Zulauf9cb530d2019-09-30 14:14:10 -060020 */
21
22#include <limits>
23#include <vector>
locke-lunarg296a3c92020-03-25 01:04:29 -060024#include <memory>
25#include <bitset>
John Zulauf9cb530d2019-09-30 14:14:10 -060026#include "synchronization_validation.h"
Jeremy Gebben5f585ae2021-02-02 09:03:06 -070027#include "sync_utils.h"
John Zulauf9cb530d2019-09-30 14:14:10 -060028
Jeremy Gebben6fbf8242021-06-21 09:14:46 -060029static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.Binding(); }
John Zulauf264cce02021-02-05 14:40:47 -070030
John Zulauf29d00532021-03-04 13:28:54 -070031static bool SimpleBinding(const IMAGE_STATE &image_state) {
Jeremy Gebben62c3bf42021-07-21 15:38:24 -060032 bool simple =
Jeremy Gebben82e11d52021-07-26 09:19:37 -060033 SimpleBinding(static_cast<const BINDABLE &>(image_state)) || image_state.IsSwapchainImage() || image_state.bind_swapchain;
John Zulauf29d00532021-03-04 13:28:54 -070034
35 // If it's not simple we must have an encoder.
36 assert(!simple || image_state.fragment_encoder.get());
37 return simple;
38}
39
John Zulauf4fa68462021-04-26 21:04:22 -060040static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
41static const std::array<AccessAddressType, static_cast<size_t>(AccessAddressType::kTypeCount)> kAddressTypes = {
John Zulauf43cc7462020-12-03 12:33:12 -070042 AccessAddressType::kLinear, AccessAddressType::kIdealized};
43
John Zulaufd5115702021-01-18 12:34:33 -070044static constexpr AccessAddressType GetAccessAddressType(const BUFFER_STATE &) { return AccessAddressType::kLinear; };
John Zulauf264cce02021-02-05 14:40:47 -070045static AccessAddressType GetAccessAddressType(const IMAGE_STATE &image) {
46 return SimpleBinding(image) ? AccessContext::ImageAddressType(image) : AccessAddressType::kIdealized;
47}
John Zulaufd5115702021-01-18 12:34:33 -070048
John Zulauf9cb530d2019-09-30 14:14:10 -060049static const char *string_SyncHazardVUID(SyncHazard hazard) {
50 switch (hazard) {
51 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070052 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060053 break;
54 case SyncHazard::READ_AFTER_WRITE:
55 return "SYNC-HAZARD-READ_AFTER_WRITE";
56 break;
57 case SyncHazard::WRITE_AFTER_READ:
58 return "SYNC-HAZARD-WRITE_AFTER_READ";
59 break;
60 case SyncHazard::WRITE_AFTER_WRITE:
61 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
62 break;
John Zulauf2f952d22020-02-10 11:34:51 -070063 case SyncHazard::READ_RACING_WRITE:
64 return "SYNC-HAZARD-READ-RACING-WRITE";
65 break;
66 case SyncHazard::WRITE_RACING_WRITE:
67 return "SYNC-HAZARD-WRITE-RACING-WRITE";
68 break;
69 case SyncHazard::WRITE_RACING_READ:
70 return "SYNC-HAZARD-WRITE-RACING-READ";
71 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060072 default:
73 assert(0);
74 }
75 return "SYNC-HAZARD-INVALID";
76}
77
John Zulauf59e25072020-07-17 10:55:21 -060078static bool IsHazardVsRead(SyncHazard hazard) {
79 switch (hazard) {
80 case SyncHazard::NONE:
81 return false;
82 break;
83 case SyncHazard::READ_AFTER_WRITE:
84 return false;
85 break;
86 case SyncHazard::WRITE_AFTER_READ:
87 return true;
88 break;
89 case SyncHazard::WRITE_AFTER_WRITE:
90 return false;
91 break;
92 case SyncHazard::READ_RACING_WRITE:
93 return false;
94 break;
95 case SyncHazard::WRITE_RACING_WRITE:
96 return false;
97 break;
98 case SyncHazard::WRITE_RACING_READ:
99 return true;
100 break;
101 default:
102 assert(0);
103 }
104 return false;
105}
106
John Zulauf9cb530d2019-09-30 14:14:10 -0600107static const char *string_SyncHazard(SyncHazard hazard) {
108 switch (hazard) {
109 case SyncHazard::NONE:
110 return "NONR";
111 break;
112 case SyncHazard::READ_AFTER_WRITE:
113 return "READ_AFTER_WRITE";
114 break;
115 case SyncHazard::WRITE_AFTER_READ:
116 return "WRITE_AFTER_READ";
117 break;
118 case SyncHazard::WRITE_AFTER_WRITE:
119 return "WRITE_AFTER_WRITE";
120 break;
John Zulauf2f952d22020-02-10 11:34:51 -0700121 case SyncHazard::READ_RACING_WRITE:
122 return "READ_RACING_WRITE";
123 break;
124 case SyncHazard::WRITE_RACING_WRITE:
125 return "WRITE_RACING_WRITE";
126 break;
127 case SyncHazard::WRITE_RACING_READ:
128 return "WRITE_RACING_READ";
129 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600130 default:
131 assert(0);
132 }
133 return "INVALID HAZARD";
134}
135
John Zulauf37ceaed2020-07-03 16:18:15 -0600136static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
137 // Return the info for the first bit found
138 const SyncStageAccessInfoType *info = nullptr;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700139 for (size_t i = 0; i < flags.size(); i++) {
140 if (flags.test(i)) {
141 info = &syncStageAccessInfoByStageAccessIndex[i];
142 break;
John Zulauf37ceaed2020-07-03 16:18:15 -0600143 }
144 }
145 return info;
146}
147
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700148static std::string string_SyncStageAccessFlags(const SyncStageAccessFlags &flags, const char *sep = "|") {
John Zulauf59e25072020-07-17 10:55:21 -0600149 std::string out_str;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700150 if (flags.none()) {
John Zulauf389c34b2020-07-28 11:19:35 -0600151 out_str = "0";
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700152 } else {
153 for (size_t i = 0; i < syncStageAccessInfoByStageAccessIndex.size(); i++) {
154 const auto &info = syncStageAccessInfoByStageAccessIndex[i];
155 if ((flags & info.stage_access_bit).any()) {
156 if (!out_str.empty()) {
157 out_str.append(sep);
158 }
159 out_str.append(info.name);
John Zulauf59e25072020-07-17 10:55:21 -0600160 }
John Zulauf59e25072020-07-17 10:55:21 -0600161 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700162 if (out_str.length() == 0) {
163 out_str.append("Unhandled SyncStageAccess");
164 }
John Zulauf59e25072020-07-17 10:55:21 -0600165 }
166 return out_str;
167}
168
John Zulauf14940722021-04-12 15:19:02 -0600169static std::string string_UsageTag(const ResourceUsageRecord &tag) {
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700170 std::stringstream out;
171
John Zulauffaea0ee2021-01-14 14:01:32 -0700172 out << "command: " << CommandTypeString(tag.command);
173 out << ", seq_no: " << tag.seq_num;
174 if (tag.sub_command != 0) {
175 out << ", subcmd: " << tag.sub_command;
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700176 }
177 return out.str();
178}
John Zulauf4fa68462021-04-26 21:04:22 -0600179static std::string string_UsageIndex(SyncStageAccessIndex usage_index) {
180 const char *stage_access_name = "INVALID_STAGE_ACCESS";
181 if (usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size())) {
182 stage_access_name = syncStageAccessInfoByStageAccessIndex[usage_index].name;
183 }
184 return std::string(stage_access_name);
185}
186
187struct NoopBarrierAction {
188 explicit NoopBarrierAction() {}
189 void operator()(ResourceAccessState *access) const {}
John Zulauf5c628d02021-05-04 15:46:36 -0600190 const bool layout_transition = false;
John Zulauf4fa68462021-04-26 21:04:22 -0600191};
192
193// NOTE: Make sure the proxy doesn't outlive from, as the proxy is pointing directly to access contexts owned by from.
194CommandBufferAccessContext::CommandBufferAccessContext(const CommandBufferAccessContext &from, AsProxyContext dummy)
195 : CommandBufferAccessContext(from.sync_state_) {
196 // Copy only the needed fields out of from for a temporary, proxy command buffer context
197 cb_state_ = from.cb_state_;
198 queue_flags_ = from.queue_flags_;
199 destroyed_ = from.destroyed_;
200 access_log_ = from.access_log_; // potentially large, but no choice given tagging lookup.
201 command_number_ = from.command_number_;
202 subcommand_number_ = from.subcommand_number_;
203 reset_count_ = from.reset_count_;
204
205 const auto *from_context = from.GetCurrentAccessContext();
206 assert(from_context);
207
208 // Construct a fully resolved single access context out of from
209 const NoopBarrierAction noop_barrier;
210 for (AccessAddressType address_type : kAddressTypes) {
211 from_context->ResolveAccessRange(address_type, kFullRange, noop_barrier,
212 &cb_access_context_.GetAccessStateMap(address_type), nullptr);
213 }
214 // The proxy has flatten the current render pass context (if any), but the async contexts are needed for hazard detection
215 cb_access_context_.ImportAsyncContexts(*from_context);
216
217 events_context_ = from.events_context_;
218
219 // We don't want to copy the full render_pass_context_ history just for the proxy.
220}
221
222std::string CommandBufferAccessContext::FormatUsage(const ResourceUsageTag tag) const {
223 std::stringstream out;
224 assert(tag < access_log_.size());
225 const auto &record = access_log_[tag];
226 out << string_UsageTag(record);
227 if (record.cb_state != cb_state_.get()) {
228 out << ", command_buffer: " << sync_state_->report_data->FormatHandle(record.cb_state->commandBuffer()).c_str();
229 if (record.cb_state->Destroyed()) {
230 out << " (destroyed)";
231 }
232
John Zulauf3c2a0b32021-07-14 11:14:52 -0600233 out << ", reset_no: " << std::to_string(record.reset_count);
John Zulauf4fa68462021-04-26 21:04:22 -0600234 } else {
235 out << ", reset_no: " << std::to_string(reset_count_);
236 }
237 return out.str();
238}
239std::string CommandBufferAccessContext::FormatUsage(const ResourceFirstAccess &access) const {
240 std::stringstream out;
241 out << "(recorded_usage: " << string_UsageIndex(access.usage_index);
242 out << ", " << FormatUsage(access.tag) << ")";
243 return out.str();
244}
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700245
John Zulauffaea0ee2021-01-14 14:01:32 -0700246std::string CommandBufferAccessContext::FormatUsage(const HazardResult &hazard) const {
John Zulauf37ceaed2020-07-03 16:18:15 -0600247 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600248 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
249 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600250 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600251 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
252 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf4fa68462021-04-26 21:04:22 -0600253 out << "(";
254 if (!hazard.recorded_access.get()) {
255 // if we have a recorded usage the usage is reported from the recorded contexts point of view
256 out << "usage: " << usage_info.name << ", ";
257 }
258 out << "prior_usage: " << stage_access_name;
John Zulauf59e25072020-07-17 10:55:21 -0600259 if (IsHazardVsRead(hazard.hazard)) {
260 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
Jeremy Gebben40a22942020-12-22 14:22:06 -0700261 out << ", read_barriers: " << string_VkPipelineStageFlags2KHR(barriers);
John Zulauf59e25072020-07-17 10:55:21 -0600262 } else {
263 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
264 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
265 }
266
John Zulauf14940722021-04-12 15:19:02 -0600267 assert(tag < access_log_.size());
John Zulauf4fa68462021-04-26 21:04:22 -0600268 out << ", " << FormatUsage(tag) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600269 return out.str();
270}
271
John Zulaufd14743a2020-07-03 09:42:39 -0600272// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
273// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
274// also reflects this special case for read hazard detection (using access instead of exec scope)
Jeremy Gebben40a22942020-12-22 14:22:06 -0700275static constexpr VkPipelineStageFlags2KHR kColorAttachmentExecScope = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700276static const SyncStageAccessFlags kColorAttachmentAccessScope =
277 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
278 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
279 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
280 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebben40a22942020-12-22 14:22:06 -0700281static constexpr VkPipelineStageFlags2KHR kDepthStencilAttachmentExecScope =
282 VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT_KHR | VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700283static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
284 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
285 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
286 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -0700287static constexpr VkPipelineStageFlags2KHR kRasterAttachmentExecScope = kDepthStencilAttachmentExecScope | kColorAttachmentExecScope;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700288static const SyncStageAccessFlags kRasterAttachmentAccessScope = kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope;
John Zulaufb027cdb2020-05-21 14:25:22 -0600289
John Zulauf8e3c3e92021-01-06 11:19:36 -0700290ResourceAccessState::OrderingBarriers ResourceAccessState::kOrderingRules = {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700291 {{VK_PIPELINE_STAGE_2_NONE_KHR, SyncStageAccessFlags()},
John Zulauf8e3c3e92021-01-06 11:19:36 -0700292 {kColorAttachmentExecScope, kColorAttachmentAccessScope},
293 {kDepthStencilAttachmentExecScope, kDepthStencilAttachmentAccessScope},
294 {kRasterAttachmentExecScope, kRasterAttachmentAccessScope}}};
295
John Zulauf7635de32020-05-29 17:14:15 -0600296// Sometimes we have an internal access conflict, and we using the kCurrentCommandTag to set and detect in temporary/proxy contexts
John Zulauf14940722021-04-12 15:19:02 -0600297static const ResourceUsageTag kCurrentCommandTag(ResourceUsageRecord::kMaxIndex);
John Zulaufb027cdb2020-05-21 14:25:22 -0600298
Jeremy Gebben62c3bf42021-07-21 15:38:24 -0600299static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) { return bindable.GetFakeBaseAddress(); }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600300
locke-lunarg3c038002020-04-30 23:08:08 -0600301inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
302 if (size == VK_WHOLE_SIZE) {
303 return (whole_size - offset);
304 }
305 return size;
306}
307
John Zulauf3e86bf02020-09-12 10:47:57 -0600308static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
309 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
310}
311
John Zulauf16adfc92020-04-08 10:28:33 -0600312template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600313static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600314 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
315}
316
John Zulauf355e49b2020-04-24 15:11:15 -0600317static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600318
John Zulauf3e86bf02020-09-12 10:47:57 -0600319static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
320 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
321}
322
323static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
324 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
325}
326
John Zulauf4a6105a2020-11-17 15:11:05 -0700327// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
328//
John Zulauf10f1f522020-12-18 12:00:35 -0700329// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
330//
John Zulauf4a6105a2020-11-17 15:11:05 -0700331// Usage:
332// Constructor() -- initializes the generator to point to the begin of the space declared.
333// * -- the current range of the generator empty signfies end
334// ++ -- advance to the next non-empty range (or end)
335
336// A wrapper for a single range with the same semantics as the actual generators below
337template <typename KeyType>
338class SingleRangeGenerator {
339 public:
340 SingleRangeGenerator(const KeyType &range) : current_(range) {}
John Zulaufd5115702021-01-18 12:34:33 -0700341 const KeyType &operator*() const { return current_; }
342 const KeyType *operator->() const { return &current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700343 SingleRangeGenerator &operator++() {
344 current_ = KeyType(); // just one real range
345 return *this;
346 }
347
348 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
349
350 private:
351 SingleRangeGenerator() = default;
352 const KeyType range_;
353 KeyType current_;
354};
355
John Zulaufae842002021-04-15 18:20:55 -0600356// Generate the ranges that are the intersection of range and the entries in the RangeMap
357template <typename RangeMap, typename KeyType = typename RangeMap::key_type>
358class MapRangesRangeGenerator {
John Zulauf4a6105a2020-11-17 15:11:05 -0700359 public:
John Zulaufd5115702021-01-18 12:34:33 -0700360 // Default constructed is safe to dereference for "empty" test, but for no other operation.
John Zulaufae842002021-04-15 18:20:55 -0600361 MapRangesRangeGenerator() : range_(), map_(nullptr), map_pos_(), current_() {
John Zulaufd5115702021-01-18 12:34:33 -0700362 // Default construction for KeyType *must* be empty range
363 assert(current_.empty());
364 }
John Zulaufae842002021-04-15 18:20:55 -0600365 MapRangesRangeGenerator(const RangeMap &filter, const KeyType &range) : range_(range), map_(&filter), map_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700366 SeekBegin();
367 }
John Zulaufae842002021-04-15 18:20:55 -0600368 MapRangesRangeGenerator(const MapRangesRangeGenerator &from) = default;
John Zulaufd5115702021-01-18 12:34:33 -0700369
John Zulauf4a6105a2020-11-17 15:11:05 -0700370 const KeyType &operator*() const { return current_; }
371 const KeyType *operator->() const { return &current_; }
John Zulaufae842002021-04-15 18:20:55 -0600372 MapRangesRangeGenerator &operator++() {
373 ++map_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700374 UpdateCurrent();
375 return *this;
376 }
377
John Zulaufae842002021-04-15 18:20:55 -0600378 bool operator==(const MapRangesRangeGenerator &other) const { return current_ == other.current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700379
John Zulaufae842002021-04-15 18:20:55 -0600380 protected:
John Zulauf4a6105a2020-11-17 15:11:05 -0700381 void UpdateCurrent() {
John Zulaufae842002021-04-15 18:20:55 -0600382 if (map_pos_ != map_->cend()) {
383 current_ = range_ & map_pos_->first;
John Zulauf4a6105a2020-11-17 15:11:05 -0700384 } else {
385 current_ = KeyType();
386 }
387 }
388 void SeekBegin() {
John Zulaufae842002021-04-15 18:20:55 -0600389 map_pos_ = map_->lower_bound(range_);
John Zulauf4a6105a2020-11-17 15:11:05 -0700390 UpdateCurrent();
391 }
John Zulaufae842002021-04-15 18:20:55 -0600392
393 // Adding this functionality here, to avoid gratuitous Base:: qualifiers in the derived class
394 // Note: Not exposed in this classes public interface to encourage using a consistent ++/empty generator semantic
395 template <typename Pred>
396 MapRangesRangeGenerator &PredicatedIncrement(Pred &pred) {
397 do {
398 ++map_pos_;
399 } while (map_pos_ != map_->cend() && map_pos_->first.intersects(range_) && !pred(map_pos_));
400 UpdateCurrent();
401 return *this;
402 }
403
John Zulauf4a6105a2020-11-17 15:11:05 -0700404 const KeyType range_;
John Zulaufae842002021-04-15 18:20:55 -0600405 const RangeMap *map_;
406 typename RangeMap::const_iterator map_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700407 KeyType current_;
408};
John Zulaufd5115702021-01-18 12:34:33 -0700409using SingleAccessRangeGenerator = SingleRangeGenerator<ResourceAccessRange>;
John Zulaufae842002021-04-15 18:20:55 -0600410using EventSimpleRangeGenerator = MapRangesRangeGenerator<SyncEventState::ScopeMap>;
John Zulauf4a6105a2020-11-17 15:11:05 -0700411
John Zulaufae842002021-04-15 18:20:55 -0600412// Generate the ranges for entries meeting the predicate that are the intersection of range and the entries in the RangeMap
413template <typename RangeMap, typename Predicate, typename KeyType = typename RangeMap::key_type>
414class PredicatedMapRangesRangeGenerator : public MapRangesRangeGenerator<RangeMap, KeyType> {
415 public:
416 using Base = MapRangesRangeGenerator<RangeMap, KeyType>;
417 // Default constructed is safe to dereference for "empty" test, but for no other operation.
418 PredicatedMapRangesRangeGenerator() : Base(), pred_() {}
419 PredicatedMapRangesRangeGenerator(const RangeMap &filter, const KeyType &range, Predicate pred)
420 : Base(filter, range), pred_(pred) {}
421 PredicatedMapRangesRangeGenerator(const PredicatedMapRangesRangeGenerator &from) = default;
422
423 PredicatedMapRangesRangeGenerator &operator++() {
424 Base::PredicatedIncrement(pred_);
425 return *this;
426 }
427
428 protected:
429 Predicate pred_;
430};
John Zulauf4a6105a2020-11-17 15:11:05 -0700431
432// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulaufae842002021-04-15 18:20:55 -0600433// Templated to allow for different Range generators or map sources...
434template <typename RangeMap, typename RangeGen, typename KeyType = typename RangeMap::key_type>
John Zulauf4a6105a2020-11-17 15:11:05 -0700435class FilteredGeneratorGenerator {
436 public:
John Zulaufd5115702021-01-18 12:34:33 -0700437 // Default constructed is safe to dereference for "empty" test, but for no other operation.
438 FilteredGeneratorGenerator() : filter_(nullptr), gen_(), filter_pos_(), current_() {
439 // Default construction for KeyType *must* be empty range
440 assert(current_.empty());
441 }
John Zulaufae842002021-04-15 18:20:55 -0600442 FilteredGeneratorGenerator(const RangeMap &filter, RangeGen &gen) : filter_(&filter), gen_(gen), filter_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700443 SeekBegin();
444 }
John Zulaufd5115702021-01-18 12:34:33 -0700445 FilteredGeneratorGenerator(const FilteredGeneratorGenerator &from) = default;
John Zulauf4a6105a2020-11-17 15:11:05 -0700446 const KeyType &operator*() const { return current_; }
447 const KeyType *operator->() const { return &current_; }
448 FilteredGeneratorGenerator &operator++() {
449 KeyType gen_range = GenRange();
450 KeyType filter_range = FilterRange();
451 current_ = KeyType();
452 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
453 if (gen_range.end > filter_range.end) {
454 // if the generated range is beyond the filter_range, advance the filter range
455 filter_range = AdvanceFilter();
456 } else {
457 gen_range = AdvanceGen();
458 }
459 current_ = gen_range & filter_range;
460 }
461 return *this;
462 }
463
464 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
465
466 private:
467 KeyType AdvanceFilter() {
468 ++filter_pos_;
469 auto filter_range = FilterRange();
470 if (filter_range.valid()) {
471 FastForwardGen(filter_range);
472 }
473 return filter_range;
474 }
475 KeyType AdvanceGen() {
John Zulaufd5115702021-01-18 12:34:33 -0700476 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700477 auto gen_range = GenRange();
478 if (gen_range.valid()) {
479 FastForwardFilter(gen_range);
480 }
481 return gen_range;
482 }
483
484 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
John Zulaufd5115702021-01-18 12:34:33 -0700485 KeyType GenRange() const { return *gen_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700486
487 KeyType FastForwardFilter(const KeyType &range) {
488 auto filter_range = FilterRange();
489 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700490 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700491 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
492 if (retry_count < kRetryLimit) {
493 ++filter_pos_;
494 filter_range = FilterRange();
495 retry_count++;
496 } else {
497 // Okay we've tried walking, do a seek.
498 filter_pos_ = filter_->lower_bound(range);
499 break;
500 }
501 }
502 return FilterRange();
503 }
504
505 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
506 // faster.
507 KeyType FastForwardGen(const KeyType &range) {
508 auto gen_range = GenRange();
509 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
John Zulaufd5115702021-01-18 12:34:33 -0700510 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700511 gen_range = GenRange();
512 }
513 return gen_range;
514 }
515
516 void SeekBegin() {
517 auto gen_range = GenRange();
518 if (gen_range.empty()) {
519 current_ = KeyType();
520 filter_pos_ = filter_->cend();
521 } else {
522 filter_pos_ = filter_->lower_bound(gen_range);
523 current_ = gen_range & FilterRange();
524 }
525 }
526
John Zulaufae842002021-04-15 18:20:55 -0600527 const RangeMap *filter_;
John Zulaufd5115702021-01-18 12:34:33 -0700528 RangeGen gen_;
John Zulaufae842002021-04-15 18:20:55 -0600529 typename RangeMap::const_iterator filter_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700530 KeyType current_;
531};
532
533using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
534
John Zulauf5c5e88d2019-12-26 11:22:02 -0700535
John Zulauf3e86bf02020-09-12 10:47:57 -0600536ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
537 VkDeviceSize stride) {
538 VkDeviceSize range_start = offset + first_index * stride;
539 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600540 if (count == UINT32_MAX) {
541 range_size = buf_whole_size - range_start;
542 } else {
543 range_size = count * stride;
544 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600545 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600546}
547
locke-lunarg654e3692020-06-04 17:19:15 -0600548SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
549 VkShaderStageFlagBits stage_flag) {
550 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
551 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
552 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
553 }
554 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
555 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
556 assert(0);
557 }
558 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
559 return stage_access->second.uniform_read;
560 }
561
562 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
563 // Because if write hazard happens, read hazard might or might not happen.
564 // But if write hazard doesn't happen, read hazard is impossible to happen.
565 if (descriptor_data.is_writable) {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700566 return stage_access->second.storage_write;
locke-lunarg654e3692020-06-04 17:19:15 -0600567 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700568 // TODO: sampled_read
569 return stage_access->second.storage_read;
locke-lunarg654e3692020-06-04 17:19:15 -0600570}
571
locke-lunarg37047832020-06-12 13:44:45 -0600572bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
573 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
574 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
575 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
576 ? true
577 : false;
578}
579
580bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
581 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
582 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
583 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
584 ? true
585 : false;
586}
587
John Zulauf355e49b2020-04-24 15:11:15 -0600588// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600589template <typename Action>
590static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
591 Action &action) {
592 // At this point the "apply over range" logic only supports a single memory binding
593 if (!SimpleBinding(image_state)) return;
594 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600595 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700596 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
597 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600598 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700599 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600600 }
601}
602
John Zulauf7635de32020-05-29 17:14:15 -0600603// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
604// Used by both validation and record operations
605//
606// The signature for Action() reflect the needs of both uses.
607template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700608void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
609 uint32_t subpass) {
John Zulauf7635de32020-05-29 17:14:15 -0600610 const auto &rp_ci = rp_state.createInfo;
611 const auto *attachment_ci = rp_ci.pAttachments;
612 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
613
614 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
615 const auto *color_attachments = subpass_ci.pColorAttachments;
616 const auto *color_resolve = subpass_ci.pResolveAttachments;
617 if (color_resolve && color_attachments) {
618 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
619 const auto &color_attach = color_attachments[i].attachment;
620 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
621 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
622 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700623 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ,
624 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600625 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700626 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
627 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600628 }
629 }
630 }
631
632 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700633 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600634 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
635 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
636 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
637 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
638 const auto src_ci = attachment_ci[src_at];
639 // The formats are required to match so we can pick either
640 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
641 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
642 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
John Zulauf7635de32020-05-29 17:14:15 -0600643
644 // Figure out which aspects are actually touched during resolve operations
645 const char *aspect_string = nullptr;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700646 AttachmentViewGen::Gen gen_type = AttachmentViewGen::Gen::kRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600647 if (resolve_depth && resolve_stencil) {
John Zulauf7635de32020-05-29 17:14:15 -0600648 aspect_string = "depth/stencil";
649 } else if (resolve_depth) {
650 // Validate depth only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700651 gen_type = AttachmentViewGen::Gen::kDepthOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600652 aspect_string = "depth";
653 } else if (resolve_stencil) {
654 // Validate all stencil only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700655 gen_type = AttachmentViewGen::Gen::kStencilOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600656 aspect_string = "stencil";
657 }
658
John Zulaufd0ec59f2021-03-13 14:25:08 -0700659 if (aspect_string) {
660 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at], gen_type,
661 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster);
662 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at], gen_type,
663 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulauf7635de32020-05-29 17:14:15 -0600664 }
665 }
666}
667
668// Action for validating resolve operations
669class ValidateResolveAction {
670 public:
John Zulauffaea0ee2021-01-14 14:01:32 -0700671 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context,
John Zulauf64ffe552021-02-06 10:25:07 -0700672 const CommandExecutionContext &ex_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600673 : render_pass_(render_pass),
674 subpass_(subpass),
675 context_(context),
John Zulauf64ffe552021-02-06 10:25:07 -0700676 ex_context_(ex_context),
John Zulauf7635de32020-05-29 17:14:15 -0600677 func_name_(func_name),
678 skip_(false) {}
679 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
John Zulaufd0ec59f2021-03-13 14:25:08 -0700680 const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage,
681 SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600682 HazardResult hazard;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700683 hazard = context_.DetectHazard(view_gen, gen_type, current_usage, ordering_rule);
John Zulauf7635de32020-05-29 17:14:15 -0600684 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -0700685 skip_ |=
John Zulauf64ffe552021-02-06 10:25:07 -0700686 ex_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700687 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
688 " to resolve attachment %" PRIu32 ". Access info %s.",
689 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name,
John Zulauf64ffe552021-02-06 10:25:07 -0700690 attachment_name, src_at, dst_at, ex_context_.FormatUsage(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600691 }
692 }
693 // Providing a mechanism for the constructing caller to get the result of the validation
694 bool GetSkip() const { return skip_; }
695
696 private:
697 VkRenderPass render_pass_;
698 const uint32_t subpass_;
699 const AccessContext &context_;
John Zulauf64ffe552021-02-06 10:25:07 -0700700 const CommandExecutionContext &ex_context_;
John Zulauf7635de32020-05-29 17:14:15 -0600701 const char *func_name_;
702 bool skip_;
703};
704
705// Update action for resolve operations
706class UpdateStateResolveAction {
707 public:
John Zulauf14940722021-04-12 15:19:02 -0600708 UpdateStateResolveAction(AccessContext &context, ResourceUsageTag tag) : context_(context), tag_(tag) {}
John Zulaufd0ec59f2021-03-13 14:25:08 -0700709 void operator()(const char *, const char *, uint32_t, uint32_t, const AttachmentViewGen &view_gen,
710 AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600711 // Ignores validation only arguments...
John Zulaufd0ec59f2021-03-13 14:25:08 -0700712 context_.UpdateAccessState(view_gen, gen_type, current_usage, ordering_rule, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600713 }
714
715 private:
716 AccessContext &context_;
John Zulauf14940722021-04-12 15:19:02 -0600717 const ResourceUsageTag tag_;
John Zulauf7635de32020-05-29 17:14:15 -0600718};
719
John Zulauf59e25072020-07-17 10:55:21 -0600720void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
John Zulauf14940722021-04-12 15:19:02 -0600721 const SyncStageAccessFlags &prior_, const ResourceUsageTag tag_) {
John Zulauf4fa68462021-04-26 21:04:22 -0600722 access_state = layer_data::make_unique<const ResourceAccessState>(*access_state_);
John Zulauf59e25072020-07-17 10:55:21 -0600723 usage_index = usage_index_;
724 hazard = hazard_;
725 prior_access = prior_;
726 tag = tag_;
727}
728
John Zulauf4fa68462021-04-26 21:04:22 -0600729void HazardResult::AddRecordedAccess(const ResourceFirstAccess &first_access) {
730 recorded_access = layer_data::make_unique<const ResourceFirstAccess>(first_access);
731}
732
John Zulauf540266b2020-04-06 18:54:53 -0600733AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
734 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600735 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600736 Reset();
737 const auto &subpass_dep = dependencies[subpass];
John Zulauf22aefed2021-03-11 18:14:35 -0700738 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
739 prev_.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
John Zulauf355e49b2020-04-24 15:11:15 -0600740 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600741 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600742 const auto prev_pass = prev_dep.first->pass;
743 const auto &prev_barriers = prev_dep.second;
744 assert(prev_dep.second.size());
745 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
746 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700747 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600748
749 async_.reserve(subpass_dep.async.size());
750 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700751 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600752 }
John Zulauf22aefed2021-03-11 18:14:35 -0700753 if (has_barrier_from_external) {
754 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
755 prev_.emplace_back(external_context, queue_flags, subpass_dep.barrier_from_external);
756 src_external_ = &prev_.back();
John Zulaufe5da6e52020-03-18 15:32:18 -0600757 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600758 if (subpass_dep.barrier_to_external.size()) {
759 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600760 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700761}
762
John Zulauf5f13a792020-03-10 07:31:21 -0600763template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700764HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600765 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600766 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600767 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600768
769 HazardResult hazard;
770 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
771 hazard = detector.Detect(prev);
772 }
773 return hazard;
774}
775
John Zulauf4a6105a2020-11-17 15:11:05 -0700776template <typename Action>
777void AccessContext::ForAll(Action &&action) {
778 for (const auto address_type : kAddressTypes) {
779 auto &accesses = GetAccessStateMap(address_type);
780 for (const auto &access : accesses) {
781 action(address_type, access);
782 }
783 }
784}
785
John Zulauf3d84f1b2020-03-09 13:33:25 -0600786// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
787// the DAG of the contexts (for example subpasses)
788template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700789HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600790 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600791 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600792
John Zulauf1a224292020-06-30 14:52:13 -0600793 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600794 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
795 // so we'll check these first
796 for (const auto &async_context : async_) {
797 hazard = async_context->DetectAsyncHazard(type, detector, range);
798 if (hazard.hazard) return hazard;
799 }
John Zulauf5f13a792020-03-10 07:31:21 -0600800 }
801
John Zulauf1a224292020-06-30 14:52:13 -0600802 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600803
John Zulauf69133422020-05-20 14:55:53 -0600804 const auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600805 const auto the_end = accesses.cend(); // End is not invalidated
806 auto pos = accesses.lower_bound(range);
John Zulauf69133422020-05-20 14:55:53 -0600807 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600808
John Zulauf3cafbf72021-03-26 16:55:19 -0600809 while (pos != the_end && pos->first.begin < range.end) {
John Zulauf69133422020-05-20 14:55:53 -0600810 // Cover any leading gap, or gap between entries
811 if (detect_prev) {
812 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
813 // Cover any leading gap, or gap between entries
814 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600815 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600816 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600817 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600818 if (hazard.hazard) return hazard;
819 }
John Zulauf69133422020-05-20 14:55:53 -0600820 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
821 gap.begin = pos->first.end;
822 }
823
824 hazard = detector.Detect(pos);
825 if (hazard.hazard) return hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600826 ++pos;
John Zulauf69133422020-05-20 14:55:53 -0600827 }
828
829 if (detect_prev) {
830 // Detect in the trailing empty as needed
831 gap.end = range.end;
832 if (gap.non_empty()) {
833 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600834 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600835 }
836
837 return hazard;
838}
839
840// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
841template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700842HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
843 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600844 auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600845 auto pos = accesses.lower_bound(range);
846 const auto the_end = accesses.end();
John Zulauf16adfc92020-04-08 10:28:33 -0600847
John Zulauf3d84f1b2020-03-09 13:33:25 -0600848 HazardResult hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600849 while (pos != the_end && pos->first.begin < range.end) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700850 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3cafbf72021-03-26 16:55:19 -0600851 if (hazard.hazard) break;
852 ++pos;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600853 }
John Zulauf16adfc92020-04-08 10:28:33 -0600854
John Zulauf3d84f1b2020-03-09 13:33:25 -0600855 return hazard;
856}
857
John Zulaufb02c1eb2020-10-06 16:33:36 -0600858struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700859 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600860 void operator()(ResourceAccessState *access) const {
861 assert(access);
862 access->ApplyBarriers(barriers, true);
863 }
864 const std::vector<SyncBarrier> &barriers;
865};
866
John Zulauf22aefed2021-03-11 18:14:35 -0700867struct ApplyTrackbackStackAction {
868 explicit ApplyTrackbackStackAction(const std::vector<SyncBarrier> &barriers_,
869 const ResourceAccessStateFunction *previous_barrier_ = nullptr)
870 : barriers(barriers_), previous_barrier(previous_barrier_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600871 void operator()(ResourceAccessState *access) const {
872 assert(access);
873 assert(!access->HasPendingState());
874 access->ApplyBarriers(barriers, false);
875 access->ApplyPendingBarriers(kCurrentCommandTag);
John Zulauf22aefed2021-03-11 18:14:35 -0700876 if (previous_barrier) {
877 assert(bool(*previous_barrier));
878 (*previous_barrier)(access);
879 }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600880 }
881 const std::vector<SyncBarrier> &barriers;
John Zulauf22aefed2021-03-11 18:14:35 -0700882 const ResourceAccessStateFunction *previous_barrier;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600883};
884
885// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
886// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
887// *different* map from dest.
888// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
889// range [first, last)
890template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600891static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
892 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600893 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600894 auto at = entry;
895 for (auto pos = first; pos != last; ++pos) {
896 // Every member of the input iterator range must fit within the remaining portion of entry
897 assert(at->first.includes(pos->first));
898 assert(at != dest->end());
899 // Trim up at to the same size as the entry to resolve
900 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600901 auto access = pos->second; // intentional copy
902 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600903 at->second.Resolve(access);
904 ++at; // Go to the remaining unused section of entry
905 }
906}
907
John Zulaufa0a98292020-09-18 09:30:10 -0600908static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
909 SyncBarrier merged = {};
910 for (const auto &barrier : barriers) {
911 merged.Merge(barrier);
912 }
913 return merged;
914}
915
John Zulaufb02c1eb2020-10-06 16:33:36 -0600916template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700917void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600918 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
919 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600920 if (!range.non_empty()) return;
921
John Zulauf355e49b2020-04-24 15:11:15 -0600922 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
923 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600924 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600925 if (current->pos_B->valid) {
926 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600927 auto access = src_pos->second; // intentional copy
928 barrier_action(&access);
929
John Zulauf16adfc92020-04-08 10:28:33 -0600930 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600931 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
932 trimmed->second.Resolve(access);
933 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600934 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600935 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600936 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600937 }
John Zulauf16adfc92020-04-08 10:28:33 -0600938 } else {
939 // we have to descend to fill this gap
940 if (recur_to_infill) {
John Zulauf22aefed2021-03-11 18:14:35 -0700941 ResourceAccessRange recurrence_range = current_range;
942 // The current context is empty for the current range, so recur to fill the gap.
943 // Since we will be recurring back up the DAG, expand the gap descent to cover the full range for which B
944 // is not valid, to minimize that recurrence
945 if (current->pos_B.at_end()) {
946 // Do the remainder here....
947 recurrence_range.end = range.end;
John Zulauf355e49b2020-04-24 15:11:15 -0600948 } else {
John Zulauf22aefed2021-03-11 18:14:35 -0700949 // Recur only over the range until B becomes valid (within the limits of range).
950 recurrence_range.end = std::min(range.end, current->pos_B->lower_bound->first.begin);
John Zulauf355e49b2020-04-24 15:11:15 -0600951 }
John Zulauf22aefed2021-03-11 18:14:35 -0700952 ResolvePreviousAccessStack(type, recurrence_range, resolve_map, infill_state, barrier_action);
953
John Zulauf355e49b2020-04-24 15:11:15 -0600954 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
955 // iterator of the outer while.
956
957 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
958 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
959 // we stepped on the dest map
John Zulauf22aefed2021-03-11 18:14:35 -0700960 const auto seek_to = recurrence_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
locke-lunarg88dbb542020-06-23 22:05:42 -0600961 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600962 current.seek(seek_to);
963 } else if (!current->pos_A->valid && infill_state) {
964 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
965 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
966 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600967 }
John Zulauf5f13a792020-03-10 07:31:21 -0600968 }
John Zulauf16adfc92020-04-08 10:28:33 -0600969 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600970 }
John Zulauf1a224292020-06-30 14:52:13 -0600971
972 // Infill if range goes passed both the current and resolve map prior contents
973 if (recur_to_infill && (current->range.end < range.end)) {
974 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
John Zulauf22aefed2021-03-11 18:14:35 -0700975 ResolvePreviousAccessStack<BarrierAction>(type, trailing_fill_range, resolve_map, infill_state, barrier_action);
John Zulauf1a224292020-06-30 14:52:13 -0600976 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600977}
978
John Zulauf22aefed2021-03-11 18:14:35 -0700979template <typename BarrierAction>
980void AccessContext::ResolvePreviousAccessStack(AccessAddressType type, const ResourceAccessRange &range,
981 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
982 const BarrierAction &previous_barrier) const {
983 ResourceAccessStateFunction stacked_barrier(std::ref(previous_barrier));
984 ResolvePreviousAccess(type, range, descent_map, infill_state, &stacked_barrier);
985}
986
John Zulauf43cc7462020-12-03 12:33:12 -0700987void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
John Zulauf22aefed2021-03-11 18:14:35 -0700988 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
989 const ResourceAccessStateFunction *previous_barrier) const {
990 if (prev_.size() == 0) {
John Zulauf5f13a792020-03-10 07:31:21 -0600991 if (range.non_empty() && infill_state) {
John Zulauf22aefed2021-03-11 18:14:35 -0700992 // Fill the empty poritions of descent_map with the default_state with the barrier function applied (iff present)
993 ResourceAccessState state_copy;
994 if (previous_barrier) {
995 assert(bool(*previous_barrier));
996 state_copy = *infill_state;
997 (*previous_barrier)(&state_copy);
998 infill_state = &state_copy;
999 }
1000 sparse_container::update_range_value(*descent_map, range, *infill_state,
1001 sparse_container::value_precedence::prefer_dest);
John Zulauf5f13a792020-03-10 07:31:21 -06001002 }
1003 } else {
1004 // Look for something to fill the gap further along.
1005 for (const auto &prev_dep : prev_) {
John Zulauf22aefed2021-03-11 18:14:35 -07001006 const ApplyTrackbackStackAction barrier_action(prev_dep.barriers, previous_barrier);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001007 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001008 }
John Zulauf5f13a792020-03-10 07:31:21 -06001009 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001010}
1011
John Zulauf4a6105a2020-11-17 15:11:05 -07001012// Non-lazy import of all accesses, WaitEvents needs this.
1013void AccessContext::ResolvePreviousAccesses() {
1014 ResourceAccessState default_state;
John Zulauf22aefed2021-03-11 18:14:35 -07001015 if (!prev_.size()) return; // If no previous contexts, nothing to do
1016
John Zulauf4a6105a2020-11-17 15:11:05 -07001017 for (const auto address_type : kAddressTypes) {
1018 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
1019 }
1020}
1021
John Zulauf43cc7462020-12-03 12:33:12 -07001022AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
1023 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -06001024}
1025
John Zulauf1507ee42020-05-18 11:33:09 -06001026static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -06001027 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
1028 ? SYNC_ACCESS_INDEX_NONE
1029 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
1030 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -06001031 return stage_access;
1032}
1033static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -06001034 const auto stage_access =
1035 (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
1036 ? SYNC_ACCESS_INDEX_NONE
1037 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
1038 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -06001039 return stage_access;
1040}
1041
John Zulauf7635de32020-05-29 17:14:15 -06001042// Caller must manage returned pointer
1043static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001044 uint32_t subpass, const AttachmentViewGenVector &attachment_views) {
John Zulauf7635de32020-05-29 17:14:15 -06001045 auto *proxy = new AccessContext(context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001046 proxy->UpdateAttachmentResolveAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
1047 proxy->UpdateAttachmentStoreAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -06001048 return proxy;
1049}
1050
John Zulaufb02c1eb2020-10-06 16:33:36 -06001051template <typename BarrierAction>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001052void AccessContext::ResolveAccessRange(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1053 BarrierAction &barrier_action, ResourceAccessRangeMap *descent_map,
1054 const ResourceAccessState *infill_state) const {
1055 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1056 if (!attachment_gen) return;
1057
1058 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1059 const AccessAddressType address_type = view_gen.GetAddressType();
1060 for (; range_gen->non_empty(); ++range_gen) {
1061 ResolveAccessRange(address_type, *range_gen, barrier_action, descent_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001062 }
John Zulauf62f10592020-04-03 12:20:02 -06001063}
1064
John Zulauf7635de32020-05-29 17:14:15 -06001065// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf64ffe552021-02-06 10:25:07 -07001066bool AccessContext::ValidateLayoutTransitions(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001067 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001068 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001069 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -06001070 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
1071 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
1072 // those affects have not been recorded yet.
1073 //
1074 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1075 // to apply and only copy then, if this proves a hot spot.
1076 std::unique_ptr<AccessContext> proxy_for_prev;
1077 TrackBack proxy_track_back;
1078
John Zulauf355e49b2020-04-24 15:11:15 -06001079 const auto &transitions = rp_state.subpass_transitions[subpass];
1080 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -06001081 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
1082
1083 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
John Zulauf22aefed2021-03-11 18:14:35 -07001084 assert(track_back);
John Zulauf7635de32020-05-29 17:14:15 -06001085 if (prev_needs_proxy) {
1086 if (!proxy_for_prev) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001087 proxy_for_prev.reset(
1088 CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass, attachment_views));
John Zulauf7635de32020-05-29 17:14:15 -06001089 proxy_track_back = *track_back;
1090 proxy_track_back.context = proxy_for_prev.get();
1091 }
1092 track_back = &proxy_track_back;
1093 }
1094 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -06001095 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001096 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001097 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1098 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
1099 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1100 string_VkImageLayout(transition.old_layout),
1101 string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07001102 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001103 }
1104 }
1105 return skip;
1106}
1107
John Zulauf64ffe552021-02-06 10:25:07 -07001108bool AccessContext::ValidateLoadOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001109 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001110 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001111 bool skip = false;
1112 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufa0a98292020-09-18 09:30:10 -06001113
John Zulauf1507ee42020-05-18 11:33:09 -06001114 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1115 if (subpass == rp_state.attachment_first_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001116 const auto &view_gen = attachment_views[i];
1117 if (!view_gen.IsValid()) continue;
John Zulauf1507ee42020-05-18 11:33:09 -06001118 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001119
1120 // Need check in the following way
1121 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1122 // vs. transition
1123 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1124 // for each aspect loaded.
1125
1126 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001127 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001128 const bool is_color = !(has_depth || has_stencil);
1129
1130 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001131 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001132
John Zulaufaff20662020-06-01 14:07:58 -06001133 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001134 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001135
John Zulaufb02c1eb2020-10-06 16:33:36 -06001136 bool checked_stencil = false;
John Zulauf57261402021-08-13 11:32:06 -06001137 if (is_color && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001138 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea, load_index, SyncOrdering::kColorAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001139 aspect = "color";
1140 } else {
John Zulauf57261402021-08-13 11:32:06 -06001141 if (has_depth && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001142 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_index,
1143 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001144 aspect = "depth";
1145 }
John Zulauf57261402021-08-13 11:32:06 -06001146 if (!hazard.hazard && has_stencil && (stencil_load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001147 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, stencil_load_index,
1148 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001149 aspect = "stencil";
1150 checked_stencil = true;
1151 }
1152 }
1153
1154 if (hazard.hazard) {
1155 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001156 const auto &sync_state = ex_context.GetSyncState();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001157 if (hazard.tag == kCurrentCommandTag) {
1158 // Hazard vs. ILT
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001159 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulaufb02c1eb2020-10-06 16:33:36 -06001160 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1161 " aspect %s during load with loadOp %s.",
1162 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1163 } else {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001164 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauf1507ee42020-05-18 11:33:09 -06001165 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001166 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001167 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf64ffe552021-02-06 10:25:07 -07001168 ex_context.FormatUsage(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001169 }
1170 }
1171 }
1172 }
1173 return skip;
1174}
1175
John Zulaufaff20662020-06-01 14:07:58 -06001176// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1177// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1178// store is part of the same Next/End operation.
1179// The latter is handled in layout transistion validation directly
John Zulauf64ffe552021-02-06 10:25:07 -07001180bool AccessContext::ValidateStoreOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufaff20662020-06-01 14:07:58 -06001181 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001182 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001183 bool skip = false;
1184 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001185
1186 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1187 if (subpass == rp_state.attachment_last_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001188 const AttachmentViewGen &view_gen = attachment_views[i];
1189 if (!view_gen.IsValid()) continue;
John Zulaufaff20662020-06-01 14:07:58 -06001190 const auto &ci = attachment_ci[i];
1191
1192 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1193 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1194 // sake, we treat DONT_CARE as writing.
1195 const bool has_depth = FormatHasDepth(ci.format);
1196 const bool has_stencil = FormatHasStencil(ci.format);
1197 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001198 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001199 if (!has_stencil && !store_op_stores) continue;
1200
1201 HazardResult hazard;
1202 const char *aspect = nullptr;
1203 bool checked_stencil = false;
1204 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001205 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
1206 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001207 aspect = "color";
1208 } else {
John Zulauf57261402021-08-13 11:32:06 -06001209 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001210 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001211 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1212 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001213 aspect = "depth";
1214 }
1215 if (!hazard.hazard && has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001216 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1217 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001218 aspect = "stencil";
1219 checked_stencil = true;
1220 }
1221 }
1222
1223 if (hazard.hazard) {
1224 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1225 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001226 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001227 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1228 " %s aspect during store with %s %s. Access info %s",
1229 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect,
John Zulauf64ffe552021-02-06 10:25:07 -07001230 op_type_string, store_op_string, ex_context.FormatUsage(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001231 }
1232 }
1233 }
1234 return skip;
1235}
1236
John Zulauf64ffe552021-02-06 10:25:07 -07001237bool AccessContext::ValidateResolveOperations(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001238 const VkRect2D &render_area, const AttachmentViewGenVector &attachment_views,
1239 const char *func_name, uint32_t subpass) const {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001240 ValidateResolveAction validate_action(rp_state.renderPass(), subpass, *this, ex_context, func_name);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001241 ResolveOperation(validate_action, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001242 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001243}
1244
John Zulauf3d84f1b2020-03-09 13:33:25 -06001245class HazardDetector {
1246 SyncStageAccessIndex usage_index_;
1247
1248 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001249 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf14940722021-04-12 15:19:02 -06001250 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001251 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001252 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001253 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001254};
1255
John Zulauf69133422020-05-20 14:55:53 -06001256class HazardDetectorWithOrdering {
1257 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001258 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001259
1260 public:
1261 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001262 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001263 }
John Zulauf14940722021-04-12 15:19:02 -06001264 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001265 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001266 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001267 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001268};
1269
John Zulauf16adfc92020-04-08 10:28:33 -06001270HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001271 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001272 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001273 const auto base_address = ResourceBaseAddress(buffer);
1274 HazardDetector detector(usage_index);
1275 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001276}
1277
John Zulauf69133422020-05-20 14:55:53 -06001278template <typename Detector>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001279HazardResult AccessContext::DetectHazard(Detector &detector, const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1280 DetectOptions options) const {
1281 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1282 if (!attachment_gen) return HazardResult();
1283
1284 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1285 const auto address_type = view_gen.GetAddressType();
1286 for (; range_gen->non_empty(); ++range_gen) {
1287 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1288 if (hazard.hazard) return hazard;
1289 }
1290
1291 return HazardResult();
1292}
1293
1294template <typename Detector>
John Zulauf69133422020-05-20 14:55:53 -06001295HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1296 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1297 const VkExtent3D &extent, DetectOptions options) const {
1298 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001299 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001300 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1301 base_address);
1302 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001303 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001304 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001305 if (hazard.hazard) return hazard;
1306 }
1307 return HazardResult();
1308}
John Zulauf110413c2021-03-20 05:38:38 -06001309template <typename Detector>
1310HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1311 const VkImageSubresourceRange &subresource_range, DetectOptions options) const {
1312 if (!SimpleBinding(image)) return HazardResult();
1313 const auto base_address = ResourceBaseAddress(image);
1314 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1315 const auto address_type = ImageAddressType(image);
1316 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf110413c2021-03-20 05:38:38 -06001317 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1318 if (hazard.hazard) return hazard;
1319 }
1320 return HazardResult();
1321}
John Zulauf69133422020-05-20 14:55:53 -06001322
John Zulauf540266b2020-04-06 18:54:53 -06001323HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1324 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1325 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001326 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1327 subresource.layerCount};
John Zulauf110413c2021-03-20 05:38:38 -06001328 HazardDetector detector(current_usage);
1329 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf1507ee42020-05-18 11:33:09 -06001330}
1331
1332HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf110413c2021-03-20 05:38:38 -06001333 const VkImageSubresourceRange &subresource_range) const {
John Zulauf69133422020-05-20 14:55:53 -06001334 HazardDetector detector(current_usage);
John Zulauf110413c2021-03-20 05:38:38 -06001335 return DetectHazard(detector, image, subresource_range, DetectOptions::kDetectAll);
John Zulauf69133422020-05-20 14:55:53 -06001336}
1337
John Zulaufd0ec59f2021-03-13 14:25:08 -07001338HazardResult AccessContext::DetectHazard(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1339 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) const {
1340 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
1341 return DetectHazard(detector, view_gen, gen_type, DetectOptions::kDetectAll);
1342}
1343
John Zulauf69133422020-05-20 14:55:53 -06001344HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001345 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001346 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001347 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001348 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001349}
1350
John Zulauf3d84f1b2020-03-09 13:33:25 -06001351class BarrierHazardDetector {
1352 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001353 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001354 SyncStageAccessFlags src_access_scope)
1355 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1356
John Zulauf5f13a792020-03-10 07:31:21 -06001357 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1358 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001359 }
John Zulauf14940722021-04-12 15:19:02 -06001360 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001361 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001362 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001363 }
1364
1365 private:
1366 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001367 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001368 SyncStageAccessFlags src_access_scope_;
1369};
1370
John Zulauf4a6105a2020-11-17 15:11:05 -07001371class EventBarrierHazardDetector {
1372 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001373 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001374 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
John Zulauf14940722021-04-12 15:19:02 -06001375 ResourceUsageTag scope_tag)
John Zulauf4a6105a2020-11-17 15:11:05 -07001376 : usage_index_(usage_index),
1377 src_exec_scope_(src_exec_scope),
1378 src_access_scope_(src_access_scope),
1379 event_scope_(event_scope),
1380 scope_pos_(event_scope.cbegin()),
1381 scope_end_(event_scope.cend()),
1382 scope_tag_(scope_tag) {}
1383
1384 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1385 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1386 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1387 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1388 if (scope_pos_ == scope_end_) return HazardResult();
1389 if (!scope_pos_->first.intersects(pos->first)) {
1390 event_scope_.lower_bound(pos->first);
1391 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1392 }
1393
1394 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1395 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1396 }
John Zulauf14940722021-04-12 15:19:02 -06001397 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07001398 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1399 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1400 }
1401
1402 private:
1403 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001404 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001405 SyncStageAccessFlags src_access_scope_;
1406 const SyncEventState::ScopeMap &event_scope_;
1407 SyncEventState::ScopeMap::const_iterator scope_pos_;
1408 SyncEventState::ScopeMap::const_iterator scope_end_;
John Zulauf14940722021-04-12 15:19:02 -06001409 const ResourceUsageTag scope_tag_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001410};
1411
Jeremy Gebben40a22942020-12-22 14:22:06 -07001412HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001413 const SyncStageAccessFlags &src_access_scope,
1414 const VkImageSubresourceRange &subresource_range,
1415 const SyncEventState &sync_event, DetectOptions options) const {
1416 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1417 // first access scope map to use, and there's no easy way to plumb it in below.
1418 const auto address_type = ImageAddressType(image);
1419 const auto &event_scope = sync_event.FirstScope(address_type);
1420
1421 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1422 event_scope, sync_event.first_scope_tag);
John Zulauf110413c2021-03-20 05:38:38 -06001423 return DetectHazard(detector, image, subresource_range, options);
John Zulauf4a6105a2020-11-17 15:11:05 -07001424}
1425
John Zulaufd0ec59f2021-03-13 14:25:08 -07001426HazardResult AccessContext::DetectImageBarrierHazard(const AttachmentViewGen &view_gen, const SyncBarrier &barrier,
1427 DetectOptions options) const {
1428 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, barrier.src_exec_scope.exec_scope,
1429 barrier.src_access_scope);
1430 return DetectHazard(detector, view_gen, AttachmentViewGen::Gen::kViewSubresource, options);
1431}
1432
Jeremy Gebben40a22942020-12-22 14:22:06 -07001433HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001434 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001435 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001436 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001437 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
John Zulauf110413c2021-03-20 05:38:38 -06001438 return DetectHazard(detector, image, subresource_range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001439}
1440
Jeremy Gebben40a22942020-12-22 14:22:06 -07001441HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001442 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001443 const VkImageMemoryBarrier &barrier) const {
1444 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1445 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1446 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1447}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001448HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07001449 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope.exec_scope,
John Zulauf110413c2021-03-20 05:38:38 -06001450 image_barrier.barrier.src_access_scope, image_barrier.range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001451}
John Zulauf355e49b2020-04-24 15:11:15 -06001452
John Zulauf9cb530d2019-09-30 14:14:10 -06001453template <typename Flags, typename Map>
1454SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1455 SyncStageAccessFlags scope = 0;
1456 for (const auto &bit_scope : map) {
1457 if (flag_mask < bit_scope.first) break;
1458
1459 if (flag_mask & bit_scope.first) {
1460 scope |= bit_scope.second;
1461 }
1462 }
1463 return scope;
1464}
1465
Jeremy Gebben40a22942020-12-22 14:22:06 -07001466SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags2KHR stages) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001467 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1468}
1469
Jeremy Gebben40a22942020-12-22 14:22:06 -07001470SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags2KHR accesses) {
1471 return AccessScopeImpl(sync_utils::ExpandAccessFlags(accesses), syncStageAccessMaskByAccessBit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001472}
1473
Jeremy Gebben40a22942020-12-22 14:22:06 -07001474// Getting from stage mask and access mask to stage/access masks is something we need to be good at...
1475SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001476 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1477 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1478 // of the union of all stage/access types for all the stages and the same unions for the access mask...
John Zulauf9cb530d2019-09-30 14:14:10 -06001479 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1480}
1481
1482template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001483void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001484 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1485 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001486 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001487 auto pos = accesses->lower_bound(range);
1488 if (pos == accesses->end() || !pos->first.intersects(range)) {
1489 // The range is empty, fill it with a default value.
1490 pos = action.Infill(accesses, pos, range);
1491 } else if (range.begin < pos->first.begin) {
1492 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001493 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001494 } else if (pos->first.begin < range.begin) {
1495 // Trim the beginning if needed
1496 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1497 ++pos;
1498 }
1499
1500 const auto the_end = accesses->end();
1501 while ((pos != the_end) && pos->first.intersects(range)) {
1502 if (pos->first.end > range.end) {
1503 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1504 }
1505
1506 pos = action(accesses, pos);
1507 if (pos == the_end) break;
1508
1509 auto next = pos;
1510 ++next;
1511 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1512 // Need to infill if next is disjoint
1513 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001514 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001515 next = action.Infill(accesses, next, new_range);
1516 }
1517 pos = next;
1518 }
1519}
John Zulaufd5115702021-01-18 12:34:33 -07001520
1521// Give a comparable interface for range generators and ranges
1522template <typename Action>
1523inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1524 assert(range);
1525 UpdateMemoryAccessState(accesses, *range, action);
1526}
1527
John Zulauf4a6105a2020-11-17 15:11:05 -07001528template <typename Action, typename RangeGen>
1529void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1530 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001531 RangeGen &range_gen = *range_gen_arg; // Non-const references must be * by style requirement but deref-ing * iterator is a pain
John Zulauf4a6105a2020-11-17 15:11:05 -07001532 for (; range_gen->non_empty(); ++range_gen) {
1533 UpdateMemoryAccessState(accesses, *range_gen, action);
1534 }
1535}
John Zulauf9cb530d2019-09-30 14:14:10 -06001536
John Zulaufd0ec59f2021-03-13 14:25:08 -07001537template <typename Action, typename RangeGen>
1538void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, const RangeGen &range_gen_prebuilt) {
1539 RangeGen range_gen(range_gen_prebuilt); // RangeGenerators can be expensive to create from scratch... initialize from built
1540 for (; range_gen->non_empty(); ++range_gen) {
1541 UpdateMemoryAccessState(accesses, *range_gen, action);
1542 }
1543}
John Zulauf9cb530d2019-09-30 14:14:10 -06001544struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001545 using Iterator = ResourceAccessRangeMap::iterator;
1546 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001547 // this is only called on gaps, and never returns a gap.
1548 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001549 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001550 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001551 }
John Zulauf5f13a792020-03-10 07:31:21 -06001552
John Zulauf5c5e88d2019-12-26 11:22:02 -07001553 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001554 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001555 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001556 return pos;
1557 }
1558
John Zulauf43cc7462020-12-03 12:33:12 -07001559 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf14940722021-04-12 15:19:02 -06001560 SyncOrdering ordering_rule_, ResourceUsageTag tag_)
John Zulauf8e3c3e92021-01-06 11:19:36 -07001561 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001562 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001563 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001564 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001565 const SyncOrdering ordering_rule;
John Zulauf14940722021-04-12 15:19:02 -06001566 const ResourceUsageTag tag;
John Zulauf9cb530d2019-09-30 14:14:10 -06001567};
1568
John Zulauf4a6105a2020-11-17 15:11:05 -07001569// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001570struct PipelineBarrierOp {
1571 SyncBarrier barrier;
1572 bool layout_transition;
1573 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1574 : barrier(barrier_), layout_transition(layout_transition_) {}
1575 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001576 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001577 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1578};
John Zulauf4a6105a2020-11-17 15:11:05 -07001579// The barrier operation for wait events
1580struct WaitEventBarrierOp {
John Zulauf14940722021-04-12 15:19:02 -06001581 ResourceUsageTag scope_tag;
John Zulauf4a6105a2020-11-17 15:11:05 -07001582 SyncBarrier barrier;
1583 bool layout_transition;
John Zulauf14940722021-04-12 15:19:02 -06001584 WaitEventBarrierOp(const ResourceUsageTag scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1585 : scope_tag(scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
John Zulauf4a6105a2020-11-17 15:11:05 -07001586 WaitEventBarrierOp() = default;
John Zulauf14940722021-04-12 15:19:02 -06001587 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(scope_tag, barrier, layout_transition); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001588};
John Zulauf1e331ec2020-12-04 18:29:38 -07001589
John Zulauf4a6105a2020-11-17 15:11:05 -07001590// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1591// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1592// of a collection is known/present.
John Zulauf5c628d02021-05-04 15:46:36 -06001593template <typename BarrierOp, typename OpVector = std::vector<BarrierOp>>
John Zulauf89311b42020-09-29 16:28:47 -06001594class ApplyBarrierOpsFunctor {
1595 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001596 using Iterator = ResourceAccessRangeMap::iterator;
John Zulauf5c628d02021-05-04 15:46:36 -06001597 // Only called with a gap, and pos at the lower_bound(range)
1598 inline Iterator Infill(ResourceAccessRangeMap *accesses, const Iterator &pos, const ResourceAccessRange &range) const {
1599 if (!infill_default_) {
1600 return pos;
1601 }
1602 ResourceAccessState default_state;
1603 auto inserted = accesses->insert(pos, std::make_pair(range, default_state));
1604 return inserted;
1605 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001606
John Zulauf5c628d02021-05-04 15:46:36 -06001607 Iterator operator()(ResourceAccessRangeMap *accesses, const Iterator &pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001608 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001609 for (const auto &op : barrier_ops_) {
1610 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001611 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001612
John Zulauf89311b42020-09-29 16:28:47 -06001613 if (resolve_) {
1614 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1615 // another walk
1616 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001617 }
1618 return pos;
1619 }
1620
John Zulauf89311b42020-09-29 16:28:47 -06001621 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulauf5c628d02021-05-04 15:46:36 -06001622 ApplyBarrierOpsFunctor(bool resolve, typename OpVector::size_type size_hint, ResourceUsageTag tag)
1623 : resolve_(resolve), infill_default_(false), barrier_ops_(), tag_(tag) {
John Zulaufd5115702021-01-18 12:34:33 -07001624 barrier_ops_.reserve(size_hint);
1625 }
John Zulauf5c628d02021-05-04 15:46:36 -06001626 void EmplaceBack(const BarrierOp &op) {
1627 barrier_ops_.emplace_back(op);
1628 infill_default_ |= op.layout_transition;
1629 }
John Zulauf89311b42020-09-29 16:28:47 -06001630
1631 private:
1632 bool resolve_;
John Zulauf5c628d02021-05-04 15:46:36 -06001633 bool infill_default_;
1634 OpVector barrier_ops_;
John Zulauf14940722021-04-12 15:19:02 -06001635 const ResourceUsageTag tag_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001636};
1637
John Zulauf4a6105a2020-11-17 15:11:05 -07001638// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1639// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1640template <typename BarrierOp>
John Zulauf5c628d02021-05-04 15:46:36 -06001641class ApplyBarrierFunctor : public ApplyBarrierOpsFunctor<BarrierOp, small_vector<BarrierOp, 1>> {
1642 using Base = ApplyBarrierOpsFunctor<BarrierOp, small_vector<BarrierOp, 1>>;
1643
John Zulauf4a6105a2020-11-17 15:11:05 -07001644 public:
John Zulauf5c628d02021-05-04 15:46:36 -06001645 ApplyBarrierFunctor(const BarrierOp &barrier_op) : Base(false, 1, kCurrentCommandTag) { Base::EmplaceBack(barrier_op); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001646};
1647
John Zulauf1e331ec2020-12-04 18:29:38 -07001648// This functor resolves the pendinging state.
John Zulauf5c628d02021-05-04 15:46:36 -06001649class ResolvePendingBarrierFunctor : public ApplyBarrierOpsFunctor<NoopBarrierAction, small_vector<NoopBarrierAction, 1>> {
1650 using Base = ApplyBarrierOpsFunctor<NoopBarrierAction, small_vector<NoopBarrierAction, 1>>;
1651
John Zulauf1e331ec2020-12-04 18:29:38 -07001652 public:
John Zulauf5c628d02021-05-04 15:46:36 -06001653 ResolvePendingBarrierFunctor(ResourceUsageTag tag) : Base(true, 0, tag) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001654};
1655
John Zulauf8e3c3e92021-01-06 11:19:36 -07001656void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001657 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001658 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001659 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001660}
1661
John Zulauf8e3c3e92021-01-06 11:19:36 -07001662void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001663 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001664 if (!SimpleBinding(buffer)) return;
1665 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001666 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001667}
John Zulauf355e49b2020-04-24 15:11:15 -06001668
John Zulauf8e3c3e92021-01-06 11:19:36 -07001669void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf110413c2021-03-20 05:38:38 -06001670 const VkImageSubresourceRange &subresource_range, const ResourceUsageTag &tag) {
1671 if (!SimpleBinding(image)) return;
1672 const auto base_address = ResourceBaseAddress(image);
1673 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1674 const auto address_type = ImageAddressType(image);
1675 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1676 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
1677}
1678void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001679 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001680 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001681 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001682 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001683 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1684 base_address);
1685 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001686 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf110413c2021-03-20 05:38:38 -06001687 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001688}
John Zulaufd0ec59f2021-03-13 14:25:08 -07001689
1690void AccessContext::UpdateAccessState(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
John Zulauf14940722021-04-12 15:19:02 -06001691 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001692 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1693 if (!gen) return;
1694 subresource_adapter::ImageRangeGenerator range_gen(*gen);
1695 const auto address_type = view_gen.GetAddressType();
1696 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1697 ApplyUpdateAction(address_type, action, &range_gen);
John Zulauf7635de32020-05-29 17:14:15 -06001698}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001699
John Zulauf8e3c3e92021-01-06 11:19:36 -07001700void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001701 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001702 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001703 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1704 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001705 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001706}
1707
John Zulaufd0ec59f2021-03-13 14:25:08 -07001708template <typename Action, typename RangeGen>
1709void AccessContext::ApplyUpdateAction(AccessAddressType address_type, const Action &action, RangeGen *range_gen_arg) {
1710 assert(range_gen_arg); // Old Google C++ styleguide require non-const object pass by * not &, but this isn't an optional arg.
1711 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, range_gen_arg);
John Zulauf540266b2020-04-06 18:54:53 -06001712}
1713
1714template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001715void AccessContext::ApplyUpdateAction(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, const Action &action) {
1716 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1717 if (!gen) return;
1718 UpdateMemoryAccessState(&GetAccessStateMap(view_gen.GetAddressType()), action, *gen);
John Zulauf540266b2020-04-06 18:54:53 -06001719}
1720
John Zulaufd0ec59f2021-03-13 14:25:08 -07001721void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state,
1722 const AttachmentViewGenVector &attachment_views, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001723 const ResourceUsageTag tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001724 UpdateStateResolveAction update(*this, tag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001725 ResolveOperation(update, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001726}
1727
John Zulaufd0ec59f2021-03-13 14:25:08 -07001728void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
John Zulauf14940722021-04-12 15:19:02 -06001729 uint32_t subpass, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001730 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001731
1732 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1733 if (rp_state.attachment_last_subpass[i] == subpass) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001734 const auto &view_gen = attachment_views[i];
1735 if (!view_gen.IsValid()) continue; // UNUSED
John Zulaufaff20662020-06-01 14:07:58 -06001736
1737 const auto &ci = attachment_ci[i];
1738 const bool has_depth = FormatHasDepth(ci.format);
1739 const bool has_stencil = FormatHasStencil(ci.format);
1740 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001741 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001742
1743 if (is_color && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001744 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
1745 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001746 } else {
John Zulaufaff20662020-06-01 14:07:58 -06001747 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001748 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1749 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001750 }
John Zulauf57261402021-08-13 11:32:06 -06001751 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001752 if (has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001753 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1754 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001755 }
1756 }
1757 }
1758 }
1759}
1760
John Zulauf540266b2020-04-06 18:54:53 -06001761template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001762void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001763 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001764 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001765 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001766 }
1767}
1768
1769void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001770 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1771 auto &context = contexts[subpass_index];
John Zulauf22aefed2021-03-11 18:14:35 -07001772 ApplyTrackbackStackAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001773 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001774 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001775 }
1776 }
1777}
1778
John Zulauf4fa68462021-04-26 21:04:22 -06001779// Caller must ensure that lifespan of this is less than from
1780void AccessContext::ImportAsyncContexts(const AccessContext &from) { async_ = from.async_; }
1781
John Zulauf355e49b2020-04-24 15:11:15 -06001782// Suitable only for *subpass* access contexts
John Zulaufd0ec59f2021-03-13 14:25:08 -07001783HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const AttachmentViewGen &attach_view) const {
1784 if (!attach_view.IsValid()) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001785
John Zulauf355e49b2020-04-24 15:11:15 -06001786 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001787 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001788
1789 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001790 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1791 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001792 HazardResult hazard = track_back.context->DetectImageBarrierHazard(attach_view, merged_barrier, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001793 if (!hazard.hazard) {
1794 // The Async hazard check is against the current context's async set.
John Zulaufd0ec59f2021-03-13 14:25:08 -07001795 hazard = DetectImageBarrierHazard(attach_view, merged_barrier, kDetectAsync);
John Zulauf355e49b2020-04-24 15:11:15 -06001796 }
John Zulaufa0a98292020-09-18 09:30:10 -06001797
John Zulauf355e49b2020-04-24 15:11:15 -06001798 return hazard;
1799}
1800
John Zulaufb02c1eb2020-10-06 16:33:36 -06001801void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001802 const AttachmentViewGenVector &attachment_views, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001803 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001804 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001805 for (const auto &transition : transitions) {
1806 const auto prev_pass = transition.prev_pass;
John Zulaufd0ec59f2021-03-13 14:25:08 -07001807 const auto &view_gen = attachment_views[transition.attachment];
1808 if (!view_gen.IsValid()) continue;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001809
1810 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1811 assert(trackback);
1812
1813 // Import the attachments into the current context
1814 const auto *prev_context = trackback->context;
1815 assert(prev_context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001816 const auto address_type = view_gen.GetAddressType();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001817 auto &target_map = GetAccessStateMap(address_type);
1818 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001819 prev_context->ResolveAccessRange(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action, &target_map,
1820 &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001821 }
1822
John Zulauf86356ca2020-10-19 11:46:41 -06001823 // If there were no transitions skip this global map walk
1824 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001825 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001826 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001827 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001828}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001829
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001830void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulauf669dfd52021-01-27 17:15:28 -07001831 auto *events_context = GetCurrentEventsContext();
1832 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06001833 events_context->ApplyBarrier(src, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001834}
1835
locke-lunarg61870c22020-06-09 14:51:50 -06001836bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1837 const char *func_name) const {
1838 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001839 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001840 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001841 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001842 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001843 return skip;
1844 }
1845
1846 using DescriptorClass = cvdescriptorset::DescriptorClass;
1847 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1848 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
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 Gebben4d51c552022-01-06 21:27:15 -07001858 const auto *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set.get();
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 Gebben4d51c552022-01-06 21:27:15 -07001994 const auto *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001995 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001996 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001997 const auto descriptor_type = binding_it.GetType();
1998 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1999 auto array_idx = 0;
2000
2001 if (binding_it.IsVariableDescriptorCount()) {
2002 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2003 }
2004 SyncStageAccessIndex sync_index =
2005 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
2006
2007 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2008 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2009 switch (descriptor->GetClass()) {
2010 case DescriptorClass::ImageSampler:
2011 case DescriptorClass::Image: {
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
Jeremy Gebben9f537102021-10-05 16:37:12 -06002195 const auto event_state = sync_state_->Get<EVENT_STATE>(event);
John Zulauf669dfd52021-01-27 17:15:28 -07002196 if (event_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002197 GetCurrentEventsContext()->Destroy(event_state.get());
John Zulaufd5115702021-01-18 12:34:33 -07002198 }
2199}
2200
John Zulaufae842002021-04-15 18:20:55 -06002201// The is the recorded cb context
John Zulauf4fa68462021-04-26 21:04:22 -06002202bool CommandBufferAccessContext::ValidateFirstUse(CommandBufferAccessContext *proxy_context, const char *func_name,
2203 uint32_t index) const {
2204 assert(proxy_context);
2205 auto *events_context = proxy_context->GetCurrentEventsContext();
2206 auto *access_context = proxy_context->GetCurrentAccessContext();
2207 const ResourceUsageTag base_tag = proxy_context->GetTagLimit();
John Zulaufae842002021-04-15 18:20:55 -06002208 bool skip = false;
2209 ResourceUsageRange tag_range = {0, 0};
2210 const AccessContext *recorded_context = GetCurrentAccessContext();
2211 assert(recorded_context);
2212 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002213 auto log_msg = [this](const HazardResult &hazard, const CommandBufferAccessContext &active_context, const char *func_name,
John Zulaufae842002021-04-15 18:20:55 -06002214 uint32_t index) {
2215 const auto cb_handle = active_context.cb_state_->commandBuffer();
2216 const auto recorded_handle = cb_state_->commandBuffer();
John Zulauf4fa68462021-04-26 21:04:22 -06002217 const auto *report_data = sync_state_->report_data;
John Zulaufae842002021-04-15 18:20:55 -06002218 return sync_state_->LogError(cb_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf4fa68462021-04-26 21:04:22 -06002219 "%s: Hazard %s for entry %" PRIu32 ", %s, Recorded access info %s. Access info %s.", func_name,
2220 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(recorded_handle).c_str(),
2221 FormatUsage(*hazard.recorded_access).c_str(), active_context.FormatUsage(hazard).c_str());
John Zulaufae842002021-04-15 18:20:55 -06002222 };
2223 for (const auto &sync_op : sync_ops_) {
John Zulauf4fa68462021-04-26 21:04:22 -06002224 // we update the range to any include layout transition first use writes,
2225 // as they are stored along with the source scope (as effective barrier) when recorded
2226 tag_range.end = sync_op.tag + 1;
John Zulauf610e28c2021-08-03 17:46:23 -06002227 skip |= sync_op.sync_op->ReplayValidate(sync_op.tag, *this, base_tag, proxy_context);
John Zulauf4fa68462021-04-26 21:04:22 -06002228
John Zulaufae842002021-04-15 18:20:55 -06002229 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context);
2230 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002231 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002232 }
2233 // NOTE: Add call to replay validate here when we add support for syncop with non-trivial replay
John Zulauf4fa68462021-04-26 21:04:22 -06002234 // Record the barrier into the proxy context.
2235 sync_op.sync_op->DoRecord(base_tag + sync_op.tag, access_context, events_context);
2236 tag_range.begin = tag_range.end;
John Zulaufae842002021-04-15 18:20:55 -06002237 }
2238
2239 // and anything after the last syncop
John Zulaufae842002021-04-15 18:20:55 -06002240 tag_range.end = ResourceUsageRecord::kMaxIndex;
2241 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context);
2242 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002243 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002244 }
2245
2246 return skip;
2247}
2248
John Zulauf4fa68462021-04-26 21:04:22 -06002249void CommandBufferAccessContext::RecordExecutedCommandBuffer(const CommandBufferAccessContext &recorded_cb_context, CMD_TYPE cmd) {
2250 auto *events_context = GetCurrentEventsContext();
2251 auto *access_context = GetCurrentAccessContext();
2252 const AccessContext *recorded_context = recorded_cb_context.GetCurrentAccessContext();
2253 assert(recorded_context);
2254
2255 // Just run through the barriers ignoring the usage from the recorded context, as Resolve will overwrite outdated state
2256 const ResourceUsageTag base_tag = GetTagLimit();
2257 for (const auto &sync_op : recorded_cb_context.sync_ops_) {
2258 // we update the range to any include layout transition first use writes,
2259 // as they are stored along with the source scope (as effective barrier) when recorded
2260 sync_op.sync_op->DoRecord(base_tag + sync_op.tag, access_context, events_context);
2261 }
2262
2263 ResourceUsageRange tag_range = ImportRecordedAccessLog(recorded_cb_context);
2264 assert(base_tag == tag_range.begin); // to ensure the to offset calculation agree
2265 ResolveRecordedContext(*recorded_context, tag_range.begin);
2266}
2267
2268void CommandBufferAccessContext::ResolveRecordedContext(const AccessContext &recorded_context, ResourceUsageTag offset) {
2269 auto tag_offset = [offset](ResourceAccessState *access) { access->OffsetTag(offset); };
2270
2271 auto *access_context = GetCurrentAccessContext();
2272 for (auto address_type : kAddressTypes) {
2273 recorded_context.ResolveAccessRange(address_type, kFullRange, tag_offset, &access_context->GetAccessStateMap(address_type),
2274 nullptr, false);
2275 }
2276}
2277
2278ResourceUsageRange CommandBufferAccessContext::ImportRecordedAccessLog(const CommandBufferAccessContext &recorded_context) {
2279 // The execution references ensure lifespan for the referenced child CB's...
2280 ResourceUsageRange tag_range(GetTagLimit(), 0);
John Zulauf3c2a0b32021-07-14 11:14:52 -06002281 cbs_referenced_.emplace(recorded_context.cb_state_);
John Zulauf4fa68462021-04-26 21:04:22 -06002282 access_log_.insert(access_log_.end(), recorded_context.access_log_.cbegin(), recorded_context.access_log_.end());
2283 tag_range.end = access_log_.size();
2284 return tag_range;
2285}
2286
John Zulaufae842002021-04-15 18:20:55 -06002287class HazardDetectFirstUse {
2288 public:
2289 HazardDetectFirstUse(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range)
2290 : recorded_use_(recorded_use), tag_range_(tag_range) {}
2291 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
2292 return pos->second.DetectHazard(recorded_use_, tag_range_);
2293 }
2294 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
2295 return pos->second.DetectAsyncHazard(recorded_use_, tag_range_, start_tag);
2296 }
2297
2298 private:
2299 const ResourceAccessState &recorded_use_;
2300 const ResourceUsageRange &tag_range_;
2301};
2302
2303// This is called with the *recorded* command buffers access context, with the *active* access context pass in, againsts which
2304// hazards will be detected
2305HazardResult AccessContext::DetectFirstUseHazard(const ResourceUsageRange &tag_range, const AccessContext &access_context) const {
2306 HazardResult hazard;
2307 for (const auto address_type : kAddressTypes) {
2308 const auto &recorded_access_map = GetAccessStateMap(address_type);
2309 for (const auto &recorded_access : recorded_access_map) {
2310 // Cull any entries not in the current tag range
2311 if (!recorded_access.second.FirstAccessInTagRange(tag_range)) continue;
2312 HazardDetectFirstUse detector(recorded_access.second, tag_range);
2313 hazard = access_context.DetectHazard(address_type, detector, recorded_access.first, DetectOptions::kDetectAll);
2314 if (hazard.hazard) break;
2315 }
2316 }
2317
2318 return hazard;
2319}
2320
John Zulauf64ffe552021-02-06 10:25:07 -07002321bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
John Zulauffaea0ee2021-01-14 14:01:32 -07002322 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002323 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002324 const auto &sync_state = ex_context.GetSyncState();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002325 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002326 if (!pipe) {
2327 return skip;
2328 }
2329
2330 const auto &create_info = pipe->create_info.graphics;
2331 if (create_info.pRasterizationState && create_info.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002332 return skip;
2333 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002334 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002335 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg37047832020-06-12 13:44:45 -06002336
John Zulauf1a224292020-06-30 14:52:13 -06002337 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002338 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002339 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2340 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002341 if (location >= subpass.colorAttachmentCount ||
2342 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002343 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002344 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002345 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2346 if (!view_gen.IsValid()) continue;
2347 HazardResult hazard =
2348 current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
2349 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment);
locke-lunarg96dc9632020-06-10 17:22:18 -06002350 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002351 const VkImageView view_handle = view_gen.GetViewState()->image_view();
John Zulaufd0ec59f2021-03-13 14:25:08 -07002352 skip |= sync_state.LogError(view_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002353 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002354 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002355 sync_state.report_data->FormatHandle(view_handle).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002356 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002357 location, ex_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002358 }
2359 }
2360 }
locke-lunarg37047832020-06-12 13:44:45 -06002361
2362 // PHASE1 TODO: Add layout based read/vs. write selection.
2363 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002364 const uint32_t depth_stencil_attachment =
Jeremy Gebben11af9792021-08-20 10:20:09 -06002365 GetSubpassDepthStencilAttachmentIndex(pipe->create_info.graphics.pDepthStencilState, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002366
2367 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2368 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2369 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002370 bool depth_write = false, stencil_write = false;
2371
2372 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002373 if (!FormatIsStencilOnly(view_state.create_info.format) && create_info.pDepthStencilState->depthTestEnable &&
2374 create_info.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002375 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2376 depth_write = true;
2377 }
2378 // PHASE1 TODO: It needs to check if stencil is writable.
2379 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2380 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2381 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002382 if (!FormatIsDepthOnly(view_state.create_info.format) && create_info.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002383 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2384 stencil_write = true;
2385 }
2386
2387 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2388 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002389 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2390 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2391 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002392 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002393 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002394 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002395 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002396 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002397 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2398 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002399 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002400 }
2401 }
2402 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002403 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2404 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2405 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002406 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002407 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002408 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002409 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002410 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002411 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2412 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002413 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002414 }
locke-lunarg61870c22020-06-09 14:51:50 -06002415 }
2416 }
2417 return skip;
2418}
2419
John Zulauf14940722021-04-12 15:19:02 -06002420void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002421 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002422 if (!pipe) {
2423 return;
2424 }
2425
2426 const auto &create_info = pipe->create_info.graphics;
2427 if (create_info.pRasterizationState && create_info.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002428 return;
2429 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002430 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002431 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002432
John Zulauf1a224292020-06-30 14:52:13 -06002433 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002434 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002435 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2436 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002437 if (location >= subpass.colorAttachmentCount ||
2438 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002439 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002440 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002441 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2442 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2443 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2444 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002445 }
2446 }
locke-lunarg37047832020-06-12 13:44:45 -06002447
2448 // PHASE1 TODO: Add layout based read/vs. write selection.
2449 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002450 const uint32_t depth_stencil_attachment =
Jeremy Gebben11af9792021-08-20 10:20:09 -06002451 GetSubpassDepthStencilAttachmentIndex(create_info.pDepthStencilState, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002452 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2453 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2454 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002455 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002456 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2457 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002458
2459 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002460 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) && create_info.pDepthStencilState->depthTestEnable &&
2461 create_info.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002462 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2463 depth_write = true;
2464 }
2465 // PHASE1 TODO: It needs to check if stencil is writable.
2466 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2467 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2468 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002469 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) && create_info.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002470 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2471 stencil_write = true;
2472 }
2473
John Zulaufd0ec59f2021-03-13 14:25:08 -07002474 if (depth_write || stencil_write) {
2475 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2476 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2477 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2478 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002479 }
locke-lunarg61870c22020-06-09 14:51:50 -06002480 }
2481}
2482
John Zulauf64ffe552021-02-06 10:25:07 -07002483bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002484 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002485 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002486 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002487 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002488 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002489 func_name);
2490
John Zulauf355e49b2020-04-24 15:11:15 -06002491 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002492 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002493 skip |=
2494 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002495 if (!skip) {
2496 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2497 // on a copy of the (empty) next context.
2498 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2499 AccessContext temp_context(next_context);
2500 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002501 skip |=
2502 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002503 }
John Zulauf7635de32020-05-29 17:14:15 -06002504 return skip;
2505}
John Zulauf64ffe552021-02-06 10:25:07 -07002506bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002507 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002508 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002509 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002510 current_subpass_);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002511 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_,
2512
2513 attachment_views_, func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002514 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002515 return skip;
2516}
2517
John Zulauf64ffe552021-02-06 10:25:07 -07002518AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002519 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002520}
2521
John Zulauf64ffe552021-02-06 10:25:07 -07002522bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2523 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002524 bool skip = false;
2525
John Zulauf7635de32020-05-29 17:14:15 -06002526 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2527 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2528 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2529 // to apply and only copy then, if this proves a hot spot.
2530 std::unique_ptr<AccessContext> proxy_for_current;
2531
John Zulauf355e49b2020-04-24 15:11:15 -06002532 // Validate the "finalLayout" transitions to external
2533 // Get them from where there we're hidding in the extra entry.
2534 const auto &final_transitions = rp_state_->subpass_transitions.back();
2535 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002536 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002537 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2538 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002539 auto *context = trackback.context;
2540
2541 if (transition.prev_pass == current_subpass_) {
2542 if (!proxy_for_current) {
2543 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
John Zulauf64ffe552021-02-06 10:25:07 -07002544 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002545 }
2546 context = proxy_for_current.get();
2547 }
2548
John Zulaufa0a98292020-09-18 09:30:10 -06002549 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2550 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002551 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002552 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002553 skip |= ex_context.GetSyncState().LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002554 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07002555 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2556 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2557 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2558 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002559 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002560 }
2561 }
2562 return skip;
2563}
2564
John Zulauf14940722021-04-12 15:19:02 -06002565void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002566 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002567 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002568}
2569
John Zulauf14940722021-04-12 15:19:02 -06002570void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002571 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2572 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002573
2574 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2575 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002576 const AttachmentViewGen &view_gen = attachment_views_[i];
2577 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002578
2579 const auto &ci = attachment_ci[i];
2580 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002581 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002582 const bool is_color = !(has_depth || has_stencil);
2583
2584 if (is_color) {
John Zulauf57261402021-08-13 11:32:06 -06002585 const SyncStageAccessIndex load_op = ColorLoadUsage(ci.loadOp);
2586 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2587 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, load_op,
2588 SyncOrdering::kColorAttachment, tag);
2589 }
John Zulauf1507ee42020-05-18 11:33:09 -06002590 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002591 if (has_depth) {
John Zulauf57261402021-08-13 11:32:06 -06002592 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.loadOp);
2593 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2594 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_op,
2595 SyncOrdering::kDepthStencilAttachment, tag);
2596 }
John Zulauf1507ee42020-05-18 11:33:09 -06002597 }
2598 if (has_stencil) {
John Zulauf57261402021-08-13 11:32:06 -06002599 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.stencilLoadOp);
2600 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2601 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, load_op,
2602 SyncOrdering::kDepthStencilAttachment, tag);
2603 }
John Zulauf1507ee42020-05-18 11:33:09 -06002604 }
2605 }
2606 }
2607 }
2608}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002609AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2610 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2611 AttachmentViewGenVector view_gens;
2612 VkExtent3D extent = CastTo3D(render_area.extent);
2613 VkOffset3D offset = CastTo3D(render_area.offset);
2614 view_gens.reserve(attachment_views.size());
2615 for (const auto *view : attachment_views) {
2616 view_gens.emplace_back(view, offset, extent);
2617 }
2618 return view_gens;
2619}
John Zulauf64ffe552021-02-06 10:25:07 -07002620RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2621 VkQueueFlags queue_flags,
2622 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2623 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002624 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002625 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002626 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002627 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002628 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002629 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002630 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002631}
John Zulauf14940722021-04-12 15:19:02 -06002632void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag tag) {
John Zulauf64ffe552021-02-06 10:25:07 -07002633 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002634 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002635 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002636 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002637}
John Zulauf1507ee42020-05-18 11:33:09 -06002638
John Zulauf14940722021-04-12 15:19:02 -06002639void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag prev_subpass_tag, const ResourceUsageTag next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002640 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulaufd0ec59f2021-03-13 14:25:08 -07002641 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
2642 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002643
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002644 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2645 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002646 current_subpass_++;
2647 assert(current_subpass_ < subpass_contexts_.size());
John Zulauffaea0ee2021-01-14 14:01:32 -07002648 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2649 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002650 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002651}
2652
John Zulauf14940722021-04-12 15:19:02 -06002653void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002654 // Add the resolve and store accesses
John Zulaufd0ec59f2021-03-13 14:25:08 -07002655 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, tag);
2656 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002657
John Zulauf355e49b2020-04-24 15:11:15 -06002658 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002659 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002660
2661 // Add the "finalLayout" transitions to external
2662 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002663 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2664 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2665 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002666 const auto &final_transitions = rp_state_->subpass_transitions.back();
2667 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002668 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002669 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002670 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002671 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002672 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002673 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002674 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002675 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002676 }
2677}
2678
Jeremy Gebben40a22942020-12-22 14:22:06 -07002679SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002680 SyncExecScope result;
2681 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002682 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2683 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002684 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2685 return result;
2686}
2687
Jeremy Gebben40a22942020-12-22 14:22:06 -07002688SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002689 SyncExecScope result;
2690 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002691 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2692 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002693 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2694 return result;
2695}
2696
2697SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002698 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002699 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002700 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002701 dst_access_scope = 0;
2702}
2703
2704template <typename Barrier>
2705SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002706 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002707 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002708 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002709 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2710}
2711
2712SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002713 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2714 if (barrier) {
2715 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002716 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002717 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002718
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002719 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002720 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002721 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2722
2723 } else {
2724 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002725 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002726 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2727
2728 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002729 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002730 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2731 }
2732}
2733
2734template <typename Barrier>
2735SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2736 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2737 src_exec_scope = src.exec_scope;
2738 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2739
2740 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002741 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002742 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002743}
2744
John Zulaufb02c1eb2020-10-06 16:33:36 -06002745// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2746void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2747 for (const auto &barrier : barriers) {
2748 ApplyBarrier(barrier, layout_transition);
2749 }
2750}
2751
John Zulauf89311b42020-09-29 16:28:47 -06002752// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2753// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2754// lazily, s.t. no previous access reports should need layout transitions.
John Zulauf14940722021-04-12 15:19:02 -06002755void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06002756 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002757 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002758 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002759 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002760 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002761 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002762 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002763}
John Zulauf9cb530d2019-09-30 14:14:10 -06002764HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2765 HazardResult hazard;
2766 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002767 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002768 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002769 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002770 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002771 }
2772 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002773 // Write operation:
2774 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2775 // If reads exists -- test only against them because either:
2776 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2777 // * the read weren't hazards, and thus if the write is safe w.r.t. the reads, no hazard vs. last_write is possible if
2778 // the current write happens after the reads, so just test the write against the reades
2779 // Otherwise test against last_write
2780 //
2781 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002782 if (last_reads.size()) {
2783 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002784 if (IsReadHazard(usage_stage, read_access)) {
2785 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2786 break;
2787 }
2788 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002789 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002790 // Write-After-Write check -- if we have a previous write to test against
2791 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002792 }
2793 }
2794 return hazard;
2795}
2796
John Zulauf4fa68462021-04-26 21:04:22 -06002797HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering ordering_rule) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002798 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf4fa68462021-04-26 21:04:22 -06002799 return DetectHazard(usage_index, ordering);
2800}
2801
2802HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const OrderingBarrier &ordering) const {
John Zulauf69133422020-05-20 14:55:53 -06002803 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2804 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002805 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002806 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002807 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2808 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002809 if (IsRead(usage_bit)) {
2810 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2811 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2812 if (is_raw_hazard) {
2813 // NOTE: we know last_write is non-zero
2814 // See if the ordering rules save us from the simple RAW check above
2815 // First check to see if the current usage is covered by the ordering rules
2816 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2817 const bool usage_is_ordered =
2818 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2819 if (usage_is_ordered) {
2820 // Now see of the most recent write (or a subsequent read) are ordered
2821 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2822 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002823 }
2824 }
John Zulauf4285ee92020-09-23 10:20:52 -06002825 if (is_raw_hazard) {
2826 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2827 }
John Zulauf5c628d02021-05-04 15:46:36 -06002828 } else if (usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2829 // For Image layout transitions, the barrier represents the first synchronization/access scope of the layout transition
2830 return DetectBarrierHazard(usage_index, ordering.exec_scope, ordering.access_scope);
John Zulauf361fb532020-07-22 10:45:39 -06002831 } else {
2832 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002833 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002834 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002835 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002836 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002837 if (usage_write_is_ordered) {
2838 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2839 ordered_stages = GetOrderedStages(ordering);
2840 }
2841 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2842 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002843 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002844 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2845 if (IsReadHazard(usage_stage, read_access)) {
2846 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2847 break;
2848 }
John Zulaufd14743a2020-07-03 09:42:39 -06002849 }
2850 }
John Zulauf2a344ca2021-09-09 17:07:19 -06002851 } else if (last_write.any() && !(last_write_is_ordered && usage_write_is_ordered)) {
2852 bool ilt_ilt_hazard = false;
2853 if ((usage_index == SYNC_IMAGE_LAYOUT_TRANSITION) && (usage_bit == last_write)) {
2854 // ILT after ILT is a special case where we check the 2nd access scope of the first ILT against the first access
2855 // scope of the second ILT, which has been passed (smuggled?) in the ordering barrier
2856 ilt_ilt_hazard = !(write_barriers & ordering.access_scope).any();
2857 }
2858 if (ilt_ilt_hazard || IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002859 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002860 }
John Zulauf69133422020-05-20 14:55:53 -06002861 }
2862 }
2863 return hazard;
2864}
2865
John Zulaufae842002021-04-15 18:20:55 -06002866HazardResult ResourceAccessState::DetectHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range) const {
2867 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002868 using Size = FirstAccesses::size_type;
2869 const auto &recorded_accesses = recorded_use.first_accesses_;
2870 Size count = recorded_accesses.size();
2871 if (count) {
2872 const auto &last_access = recorded_accesses.back();
2873 bool do_write_last = IsWrite(last_access.usage_index);
2874 if (do_write_last) --count;
John Zulaufae842002021-04-15 18:20:55 -06002875
John Zulauf4fa68462021-04-26 21:04:22 -06002876 for (Size i = 0; i < count; ++count) {
2877 const auto &first = recorded_accesses[i];
2878 // Skip and quit logic
2879 if (first.tag < tag_range.begin) continue;
2880 if (first.tag >= tag_range.end) {
2881 do_write_last = false; // ignore last since we know it can't be in tag_range
2882 break;
2883 }
2884
2885 hazard = DetectHazard(first.usage_index, first.ordering_rule);
2886 if (hazard.hazard) {
2887 hazard.AddRecordedAccess(first);
2888 break;
2889 }
2890 }
2891
2892 if (do_write_last && tag_range.includes(last_access.tag)) {
2893 // Writes are a bit special... both for the "most recent" access logic, and layout transition specific logic
2894 OrderingBarrier barrier = GetOrderingRules(last_access.ordering_rule);
2895 if (last_access.usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2896 // Or in the layout first access scope as a barrier... IFF the usage is an ILT
2897 // this was saved off in the "apply barriers" logic to simplify ILT access checks as they straddle
2898 // the barrier that applies them
2899 barrier |= recorded_use.first_write_layout_ordering_;
2900 }
2901 // Any read stages present in the recorded context (this) are most recent to the write, and thus mask those stages in
2902 // the active context
2903 if (recorded_use.first_read_stages_) {
2904 // we need to ignore the first use read stage in the active context (so we add them to the ordering rule),
2905 // reads in the active context are not "most recent" as all recorded context operations are *after* them
2906 // This supresses only RAW checks for stages present in the recorded context, but not those only present in the
2907 // active context.
2908 barrier.exec_scope |= recorded_use.first_read_stages_;
2909 // if there are any first use reads, we suppress WAW by injecting the active context write in the ordering rule
2910 barrier.access_scope |= FlagBit(last_access.usage_index);
2911 }
2912 hazard = DetectHazard(last_access.usage_index, barrier);
2913 if (hazard.hazard) {
2914 hazard.AddRecordedAccess(last_access);
2915 }
2916 }
John Zulaufae842002021-04-15 18:20:55 -06002917 }
2918 return hazard;
2919}
2920
John Zulauf2f952d22020-02-10 11:34:51 -07002921// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf14940722021-04-12 15:19:02 -06002922HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002923 HazardResult hazard;
2924 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002925 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2926 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2927 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002928 if (IsRead(usage)) {
John Zulauf14940722021-04-12 15:19:02 -06002929 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002930 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002931 }
2932 } else {
John Zulauf14940722021-04-12 15:19:02 -06002933 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002934 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002935 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002936 // 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 -07002937 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06002938 if (read_access.tag >= start_tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002939 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002940 break;
2941 }
2942 }
John Zulauf2f952d22020-02-10 11:34:51 -07002943 }
2944 }
2945 return hazard;
2946}
2947
John Zulaufae842002021-04-15 18:20:55 -06002948HazardResult ResourceAccessState::DetectAsyncHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
2949 ResourceUsageTag start_tag) const {
2950 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002951 for (const auto &first : recorded_use.first_accesses_) {
John Zulaufae842002021-04-15 18:20:55 -06002952 // Skip and quit logic
2953 if (first.tag < tag_range.begin) continue;
2954 if (first.tag >= tag_range.end) break;
John Zulaufae842002021-04-15 18:20:55 -06002955
2956 hazard = DetectAsyncHazard(first.usage_index, start_tag);
John Zulauf4fa68462021-04-26 21:04:22 -06002957 if (hazard.hazard) {
2958 hazard.AddRecordedAccess(first);
2959 break;
2960 }
John Zulaufae842002021-04-15 18:20:55 -06002961 }
2962 return hazard;
2963}
2964
Jeremy Gebben40a22942020-12-22 14:22:06 -07002965HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002966 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002967 // Only supporting image layout transitions for now
2968 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2969 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002970 // only test for WAW if there no intervening read operations.
2971 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002972 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002973 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002974 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002975 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002976 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002977 break;
2978 }
2979 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002980 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2981 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2982 }
2983
2984 return hazard;
2985}
2986
Jeremy Gebben40a22942020-12-22 14:22:06 -07002987HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07002988 const SyncStageAccessFlags &src_access_scope,
John Zulauf14940722021-04-12 15:19:02 -06002989 const ResourceUsageTag event_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002990 // Only supporting image layout transitions for now
2991 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2992 HazardResult hazard;
2993 // only test for WAW if there no intervening read operations.
2994 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2995
John Zulaufab7756b2020-12-29 16:10:16 -07002996 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002997 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
2998 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07002999 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06003000 if (read_access.tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003001 // The read is in the events first synchronization scope, so we use a barrier hazard check
3002 // If the read stage is not in the src sync scope
3003 // *AND* not execution chained with an existing sync barrier (that's the or)
3004 // then the barrier access is unsafe (R/W after R)
3005 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3006 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3007 break;
3008 }
3009 } else {
3010 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3011 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3012 }
3013 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003014 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003015 // 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 -06003016 if (write_tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003017 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3018 // So do a normal barrier hazard check
3019 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3020 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3021 }
3022 } else {
3023 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003024 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3025 }
John Zulaufd14743a2020-07-03 09:42:39 -06003026 }
John Zulauf361fb532020-07-22 10:45:39 -06003027
John Zulauf0cb5be22020-01-23 12:18:22 -07003028 return hazard;
3029}
3030
John Zulauf5f13a792020-03-10 07:31:21 -06003031// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3032// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3033// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3034void ResourceAccessState::Resolve(const ResourceAccessState &other) {
John Zulauf14940722021-04-12 15:19:02 -06003035 if (write_tag < other.write_tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003036 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3037 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003038 *this = other;
John Zulauf14940722021-04-12 15:19:02 -06003039 } else if (other.write_tag == write_tag) {
3040 // 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 -06003041 // dependency chaining logic or any stage expansion)
3042 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003043 pending_write_barriers |= other.pending_write_barriers;
3044 pending_layout_transition |= other.pending_layout_transition;
3045 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf4fa68462021-04-26 21:04:22 -06003046 pending_layout_ordering_ |= other.pending_layout_ordering_;
John Zulauf5f13a792020-03-10 07:31:21 -06003047
John Zulaufd14743a2020-07-03 09:42:39 -06003048 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07003049 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06003050 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07003051 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003052 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003053 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003054 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003055 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3056 // but we should wait on profiling data for that.
3057 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003058 auto &my_read = last_reads[my_read_index];
3059 if (other_read.stage == my_read.stage) {
John Zulauf14940722021-04-12 15:19:02 -06003060 if (my_read.tag < other_read.tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003061 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003062 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003063 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003064 my_read.pending_dep_chain = other_read.pending_dep_chain;
3065 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3066 // May require tracking more than one access per stage.
3067 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003068 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06003069 // Since I'm overwriting the fragement stage read, also update the input attachment info
3070 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003071 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003072 }
John Zulauf14940722021-04-12 15:19:02 -06003073 } else if (other_read.tag == my_read.tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06003074 // The read tags match so merge the barriers
3075 my_read.barriers |= other_read.barriers;
3076 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003077 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003078
John Zulauf5f13a792020-03-10 07:31:21 -06003079 break;
3080 }
3081 }
3082 } else {
3083 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07003084 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06003085 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003086 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003087 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003088 }
John Zulauf5f13a792020-03-10 07:31:21 -06003089 }
3090 }
John Zulauf361fb532020-07-22 10:45:39 -06003091 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003092 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3093 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07003094
3095 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
3096 // of the copy and other into this using the update first logic.
3097 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
3098 // of the other first_accesses... )
3099 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
3100 FirstAccesses firsts(std::move(first_accesses_));
3101 first_accesses_.clear();
3102 first_read_stages_ = 0U;
3103 auto a = firsts.begin();
3104 auto a_end = firsts.end();
3105 for (auto &b : other.first_accesses_) {
John Zulauf14940722021-04-12 15:19:02 -06003106 // TODO: Determine whether some tag offset will be needed for PHASE II
3107 while ((a != a_end) && (a->tag < b.tag)) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003108 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3109 ++a;
3110 }
3111 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
3112 }
3113 for (; a != a_end; ++a) {
3114 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3115 }
3116 }
John Zulauf5f13a792020-03-10 07:31:21 -06003117}
3118
John Zulauf14940722021-04-12 15:19:02 -06003119void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003120 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3121 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003122 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003123 // Mulitple outstanding reads may be of interest and do dependency chains independently
3124 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3125 const auto usage_stage = PipelineStageBit(usage_index);
3126 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003127 for (auto &read_access : last_reads) {
3128 if (read_access.stage == usage_stage) {
3129 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003130 break;
3131 }
3132 }
3133 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07003134 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003135 last_read_stages |= usage_stage;
3136 }
John Zulauf4285ee92020-09-23 10:20:52 -06003137
3138 // 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 -07003139 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003140 // TODO Revisit re: multiple reads for a given stage
3141 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003142 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003143 } else {
3144 // Assume write
3145 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003146 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003147 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003148 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06003149}
John Zulauf5f13a792020-03-10 07:31:21 -06003150
John Zulauf89311b42020-09-29 16:28:47 -06003151// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3152// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3153// We can overwrite them as *this* write is now after them.
3154//
3155// 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 -06003156void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003157 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06003158 last_read_stages = 0;
3159 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003160 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003161
3162 write_barriers = 0;
3163 write_dependency_chain = 0;
3164 write_tag = tag;
3165 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003166}
3167
John Zulauf89311b42020-09-29 16:28:47 -06003168// Apply the memory barrier without updating the existing barriers. The execution barrier
3169// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3170// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3171// replace the current write barriers or add to them, so accumulate to pending as well.
3172void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3173 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3174 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003175 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3176 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3177 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3178 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07003179 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003180 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003181 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003182 if (layout_transition) {
3183 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3184 }
John Zulaufa0a98292020-09-18 09:30:10 -06003185 }
John Zulauf89311b42020-09-29 16:28:47 -06003186 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3187 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003188
John Zulauf89311b42020-09-29 16:28:47 -06003189 if (!pending_layout_transition) {
3190 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3191 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003192 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06003193 // 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 -07003194 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
3195 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003196 }
3197 }
John Zulaufa0a98292020-09-18 09:30:10 -06003198 }
John Zulaufa0a98292020-09-18 09:30:10 -06003199}
3200
John Zulauf4a6105a2020-11-17 15:11:05 -07003201// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3202// changes the "chaining" state, but to keep barriers independent. See discussion above.
John Zulauf14940722021-04-12 15:19:02 -06003203void ResourceAccessState::ApplyBarrier(const ResourceUsageTag scope_tag, const SyncBarrier &barrier, bool layout_transition) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003204 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3205 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3206 // in order to know if it's in the excecution scope
3207 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3208 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3209 // errors w.r.t. "most recent" accesses.
John Zulauf14940722021-04-12 15:19:02 -06003210 if (layout_transition || ((write_tag < scope_tag) && (barrier.src_access_scope & last_write).any())) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003211 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003212 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003213 if (layout_transition) {
3214 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3215 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003216 }
3217 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3218 pending_layout_transition |= layout_transition;
3219
3220 if (!pending_layout_transition) {
3221 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3222 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003223 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003224 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3225 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3226 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3227 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3228 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3229 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3230 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulauf14940722021-04-12 15:19:02 -06003231 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 -07003232 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003233 }
3234 }
3235 }
3236}
John Zulauf14940722021-04-12 15:19:02 -06003237void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag tag) {
John Zulauf89311b42020-09-29 16:28:47 -06003238 if (pending_layout_transition) {
John Zulauf4fa68462021-04-26 21:04:22 -06003239 // 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 -06003240 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07003241 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf4fa68462021-04-26 21:04:22 -06003242 TouchupFirstForLayoutTransition(tag, pending_layout_ordering_);
3243 pending_layout_ordering_ = OrderingBarrier();
John Zulauf89311b42020-09-29 16:28:47 -06003244 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003245 }
John Zulauf89311b42020-09-29 16:28:47 -06003246
3247 // Apply the accumulate execution barriers (and thus update chaining information)
John Zulauf4fa68462021-04-26 21:04:22 -06003248 // for layout transition, last_reads is reset by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003249 for (auto &read_access : last_reads) {
3250 read_access.barriers |= read_access.pending_dep_chain;
3251 read_execution_barriers |= read_access.barriers;
3252 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003253 }
3254
3255 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3256 write_dependency_chain |= pending_write_dep_chain;
3257 write_barriers |= pending_write_barriers;
3258 pending_write_dep_chain = 0;
3259 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003260}
3261
John Zulaufae842002021-04-15 18:20:55 -06003262bool ResourceAccessState::FirstAccessInTagRange(const ResourceUsageRange &tag_range) const {
3263 if (!first_accesses_.size()) return false;
3264 const ResourceUsageRange first_access_range = {first_accesses_.front().tag, first_accesses_.back().tag + 1};
3265 return tag_range.intersects(first_access_range);
3266}
3267
John Zulauf59e25072020-07-17 10:55:21 -06003268// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07003269VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
3270 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003271
John Zulaufab7756b2020-12-29 16:10:16 -07003272 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003273 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003274 barriers = read_access.barriers;
3275 break;
John Zulauf59e25072020-07-17 10:55:21 -06003276 }
3277 }
John Zulauf4285ee92020-09-23 10:20:52 -06003278
John Zulauf59e25072020-07-17 10:55:21 -06003279 return barriers;
3280}
3281
Jeremy Gebben40a22942020-12-22 14:22:06 -07003282inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003283 assert(IsRead(usage));
3284 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3285 // * the previous reads are not hazards, and thus last_write must be visible and available to
3286 // any reads that happen after.
3287 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3288 // 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 -07003289 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003290}
3291
Jeremy Gebben40a22942020-12-22 14:22:06 -07003292VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003293 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07003294 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06003295 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003296 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003297 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003298 // 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 -07003299 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06003300 }
3301
3302 return ordered_stages;
3303}
3304
John Zulauf14940722021-04-12 15:19:02 -06003305void ResourceAccessState::UpdateFirst(const ResourceUsageTag tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003306 // Only record until we record a write.
3307 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003308 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003309 if (0 == (usage_stage & first_read_stages_)) {
3310 // If this is a read we haven't seen or a write, record.
John Zulauf4fa68462021-04-26 21:04:22 -06003311 // We always need to know what stages were found prior to write
John Zulauffaea0ee2021-01-14 14:01:32 -07003312 first_read_stages_ |= usage_stage;
John Zulauf4fa68462021-04-26 21:04:22 -06003313 if (0 == (read_execution_barriers & usage_stage)) {
3314 // If this stage isn't masked then we add it (since writes map to usage_stage 0, this also records writes)
3315 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3316 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003317 }
3318 }
3319}
3320
John Zulauf4fa68462021-04-26 21:04:22 -06003321void ResourceAccessState::TouchupFirstForLayoutTransition(ResourceUsageTag tag, const OrderingBarrier &layout_ordering) {
3322 // Only call this after recording an image layout transition
3323 assert(first_accesses_.size());
3324 if (first_accesses_.back().tag == tag) {
3325 // If this layout transition is the the first write, add the additional ordering rules that guard the ILT
Samuel Iglesias Gonsálvez9b4660b2021-10-21 08:50:39 +02003326 assert(first_accesses_.back().usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
John Zulauf4fa68462021-04-26 21:04:22 -06003327 first_write_layout_ordering_ = layout_ordering;
3328 }
3329}
3330
John Zulaufd1f85d42020-04-15 12:23:15 -06003331void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003332 auto *access_context = GetAccessContextNoInsert(command_buffer);
3333 if (access_context) {
3334 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003335 }
3336}
3337
John Zulaufd1f85d42020-04-15 12:23:15 -06003338void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3339 auto access_found = cb_access_state.find(command_buffer);
3340 if (access_found != cb_access_state.end()) {
3341 access_found->second->Reset();
John Zulauf4fa68462021-04-26 21:04:22 -06003342 access_found->second->MarkDestroyed();
John Zulaufd1f85d42020-04-15 12:23:15 -06003343 cb_access_state.erase(access_found);
3344 }
3345}
3346
John Zulauf9cb530d2019-09-30 14:14:10 -06003347bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3348 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3349 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003350 const auto *cb_context = GetAccessContext(commandBuffer);
3351 assert(cb_context);
3352 if (!cb_context) return skip;
3353 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003354
John Zulauf3d84f1b2020-03-09 13:33:25 -06003355 // If we have no previous accesses, we have no hazards
Jeremy Gebben9f537102021-10-05 16:37:12 -06003356 const auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3357 const auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003358
3359 for (uint32_t region = 0; region < regionCount; region++) {
3360 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003361 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003362 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003363 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003364 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003365 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003366 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003367 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003368 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003369 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003370 }
John Zulauf16adfc92020-04-08 10:28:33 -06003371 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003372 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003373 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003374 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003375 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003376 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003377 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003378 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003379 }
3380 }
3381 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003382 }
3383 return skip;
3384}
3385
3386void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3387 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003388 auto *cb_context = GetAccessContext(commandBuffer);
3389 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003390 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003391 auto *context = cb_context->GetCurrentAccessContext();
3392
Jeremy Gebben9f537102021-10-05 16:37:12 -06003393 const auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3394 const auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003395
3396 for (uint32_t region = 0; region < regionCount; region++) {
3397 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003398 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003399 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003400 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003401 }
John Zulauf16adfc92020-04-08 10:28:33 -06003402 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003403 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003404 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003405 }
3406 }
3407}
3408
John Zulauf4a6105a2020-11-17 15:11:05 -07003409void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3410 // Clear out events from the command buffer contexts
3411 for (auto &cb_context : cb_access_state) {
3412 cb_context.second->RecordDestroyEvent(event);
3413 }
3414}
3415
Jeff Leger178b1e52020-10-05 12:22:23 -04003416bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3417 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3418 bool skip = false;
3419 const auto *cb_context = GetAccessContext(commandBuffer);
3420 assert(cb_context);
3421 if (!cb_context) return skip;
3422 const auto *context = cb_context->GetCurrentAccessContext();
3423
3424 // If we have no previous accesses, we have no hazards
Jeremy Gebben9f537102021-10-05 16:37:12 -06003425 const auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3426 const auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003427
3428 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3429 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3430 if (src_buffer) {
3431 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003432 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003433 if (hazard.hazard) {
3434 // TODO -- add tag information to log msg when useful.
3435 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3436 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3437 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003438 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003439 }
3440 }
3441 if (dst_buffer && !skip) {
3442 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003443 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003444 if (hazard.hazard) {
3445 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3446 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3447 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003448 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003449 }
3450 }
3451 if (skip) break;
3452 }
3453 return skip;
3454}
3455
3456void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3457 auto *cb_context = GetAccessContext(commandBuffer);
3458 assert(cb_context);
3459 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3460 auto *context = cb_context->GetCurrentAccessContext();
3461
Jeremy Gebben9f537102021-10-05 16:37:12 -06003462 const auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3463 const auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003464
3465 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3466 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3467 if (src_buffer) {
3468 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003469 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003470 }
3471 if (dst_buffer) {
3472 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003473 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003474 }
3475 }
3476}
3477
John Zulauf5c5e88d2019-12-26 11:22:02 -07003478bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3479 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3480 const VkImageCopy *pRegions) const {
3481 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003482 const auto *cb_access_context = GetAccessContext(commandBuffer);
3483 assert(cb_access_context);
3484 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003485
John Zulauf3d84f1b2020-03-09 13:33:25 -06003486 const auto *context = cb_access_context->GetCurrentAccessContext();
3487 assert(context);
3488 if (!context) return skip;
3489
Jeremy Gebben9f537102021-10-05 16:37:12 -06003490 const auto src_image = Get<IMAGE_STATE>(srcImage);
3491 const auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003492 for (uint32_t region = 0; region < regionCount; region++) {
3493 const auto &copy_region = pRegions[region];
3494 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003495 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003496 copy_region.srcOffset, copy_region.extent);
3497 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003498 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003499 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003500 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003501 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003502 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003503 }
3504
3505 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003506 VkExtent3D dst_copy_extent =
3507 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003508 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003509 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003510 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003511 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003512 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003513 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003514 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003515 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003516 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003517 }
3518 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003519
John Zulauf5c5e88d2019-12-26 11:22:02 -07003520 return skip;
3521}
3522
3523void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3524 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3525 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003526 auto *cb_access_context = GetAccessContext(commandBuffer);
3527 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003528 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003529 auto *context = cb_access_context->GetCurrentAccessContext();
3530 assert(context);
3531
Jeremy Gebben9f537102021-10-05 16:37:12 -06003532 auto src_image = Get<IMAGE_STATE>(srcImage);
3533 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003534
3535 for (uint32_t region = 0; region < regionCount; region++) {
3536 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003537 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003538 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003539 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003540 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003541 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003542 VkExtent3D dst_copy_extent =
3543 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003544 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003545 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003546 }
3547 }
3548}
3549
Jeff Leger178b1e52020-10-05 12:22:23 -04003550bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3551 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3552 bool skip = false;
3553 const auto *cb_access_context = GetAccessContext(commandBuffer);
3554 assert(cb_access_context);
3555 if (!cb_access_context) return skip;
3556
3557 const auto *context = cb_access_context->GetCurrentAccessContext();
3558 assert(context);
3559 if (!context) return skip;
3560
Jeremy Gebben9f537102021-10-05 16:37:12 -06003561 const auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3562 const auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04003563 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3564 const auto &copy_region = pCopyImageInfo->pRegions[region];
3565 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003566 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003567 copy_region.srcOffset, copy_region.extent);
3568 if (hazard.hazard) {
3569 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3570 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3571 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003572 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003573 }
3574 }
3575
3576 if (dst_image) {
3577 VkExtent3D dst_copy_extent =
3578 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003579 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003580 copy_region.dstOffset, dst_copy_extent);
3581 if (hazard.hazard) {
3582 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3583 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3584 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003585 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003586 }
3587 if (skip) break;
3588 }
3589 }
3590
3591 return skip;
3592}
3593
3594void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3595 auto *cb_access_context = GetAccessContext(commandBuffer);
3596 assert(cb_access_context);
3597 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3598 auto *context = cb_access_context->GetCurrentAccessContext();
3599 assert(context);
3600
Jeremy Gebben9f537102021-10-05 16:37:12 -06003601 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3602 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04003603
3604 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3605 const auto &copy_region = pCopyImageInfo->pRegions[region];
3606 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003607 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003608 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003609 }
3610 if (dst_image) {
3611 VkExtent3D dst_copy_extent =
3612 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003613 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003614 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003615 }
3616 }
3617}
3618
John Zulauf9cb530d2019-09-30 14:14:10 -06003619bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3620 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3621 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3622 uint32_t bufferMemoryBarrierCount,
3623 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3624 uint32_t imageMemoryBarrierCount,
3625 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3626 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003627 const auto *cb_access_context = GetAccessContext(commandBuffer);
3628 assert(cb_access_context);
3629 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003630
John Zulauf36ef9282021-02-02 11:47:24 -07003631 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3632 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3633 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3634 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003635 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003636 return skip;
3637}
3638
3639void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3640 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3641 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3642 uint32_t bufferMemoryBarrierCount,
3643 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3644 uint32_t imageMemoryBarrierCount,
3645 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003646 auto *cb_access_context = GetAccessContext(commandBuffer);
3647 assert(cb_access_context);
3648 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003649
John Zulauf1bf30522021-09-03 15:39:06 -06003650 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(),
3651 srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount,
3652 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
3653 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06003654}
3655
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003656bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3657 const VkDependencyInfoKHR *pDependencyInfo) const {
3658 bool skip = false;
3659 const auto *cb_access_context = GetAccessContext(commandBuffer);
3660 assert(cb_access_context);
3661 if (!cb_access_context) return skip;
3662
3663 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3664 skip = pipeline_barrier.Validate(*cb_access_context);
3665 return skip;
3666}
3667
3668void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3669 auto *cb_access_context = GetAccessContext(commandBuffer);
3670 assert(cb_access_context);
3671 if (!cb_access_context) return;
3672
John Zulauf1bf30522021-09-03 15:39:06 -06003673 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(),
3674 *pDependencyInfo);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003675}
3676
John Zulauf9cb530d2019-09-30 14:14:10 -06003677void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3678 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3679 // The state tracker sets up the device state
3680 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3681
John Zulauf5f13a792020-03-10 07:31:21 -06003682 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3683 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003684 // TODO: Find a good way to do this hooklessly.
3685 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3686 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3687 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3688
John Zulaufd1f85d42020-04-15 12:23:15 -06003689 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3690 sync_device_state->ResetCommandBufferCallback(command_buffer);
3691 });
3692 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3693 sync_device_state->FreeCommandBufferCallback(command_buffer);
3694 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003695}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003696
John Zulauf355e49b2020-04-24 15:11:15 -06003697bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003698 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003699 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003700 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003701 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003702 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003703 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003704 }
John Zulauf355e49b2020-04-24 15:11:15 -06003705 return skip;
3706}
3707
3708bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3709 VkSubpassContents contents) const {
3710 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003711 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003712 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003713 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003714 return skip;
3715}
3716
3717bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003718 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003719 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003720 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003721 return skip;
3722}
3723
3724bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3725 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003726 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003727 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003728 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003729 return skip;
3730}
3731
John Zulauf3d84f1b2020-03-09 13:33:25 -06003732void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3733 VkResult result) {
3734 // The state tracker sets up the command buffer state
3735 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3736
3737 // Create/initialize the structure that trackers accesses at the command buffer scope.
3738 auto cb_access_context = GetAccessContext(commandBuffer);
3739 assert(cb_access_context);
3740 cb_access_context->Reset();
3741}
3742
3743void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003744 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003745 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003746 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003747 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003748 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003749 }
3750}
3751
3752void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3753 VkSubpassContents contents) {
3754 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003755 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003756 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003757 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003758}
3759
3760void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3761 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3762 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003763 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003764}
3765
3766void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3767 const VkRenderPassBeginInfo *pRenderPassBegin,
3768 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3769 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003770 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003771}
3772
Mike Schuchardt2df08912020-12-15 16:28:09 -08003773bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003774 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003775 bool skip = false;
3776
3777 auto cb_context = GetAccessContext(commandBuffer);
3778 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003779 if (!cb_context) return skip;
sfricke-samsung85584a72021-09-30 21:43:38 -07003780 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003781 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003782}
3783
3784bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3785 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003786 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003787 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003788 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003789 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3790 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003791 return skip;
3792}
3793
Mike Schuchardt2df08912020-12-15 16:28:09 -08003794bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3795 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003796 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003797 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003798 return skip;
3799}
3800
3801bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3802 const VkSubpassEndInfo *pSubpassEndInfo) const {
3803 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003804 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003805 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003806}
3807
3808void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003809 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003810 auto cb_context = GetAccessContext(commandBuffer);
3811 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003812 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003813
sfricke-samsung85584a72021-09-30 21:43:38 -07003814 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003815 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003816}
3817
3818void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3819 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003820 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003821 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003822 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003823}
3824
3825void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3826 const VkSubpassEndInfo *pSubpassEndInfo) {
3827 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003828 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003829}
3830
3831void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3832 const VkSubpassEndInfo *pSubpassEndInfo) {
3833 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003834 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003835}
3836
sfricke-samsung85584a72021-09-30 21:43:38 -07003837bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3838 CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003839 bool skip = false;
3840
3841 auto cb_context = GetAccessContext(commandBuffer);
3842 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003843 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003844
sfricke-samsung85584a72021-09-30 21:43:38 -07003845 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003846 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003847 return skip;
3848}
3849
3850bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3851 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003852 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003853 return skip;
3854}
3855
Mike Schuchardt2df08912020-12-15 16:28:09 -08003856bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003857 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003858 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003859 return skip;
3860}
3861
3862bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003863 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003864 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003865 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003866 return skip;
3867}
3868
sfricke-samsung85584a72021-09-30 21:43:38 -07003869void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003870 // Resolve the all subpass contexts to the command buffer contexts
3871 auto cb_context = GetAccessContext(commandBuffer);
3872 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003873 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003874
sfricke-samsung85584a72021-09-30 21:43:38 -07003875 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003876 sync_op.Record(cb_context);
3877 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003878}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003879
John Zulauf33fc1d52020-07-17 11:01:10 -06003880// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3881// updates to a resource which do not conflict at the byte level.
3882// TODO: Revisit this rule to see if it needs to be tighter or looser
3883// TODO: Add programatic control over suppression heuristics
3884bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3885 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3886}
3887
John Zulauf3d84f1b2020-03-09 13:33:25 -06003888void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003889 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003890 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003891}
3892
3893void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003894 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003895 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003896}
3897
3898void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003899 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf5a1a5382020-06-22 17:23:25 -06003900 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003901}
locke-lunarga19c71d2020-03-02 18:17:04 -07003902
Jeff Leger178b1e52020-10-05 12:22:23 -04003903template <typename BufferImageCopyRegionType>
3904bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3905 VkImageLayout dstImageLayout, uint32_t regionCount,
3906 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003907 bool skip = false;
3908 const auto *cb_access_context = GetAccessContext(commandBuffer);
3909 assert(cb_access_context);
3910 if (!cb_access_context) return skip;
3911
Jeff Leger178b1e52020-10-05 12:22:23 -04003912 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3913 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3914
locke-lunarga19c71d2020-03-02 18:17:04 -07003915 const auto *context = cb_access_context->GetCurrentAccessContext();
3916 assert(context);
3917 if (!context) return skip;
3918
Jeremy Gebben9f537102021-10-05 16:37:12 -06003919 const auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3920 const auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003921
3922 for (uint32_t region = 0; region < regionCount; region++) {
3923 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003924 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003925 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003926 if (src_buffer) {
3927 ResourceAccessRange src_range =
3928 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003929 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07003930 if (hazard.hazard) {
3931 // PHASE1 TODO -- add tag information to log msg when useful.
3932 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3933 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3934 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003935 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003936 }
3937 }
3938
Jeremy Gebben40a22942020-12-22 14:22:06 -07003939 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07003940 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003941 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003942 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003943 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003944 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003945 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003946 }
3947 if (skip) break;
3948 }
3949 if (skip) break;
3950 }
3951 return skip;
3952}
3953
Jeff Leger178b1e52020-10-05 12:22:23 -04003954bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3955 VkImageLayout dstImageLayout, uint32_t regionCount,
3956 const VkBufferImageCopy *pRegions) const {
3957 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3958 COPY_COMMAND_VERSION_1);
3959}
3960
3961bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3962 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3963 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3964 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3965 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3966}
3967
3968template <typename BufferImageCopyRegionType>
3969void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3970 VkImageLayout dstImageLayout, uint32_t regionCount,
3971 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003972 auto *cb_access_context = GetAccessContext(commandBuffer);
3973 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003974
3975 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3976 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3977
3978 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003979 auto *context = cb_access_context->GetCurrentAccessContext();
3980 assert(context);
3981
Jeremy Gebben9f537102021-10-05 16:37:12 -06003982 const auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3983 const auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003984
3985 for (uint32_t region = 0; region < regionCount; region++) {
3986 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07003987 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003988 if (src_buffer) {
3989 ResourceAccessRange src_range =
3990 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003991 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003992 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07003993 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003994 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003995 }
3996 }
3997}
3998
Jeff Leger178b1e52020-10-05 12:22:23 -04003999void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4000 VkImageLayout dstImageLayout, uint32_t regionCount,
4001 const VkBufferImageCopy *pRegions) {
4002 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
4003 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4004}
4005
4006void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4007 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4008 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4009 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4010 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4011 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4012}
4013
4014template <typename BufferImageCopyRegionType>
4015bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4016 VkBuffer dstBuffer, uint32_t regionCount,
4017 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004018 bool skip = false;
4019 const auto *cb_access_context = GetAccessContext(commandBuffer);
4020 assert(cb_access_context);
4021 if (!cb_access_context) return skip;
4022
Jeff Leger178b1e52020-10-05 12:22:23 -04004023 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4024 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
4025
locke-lunarga19c71d2020-03-02 18:17:04 -07004026 const auto *context = cb_access_context->GetCurrentAccessContext();
4027 assert(context);
4028 if (!context) return skip;
4029
Jeremy Gebben9f537102021-10-05 16:37:12 -06004030 const auto src_image = Get<IMAGE_STATE>(srcImage);
4031 const auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004032 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
locke-lunarga19c71d2020-03-02 18:17:04 -07004033 for (uint32_t region = 0; region < regionCount; region++) {
4034 const auto &copy_region = pRegions[region];
4035 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004036 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004037 copy_region.imageOffset, copy_region.imageExtent);
4038 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004039 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004040 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004041 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004042 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004043 }
John Zulauf477700e2021-01-06 11:41:49 -07004044 if (dst_mem) {
4045 ResourceAccessRange dst_range =
4046 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004047 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07004048 if (hazard.hazard) {
4049 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4050 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4051 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004052 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07004053 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004054 }
4055 }
4056 if (skip) break;
4057 }
4058 return skip;
4059}
4060
Jeff Leger178b1e52020-10-05 12:22:23 -04004061bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4062 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4063 const VkBufferImageCopy *pRegions) const {
4064 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
4065 COPY_COMMAND_VERSION_1);
4066}
4067
4068bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4069 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4070 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4071 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4072 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4073}
4074
4075template <typename BufferImageCopyRegionType>
4076void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4077 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
4078 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004079 auto *cb_access_context = GetAccessContext(commandBuffer);
4080 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004081
4082 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4083 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
4084
4085 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004086 auto *context = cb_access_context->GetCurrentAccessContext();
4087 assert(context);
4088
Jeremy Gebben9f537102021-10-05 16:37:12 -06004089 const auto src_image = Get<IMAGE_STATE>(srcImage);
4090 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004091 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06004092 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004093
4094 for (uint32_t region = 0; region < regionCount; region++) {
4095 const auto &copy_region = pRegions[region];
4096 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004097 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004098 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004099 if (dst_buffer) {
4100 ResourceAccessRange dst_range =
4101 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004102 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004103 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004104 }
4105 }
4106}
4107
Jeff Leger178b1e52020-10-05 12:22:23 -04004108void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4109 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4110 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
4111 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4112}
4113
4114void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4115 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4116 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4117 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4118 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4119 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4120}
4121
4122template <typename RegionType>
4123bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4124 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4125 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004126 bool skip = false;
4127 const auto *cb_access_context = GetAccessContext(commandBuffer);
4128 assert(cb_access_context);
4129 if (!cb_access_context) return skip;
4130
4131 const auto *context = cb_access_context->GetCurrentAccessContext();
4132 assert(context);
4133 if (!context) return skip;
4134
Jeremy Gebben9f537102021-10-05 16:37:12 -06004135 const auto src_image = Get<IMAGE_STATE>(srcImage);
4136 const auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004137
4138 for (uint32_t region = 0; region < regionCount; region++) {
4139 const auto &blit_region = pRegions[region];
4140 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004141 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4142 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4143 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4144 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4145 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4146 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004147 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004148 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004149 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004150 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004151 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004152 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004153 }
4154 }
4155
4156 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004157 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4158 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4159 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4160 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4161 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4162 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004163 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004164 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004165 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004166 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004167 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004168 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004169 }
4170 if (skip) break;
4171 }
4172 }
4173
4174 return skip;
4175}
4176
Jeff Leger178b1e52020-10-05 12:22:23 -04004177bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4178 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4179 const VkImageBlit *pRegions, VkFilter filter) const {
4180 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4181 "vkCmdBlitImage");
4182}
4183
4184bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4185 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4186 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4187 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4188 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4189}
4190
4191template <typename RegionType>
4192void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4193 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4194 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004195 auto *cb_access_context = GetAccessContext(commandBuffer);
4196 assert(cb_access_context);
4197 auto *context = cb_access_context->GetCurrentAccessContext();
4198 assert(context);
4199
Jeremy Gebben9f537102021-10-05 16:37:12 -06004200 auto src_image = Get<IMAGE_STATE>(srcImage);
4201 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004202
4203 for (uint32_t region = 0; region < regionCount; region++) {
4204 const auto &blit_region = pRegions[region];
4205 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004206 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4207 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4208 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4209 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4210 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4211 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004212 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004213 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004214 }
4215 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004216 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4217 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4218 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4219 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4220 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4221 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004222 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004223 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004224 }
4225 }
4226}
locke-lunarg36ba2592020-04-03 09:42:04 -06004227
Jeff Leger178b1e52020-10-05 12:22:23 -04004228void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4229 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4230 const VkImageBlit *pRegions, VkFilter filter) {
4231 auto *cb_access_context = GetAccessContext(commandBuffer);
4232 assert(cb_access_context);
4233 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4234 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4235 pRegions, filter);
4236 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4237}
4238
4239void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4240 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4241 auto *cb_access_context = GetAccessContext(commandBuffer);
4242 assert(cb_access_context);
4243 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4244 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4245 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4246 pBlitImageInfo->filter, tag);
4247}
4248
John Zulauffaea0ee2021-01-14 14:01:32 -07004249bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4250 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
4251 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
4252 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004253 bool skip = false;
4254 if (drawCount == 0) return skip;
4255
Jeremy Gebben9f537102021-10-05 16:37:12 -06004256 const auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004257 VkDeviceSize size = struct_size;
4258 if (drawCount == 1 || stride == size) {
4259 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004260 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004261 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4262 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004263 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004264 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004265 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004266 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004267 }
4268 } else {
4269 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004270 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004271 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4272 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004273 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004274 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4275 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004276 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004277 break;
4278 }
4279 }
4280 }
4281 return skip;
4282}
4283
John Zulauf14940722021-04-12 15:19:02 -06004284void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag tag, const VkDeviceSize struct_size,
locke-lunarg61870c22020-06-09 14:51:50 -06004285 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4286 uint32_t stride) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06004287 const auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004288 VkDeviceSize size = struct_size;
4289 if (drawCount == 1 || stride == size) {
4290 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004291 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004292 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004293 } else {
4294 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004295 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004296 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4297 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004298 }
4299 }
4300}
4301
John Zulauffaea0ee2021-01-14 14:01:32 -07004302bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4303 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4304 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004305 bool skip = false;
4306
Jeremy Gebben9f537102021-10-05 16:37:12 -06004307 const auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004308 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004309 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4310 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004311 skip |= LogError(count_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004312 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004313 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004314 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004315 }
4316 return skip;
4317}
4318
John Zulauf14940722021-04-12 15:19:02 -06004319void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag tag, VkBuffer buffer, VkDeviceSize offset) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06004320 const auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004321 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004322 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004323}
4324
locke-lunarg36ba2592020-04-03 09:42:04 -06004325bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004326 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004327 const auto *cb_access_context = GetAccessContext(commandBuffer);
4328 assert(cb_access_context);
4329 if (!cb_access_context) return skip;
4330
locke-lunarg61870c22020-06-09 14:51:50 -06004331 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004332 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004333}
4334
4335void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004336 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004337 auto *cb_access_context = GetAccessContext(commandBuffer);
4338 assert(cb_access_context);
4339 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004340
locke-lunarg61870c22020-06-09 14:51:50 -06004341 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004342}
locke-lunarge1a67022020-04-29 00:15:36 -06004343
4344bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004345 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004346 const auto *cb_access_context = GetAccessContext(commandBuffer);
4347 assert(cb_access_context);
4348 if (!cb_access_context) return skip;
4349
4350 const auto *context = cb_access_context->GetCurrentAccessContext();
4351 assert(context);
4352 if (!context) return skip;
4353
locke-lunarg61870c22020-06-09 14:51:50 -06004354 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004355 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4356 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004357 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004358}
4359
4360void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004361 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004362 auto *cb_access_context = GetAccessContext(commandBuffer);
4363 assert(cb_access_context);
4364 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4365 auto *context = cb_access_context->GetCurrentAccessContext();
4366 assert(context);
4367
locke-lunarg61870c22020-06-09 14:51:50 -06004368 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4369 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004370}
4371
4372bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4373 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004374 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004375 const auto *cb_access_context = GetAccessContext(commandBuffer);
4376 assert(cb_access_context);
4377 if (!cb_access_context) return skip;
4378
locke-lunarg61870c22020-06-09 14:51:50 -06004379 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4380 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4381 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004382 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004383}
4384
4385void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4386 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004387 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004388 auto *cb_access_context = GetAccessContext(commandBuffer);
4389 assert(cb_access_context);
4390 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004391
locke-lunarg61870c22020-06-09 14:51:50 -06004392 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4393 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4394 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004395}
4396
4397bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4398 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004399 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004400 const auto *cb_access_context = GetAccessContext(commandBuffer);
4401 assert(cb_access_context);
4402 if (!cb_access_context) return skip;
4403
locke-lunarg61870c22020-06-09 14:51:50 -06004404 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4405 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4406 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004407 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004408}
4409
4410void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4411 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004412 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004413 auto *cb_access_context = GetAccessContext(commandBuffer);
4414 assert(cb_access_context);
4415 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004416
locke-lunarg61870c22020-06-09 14:51:50 -06004417 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4418 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4419 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004420}
4421
4422bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4423 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004424 bool skip = false;
4425 if (drawCount == 0) return skip;
4426
locke-lunargff255f92020-05-13 18:53:52 -06004427 const auto *cb_access_context = GetAccessContext(commandBuffer);
4428 assert(cb_access_context);
4429 if (!cb_access_context) return skip;
4430
4431 const auto *context = cb_access_context->GetCurrentAccessContext();
4432 assert(context);
4433 if (!context) return skip;
4434
locke-lunarg61870c22020-06-09 14:51:50 -06004435 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4436 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004437 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4438 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004439
4440 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4441 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4442 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004443 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004444 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004445}
4446
4447void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4448 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004449 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004450 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004451 auto *cb_access_context = GetAccessContext(commandBuffer);
4452 assert(cb_access_context);
4453 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4454 auto *context = cb_access_context->GetCurrentAccessContext();
4455 assert(context);
4456
locke-lunarg61870c22020-06-09 14:51:50 -06004457 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4458 cb_access_context->RecordDrawSubpassAttachment(tag);
4459 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004460
4461 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4462 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4463 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004464 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004465}
4466
4467bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4468 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004469 bool skip = false;
4470 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004471 const auto *cb_access_context = GetAccessContext(commandBuffer);
4472 assert(cb_access_context);
4473 if (!cb_access_context) return skip;
4474
4475 const auto *context = cb_access_context->GetCurrentAccessContext();
4476 assert(context);
4477 if (!context) return skip;
4478
locke-lunarg61870c22020-06-09 14:51:50 -06004479 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4480 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004481 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4482 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004483
4484 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4485 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4486 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004487 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004488 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004489}
4490
4491void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4492 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004493 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004494 auto *cb_access_context = GetAccessContext(commandBuffer);
4495 assert(cb_access_context);
4496 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4497 auto *context = cb_access_context->GetCurrentAccessContext();
4498 assert(context);
4499
locke-lunarg61870c22020-06-09 14:51:50 -06004500 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4501 cb_access_context->RecordDrawSubpassAttachment(tag);
4502 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004503
4504 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4505 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4506 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004507 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004508}
4509
4510bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4511 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4512 uint32_t stride, const char *function) const {
4513 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004514 const auto *cb_access_context = GetAccessContext(commandBuffer);
4515 assert(cb_access_context);
4516 if (!cb_access_context) return skip;
4517
4518 const auto *context = cb_access_context->GetCurrentAccessContext();
4519 assert(context);
4520 if (!context) return skip;
4521
locke-lunarg61870c22020-06-09 14:51:50 -06004522 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4523 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004524 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4525 maxDrawCount, stride, function);
4526 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004527
4528 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4529 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4530 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004531 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004532 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004533}
4534
4535bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4536 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4537 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004538 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4539 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004540}
4541
sfricke-samsung85584a72021-09-30 21:43:38 -07004542void SyncValidator::RecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4543 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4544 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004545 auto *cb_access_context = GetAccessContext(commandBuffer);
4546 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004547 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004548 auto *context = cb_access_context->GetCurrentAccessContext();
4549 assert(context);
4550
locke-lunarg61870c22020-06-09 14:51:50 -06004551 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4552 cb_access_context->RecordDrawSubpassAttachment(tag);
4553 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4554 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004555
4556 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4557 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4558 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004559 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004560}
4561
sfricke-samsung85584a72021-09-30 21:43:38 -07004562void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4563 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4564 uint32_t stride) {
4565 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4566 stride);
4567 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4568 CMD_DRAWINDIRECTCOUNT);
4569}
locke-lunarge1a67022020-04-29 00:15:36 -06004570bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4571 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4572 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004573 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4574 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004575}
4576
4577void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4578 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4579 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004580 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4581 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004582 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4583 CMD_DRAWINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004584}
4585
4586bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4587 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4588 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004589 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4590 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004591}
4592
4593void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4594 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4595 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004596 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4597 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004598 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4599 CMD_DRAWINDIRECTCOUNTAMD);
locke-lunargff255f92020-05-13 18:53:52 -06004600}
4601
4602bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4603 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4604 uint32_t stride, const char *function) const {
4605 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004606 const auto *cb_access_context = GetAccessContext(commandBuffer);
4607 assert(cb_access_context);
4608 if (!cb_access_context) return skip;
4609
4610 const auto *context = cb_access_context->GetCurrentAccessContext();
4611 assert(context);
4612 if (!context) return skip;
4613
locke-lunarg61870c22020-06-09 14:51:50 -06004614 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4615 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004616 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4617 offset, maxDrawCount, stride, function);
4618 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004619
4620 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4621 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4622 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004623 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004624 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004625}
4626
4627bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4628 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4629 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004630 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4631 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004632}
4633
sfricke-samsung85584a72021-09-30 21:43:38 -07004634void SyncValidator::RecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4635 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4636 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004637 auto *cb_access_context = GetAccessContext(commandBuffer);
4638 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004639 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004640 auto *context = cb_access_context->GetCurrentAccessContext();
4641 assert(context);
4642
locke-lunarg61870c22020-06-09 14:51:50 -06004643 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4644 cb_access_context->RecordDrawSubpassAttachment(tag);
4645 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4646 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004647
4648 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4649 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004650 // We will update the index and vertex buffer in SubmitQueue in the future.
4651 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004652}
4653
sfricke-samsung85584a72021-09-30 21:43:38 -07004654void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4655 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4656 uint32_t maxDrawCount, uint32_t stride) {
4657 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4658 maxDrawCount, stride);
4659 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4660 CMD_DRAWINDEXEDINDIRECTCOUNT);
4661}
4662
locke-lunarge1a67022020-04-29 00:15:36 -06004663bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4664 VkDeviceSize offset, VkBuffer countBuffer,
4665 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4666 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004667 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4668 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004669}
4670
4671void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4672 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4673 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004674 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4675 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004676 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4677 CMD_DRAWINDEXEDINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004678}
4679
4680bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4681 VkDeviceSize offset, VkBuffer countBuffer,
4682 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4683 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004684 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4685 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004686}
4687
4688void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4689 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4690 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004691 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4692 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004693 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4694 CMD_DRAWINDEXEDINDIRECTCOUNTAMD);
locke-lunarge1a67022020-04-29 00:15:36 -06004695}
4696
4697bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4698 const VkClearColorValue *pColor, uint32_t rangeCount,
4699 const VkImageSubresourceRange *pRanges) const {
4700 bool skip = false;
4701 const auto *cb_access_context = GetAccessContext(commandBuffer);
4702 assert(cb_access_context);
4703 if (!cb_access_context) return skip;
4704
4705 const auto *context = cb_access_context->GetCurrentAccessContext();
4706 assert(context);
4707 if (!context) return skip;
4708
Jeremy Gebben9f537102021-10-05 16:37:12 -06004709 const auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004710
4711 for (uint32_t index = 0; index < rangeCount; index++) {
4712 const auto &range = pRanges[index];
4713 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004714 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004715 if (hazard.hazard) {
4716 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004717 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004718 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004719 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004720 }
4721 }
4722 }
4723 return skip;
4724}
4725
4726void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4727 const VkClearColorValue *pColor, uint32_t rangeCount,
4728 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004729 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004730 auto *cb_access_context = GetAccessContext(commandBuffer);
4731 assert(cb_access_context);
4732 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4733 auto *context = cb_access_context->GetCurrentAccessContext();
4734 assert(context);
4735
Jeremy Gebben9f537102021-10-05 16:37:12 -06004736 const auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004737
4738 for (uint32_t index = 0; index < rangeCount; index++) {
4739 const auto &range = pRanges[index];
4740 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004741 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004742 }
4743 }
4744}
4745
4746bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4747 VkImageLayout imageLayout,
4748 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4749 const VkImageSubresourceRange *pRanges) const {
4750 bool skip = false;
4751 const auto *cb_access_context = GetAccessContext(commandBuffer);
4752 assert(cb_access_context);
4753 if (!cb_access_context) return skip;
4754
4755 const auto *context = cb_access_context->GetCurrentAccessContext();
4756 assert(context);
4757 if (!context) return skip;
4758
Jeremy Gebben9f537102021-10-05 16:37:12 -06004759 const auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004760
4761 for (uint32_t index = 0; index < rangeCount; index++) {
4762 const auto &range = pRanges[index];
4763 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004764 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004765 if (hazard.hazard) {
4766 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004767 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004768 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004769 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004770 }
4771 }
4772 }
4773 return skip;
4774}
4775
4776void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4777 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4778 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004779 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004780 auto *cb_access_context = GetAccessContext(commandBuffer);
4781 assert(cb_access_context);
4782 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4783 auto *context = cb_access_context->GetCurrentAccessContext();
4784 assert(context);
4785
Jeremy Gebben9f537102021-10-05 16:37:12 -06004786 const auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004787
4788 for (uint32_t index = 0; index < rangeCount; index++) {
4789 const auto &range = pRanges[index];
4790 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004791 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004792 }
4793 }
4794}
4795
4796bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4797 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4798 VkDeviceSize dstOffset, VkDeviceSize stride,
4799 VkQueryResultFlags flags) const {
4800 bool skip = false;
4801 const auto *cb_access_context = GetAccessContext(commandBuffer);
4802 assert(cb_access_context);
4803 if (!cb_access_context) return skip;
4804
4805 const auto *context = cb_access_context->GetCurrentAccessContext();
4806 assert(context);
4807 if (!context) return skip;
4808
Jeremy Gebben9f537102021-10-05 16:37:12 -06004809 const auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004810
4811 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004812 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004813 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004814 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004815 skip |=
4816 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4817 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004818 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004819 }
4820 }
locke-lunargff255f92020-05-13 18:53:52 -06004821
4822 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004823 return skip;
4824}
4825
4826void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4827 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4828 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004829 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4830 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004831 auto *cb_access_context = GetAccessContext(commandBuffer);
4832 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004833 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004834 auto *context = cb_access_context->GetCurrentAccessContext();
4835 assert(context);
4836
Jeremy Gebben9f537102021-10-05 16:37:12 -06004837 const auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004838
4839 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004840 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004841 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004842 }
locke-lunargff255f92020-05-13 18:53:52 -06004843
4844 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004845}
4846
4847bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4848 VkDeviceSize size, uint32_t data) const {
4849 bool skip = false;
4850 const auto *cb_access_context = GetAccessContext(commandBuffer);
4851 assert(cb_access_context);
4852 if (!cb_access_context) return skip;
4853
4854 const auto *context = cb_access_context->GetCurrentAccessContext();
4855 assert(context);
4856 if (!context) return skip;
4857
Jeremy Gebben9f537102021-10-05 16:37:12 -06004858 const auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004859
4860 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004861 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004862 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004863 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004864 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004865 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004866 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004867 }
4868 }
4869 return skip;
4870}
4871
4872void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4873 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004874 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004875 auto *cb_access_context = GetAccessContext(commandBuffer);
4876 assert(cb_access_context);
4877 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4878 auto *context = cb_access_context->GetCurrentAccessContext();
4879 assert(context);
4880
Jeremy Gebben9f537102021-10-05 16:37:12 -06004881 const auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004882
4883 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004884 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004885 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004886 }
4887}
4888
4889bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4890 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4891 const VkImageResolve *pRegions) const {
4892 bool skip = false;
4893 const auto *cb_access_context = GetAccessContext(commandBuffer);
4894 assert(cb_access_context);
4895 if (!cb_access_context) return skip;
4896
4897 const auto *context = cb_access_context->GetCurrentAccessContext();
4898 assert(context);
4899 if (!context) return skip;
4900
Jeremy Gebben9f537102021-10-05 16:37:12 -06004901 const auto src_image = Get<IMAGE_STATE>(srcImage);
4902 const auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06004903
4904 for (uint32_t region = 0; region < regionCount; region++) {
4905 const auto &resolve_region = pRegions[region];
4906 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004907 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004908 resolve_region.srcOffset, resolve_region.extent);
4909 if (hazard.hazard) {
4910 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004911 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004912 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004913 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004914 }
4915 }
4916
4917 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004918 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004919 resolve_region.dstOffset, resolve_region.extent);
4920 if (hazard.hazard) {
4921 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004922 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004923 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004924 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004925 }
4926 if (skip) break;
4927 }
4928 }
4929
4930 return skip;
4931}
4932
4933void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4934 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4935 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004936 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4937 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004938 auto *cb_access_context = GetAccessContext(commandBuffer);
4939 assert(cb_access_context);
4940 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4941 auto *context = cb_access_context->GetCurrentAccessContext();
4942 assert(context);
4943
Jeremy Gebben9f537102021-10-05 16:37:12 -06004944 auto src_image = Get<IMAGE_STATE>(srcImage);
4945 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06004946
4947 for (uint32_t region = 0; region < regionCount; region++) {
4948 const auto &resolve_region = pRegions[region];
4949 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004950 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004951 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004952 }
4953 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004954 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004955 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004956 }
4957 }
4958}
4959
Jeff Leger178b1e52020-10-05 12:22:23 -04004960bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4961 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4962 bool skip = false;
4963 const auto *cb_access_context = GetAccessContext(commandBuffer);
4964 assert(cb_access_context);
4965 if (!cb_access_context) return skip;
4966
4967 const auto *context = cb_access_context->GetCurrentAccessContext();
4968 assert(context);
4969 if (!context) return skip;
4970
Jeremy Gebben9f537102021-10-05 16:37:12 -06004971 const auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4972 const auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04004973
4974 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4975 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4976 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004977 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004978 resolve_region.srcOffset, resolve_region.extent);
4979 if (hazard.hazard) {
4980 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4981 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4982 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004983 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004984 }
4985 }
4986
4987 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004988 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004989 resolve_region.dstOffset, resolve_region.extent);
4990 if (hazard.hazard) {
4991 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4992 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4993 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004994 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004995 }
4996 if (skip) break;
4997 }
4998 }
4999
5000 return skip;
5001}
5002
5003void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5004 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5005 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5006 auto *cb_access_context = GetAccessContext(commandBuffer);
5007 assert(cb_access_context);
5008 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
5009 auto *context = cb_access_context->GetCurrentAccessContext();
5010 assert(context);
5011
Jeremy Gebben9f537102021-10-05 16:37:12 -06005012 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5013 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005014
5015 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5016 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5017 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005018 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005019 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005020 }
5021 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005022 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005023 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005024 }
5025 }
5026}
5027
locke-lunarge1a67022020-04-29 00:15:36 -06005028bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5029 VkDeviceSize dataSize, const void *pData) const {
5030 bool skip = false;
5031 const auto *cb_access_context = GetAccessContext(commandBuffer);
5032 assert(cb_access_context);
5033 if (!cb_access_context) return skip;
5034
5035 const auto *context = cb_access_context->GetCurrentAccessContext();
5036 assert(context);
5037 if (!context) return skip;
5038
Jeremy Gebben9f537102021-10-05 16:37:12 -06005039 const auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005040
5041 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005042 // VK_WHOLE_SIZE not allowed
5043 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005044 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005045 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005046 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005047 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005048 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005049 }
5050 }
5051 return skip;
5052}
5053
5054void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5055 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005056 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005057 auto *cb_access_context = GetAccessContext(commandBuffer);
5058 assert(cb_access_context);
5059 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5060 auto *context = cb_access_context->GetCurrentAccessContext();
5061 assert(context);
5062
Jeremy Gebben9f537102021-10-05 16:37:12 -06005063 const auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005064
5065 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005066 // VK_WHOLE_SIZE not allowed
5067 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005068 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005069 }
5070}
locke-lunargff255f92020-05-13 18:53:52 -06005071
5072bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5073 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5074 bool skip = false;
5075 const auto *cb_access_context = GetAccessContext(commandBuffer);
5076 assert(cb_access_context);
5077 if (!cb_access_context) return skip;
5078
5079 const auto *context = cb_access_context->GetCurrentAccessContext();
5080 assert(context);
5081 if (!context) return skip;
5082
Jeremy Gebben9f537102021-10-05 16:37:12 -06005083 const auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005084
5085 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005086 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005087 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06005088 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005089 skip |=
5090 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5091 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005092 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005093 }
5094 }
5095 return skip;
5096}
5097
5098void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5099 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005100 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005101 auto *cb_access_context = GetAccessContext(commandBuffer);
5102 assert(cb_access_context);
5103 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5104 auto *context = cb_access_context->GetCurrentAccessContext();
5105 assert(context);
5106
Jeremy Gebben9f537102021-10-05 16:37:12 -06005107 const auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005108
5109 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005110 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005111 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06005112 }
5113}
John Zulauf49beb112020-11-04 16:06:31 -07005114
5115bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5116 bool skip = false;
5117 const auto *cb_context = GetAccessContext(commandBuffer);
5118 assert(cb_context);
5119 if (!cb_context) return skip;
5120
John Zulauf36ef9282021-02-02 11:47:24 -07005121 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005122 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005123}
5124
5125void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5126 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5127 auto *cb_context = GetAccessContext(commandBuffer);
5128 assert(cb_context);
5129 if (!cb_context) return;
John Zulauf1bf30522021-09-03 15:39:06 -06005130 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005131}
5132
John Zulauf4edde622021-02-15 08:54:50 -07005133bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5134 const VkDependencyInfoKHR *pDependencyInfo) const {
5135 bool skip = false;
5136 const auto *cb_context = GetAccessContext(commandBuffer);
5137 assert(cb_context);
5138 if (!cb_context || !pDependencyInfo) return skip;
5139
5140 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5141 return set_event_op.Validate(*cb_context);
5142}
5143
5144void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5145 const VkDependencyInfoKHR *pDependencyInfo) {
5146 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
5147 auto *cb_context = GetAccessContext(commandBuffer);
5148 assert(cb_context);
5149 if (!cb_context || !pDependencyInfo) return;
5150
John Zulauf1bf30522021-09-03 15:39:06 -06005151 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
John Zulauf4edde622021-02-15 08:54:50 -07005152}
5153
John Zulauf49beb112020-11-04 16:06:31 -07005154bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5155 VkPipelineStageFlags stageMask) const {
5156 bool skip = false;
5157 const auto *cb_context = GetAccessContext(commandBuffer);
5158 assert(cb_context);
5159 if (!cb_context) return skip;
5160
John Zulauf36ef9282021-02-02 11:47:24 -07005161 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005162 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005163}
5164
5165void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5166 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5167 auto *cb_context = GetAccessContext(commandBuffer);
5168 assert(cb_context);
5169 if (!cb_context) return;
5170
John Zulauf1bf30522021-09-03 15:39:06 -06005171 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
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
John Zulauf1bf30522021-09-03 15:39:06 -06005192 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf4edde622021-02-15 08:54:50 -07005193}
5194
John Zulauf49beb112020-11-04 16:06:31 -07005195bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5196 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5197 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5198 uint32_t bufferMemoryBarrierCount,
5199 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5200 uint32_t imageMemoryBarrierCount,
5201 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5202 bool skip = false;
5203 const auto *cb_context = GetAccessContext(commandBuffer);
5204 assert(cb_context);
5205 if (!cb_context) return skip;
5206
John Zulauf36ef9282021-02-02 11:47:24 -07005207 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
5208 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
5209 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07005210 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005211}
5212
5213void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5214 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5215 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5216 uint32_t bufferMemoryBarrierCount,
5217 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5218 uint32_t imageMemoryBarrierCount,
5219 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5220 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5221 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5222 imageMemoryBarrierCount, pImageMemoryBarriers);
5223
5224 auto *cb_context = GetAccessContext(commandBuffer);
5225 assert(cb_context);
5226 if (!cb_context) return;
5227
John Zulauf1bf30522021-09-03 15:39:06 -06005228 cb_context->RecordSyncOp<SyncOpWaitEvents>(
John Zulauf610e28c2021-08-03 17:46:23 -06005229 CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
John Zulauf1bf30522021-09-03 15:39:06 -06005230 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf4a6105a2020-11-17 15:11:05 -07005231}
5232
John Zulauf4edde622021-02-15 08:54:50 -07005233bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5234 const VkDependencyInfoKHR *pDependencyInfos) const {
5235 bool skip = false;
5236 const auto *cb_context = GetAccessContext(commandBuffer);
5237 assert(cb_context);
5238 if (!cb_context) return skip;
5239
5240 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5241 skip |= wait_events_op.Validate(*cb_context);
5242 return skip;
5243}
5244
5245void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5246 const VkDependencyInfoKHR *pDependencyInfos) {
5247 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5248
5249 auto *cb_context = GetAccessContext(commandBuffer);
5250 assert(cb_context);
5251 if (!cb_context) return;
5252
John Zulauf1bf30522021-09-03 15:39:06 -06005253 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5254 pDependencyInfos);
John Zulauf4edde622021-02-15 08:54:50 -07005255}
5256
John Zulauf4a6105a2020-11-17 15:11:05 -07005257void SyncEventState::ResetFirstScope() {
5258 for (const auto address_type : kAddressTypes) {
5259 first_scope[static_cast<size_t>(address_type)].clear();
5260 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005261 scope = SyncExecScope();
John Zulauf78b1f892021-09-20 15:02:09 -06005262 first_scope_set = false;
5263 first_scope_tag = 0;
John Zulauf4a6105a2020-11-17 15:11:05 -07005264}
5265
5266// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07005267SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005268 IgnoreReason reason = NotIgnored;
5269
John Zulauf4edde622021-02-15 08:54:50 -07005270 if ((CMD_WAITEVENTS2KHR == cmd) && (CMD_SETEVENT == last_command)) {
5271 reason = SetVsWait2;
5272 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
5273 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07005274 } else if (unsynchronized_set) {
5275 reason = SetRace;
John Zulauf78b1f892021-09-20 15:02:09 -06005276 } else if (first_scope_set) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005277 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005278 if (missing_bits) reason = MissingStageBits;
5279 }
5280
5281 return reason;
5282}
5283
Jeremy Gebben40a22942020-12-22 14:22:06 -07005284bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005285 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5286 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5287 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005288}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005289
John Zulauf36ef9282021-02-02 11:47:24 -07005290SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5291 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5292 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005293 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5294 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5295 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07005296 : SyncOpBase(cmd), barriers_(1) {
5297 auto &barrier_set = barriers_[0];
5298 barrier_set.dependency_flags = dependencyFlags;
5299 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
5300 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005301 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07005302 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
5303 pMemoryBarriers);
5304 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5305 bufferMemoryBarrierCount, pBufferMemoryBarriers);
5306 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5307 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005308}
5309
John Zulauf4edde622021-02-15 08:54:50 -07005310SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
5311 const VkDependencyInfoKHR *dep_infos)
5312 : SyncOpBase(cmd), barriers_(event_count) {
5313 for (uint32_t i = 0; i < event_count; i++) {
5314 const auto &dep_info = dep_infos[i];
5315 auto &barrier_set = barriers_[i];
5316 barrier_set.dependency_flags = dep_info.dependencyFlags;
5317 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5318 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5319 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5320 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5321 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5322 dep_info.pMemoryBarriers);
5323 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5324 dep_info.pBufferMemoryBarriers);
5325 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5326 dep_info.pImageMemoryBarriers);
5327 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005328}
5329
John Zulauf36ef9282021-02-02 11:47:24 -07005330SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005331 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5332 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5333 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5334 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5335 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005336 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005337 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5338
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005339SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5340 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005341 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005342
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005343bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5344 bool skip = false;
5345 const auto *context = cb_context.GetCurrentAccessContext();
5346 assert(context);
5347 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005348 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5349
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005350 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005351 const auto &barrier_set = barriers_[0];
5352 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5353 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5354 const auto *image_state = image_barrier.image.get();
5355 if (!image_state) continue;
5356 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5357 if (hazard.hazard) {
5358 // PHASE1 TODO -- add tag information to log msg when useful.
5359 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005360 const auto image_handle = image_state->image();
John Zulauf6fdf3d02021-03-05 16:50:47 -07005361 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5362 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5363 string_SyncHazard(hazard.hazard), image_barrier.index,
5364 sync_state.report_data->FormatHandle(image_handle).c_str(),
5365 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005366 }
5367 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005368 return skip;
5369}
5370
John Zulaufd5115702021-01-18 12:34:33 -07005371struct SyncOpPipelineBarrierFunctorFactory {
5372 using BarrierOpFunctor = PipelineBarrierOp;
5373 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5374 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5375 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5376 using BufferRange = ResourceAccessRange;
5377 using ImageRange = subresource_adapter::ImageRangeGenerator;
5378 using GlobalRange = ResourceAccessRange;
5379
5380 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5381 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5382 }
John Zulauf14940722021-04-12 15:19:02 -06005383 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005384 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5385 }
5386 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5387 return GlobalBarrierOpFunctor(barrier, false);
5388 }
5389
5390 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5391 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5392 const auto base_address = ResourceBaseAddress(buffer);
5393 return (range + base_address);
5394 }
John Zulauf110413c2021-03-20 05:38:38 -06005395 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005396 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005397
5398 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005399 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005400 return range_gen;
5401 }
5402 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5403};
5404
5405template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005406void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005407 AccessContext *context) {
5408 for (const auto &barrier : barriers) {
5409 const auto *state = barrier.GetState();
5410 if (state) {
5411 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5412 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5413 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5414 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5415 }
5416 }
5417}
5418
5419template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005420void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005421 AccessContext *access_context) {
5422 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5423 for (const auto &barrier : barriers) {
5424 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5425 }
5426 for (const auto address_type : kAddressTypes) {
5427 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5428 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5429 }
5430}
5431
John Zulauf8eda1562021-04-13 17:06:41 -06005432ResourceUsageTag SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005433 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf8eda1562021-04-13 17:06:41 -06005434 auto *events_context = cb_context->GetCurrentEventsContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005435 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf4fa68462021-04-26 21:04:22 -06005436 DoRecord(tag, access_context, events_context);
5437 return tag;
5438}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005439
John Zulauf4fa68462021-04-26 21:04:22 -06005440void SyncOpPipelineBarrier::DoRecord(const ResourceUsageTag tag, AccessContext *access_context,
5441 SyncEventsContext *events_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06005442 SyncOpPipelineBarrierFunctorFactory factory;
John Zulauf4edde622021-02-15 08:54:50 -07005443 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5444 assert(barriers_.size() == 1);
5445 const auto &barrier_set = barriers_[0];
5446 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5447 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5448 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulauf4edde622021-02-15 08:54:50 -07005449 if (barrier_set.single_exec_scope) {
John Zulauf8eda1562021-04-13 17:06:41 -06005450 events_context->ApplyBarrier(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005451 } else {
5452 for (const auto &barrier : barrier_set.memory_barriers) {
John Zulauf8eda1562021-04-13 17:06:41 -06005453 events_context->ApplyBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005454 }
5455 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005456}
5457
John Zulauf8eda1562021-04-13 17:06:41 -06005458bool SyncOpPipelineBarrier::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06005459 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf4fa68462021-04-26 21:04:22 -06005460 // No Validation for replay, as the layout transition accesses are checked directly, and the src*Mask ordering is captured
5461 // with first access information.
John Zulauf8eda1562021-04-13 17:06:41 -06005462 return false;
5463}
5464
John Zulauf4edde622021-02-15 08:54:50 -07005465void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5466 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5467 const VkMemoryBarrier *barriers) {
5468 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005469 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005470 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005471 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005472 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005473 }
5474 if (0 == memory_barrier_count) {
5475 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005476 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005477 }
John Zulauf4edde622021-02-15 08:54:50 -07005478 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005479}
5480
John Zulauf4edde622021-02-15 08:54:50 -07005481void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5482 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5483 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5484 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005485 for (uint32_t index = 0; index < barrier_count; index++) {
5486 const auto &barrier = barriers[index];
Jeremy Gebben9f537102021-10-05 16:37:12 -06005487 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005488 if (buffer) {
5489 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5490 const auto range = MakeRange(barrier.offset, barrier_size);
5491 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005492 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005493 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005494 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005495 }
5496 }
5497}
5498
John Zulauf4edde622021-02-15 08:54:50 -07005499void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
5500 uint32_t memory_barrier_count, const VkMemoryBarrier2KHR *barriers) {
5501 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005502 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005503 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005504 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5505 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5506 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005507 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005508 }
John Zulauf4edde622021-02-15 08:54:50 -07005509 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005510}
5511
John Zulauf4edde622021-02-15 08:54:50 -07005512void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5513 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5514 const VkBufferMemoryBarrier2KHR *barriers) {
5515 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005516 for (uint32_t index = 0; index < barrier_count; index++) {
5517 const auto &barrier = barriers[index];
5518 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5519 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9f537102021-10-05 16:37:12 -06005520 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005521 if (buffer) {
5522 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5523 const auto range = MakeRange(barrier.offset, barrier_size);
5524 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005525 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005526 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005527 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005528 }
5529 }
5530}
5531
John Zulauf4edde622021-02-15 08:54:50 -07005532void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5533 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5534 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5535 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005536 for (uint32_t index = 0; index < barrier_count; index++) {
5537 const auto &barrier = barriers[index];
Jeremy Gebben9f537102021-10-05 16:37:12 -06005538 const auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005539 if (image) {
5540 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5541 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005542 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005543 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005544 image_memory_barriers.emplace_back();
5545 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005546 }
5547 }
5548}
John Zulaufd5115702021-01-18 12:34:33 -07005549
John Zulauf4edde622021-02-15 08:54:50 -07005550void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5551 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5552 const VkImageMemoryBarrier2KHR *barriers) {
5553 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005554 for (uint32_t index = 0; index < barrier_count; index++) {
5555 const auto &barrier = barriers[index];
5556 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5557 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9f537102021-10-05 16:37:12 -06005558 const auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005559 if (image) {
5560 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5561 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005562 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005563 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005564 image_memory_barriers.emplace_back();
5565 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005566 }
5567 }
5568}
5569
John Zulauf36ef9282021-02-02 11:47:24 -07005570SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005571 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5572 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5573 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5574 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005575 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005576 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5577 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005578 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005579}
5580
John Zulauf4edde622021-02-15 08:54:50 -07005581SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5582 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5583 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5584 MakeEventsList(sync_state, eventCount, pEvents);
5585 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5586}
5587
John Zulauf610e28c2021-08-03 17:46:23 -06005588const char *const SyncOpWaitEvents::kIgnored = "Wait operation is ignored for this event.";
5589
John Zulaufd5115702021-01-18 12:34:33 -07005590bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005591 bool skip = false;
5592 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005593 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005594
John Zulauf610e28c2021-08-03 17:46:23 -06005595 // This is only interesting at record and not replay (Execute/Submit) time.
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
John Zulauf610e28c2021-08-03 17:46:23 -06005622 // The rest is common to record time and replay time.
5623 skip |= DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
5624 return skip;
5625}
5626
5627bool SyncOpWaitEvents::DoValidate(const CommandBufferAccessContext &cb_context, const ResourceUsageTag base_tag) const {
5628 bool skip = false;
5629 const auto &sync_state = cb_context.GetSyncState();
5630
Jeremy Gebben40a22942020-12-22 14:22:06 -07005631 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005632 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005633 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005634 const auto *events_context = cb_context.GetCurrentEventsContext();
5635 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005636 size_t barrier_set_index = 0;
5637 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
John Zulauf78394fc2021-07-12 15:41:40 -06005638 for (const auto &event : events_) {
5639 const auto *sync_event = events_context->Get(event.get());
5640 const auto &barrier_set = barriers_[barrier_set_index];
5641 if (!sync_event) {
5642 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5643 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5644 // new validation error... wait without previously submitted set event...
5645 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 -07005646 barrier_set_index += barrier_set_incr;
John Zulauf78394fc2021-07-12 15:41:40 -06005647 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005648 }
John Zulauf610e28c2021-08-03 17:46:23 -06005649
5650 // For replay calls, don't revalidate "same command buffer" events
5651 if (sync_event->last_command_tag > base_tag) continue;
5652
John Zulauf78394fc2021-07-12 15:41:40 -06005653 const auto event_handle = sync_event->event->event();
5654 // TODO add "destroyed" checks
5655
John Zulauf78b1f892021-09-20 15:02:09 -06005656 if (sync_event->first_scope_set) {
5657 // Only accumulate barrier and event stages if there is a pending set in the current context
5658 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5659 event_stage_masks |= sync_event->scope.mask_param;
5660 }
5661
John Zulauf78394fc2021-07-12 15:41:40 -06005662 const auto &src_exec_scope = barrier_set.src_exec_scope;
John Zulauf78b1f892021-09-20 15:02:09 -06005663
John Zulauf78394fc2021-07-12 15:41:40 -06005664 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5665 if (ignore_reason) {
5666 switch (ignore_reason) {
5667 case SyncEventState::ResetWaitRace:
5668 case SyncEventState::Reset2WaitRace: {
5669 // Four permuations of Reset and Wait calls...
5670 const char *vuid =
5671 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5672 if (ignore_reason == SyncEventState::Reset2WaitRace) {
5673 vuid = (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2KHR-event-03831"
5674 : "VUID-vkCmdResetEvent2KHR-event-03832";
5675 }
5676 const char *const message =
5677 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5678 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5679 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
John Zulauf610e28c2021-08-03 17:46:23 -06005680 CommandTypeString(sync_event->last_command), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005681 break;
5682 }
5683 case SyncEventState::SetRace: {
5684 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5685 // this event
5686 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5687 const char *const message =
5688 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5689 const char *const reason = "First synchronization scope is undefined.";
5690 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5691 sync_state.report_data->FormatHandle(event_handle).c_str(),
John Zulauf610e28c2021-08-03 17:46:23 -06005692 CommandTypeString(sync_event->last_command), reason, kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005693 break;
5694 }
5695 case SyncEventState::MissingStageBits: {
5696 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5697 // Issue error message that event waited for is not in wait events scope
5698 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5699 const char *const message = "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5700 ". Bits missing from srcStageMask %s. %s";
5701 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5702 sync_state.report_data->FormatHandle(event_handle).c_str(),
5703 sync_event->scope.mask_param, src_exec_scope.mask_param,
John Zulauf610e28c2021-08-03 17:46:23 -06005704 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005705 break;
5706 }
5707 case SyncEventState::SetVsWait2: {
5708 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2KHR-pEvents-03837",
5709 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5710 sync_state.report_data->FormatHandle(event_handle).c_str(),
5711 CommandTypeString(sync_event->last_command));
5712 break;
5713 }
5714 default:
5715 assert(ignore_reason == SyncEventState::NotIgnored);
5716 }
5717 } else if (barrier_set.image_memory_barriers.size()) {
5718 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5719 const auto *context = cb_context.GetCurrentAccessContext();
5720 assert(context);
5721 for (const auto &image_memory_barrier : image_memory_barriers) {
5722 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5723 const auto *image_state = image_memory_barrier.image.get();
5724 if (!image_state) continue;
5725 const auto &subresource_range = image_memory_barrier.range;
5726 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5727 const auto hazard =
5728 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5729 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5730 if (hazard.hazard) {
5731 skip |= sync_state.LogError(image_state->image(), string_SyncHazardVUID(hazard.hazard),
5732 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5733 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
5734 sync_state.report_data->FormatHandle(image_state->image()).c_str(),
5735 cb_context.FormatUsage(hazard).c_str());
5736 break;
5737 }
5738 }
5739 }
5740 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5741 // 03839
5742 barrier_set_index += barrier_set_incr;
5743 }
John Zulaufd5115702021-01-18 12:34:33 -07005744
5745 // 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 -07005746 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 -07005747 if (extra_stage_bits) {
5748 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005749 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5750 const char *const vuid =
5751 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2KHR-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005752 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005753 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulauf610e28c2021-08-03 17:46:23 -06005754 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005755 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005756 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005757 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005758 " vkCmdSetEvent may be in previously submitted command buffer.");
5759 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005760 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005761 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005762 }
5763 }
5764 return skip;
5765}
5766
5767struct SyncOpWaitEventsFunctorFactory {
5768 using BarrierOpFunctor = WaitEventBarrierOp;
5769 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5770 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5771 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5772 using BufferRange = EventSimpleRangeGenerator;
5773 using ImageRange = EventImageRangeGenerator;
5774 using GlobalRange = EventSimpleRangeGenerator;
5775
5776 // Need to restrict to only valid exec and access scope for this event
5777 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5778 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005779 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005780 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5781 return barrier;
5782 }
5783 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5784 auto barrier = RestrictToEvent(barrier_arg);
5785 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5786 }
John Zulauf14940722021-04-12 15:19:02 -06005787 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005788 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5789 }
5790 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5791 auto barrier = RestrictToEvent(barrier_arg);
5792 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5793 }
5794
5795 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5796 const AccessAddressType address_type = GetAccessAddressType(buffer);
5797 const auto base_address = ResourceBaseAddress(buffer);
5798 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5799 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5800 return filtered_range_gen;
5801 }
John Zulauf110413c2021-03-20 05:38:38 -06005802 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07005803 if (!SimpleBinding(image)) return ImageRange();
5804 const auto address_type = GetAccessAddressType(image);
5805 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005806 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005807 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5808
5809 return filtered_range_gen;
5810 }
5811 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5812 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5813 }
5814 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5815 SyncEventState *sync_event;
5816};
5817
John Zulauf8eda1562021-04-13 17:06:41 -06005818ResourceUsageTag SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07005819 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07005820 auto *access_context = cb_context->GetCurrentAccessContext();
5821 assert(access_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005822 if (!access_context) return tag;
John Zulauf669dfd52021-01-27 17:15:28 -07005823 auto *events_context = cb_context->GetCurrentEventsContext();
5824 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005825 if (!events_context) return tag;
John Zulaufd5115702021-01-18 12:34:33 -07005826
John Zulauf610e28c2021-08-03 17:46:23 -06005827 DoRecord(tag, access_context, events_context);
5828 return tag;
5829}
5830
5831void SyncOpWaitEvents::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005832 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
5833 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
5834 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
5835 access_context->ResolvePreviousAccesses();
5836
John Zulauf4edde622021-02-15 08:54:50 -07005837 size_t barrier_set_index = 0;
5838 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5839 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07005840 for (auto &event_shared : events_) {
5841 if (!event_shared.get()) continue;
5842 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07005843
John Zulauf4edde622021-02-15 08:54:50 -07005844 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06005845 sync_event->last_command_tag = tag;
John Zulaufd5115702021-01-18 12:34:33 -07005846
John Zulauf4edde622021-02-15 08:54:50 -07005847 const auto &barrier_set = barriers_[barrier_set_index];
5848 const auto &dst = barrier_set.dst_exec_scope;
5849 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07005850 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
5851 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
5852 // of the barriers is maintained.
5853 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07005854 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5855 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5856 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07005857
5858 // Apply the global barrier to the event itself (for race condition tracking)
5859 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
5860 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
5861 sync_event->barriers |= dst.exec_scope;
5862 } else {
5863 // We ignored this wait, so we don't have any effective synchronization barriers for it.
5864 sync_event->barriers = 0U;
5865 }
John Zulauf4edde622021-02-15 08:54:50 -07005866 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005867 }
5868
5869 // Apply the pending barriers
5870 ResolvePendingBarrierFunctor apply_pending_action(tag);
5871 access_context->ApplyToContext(apply_pending_action);
5872}
5873
John Zulauf8eda1562021-04-13 17:06:41 -06005874bool SyncOpWaitEvents::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06005875 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
5876 return DoValidate(*active_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06005877}
5878
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005879bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5880 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5881 bool skip = false;
5882 const auto *cb_access_context = GetAccessContext(commandBuffer);
5883 assert(cb_access_context);
5884 if (!cb_access_context) return skip;
5885
5886 const auto *context = cb_access_context->GetCurrentAccessContext();
5887 assert(context);
5888 if (!context) return skip;
5889
Jeremy Gebben9f537102021-10-05 16:37:12 -06005890 const auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005891
5892 if (dst_buffer) {
5893 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5894 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
5895 if (hazard.hazard) {
5896 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5897 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
5898 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
John Zulauf14940722021-04-12 15:19:02 -06005899 cb_access_context->FormatUsage(hazard).c_str());
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005900 }
5901 }
5902 return skip;
5903}
5904
John Zulauf669dfd52021-01-27 17:15:28 -07005905void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07005906 events_.reserve(event_count);
5907 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06005908 events_.emplace_back(sync_state.Get<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07005909 }
5910}
John Zulauf6ce24372021-01-30 05:56:25 -07005911
John Zulauf36ef9282021-02-02 11:47:24 -07005912SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005913 VkPipelineStageFlags2KHR stageMask)
Jeremy Gebben9f537102021-10-05 16:37:12 -06005914 : SyncOpBase(cmd), event_(sync_state.Get<EVENT_STATE>(event)), exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005915
John Zulauf1bf30522021-09-03 15:39:06 -06005916bool SyncOpResetEvent::Validate(const CommandBufferAccessContext& cb_context) const {
5917 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
5918}
5919
5920bool SyncOpResetEvent::DoValidate(const CommandBufferAccessContext & cb_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005921 auto *events_context = cb_context.GetCurrentEventsContext();
5922 assert(events_context);
5923 bool skip = false;
5924 if (!events_context) return skip;
5925
5926 const auto &sync_state = cb_context.GetSyncState();
5927 const auto *sync_event = events_context->Get(event_);
5928 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5929
John Zulauf1bf30522021-09-03 15:39:06 -06005930 if (sync_event->last_command_tag > base_tag) return skip; // if we validated this in recording of the secondary, don't repeat
5931
John Zulauf6ce24372021-01-30 05:56:25 -07005932 const char *const set_wait =
5933 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5934 "hazards.";
5935 const char *message = set_wait; // Only one message this call.
5936 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
5937 const char *vuid = nullptr;
5938 switch (sync_event->last_command) {
5939 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005940 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005941 // Needs a barrier between set and reset
5942 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
5943 break;
John Zulauf4edde622021-02-15 08:54:50 -07005944 case CMD_WAITEVENTS:
5945 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07005946 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
5947 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
5948 break;
5949 }
5950 default:
5951 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07005952 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
5953 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07005954 break;
5955 }
5956 if (vuid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005957 skip |= sync_state.LogError(event_->event(), vuid, message, CmdName(),
5958 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005959 CommandTypeString(sync_event->last_command));
5960 }
5961 }
5962 return skip;
5963}
5964
John Zulauf8eda1562021-04-13 17:06:41 -06005965ResourceUsageTag SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
5966 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07005967 auto *events_context = cb_context->GetCurrentEventsContext();
5968 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005969 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07005970
5971 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06005972 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07005973
5974 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07005975 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06005976 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07005977 sync_event->unsynchronized_set = CMD_NONE;
5978 sync_event->ResetFirstScope();
5979 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06005980
5981 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07005982}
5983
John Zulauf8eda1562021-04-13 17:06:41 -06005984bool SyncOpResetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06005985 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf1bf30522021-09-03 15:39:06 -06005986 return DoValidate(*active_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06005987}
5988
John Zulauf4fa68462021-04-26 21:04:22 -06005989void SyncOpResetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06005990
John Zulauf36ef9282021-02-02 11:47:24 -07005991SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005992 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005993 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06005994 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07005995 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
5996 dep_info_() {}
5997
5998SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
5999 const VkDependencyInfoKHR &dep_info)
6000 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006001 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006002 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
6003 dep_info_(new safe_VkDependencyInfoKHR(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006004
6005bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf610e28c2021-08-03 17:46:23 -06006006 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6007}
6008bool SyncOpSetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6009 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
6010 assert(active_context);
6011 return DoValidate(*active_context, base_tag);
6012}
6013
6014bool SyncOpSetEvent::DoValidate(const CommandBufferAccessContext &cb_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006015 bool skip = false;
6016
6017 const auto &sync_state = cb_context.GetSyncState();
6018 auto *events_context = cb_context.GetCurrentEventsContext();
6019 assert(events_context);
6020 if (!events_context) return skip;
6021
6022 const auto *sync_event = events_context->Get(event_);
6023 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6024
John Zulauf610e28c2021-08-03 17:46:23 -06006025 if (sync_event->last_command_tag >= base_tag) return skip; // for replay we don't want to revalidate internal "last commmand"
6026
John Zulauf6ce24372021-01-30 05:56:25 -07006027 const char *const reset_set =
6028 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6029 "hazards.";
6030 const char *const wait =
6031 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
6032
6033 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07006034 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07006035 const char *message = nullptr;
6036 switch (sync_event->last_command) {
6037 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006038 case CMD_RESETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006039 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07006040 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07006041 message = reset_set;
6042 break;
6043 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006044 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006045 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07006046 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07006047 message = reset_set;
6048 break;
6049 case CMD_WAITEVENTS:
John Zulauf4edde622021-02-15 08:54:50 -07006050 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006051 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07006052 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07006053 message = wait;
6054 break;
6055 default:
6056 // The only other valid last command that wasn't one.
6057 assert(sync_event->last_command == CMD_NONE);
6058 break;
6059 }
John Zulauf4edde622021-02-15 08:54:50 -07006060 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07006061 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07006062 std::string vuid("SYNC-");
6063 vuid.append(CmdName()).append(vuid_stem);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006064 skip |= sync_state.LogError(event_->event(), vuid.c_str(), message, CmdName(),
6065 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006066 CommandTypeString(sync_event->last_command));
6067 }
6068 }
6069
6070 return skip;
6071}
6072
John Zulauf8eda1562021-04-13 17:06:41 -06006073ResourceUsageTag SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006074 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006075 auto *events_context = cb_context->GetCurrentEventsContext();
6076 auto *access_context = cb_context->GetCurrentAccessContext();
6077 assert(events_context);
John Zulauf610e28c2021-08-03 17:46:23 -06006078 if (access_context && events_context) {
6079 DoRecord(tag, access_context, events_context);
6080 }
6081 return tag;
6082}
John Zulauf6ce24372021-01-30 05:56:25 -07006083
John Zulauf610e28c2021-08-03 17:46:23 -06006084void SyncOpSetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006085 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf610e28c2021-08-03 17:46:23 -06006086 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006087
6088 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
6089 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
6090 // any issues caused by naive scope setting here.
6091
6092 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
6093 // Given:
6094 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
6095 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
6096
6097 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
6098 sync_event->unsynchronized_set = sync_event->last_command;
6099 sync_event->ResetFirstScope();
John Zulauf78b1f892021-09-20 15:02:09 -06006100 } else if (!sync_event->first_scope_set) {
John Zulauf6ce24372021-01-30 05:56:25 -07006101 // We only set the scope if there isn't one
6102 sync_event->scope = src_exec_scope_;
6103
6104 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
6105 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
6106 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
6107 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
6108 }
6109 };
6110 access_context->ForAll(set_scope);
6111 sync_event->unsynchronized_set = CMD_NONE;
John Zulauf78b1f892021-09-20 15:02:09 -06006112 sync_event->first_scope_set = true;
John Zulauf6ce24372021-01-30 05:56:25 -07006113 sync_event->first_scope_tag = tag;
6114 }
John Zulauf4edde622021-02-15 08:54:50 -07006115 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
6116 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006117 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006118 sync_event->barriers = 0U;
6119}
John Zulauf64ffe552021-02-06 10:25:07 -07006120
6121SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
6122 const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07006123 const VkSubpassBeginInfo *pSubpassBeginInfo)
6124 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006125 if (pRenderPassBegin) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006126 rp_state_ = sync_state.Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
John Zulauf64ffe552021-02-06 10:25:07 -07006127 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
Jeremy Gebben9f537102021-10-05 16:37:12 -06006128 const auto fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07006129 if (fb_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006130 shared_attachments_ = sync_state.GetAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
John Zulauf64ffe552021-02-06 10:25:07 -07006131 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
6132 // Note that this a safe to presist as long as shared_attachments is not cleared
6133 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08006134 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07006135 attachments_.emplace_back(attachment.get());
6136 }
6137 }
6138 if (pSubpassBeginInfo) {
6139 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
6140 }
6141 }
6142}
6143
6144bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6145 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
6146 bool skip = false;
6147
6148 assert(rp_state_.get());
6149 if (nullptr == rp_state_.get()) return skip;
6150 auto &rp_state = *rp_state_.get();
6151
6152 const uint32_t subpass = 0;
6153
6154 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
6155 // hasn't happened yet)
6156 const std::vector<AccessContext> empty_context_vector;
6157 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
6158 cb_context.GetCurrentAccessContext());
6159
6160 // Validate attachment operations
6161 if (attachments_.size() == 0) return skip;
6162 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07006163
6164 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
6165 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
6166 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
6167 // operations (though it's currently a messy approach)
6168 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
6169 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006170
6171 // Validate load operations if there were no layout transition hazards
6172 if (!skip) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07006173 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kCurrentCommandTag);
6174 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006175 }
6176
6177 return skip;
6178}
6179
John Zulauf8eda1562021-04-13 17:06:41 -06006180ResourceUsageTag SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
6181 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf64ffe552021-02-06 10:25:07 -07006182 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
6183 assert(rp_state_.get());
John Zulauf8eda1562021-04-13 17:06:41 -06006184 if (nullptr == rp_state_.get()) return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006185 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006186
6187 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006188}
6189
John Zulauf8eda1562021-04-13 17:06:41 -06006190bool SyncOpBeginRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006191 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006192 return false;
6193}
6194
John Zulauf4fa68462021-04-26 21:04:22 -06006195void SyncOpBeginRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
6196}
John Zulauf8eda1562021-04-13 17:06:41 -06006197
John Zulauf64ffe552021-02-06 10:25:07 -07006198SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07006199 const VkSubpassEndInfo *pSubpassEndInfo)
6200 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006201 if (pSubpassBeginInfo) {
6202 subpass_begin_info_.initialize(pSubpassBeginInfo);
6203 }
6204 if (pSubpassEndInfo) {
6205 subpass_end_info_.initialize(pSubpassEndInfo);
6206 }
6207}
6208
6209bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
6210 bool skip = false;
6211 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6212 if (!renderpass_context) return skip;
6213
6214 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
6215 return skip;
6216}
6217
John Zulauf8eda1562021-04-13 17:06:41 -06006218ResourceUsageTag SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006219 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06006220 // TODO PHASE2 Need to fix renderpass tagging/segregation of barrier and access operations for QueueSubmit time validation
6221 auto prev_tag = cb_context->NextCommandTag(cmd_);
6222 auto next_tag = cb_context->NextSubcommandTag(cmd_);
6223
6224 cb_context->RecordNextSubpass(prev_tag, next_tag);
6225 // TODO PHASE2 This needs to be the tag of the barrier operations
6226 return prev_tag;
6227}
6228
6229bool SyncOpNextSubpass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006230 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006231 return false;
John Zulauf64ffe552021-02-06 10:25:07 -07006232}
6233
sfricke-samsung85584a72021-09-30 21:43:38 -07006234SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo)
6235 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006236 if (pSubpassEndInfo) {
6237 subpass_end_info_.initialize(pSubpassEndInfo);
6238 }
6239}
6240
John Zulauf4fa68462021-04-26 21:04:22 -06006241void SyncOpNextSubpass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006242
John Zulauf64ffe552021-02-06 10:25:07 -07006243bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6244 bool skip = false;
6245 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6246
6247 if (!renderpass_context) return skip;
6248 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
6249 return skip;
6250}
6251
John Zulauf8eda1562021-04-13 17:06:41 -06006252ResourceUsageTag SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006253 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06006254 const auto tag = cb_context->NextCommandTag(cmd_);
6255 cb_context->RecordEndRenderPass(tag);
6256 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006257}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006258
John Zulauf8eda1562021-04-13 17:06:41 -06006259bool SyncOpEndRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006260 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006261 return false;
6262}
6263
John Zulauf4fa68462021-04-26 21:04:22 -06006264void SyncOpEndRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006265
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006266void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6267 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
6268 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
6269 auto *cb_access_context = GetAccessContext(commandBuffer);
6270 assert(cb_access_context);
6271 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
6272 auto *context = cb_access_context->GetCurrentAccessContext();
6273 assert(context);
6274
Jeremy Gebben9f537102021-10-05 16:37:12 -06006275 const auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006276
6277 if (dst_buffer) {
6278 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6279 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
6280 }
6281}
John Zulaufd05c5842021-03-26 11:32:16 -06006282
John Zulaufae842002021-04-15 18:20:55 -06006283bool SyncValidator::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6284 const VkCommandBuffer *pCommandBuffers) const {
6285 bool skip = StateTracker::PreCallValidateCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
6286 const char *func_name = "vkCmdExecuteCommands";
6287 const auto *cb_context = GetAccessContext(commandBuffer);
6288 assert(cb_context);
John Zulauf4fa68462021-04-26 21:04:22 -06006289
6290 // Heavyweight, but we need a proxy copy of the active command buffer access context
6291 CommandBufferAccessContext proxy_cb_context(*cb_context, CommandBufferAccessContext::AsProxyContext());
John Zulaufae842002021-04-15 18:20:55 -06006292
6293 // Make working copies of the access and events contexts
John Zulauf4fa68462021-04-26 21:04:22 -06006294 proxy_cb_context.NextCommandTag(CMD_EXECUTECOMMANDS);
John Zulaufae842002021-04-15 18:20:55 -06006295
6296 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
John Zulauf4fa68462021-04-26 21:04:22 -06006297 proxy_cb_context.NextSubcommandTag(CMD_EXECUTECOMMANDS);
John Zulaufae842002021-04-15 18:20:55 -06006298 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6299 if (!recorded_cb_context) continue;
John Zulauf4fa68462021-04-26 21:04:22 -06006300
6301 const auto *recorded_context = recorded_cb_context->GetCurrentAccessContext();
6302 assert(recorded_context);
6303 skip |= recorded_cb_context->ValidateFirstUse(&proxy_cb_context, func_name, cb_index);
6304
6305 // The barriers have already been applied in ValidatFirstUse
6306 ResourceUsageRange tag_range = proxy_cb_context.ImportRecordedAccessLog(*recorded_cb_context);
6307 proxy_cb_context.ResolveRecordedContext(*recorded_context, tag_range.begin);
John Zulaufae842002021-04-15 18:20:55 -06006308 }
6309
John Zulaufae842002021-04-15 18:20:55 -06006310 return skip;
6311}
6312
6313void SyncValidator::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6314 const VkCommandBuffer *pCommandBuffers) {
6315 StateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
John Zulauf4fa68462021-04-26 21:04:22 -06006316 auto *cb_context = GetAccessContext(commandBuffer);
6317 assert(cb_context);
6318 cb_context->NextCommandTag(CMD_EXECUTECOMMANDS);
6319 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
6320 cb_context->NextSubcommandTag(CMD_EXECUTECOMMANDS);
6321 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6322 if (!recorded_cb_context) continue;
6323 cb_context->RecordExecutedCommandBuffer(*recorded_cb_context, CMD_EXECUTECOMMANDS);
6324 }
John Zulaufae842002021-04-15 18:20:55 -06006325}
6326
John Zulaufd0ec59f2021-03-13 14:25:08 -07006327AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
6328 : view_(view), view_mask_(), gen_store_() {
6329 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
6330 const IMAGE_STATE &image_state = *view_->image_state.get();
6331 const auto base_address = ResourceBaseAddress(image_state);
6332 const auto *encoder = image_state.fragment_encoder.get();
6333 if (!encoder) return;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06006334 // Get offset and extent for the view, accounting for possible depth slicing
6335 const VkOffset3D zero_offset = view->GetOffset();
6336 const VkExtent3D &image_extent = view->GetExtent();
John Zulaufd0ec59f2021-03-13 14:25:08 -07006337 // Intentional copy
6338 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
6339 view_mask_ = subres_range.aspectMask;
6340 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
6341 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6342
6343 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
6344 if (depth && (depth != view_mask_)) {
6345 subres_range.aspectMask = depth;
6346 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6347 }
6348 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
6349 if (stencil && (stencil != view_mask_)) {
6350 subres_range.aspectMask = stencil;
6351 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6352 }
6353}
6354
6355const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
6356 const ImageRangeGen *got = nullptr;
6357 switch (gen_type) {
6358 case kViewSubresource:
6359 got = &gen_store_[kViewSubresource];
6360 break;
6361 case kRenderArea:
6362 got = &gen_store_[kRenderArea];
6363 break;
6364 case kDepthOnlyRenderArea:
6365 got =
6366 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
6367 break;
6368 case kStencilOnlyRenderArea:
6369 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
6370 : &gen_store_[Gen::kStencilOnlyRenderArea];
6371 break;
6372 default:
6373 assert(got);
6374 }
6375 return got;
6376}
6377
6378AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
6379 assert(IsValid());
6380 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
6381 if (depth_op) {
6382 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
6383 if (stencil_op) {
6384 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6385 return kRenderArea;
6386 }
6387 return kDepthOnlyRenderArea;
6388 }
6389 if (stencil_op) {
6390 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6391 return kStencilOnlyRenderArea;
6392 }
6393
6394 assert(depth_op || stencil_op);
6395 return kRenderArea;
6396}
6397
6398AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }
John Zulauf8eda1562021-04-13 17:06:41 -06006399
6400void SyncEventsContext::ApplyBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
6401 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
6402 for (auto &event_pair : map_) {
6403 assert(event_pair.second); // Shouldn't be storing empty
6404 auto &sync_event = *event_pair.second;
6405 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
6406 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
6407 sync_event.barriers |= dst.exec_scope;
6408 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6409 }
6410 }
6411}