blob: e76eb1b65779e51c8dadff578eb41449a3b06876 [file] [log] [blame]
John Zulauf9cb530d2019-09-30 14:14:10 -06001/* Copyright (c) 2019 The Khronos Group Inc.
2 * Copyright (c) 2019 Valve Corporation
3 * Copyright (c) 2019 LunarG, Inc.
4 *
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>
18 */
19
20#include <limits>
21#include <vector>
locke-lunarg296a3c92020-03-25 01:04:29 -060022#include <memory>
23#include <bitset>
John Zulauf9cb530d2019-09-30 14:14:10 -060024#include "synchronization_validation.h"
25
26static const char *string_SyncHazardVUID(SyncHazard hazard) {
27 switch (hazard) {
28 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070029 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060030 break;
31 case SyncHazard::READ_AFTER_WRITE:
32 return "SYNC-HAZARD-READ_AFTER_WRITE";
33 break;
34 case SyncHazard::WRITE_AFTER_READ:
35 return "SYNC-HAZARD-WRITE_AFTER_READ";
36 break;
37 case SyncHazard::WRITE_AFTER_WRITE:
38 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
39 break;
John Zulauf2f952d22020-02-10 11:34:51 -070040 case SyncHazard::READ_RACING_WRITE:
41 return "SYNC-HAZARD-READ-RACING-WRITE";
42 break;
43 case SyncHazard::WRITE_RACING_WRITE:
44 return "SYNC-HAZARD-WRITE-RACING-WRITE";
45 break;
46 case SyncHazard::WRITE_RACING_READ:
47 return "SYNC-HAZARD-WRITE-RACING-READ";
48 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060049 default:
50 assert(0);
51 }
52 return "SYNC-HAZARD-INVALID";
53}
54
55static const char *string_SyncHazard(SyncHazard hazard) {
56 switch (hazard) {
57 case SyncHazard::NONE:
58 return "NONR";
59 break;
60 case SyncHazard::READ_AFTER_WRITE:
61 return "READ_AFTER_WRITE";
62 break;
63 case SyncHazard::WRITE_AFTER_READ:
64 return "WRITE_AFTER_READ";
65 break;
66 case SyncHazard::WRITE_AFTER_WRITE:
67 return "WRITE_AFTER_WRITE";
68 break;
John Zulauf2f952d22020-02-10 11:34:51 -070069 case SyncHazard::READ_RACING_WRITE:
70 return "READ_RACING_WRITE";
71 break;
72 case SyncHazard::WRITE_RACING_WRITE:
73 return "WRITE_RACING_WRITE";
74 break;
75 case SyncHazard::WRITE_RACING_READ:
76 return "WRITE_RACING_READ";
77 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060078 default:
79 assert(0);
80 }
81 return "INVALID HAZARD";
82}
83
John Zulaufb027cdb2020-05-21 14:25:22 -060084static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
85static constexpr SyncStageAccessFlags kColorAttachmentAccessScope =
86 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
87 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
88 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT;
89static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
90 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
91static constexpr SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
92 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
93 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
94 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
95 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
96
97static constexpr SyncOrderingBarrier kColorAttachmentRasterOrder = {kColorAttachmentExecScope, kColorAttachmentAccessScope};
98static constexpr SyncOrderingBarrier kDepthStencilAttachmentRasterOrder = {kDepthStencilAttachmentExecScope,
99 kDepthStencilAttachmentAccessScope};
100static constexpr SyncOrderingBarrier kAttachmentRasterOrder = {kDepthStencilAttachmentExecScope | kColorAttachmentExecScope,
101 kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope};
John Zulauf7635de32020-05-29 17:14:15 -0600102// Sometimes we have an internal access conflict, and we using the kCurrentCommandTag to set and detect in temporary/proxy contexts
103static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex);
John Zulaufb027cdb2020-05-21 14:25:22 -0600104
locke-lunarg3c038002020-04-30 23:08:08 -0600105inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
106 if (size == VK_WHOLE_SIZE) {
107 return (whole_size - offset);
108 }
109 return size;
110}
111
John Zulauf16adfc92020-04-08 10:28:33 -0600112template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600113static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600114 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
115}
116
John Zulauf355e49b2020-04-24 15:11:15 -0600117static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600118
John Zulauf0cb5be22020-01-23 12:18:22 -0700119// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
120VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
121 VkPipelineStageFlags expanded = stage_mask;
122 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
123 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
124 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
125 if (all_commands.first & queue_flags) {
126 expanded |= all_commands.second;
127 }
128 }
129 }
130 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
131 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
132 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
133 }
134 return expanded;
135}
136
John Zulauf36bcf6a2020-02-03 15:12:52 -0700137VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
138 std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
139 VkPipelineStageFlags unscanned = stage_mask;
140 VkPipelineStageFlags related = 0;
141 for (const auto entry : map) {
142 const auto stage = entry.first;
143 if (stage & unscanned) {
144 related = related | entry.second;
145 unscanned = unscanned & ~stage;
146 if (!unscanned) break;
147 }
148 }
149 return related;
150}
151
152VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
153 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
154}
155
156VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
157 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
158}
159
John Zulauf5c5e88d2019-12-26 11:22:02 -0700160static const ResourceAccessRange full_range(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700161
John Zulauf355e49b2020-04-24 15:11:15 -0600162// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
163const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
164 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
165
John Zulauf7635de32020-05-29 17:14:15 -0600166// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
167// Used by both validation and record operations
168//
169// The signature for Action() reflect the needs of both uses.
170template <typename Action>
171void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
172 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
173 VkExtent3D extent = CastTo3D(render_area.extent);
174 VkOffset3D offset = CastTo3D(render_area.offset);
175 const auto &rp_ci = rp_state.createInfo;
176 const auto *attachment_ci = rp_ci.pAttachments;
177 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
178
179 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
180 const auto *color_attachments = subpass_ci.pColorAttachments;
181 const auto *color_resolve = subpass_ci.pResolveAttachments;
182 if (color_resolve && color_attachments) {
183 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
184 const auto &color_attach = color_attachments[i].attachment;
185 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
186 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
187 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
188 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
189 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
190 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
191 }
192 }
193 }
194
195 // Depth stencil resolve only if the extension is present
196 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
197 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
198 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
199 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
200 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
201 const auto src_ci = attachment_ci[src_at];
202 // The formats are required to match so we can pick either
203 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
204 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
205 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
206 VkImageAspectFlags aspect_mask = 0u;
207
208 // Figure out which aspects are actually touched during resolve operations
209 const char *aspect_string = nullptr;
210 if (resolve_depth && resolve_stencil) {
211 // Validate all aspects together
212 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
213 aspect_string = "depth/stencil";
214 } else if (resolve_depth) {
215 // Validate depth only
216 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
217 aspect_string = "depth";
218 } else if (resolve_stencil) {
219 // Validate all stencil only
220 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
221 aspect_string = "stencil";
222 }
223
224 if (aspect_mask) {
225 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
226 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kDepthStencilAttachmentRasterOrder, offset, extent,
227 aspect_mask);
228 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
229 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
230 }
231 }
232}
233
234// Action for validating resolve operations
235class ValidateResolveAction {
236 public:
237 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
238 const char *func_name)
239 : render_pass_(render_pass),
240 subpass_(subpass),
241 context_(context),
242 sync_state_(sync_state),
243 func_name_(func_name),
244 skip_(false) {}
245 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
246 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
247 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
248 HazardResult hazard;
249 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
250 if (hazard.hazard) {
251 skip_ |= sync_state_.LogError(
252 render_pass_, string_SyncHazardVUID(hazard.hazard),
253 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32 " to resolve attachment %" PRIu32 ".",
254 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name, src_at, dst_at);
255 }
256 }
257 // Providing a mechanism for the constructing caller to get the result of the validation
258 bool GetSkip() const { return skip_; }
259
260 private:
261 VkRenderPass render_pass_;
262 const uint32_t subpass_;
263 const AccessContext &context_;
264 const SyncValidator &sync_state_;
265 const char *func_name_;
266 bool skip_;
267};
268
269// Update action for resolve operations
270class UpdateStateResolveAction {
271 public:
272 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
273 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
274 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
275 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
276 // Ignores validation only arguments...
277 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
278 }
279
280 private:
281 AccessContext &context_;
282 const ResourceUsageTag &tag_;
283};
284
John Zulauf540266b2020-04-06 18:54:53 -0600285AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
286 const std::vector<SubpassDependencyGraphNode> &dependencies,
287 const std::vector<AccessContext> &contexts, AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600288 Reset();
289 const auto &subpass_dep = dependencies[subpass];
290 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600291 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600292 for (const auto &prev_dep : subpass_dep.prev) {
293 assert(prev_dep.dependency);
294 const auto dep = *prev_dep.dependency;
John Zulauf540266b2020-04-06 18:54:53 -0600295 prev_.emplace_back(const_cast<AccessContext *>(&contexts[dep.srcSubpass]), queue_flags, dep);
John Zulauf355e49b2020-04-24 15:11:15 -0600296 prev_by_subpass_[dep.srcSubpass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700297 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600298
299 async_.reserve(subpass_dep.async.size());
300 for (const auto async_subpass : subpass_dep.async) {
John Zulauf540266b2020-04-06 18:54:53 -0600301 async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass]));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600302 }
John Zulaufe5da6e52020-03-18 15:32:18 -0600303 if (subpass_dep.barrier_from_external) {
304 src_external_ = TrackBack(external_context, queue_flags, *subpass_dep.barrier_from_external);
305 } else {
306 src_external_ = TrackBack();
307 }
308 if (subpass_dep.barrier_to_external) {
309 dst_external_ = TrackBack(this, queue_flags, *subpass_dep.barrier_to_external);
310 } else {
311 dst_external_ = TrackBack();
John Zulauf3d84f1b2020-03-09 13:33:25 -0600312 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700313}
314
John Zulauf5f13a792020-03-10 07:31:21 -0600315template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600316HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600317 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600318 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600319 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600320
321 HazardResult hazard;
322 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
323 hazard = detector.Detect(prev);
324 }
325 return hazard;
326}
327
John Zulauf3d84f1b2020-03-09 13:33:25 -0600328// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
329// the DAG of the contexts (for example subpasses)
330template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600331HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
332 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600333 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600334
John Zulauf355e49b2020-04-24 15:11:15 -0600335 if (static_cast<uint32_t>(options) | DetectOptions::kDetectAsync) {
336 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
337 // so we'll check these first
338 for (const auto &async_context : async_) {
339 hazard = async_context->DetectAsyncHazard(type, detector, range);
340 if (hazard.hazard) return hazard;
341 }
John Zulauf5f13a792020-03-10 07:31:21 -0600342 }
343
John Zulauf69133422020-05-20 14:55:53 -0600344 const bool detect_prev = (static_cast<uint32_t>(options) | DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600345
John Zulauf69133422020-05-20 14:55:53 -0600346 const auto &accesses = GetAccessStateMap(type);
347 const auto from = accesses.lower_bound(range);
348 const auto to = accesses.upper_bound(range);
349 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600350
John Zulauf69133422020-05-20 14:55:53 -0600351 for (auto pos = from; pos != to; ++pos) {
352 // Cover any leading gap, or gap between entries
353 if (detect_prev) {
354 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
355 // Cover any leading gap, or gap between entries
356 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600357 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600358 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600359 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600360 if (hazard.hazard) return hazard;
361 }
John Zulauf69133422020-05-20 14:55:53 -0600362 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
363 gap.begin = pos->first.end;
364 }
365
366 hazard = detector.Detect(pos);
367 if (hazard.hazard) return hazard;
368 }
369
370 if (detect_prev) {
371 // Detect in the trailing empty as needed
372 gap.end = range.end;
373 if (gap.non_empty()) {
374 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600375 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600376 }
377
378 return hazard;
379}
380
381// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
382template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600383HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600384 auto &accesses = GetAccessStateMap(type);
385 const auto from = accesses.lower_bound(range);
386 const auto to = accesses.upper_bound(range);
387
John Zulauf3d84f1b2020-03-09 13:33:25 -0600388 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600389 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
390 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600391 }
John Zulauf16adfc92020-04-08 10:28:33 -0600392
John Zulauf3d84f1b2020-03-09 13:33:25 -0600393 return hazard;
394}
395
John Zulauf355e49b2020-04-24 15:11:15 -0600396// Returns the last resolved entry
397static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
398 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
399 const SyncBarrier *barrier) {
400 auto at = entry;
401 for (auto pos = first; pos != last; ++pos) {
402 // Every member of the input iterator range must fit within the remaining portion of entry
403 assert(at->first.includes(pos->first));
404 assert(at != dest->end());
405 // Trim up at to the same size as the entry to resolve
406 at = sparse_container::split(at, *dest, pos->first);
407 auto access = pos->second;
408 if (barrier) {
409 access.ApplyBarrier(*barrier);
410 }
411 at->second.Resolve(access);
412 ++at; // Go to the remaining unused section of entry
413 }
414}
415
416void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const SyncBarrier *barrier,
417 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
418 bool recur_to_infill) const {
419 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
420 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf16adfc92020-04-08 10:28:33 -0600421 if (current->pos_B->valid) {
422 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600423 auto access = src_pos->second;
424 if (barrier) {
425 access.ApplyBarrier(*barrier);
426 }
John Zulauf16adfc92020-04-08 10:28:33 -0600427 if (current->pos_A->valid) {
428 current.trim_A();
John Zulauf355e49b2020-04-24 15:11:15 -0600429 current->pos_A->lower_bound->second.Resolve(access);
John Zulauf5f13a792020-03-10 07:31:21 -0600430 } else {
John Zulauf355e49b2020-04-24 15:11:15 -0600431 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, access));
432 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600433 }
John Zulauf16adfc92020-04-08 10:28:33 -0600434 } else {
435 // we have to descend to fill this gap
436 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600437 if (current->pos_A->valid) {
438 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
439 ResourceAccessRangeMap gap_map;
440 ResolvePreviousAccess(type, current->range, &gap_map, infill_state);
441 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier);
442 } else {
443 // There isn't anything in dest in current->range, so we can accumulate directly into it.
444 ResolvePreviousAccess(type, current->range, resolve_map, infill_state);
445 if (barrier) {
446 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
447 for (auto pos = resolve_map->lower_bound(current->range); pos != current->pos_A->lower_bound; ++pos) {
448 pos->second.ApplyBarrier(*barrier);
449 }
450 }
451 }
452 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
453 // iterator of the outer while.
454
455 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
456 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
457 // we stepped on the dest map
458 const auto seek_to = current->range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
459 current.invalidate_A(); // Changes current->range
460 current.seek(seek_to);
461 } else if (!current->pos_A->valid && infill_state) {
462 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
463 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
464 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600465 }
John Zulauf5f13a792020-03-10 07:31:21 -0600466 }
John Zulauf16adfc92020-04-08 10:28:33 -0600467 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600468 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600469}
470
John Zulauf355e49b2020-04-24 15:11:15 -0600471void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
472 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600473 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600474 if (range.non_empty() && infill_state) {
475 descent_map->insert(std::make_pair(range, *infill_state));
476 }
477 } else {
478 // Look for something to fill the gap further along.
479 for (const auto &prev_dep : prev_) {
John Zulauf355e49b2020-04-24 15:11:15 -0600480 prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600481 }
482
John Zulaufe5da6e52020-03-18 15:32:18 -0600483 if (src_external_.context) {
John Zulauf355e49b2020-04-24 15:11:15 -0600484 src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600485 }
486 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600487}
488
John Zulauf16adfc92020-04-08 10:28:33 -0600489AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600490 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600491}
492
493VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
494 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
495}
496
John Zulauf355e49b2020-04-24 15:11:15 -0600497static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600498
John Zulauf1507ee42020-05-18 11:33:09 -0600499static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
500 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
501 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
502 return stage_access;
503}
504static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
505 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
506 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
507 return stage_access;
508}
509
John Zulauf7635de32020-05-29 17:14:15 -0600510// Caller must manage returned pointer
511static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
512 uint32_t subpass, const VkRect2D &render_area,
513 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
514 auto *proxy = new AccessContext(context);
515 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600516 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600517 return proxy;
518}
519
John Zulauf540266b2020-04-06 18:54:53 -0600520void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600521 AddressType address_type, ResourceAccessRangeMap *descent_map,
522 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600523 if (!SimpleBinding(image_state)) return;
524
John Zulauf62f10592020-04-03 12:20:02 -0600525 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600526 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600527 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600528 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600529 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600530 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600531 }
532}
533
John Zulauf7635de32020-05-29 17:14:15 -0600534// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600535bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600536 const VkRect2D &render_area, uint32_t subpass,
537 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
538 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600539 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600540 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
541 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
542 // those affects have not been recorded yet.
543 //
544 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
545 // to apply and only copy then, if this proves a hot spot.
546 std::unique_ptr<AccessContext> proxy_for_prev;
547 TrackBack proxy_track_back;
548
John Zulauf355e49b2020-04-24 15:11:15 -0600549 const auto &transitions = rp_state.subpass_transitions[subpass];
550 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600551 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
552
553 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
554 if (prev_needs_proxy) {
555 if (!proxy_for_prev) {
556 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
557 render_area, attachment_views));
558 proxy_track_back = *track_back;
559 proxy_track_back.context = proxy_for_prev.get();
560 }
561 track_back = &proxy_track_back;
562 }
563 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600564 if (hazard.hazard) {
565 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
566 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " image layout transition.",
567 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment);
568 }
569 }
570 return skip;
571}
572
John Zulauf1507ee42020-05-18 11:33:09 -0600573bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600574 const VkRect2D &render_area, uint32_t subpass,
575 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
576 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600577 bool skip = false;
578 const auto *attachment_ci = rp_state.createInfo.pAttachments;
579 VkExtent3D extent = CastTo3D(render_area.extent);
580 VkOffset3D offset = CastTo3D(render_area.offset);
581 const auto external_access_scope = src_external_.barrier.dst_access_scope;
John Zulauf1507ee42020-05-18 11:33:09 -0600582
583 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
584 if (subpass == rp_state.attachment_first_subpass[i]) {
585 if (attachment_views[i] == nullptr) continue;
586 const IMAGE_VIEW_STATE &view = *attachment_views[i];
587 const IMAGE_STATE *image = view.image_state.get();
588 if (image == nullptr) continue;
589 const auto &ci = attachment_ci[i];
590 const bool is_transition = rp_state.attachment_first_is_transition[i];
591
592 // Need check in the following way
593 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
594 // vs. transition
595 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
596 // for each aspect loaded.
597
598 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600599 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600600 const bool is_color = !(has_depth || has_stencil);
601
602 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
603 const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U;
604 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
605 const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U;
606
John Zulaufaff20662020-06-01 14:07:58 -0600607 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600608 const char *aspect = nullptr;
609 if (is_transition) {
610 // For transition w
611 SyncHazard transition_hazard = SyncHazard::NONE;
612 bool checked_stencil = false;
613 if (load_mask) {
614 if ((load_mask & external_access_scope) != load_mask) {
615 transition_hazard =
616 SyncStageAccess::HasWrite(load_mask) ? SyncHazard::WRITE_AFTER_WRITE : SyncHazard::READ_AFTER_WRITE;
617 aspect = is_color ? "color" : "depth";
618 }
619 if (!transition_hazard && stencil_mask) {
620 if ((stencil_mask & external_access_scope) != stencil_mask) {
621 transition_hazard = SyncStageAccess::HasWrite(stencil_mask) ? SyncHazard::WRITE_AFTER_WRITE
622 : SyncHazard::READ_AFTER_WRITE;
623 aspect = "stencil";
624 checked_stencil = true;
625 }
626 }
627 }
628 if (transition_hazard) {
629 // Hazard vs. ILT
630 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
631 skip |=
632 sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
633 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
634 " aspect %s during load with loadOp %s.",
635 func_name, string_SyncHazard(transition_hazard), subpass, i, aspect, load_op_string);
636 }
637 } else {
638 auto hazard_range = view.normalized_subresource_range;
639 bool checked_stencil = false;
640 if (is_color) {
641 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
642 aspect = "color";
643 } else {
644 if (has_depth) {
645 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
646 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
647 aspect = "depth";
648 }
649 if (!hazard.hazard && has_stencil) {
650 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
651 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
652 aspect = "stencil";
653 checked_stencil = true;
654 }
655 }
656
657 if (hazard.hazard) {
658 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
659 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
660 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
661 " aspect %s during load with loadOp %s.",
662 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
663 }
664 }
665 }
666 }
667 return skip;
668}
669
John Zulaufaff20662020-06-01 14:07:58 -0600670// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
671// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
672// store is part of the same Next/End operation.
673// The latter is handled in layout transistion validation directly
674bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
675 const VkRect2D &render_area, uint32_t subpass,
676 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
677 const char *func_name) const {
678 bool skip = false;
679 const auto *attachment_ci = rp_state.createInfo.pAttachments;
680 VkExtent3D extent = CastTo3D(render_area.extent);
681 VkOffset3D offset = CastTo3D(render_area.offset);
682
683 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
684 if (subpass == rp_state.attachment_last_subpass[i]) {
685 if (attachment_views[i] == nullptr) continue;
686 const IMAGE_VIEW_STATE &view = *attachment_views[i];
687 const IMAGE_STATE *image = view.image_state.get();
688 if (image == nullptr) continue;
689 const auto &ci = attachment_ci[i];
690
691 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
692 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
693 // sake, we treat DONT_CARE as writing.
694 const bool has_depth = FormatHasDepth(ci.format);
695 const bool has_stencil = FormatHasStencil(ci.format);
696 const bool is_color = !(has_depth || has_stencil);
697 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
698 if (!has_stencil && !store_op_stores) continue;
699
700 HazardResult hazard;
701 const char *aspect = nullptr;
702 bool checked_stencil = false;
703 if (is_color) {
704 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
705 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
706 aspect = "color";
707 } else {
708 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
709 auto hazard_range = view.normalized_subresource_range;
710 if (has_depth && store_op_stores) {
711 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
712 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
713 kAttachmentRasterOrder, offset, extent);
714 aspect = "depth";
715 }
716 if (!hazard.hazard && has_stencil && stencil_op_stores) {
717 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
718 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
719 kAttachmentRasterOrder, offset, extent);
720 aspect = "stencil";
721 checked_stencil = true;
722 }
723 }
724
725 if (hazard.hazard) {
726 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
727 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
728 skip |= sync_state.LogError(
729 rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
730 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " %s aspect during store with %s %s.", func_name,
731 string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string, store_op_string);
732 }
733 }
734 }
735 return skip;
736}
737
John Zulaufb027cdb2020-05-21 14:25:22 -0600738bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
739 const VkRect2D &render_area,
740 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
741 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600742 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
743 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
744 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600745}
746
John Zulauf3d84f1b2020-03-09 13:33:25 -0600747class HazardDetector {
748 SyncStageAccessIndex usage_index_;
749
750 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600751 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600752 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
753 return pos->second.DetectAsyncHazard(usage_index_);
754 }
755 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
756};
757
John Zulauf69133422020-05-20 14:55:53 -0600758class HazardDetectorWithOrdering {
759 const SyncStageAccessIndex usage_index_;
760 const SyncOrderingBarrier &ordering_;
761
762 public:
763 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
764 return pos->second.DetectHazard(usage_index_, ordering_);
765 }
766 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
767 return pos->second.DetectAsyncHazard(usage_index_);
768 }
769 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
770 : usage_index_(usage), ordering_(ordering) {}
771};
772
John Zulauf16adfc92020-04-08 10:28:33 -0600773HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600774 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600775 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600776 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600777}
778
John Zulauf16adfc92020-04-08 10:28:33 -0600779HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600780 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600781 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600782 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600783}
784
John Zulauf69133422020-05-20 14:55:53 -0600785template <typename Detector>
786HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
787 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
788 const VkExtent3D &extent, DetectOptions options) const {
789 if (!SimpleBinding(image)) return HazardResult();
790 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
791 const auto address_type = ImageAddressType(image);
792 const auto base_address = ResourceBaseAddress(image);
793 for (; range_gen->non_empty(); ++range_gen) {
794 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
795 if (hazard.hazard) return hazard;
796 }
797 return HazardResult();
798}
799
John Zulauf540266b2020-04-06 18:54:53 -0600800HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
801 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
802 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700803 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
804 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -0600805 return DetectHazard(image, current_usage, subresource_range, offset, extent);
806}
807
808HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
809 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
810 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -0600811 HazardDetector detector(current_usage);
812 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
813}
814
815HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
816 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
817 const VkOffset3D &offset, const VkExtent3D &extent) const {
818 HazardDetectorWithOrdering detector(current_usage, ordering);
819 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -0600820}
821
John Zulaufb027cdb2020-05-21 14:25:22 -0600822// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
823// should have reported the issue regarding an invalid attachment entry
824HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
825 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
826 VkImageAspectFlags aspect_mask) const {
827 if (view != nullptr) {
828 const IMAGE_STATE *image = view->image_state.get();
829 if (image != nullptr) {
830 auto *detect_range = &view->normalized_subresource_range;
831 VkImageSubresourceRange masked_range;
832 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
833 masked_range = view->normalized_subresource_range;
834 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
835 detect_range = &masked_range;
836 }
837
838 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
839 if (detect_range->aspectMask) {
840 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
841 }
842 }
843 }
844 return HazardResult();
845}
John Zulauf3d84f1b2020-03-09 13:33:25 -0600846class BarrierHazardDetector {
847 public:
848 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
849 SyncStageAccessFlags src_access_scope)
850 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
851
John Zulauf5f13a792020-03-10 07:31:21 -0600852 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
853 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -0700854 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600855 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
856 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
857 return pos->second.DetectAsyncHazard(usage_index_);
858 }
859
860 private:
861 SyncStageAccessIndex usage_index_;
862 VkPipelineStageFlags src_exec_scope_;
863 SyncStageAccessFlags src_access_scope_;
864};
865
John Zulauf16adfc92020-04-08 10:28:33 -0600866HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -0600867 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600868 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600869 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -0600870 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700871}
872
John Zulauf16adfc92020-04-08 10:28:33 -0600873HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600874 SyncStageAccessFlags src_access_scope,
875 const VkImageSubresourceRange &subresource_range,
876 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -0600877 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
878 VkOffset3D zero_offset = {0, 0, 0};
879 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700880}
881
John Zulauf355e49b2020-04-24 15:11:15 -0600882HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
883 SyncStageAccessFlags src_stage_accesses,
884 const VkImageMemoryBarrier &barrier) const {
885 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
886 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
887 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
888}
889
John Zulauf9cb530d2019-09-30 14:14:10 -0600890template <typename Flags, typename Map>
891SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
892 SyncStageAccessFlags scope = 0;
893 for (const auto &bit_scope : map) {
894 if (flag_mask < bit_scope.first) break;
895
896 if (flag_mask & bit_scope.first) {
897 scope |= bit_scope.second;
898 }
899 }
900 return scope;
901}
902
903SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
904 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
905}
906
907SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
908 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
909}
910
911// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
912SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -0600913 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
914 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
915 // 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 -0600916 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
917}
918
919template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -0700920void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -0600921 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
922 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -0600923 auto pos = accesses->lower_bound(range);
924 if (pos == accesses->end() || !pos->first.intersects(range)) {
925 // The range is empty, fill it with a default value.
926 pos = action.Infill(accesses, pos, range);
927 } else if (range.begin < pos->first.begin) {
928 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -0700929 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -0600930 } else if (pos->first.begin < range.begin) {
931 // Trim the beginning if needed
932 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
933 ++pos;
934 }
935
936 const auto the_end = accesses->end();
937 while ((pos != the_end) && pos->first.intersects(range)) {
938 if (pos->first.end > range.end) {
939 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
940 }
941
942 pos = action(accesses, pos);
943 if (pos == the_end) break;
944
945 auto next = pos;
946 ++next;
947 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
948 // Need to infill if next is disjoint
949 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -0700950 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -0600951 next = action.Infill(accesses, next, new_range);
952 }
953 pos = next;
954 }
955}
956
957struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700958 using Iterator = ResourceAccessRangeMap::iterator;
959 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600960 // this is only called on gaps, and never returns a gap.
961 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -0600962 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600963 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -0600964 }
John Zulauf5f13a792020-03-10 07:31:21 -0600965
John Zulauf5c5e88d2019-12-26 11:22:02 -0700966 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -0600967 auto &access_state = pos->second;
968 access_state.Update(usage, tag);
969 return pos;
970 }
971
John Zulauf16adfc92020-04-08 10:28:33 -0600972 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -0600973 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -0600974 : type(type_), context(context_), usage(usage_), tag(tag_) {}
975 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -0600976 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -0600977 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -0600978 const ResourceUsageTag &tag;
979};
980
981struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700982 using Iterator = ResourceAccessRangeMap::iterator;
983 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -0600984
John Zulauf5c5e88d2019-12-26 11:22:02 -0700985 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -0600986 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700987 access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -0600988 return pos;
989 }
990
John Zulauf36bcf6a2020-02-03 15:12:52 -0700991 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
992 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
993 : src_exec_scope(src_exec_scope_),
994 src_access_scope(src_access_scope_),
995 dst_exec_scope(dst_exec_scope_),
996 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -0600997
John Zulauf36bcf6a2020-02-03 15:12:52 -0700998 VkPipelineStageFlags src_exec_scope;
999 SyncStageAccessFlags src_access_scope;
1000 VkPipelineStageFlags dst_exec_scope;
1001 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001002};
1003
1004struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001005 using Iterator = ResourceAccessRangeMap::iterator;
1006 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001007
John Zulauf5c5e88d2019-12-26 11:22:02 -07001008 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001009 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001010 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001011
1012 for (const auto &functor : barrier_functor) {
1013 functor(accesses, pos);
1014 }
1015 return pos;
1016 }
1017
John Zulauf36bcf6a2020-02-03 15:12:52 -07001018 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
1019 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -06001020 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001021 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001022 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
1023 barrier_functor.reserve(memoryBarrierCount);
1024 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
1025 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -07001026 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
1027 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -06001028 }
1029 }
1030
John Zulauf36bcf6a2020-02-03 15:12:52 -07001031 const VkPipelineStageFlags src_exec_scope;
1032 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001033 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
1034};
1035
John Zulauf355e49b2020-04-24 15:11:15 -06001036void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1037 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001038 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1039 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001040}
1041
John Zulauf16adfc92020-04-08 10:28:33 -06001042void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001043 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001044 if (!SimpleBinding(buffer)) return;
1045 const auto base_address = ResourceBaseAddress(buffer);
1046 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1047}
John Zulauf355e49b2020-04-24 15:11:15 -06001048
John Zulauf540266b2020-04-06 18:54:53 -06001049void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001050 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001051 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001052 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001053 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001054 const auto address_type = ImageAddressType(image);
1055 const auto base_address = ResourceBaseAddress(image);
1056 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001057 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001058 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001059 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001060}
John Zulauf7635de32020-05-29 17:14:15 -06001061void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1062 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1063 if (view != nullptr) {
1064 const IMAGE_STATE *image = view->image_state.get();
1065 if (image != nullptr) {
1066 auto *update_range = &view->normalized_subresource_range;
1067 VkImageSubresourceRange masked_range;
1068 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1069 masked_range = view->normalized_subresource_range;
1070 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1071 update_range = &masked_range;
1072 }
1073 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1074 }
1075 }
1076}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001077
John Zulauf355e49b2020-04-24 15:11:15 -06001078void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1079 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1080 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001081 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1082 subresource.layerCount};
1083 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1084}
1085
John Zulauf540266b2020-04-06 18:54:53 -06001086template <typename Action>
1087void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001088 if (!SimpleBinding(buffer)) return;
1089 const auto base_address = ResourceBaseAddress(buffer);
1090 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001091}
1092
1093template <typename Action>
1094void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1095 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001096 if (!SimpleBinding(image)) return;
1097 const auto address_type = ImageAddressType(image);
1098 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001099
locke-lunargae26eac2020-04-16 15:29:05 -06001100 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001101 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001102
John Zulauf16adfc92020-04-08 10:28:33 -06001103 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001104 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001105 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001106 }
1107}
1108
John Zulauf7635de32020-05-29 17:14:15 -06001109void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1110 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1111 const ResourceUsageTag &tag) {
1112 UpdateStateResolveAction update(*this, tag);
1113 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1114}
1115
John Zulaufaff20662020-06-01 14:07:58 -06001116void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1117 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1118 const ResourceUsageTag &tag) {
1119 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1120 VkExtent3D extent = CastTo3D(render_area.extent);
1121 VkOffset3D offset = CastTo3D(render_area.offset);
1122
1123 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1124 if (rp_state.attachment_last_subpass[i] == subpass) {
1125 if (attachment_views[i] == nullptr) continue; // UNUSED
1126 const auto &view = *attachment_views[i];
1127 const IMAGE_STATE *image = view.image_state.get();
1128 if (image == nullptr) continue;
1129
1130 const auto &ci = attachment_ci[i];
1131 const bool has_depth = FormatHasDepth(ci.format);
1132 const bool has_stencil = FormatHasStencil(ci.format);
1133 const bool is_color = !(has_depth || has_stencil);
1134 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1135
1136 if (is_color && store_op_stores) {
1137 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1138 offset, extent, tag);
1139 } else {
1140 auto update_range = view.normalized_subresource_range;
1141 if (has_depth && store_op_stores) {
1142 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1143 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1144 tag);
1145 }
1146 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1147 if (has_stencil && stencil_op_stores) {
1148 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1149 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1150 tag);
1151 }
1152 }
1153 }
1154 }
1155}
1156
John Zulauf540266b2020-04-06 18:54:53 -06001157template <typename Action>
1158void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1159 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001160 for (const auto address_type : kAddressTypes) {
1161 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001162 }
1163}
1164
1165void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001166 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1167 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -06001168 for (const auto address_type : kAddressTypes) {
John Zulauf355e49b2020-04-24 15:11:15 -06001169 context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier,
1170 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001171 }
1172 }
1173}
1174
John Zulauf355e49b2020-04-24 15:11:15 -06001175void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1176 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1177 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
1178 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1179 UpdateMemoryAccess(image, subresource_range, barrier_action);
1180}
1181
John Zulauf7635de32020-05-29 17:14:15 -06001182// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001183void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1184 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1185 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
1186 bool layout_transition, const ResourceUsageTag &tag) {
1187 if (layout_transition) {
1188 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
1189 tag);
1190 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
1191 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -06001192 } else {
1193 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -06001194 }
John Zulauf355e49b2020-04-24 15:11:15 -06001195}
1196
John Zulauf7635de32020-05-29 17:14:15 -06001197// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001198void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
1199 const VkImageSubresourceRange &subresource_range, bool layout_transition,
1200 const ResourceUsageTag &tag) {
1201 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
1202 subresource_range, layout_transition, tag);
1203}
1204
1205// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001206HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001207 if (!attach_view) return HazardResult();
1208 const auto image_state = attach_view->image_state.get();
1209 if (!image_state) return HazardResult();
1210
John Zulauf355e49b2020-04-24 15:11:15 -06001211 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001212 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001213
1214 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulauf7635de32020-05-29 17:14:15 -06001215 auto hazard = track_back.context->DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope,
1216 track_back.barrier.src_access_scope,
1217 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001218 if (!hazard.hazard) {
1219 // The Async hazard check is against the current context's async set.
John Zulauf7635de32020-05-29 17:14:15 -06001220 hazard = DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope, track_back.barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001221 attach_view->normalized_subresource_range, kDetectAsync);
1222 }
1223 return hazard;
1224}
1225
1226// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1227bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1228
1229 const VkRenderPassBeginInfo *pRenderPassBegin,
1230 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1231 const char *func_name) const {
1232 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1233 bool skip = false;
1234 uint32_t subpass = 0;
1235 const auto &transitions = rp_state.subpass_transitions[subpass];
1236 if (transitions.size()) {
1237 const std::vector<AccessContext> empty_context_vector;
1238 // Create context we can use to validate against...
1239 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1240 const_cast<AccessContext *>(&cb_access_context_));
1241
1242 assert(pRenderPassBegin);
1243 if (nullptr == pRenderPassBegin) return skip;
1244
1245 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1246 assert(fb_state);
1247 if (nullptr == fb_state) return skip;
1248
1249 // Create a limited array of views (which we'll need to toss
1250 std::vector<const IMAGE_VIEW_STATE *> views;
1251 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
1252 const auto attachment_count = count_attachment.first;
1253 const auto *attachments = count_attachment.second;
1254 views.resize(attachment_count, nullptr);
1255 for (const auto &transition : transitions) {
1256 assert(transition.attachment < attachment_count);
1257 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
1258 }
1259
John Zulauf7635de32020-05-29 17:14:15 -06001260 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
1261 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001262 }
1263 return skip;
1264}
1265
1266bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001267 bool skip = false;
John Zulauf1507ee42020-05-18 11:33:09 -06001268 skip |=
1269 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001270
1271 return skip;
1272}
1273
1274bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1275 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001276 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001277 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -06001278 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1279 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001280
1281 return skip;
1282}
1283
1284void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1285 assert(sync_state_);
1286 if (!cb_state_) return;
1287
1288 // Create an access context the current renderpass.
1289 render_pass_contexts_.emplace_back(&cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06001290 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf355e49b2020-04-24 15:11:15 -06001291 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001292 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001293}
1294
John Zulauf355e49b2020-04-24 15:11:15 -06001295void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001296 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001297 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001298 current_context_ = &current_renderpass_context_->CurrentContext();
1299}
1300
John Zulauf355e49b2020-04-24 15:11:15 -06001301void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001302 assert(current_renderpass_context_);
1303 if (!current_renderpass_context_) return;
1304
John Zulauf7635de32020-05-29 17:14:15 -06001305 current_renderpass_context_->RecordEndRenderPass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001306 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001307 current_renderpass_context_ = nullptr;
1308}
1309
John Zulauf1507ee42020-05-18 11:33:09 -06001310bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1311 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001312 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001313 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001314 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1315 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001316 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1317 func_name);
1318
John Zulauf355e49b2020-04-24 15:11:15 -06001319 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06001320 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06001321 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1322 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1323 return skip;
1324}
1325bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
1326 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001327 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06001328 bool skip = false;
1329 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1330 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001331 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1332 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06001333 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001334 return skip;
1335}
1336
John Zulauf7635de32020-05-29 17:14:15 -06001337AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
1338 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
1339}
1340
1341bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
1342 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001343 bool skip = false;
1344
John Zulauf7635de32020-05-29 17:14:15 -06001345 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
1346 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
1347 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1348 // to apply and only copy then, if this proves a hot spot.
1349 std::unique_ptr<AccessContext> proxy_for_current;
1350
John Zulauf355e49b2020-04-24 15:11:15 -06001351 // Validate the "finalLayout" transitions to external
1352 // Get them from where there we're hidding in the extra entry.
1353 const auto &final_transitions = rp_state_->subpass_transitions.back();
1354 for (const auto &transition : final_transitions) {
1355 const auto &attach_view = attachment_views_[transition.attachment];
1356 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
1357 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06001358 auto *context = trackback.context;
1359
1360 if (transition.prev_pass == current_subpass_) {
1361 if (!proxy_for_current) {
1362 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
1363 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
1364 }
1365 context = proxy_for_current.get();
1366 }
1367
1368 auto hazard = context->DetectImageBarrierHazard(
John Zulauf355e49b2020-04-24 15:11:15 -06001369 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
1370 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
1371 if (hazard.hazard) {
1372 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
1373 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
1374 " final image layout transition.",
1375 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment);
1376 }
1377 }
1378 return skip;
1379}
1380
1381void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
1382 // Add layout transitions...
1383 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
1384 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06001385 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06001386 for (const auto &transition : transitions) {
1387 const auto attachment_view = attachment_views_[transition.attachment];
1388 if (!attachment_view) continue;
1389 const auto image = attachment_view->image_state.get();
1390 if (!image) continue;
1391
1392 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -06001393 auto insert_pair = view_seen.insert(attachment_view);
1394 if (insert_pair.second) {
1395 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
1396 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
1397
1398 } else {
1399 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
1400 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
1401 auto barrier_to_transition = barrier->barrier;
1402 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
1403 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
1404 }
John Zulauf355e49b2020-04-24 15:11:15 -06001405 }
1406}
1407
John Zulauf1507ee42020-05-18 11:33:09 -06001408void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
1409 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
1410 auto &subpass_context = subpass_contexts_[current_subpass_];
1411 VkExtent3D extent = CastTo3D(render_area.extent);
1412 VkOffset3D offset = CastTo3D(render_area.offset);
1413
1414 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
1415 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
1416 if (attachment_views_[i] == nullptr) continue; // UNUSED
1417 const auto &view = *attachment_views_[i];
1418 const IMAGE_STATE *image = view.image_state.get();
1419 if (image == nullptr) continue;
1420
1421 const auto &ci = attachment_ci[i];
1422 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001423 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001424 const bool is_color = !(has_depth || has_stencil);
1425
1426 if (is_color) {
1427 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
1428 extent, tag);
1429 } else {
1430 auto update_range = view.normalized_subresource_range;
1431 if (has_depth) {
1432 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1433 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
1434 }
1435 if (has_stencil) {
1436 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1437 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
1438 tag);
1439 }
1440 }
1441 }
1442 }
1443}
1444
John Zulauf355e49b2020-04-24 15:11:15 -06001445void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
1446 VkQueueFlags queue_flags, const ResourceUsageTag &tag) {
1447 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06001448 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06001449 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
1450 // Add this for all subpasses here so that they exsist during next subpass validation
1451 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
1452 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context_);
1453 }
1454 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
1455
1456 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06001457 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001458}
John Zulauf1507ee42020-05-18 11:33:09 -06001459
1460void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001461 // Resolves are against *prior* subpass context and thus *before* the subpass increment
1462 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001463 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06001464
John Zulauf355e49b2020-04-24 15:11:15 -06001465 current_subpass_++;
1466 assert(current_subpass_ < subpass_contexts_.size());
1467 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06001468 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001469}
1470
John Zulauf7635de32020-05-29 17:14:15 -06001471void RenderPassAccessContext::RecordEndRenderPass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001472 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06001473 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001474 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06001475
John Zulauf355e49b2020-04-24 15:11:15 -06001476 // Export the accesses from the renderpass...
1477 external_context_->ResolveChildContexts(subpass_contexts_);
1478
1479 // Add the "finalLayout" transitions to external
1480 // Get them from where there we're hidding in the extra entry.
1481 const auto &final_transitions = rp_state_->subpass_transitions.back();
1482 for (const auto &transition : final_transitions) {
1483 const auto &attachment = attachment_views_[transition.attachment];
1484 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
1485 assert(external_context_ == last_trackback.context);
1486 external_context_->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
1487 attachment->normalized_subresource_range, true, tag);
1488 }
1489}
1490
John Zulauf3d84f1b2020-03-09 13:33:25 -06001491SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
1492 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
1493 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
1494 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
1495 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
1496 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
1497 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
1498}
1499
1500void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
1501 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
1502 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
1503}
1504
John Zulauf9cb530d2019-09-30 14:14:10 -06001505HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
1506 HazardResult hazard;
1507 auto usage = FlagBit(usage_index);
1508 if (IsRead(usage)) {
John Zulaufc9201222020-05-13 15:13:03 -06001509 if (last_write && IsWriteHazard(usage)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001510 hazard.Set(READ_AFTER_WRITE, write_tag);
1511 }
1512 } else {
1513 // Assume write
1514 // TODO determine what to do with READ-WRITE usage states if any
1515 // Write-After-Write check -- if we have a previous write to test against
1516 if (last_write && IsWriteHazard(usage)) {
1517 hazard.Set(WRITE_AFTER_WRITE, write_tag);
1518 } else {
John Zulauf69133422020-05-20 14:55:53 -06001519 // Look for casus belli for WAR
John Zulauf9cb530d2019-09-30 14:14:10 -06001520 const auto usage_stage = PipelineStageBit(usage_index);
1521 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1522 if (IsReadHazard(usage_stage, last_reads[read_index])) {
1523 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
1524 break;
1525 }
1526 }
1527 }
1528 }
1529 return hazard;
1530}
1531
John Zulauf69133422020-05-20 14:55:53 -06001532HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
1533 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
1534 HazardResult hazard;
1535 const auto usage = FlagBit(usage_index);
1536 const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good.
1537 if (IsRead(usage)) {
1538 if (!write_is_ordered && IsWriteHazard(usage)) {
1539 hazard.Set(READ_AFTER_WRITE, write_tag);
1540 }
1541 } else {
1542 if (!write_is_ordered && IsWriteHazard(usage)) {
1543 hazard.Set(WRITE_AFTER_WRITE, write_tag);
1544 } else {
1545 const auto usage_stage = PipelineStageBit(usage_index);
1546 const auto unordered_reads = last_read_stages & ~ordering.exec_scope;
1547 if (unordered_reads) {
1548 // Look for any WAR hazards outside the ordered set of stages
1549 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1550 if (last_reads[read_index].stage & unordered_reads) {
1551 if (IsReadHazard(usage_stage, last_reads[read_index])) {
1552 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
1553 break;
1554 }
1555 }
1556 }
1557 }
1558 }
1559 }
1560 return hazard;
1561}
1562
John Zulauf2f952d22020-02-10 11:34:51 -07001563// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06001564HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07001565 HazardResult hazard;
1566 auto usage = FlagBit(usage_index);
1567 if (IsRead(usage)) {
1568 if (last_write != 0) {
1569 hazard.Set(READ_RACING_WRITE, write_tag);
1570 }
1571 } else {
1572 if (last_write != 0) {
1573 hazard.Set(WRITE_RACING_WRITE, write_tag);
1574 } else if (last_read_count > 0) {
1575 hazard.Set(WRITE_RACING_READ, last_reads[0].tag);
1576 }
1577 }
1578 return hazard;
1579}
1580
John Zulauf36bcf6a2020-02-03 15:12:52 -07001581HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1582 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07001583 // Only supporting image layout transitions for now
1584 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
1585 HazardResult hazard;
1586 if (last_write) {
1587 // If the previous write is *not* in the 1st access scope
1588 // *AND* the current barrier is not in the dependency chain
1589 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
1590 // then the barrier access is unsafe (R/W after W)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001591 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
John Zulauf0cb5be22020-01-23 12:18:22 -07001592 // TODO: Do we need a difference hazard name for this?
1593 hazard.Set(WRITE_AFTER_WRITE, write_tag);
1594 }
John Zulauf355e49b2020-04-24 15:11:15 -06001595 }
1596 if (!hazard.hazard) {
1597 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07001598 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07001599 const auto &read_access = last_reads[read_index];
1600 // If the read stage is not in the src sync sync
1601 // *AND* not execution chained with an existing sync barrier (that's the or)
1602 // then the barrier access is unsafe (R/W after R)
1603 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
1604 hazard.Set(WRITE_AFTER_READ, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07001605 break;
1606 }
1607 }
1608 }
1609 return hazard;
1610}
1611
John Zulauf5f13a792020-03-10 07:31:21 -06001612// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
1613// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
1614// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
1615void ResourceAccessState::Resolve(const ResourceAccessState &other) {
1616 if (write_tag.IsBefore(other.write_tag)) {
1617 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
1618 *this = other;
1619 } else if (!other.write_tag.IsBefore(write_tag)) {
1620 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
1621 // dependency chaining logic or any stage expansion)
1622 write_barriers |= other.write_barriers;
1623
1624 // Merge that read states
1625 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
1626 auto &other_read = other.last_reads[other_read_index];
1627 if (last_read_stages & other_read.stage) {
1628 // Merge in the barriers for read stages that exist in *both* this and other
1629 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
1630 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
1631 auto &my_read = last_reads[my_read_index];
1632 if (other_read.stage == my_read.stage) {
1633 if (my_read.tag.IsBefore(other_read.tag)) {
1634 my_read.tag = other_read.tag;
1635 }
1636 my_read.barriers |= other_read.barriers;
1637 break;
1638 }
1639 }
1640 } else {
1641 // The other read stage doesn't exist in this, so add it.
1642 last_reads[last_read_count] = other_read;
1643 last_read_count++;
1644 last_read_stages |= other_read.stage;
1645 }
1646 }
1647 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
1648 // it.
1649}
1650
John Zulauf9cb530d2019-09-30 14:14:10 -06001651void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
1652 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
1653 const auto usage_bit = FlagBit(usage_index);
1654 if (IsRead(usage_index)) {
1655 // Mulitple outstanding reads may be of interest and do dependency chains independently
1656 // However, for purposes of barrier tracking, only one read per pipeline stage matters
1657 const auto usage_stage = PipelineStageBit(usage_index);
1658 if (usage_stage & last_read_stages) {
1659 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1660 ReadState &access = last_reads[read_index];
1661 if (access.stage == usage_stage) {
1662 access.barriers = 0;
1663 access.tag = tag;
1664 break;
1665 }
1666 }
1667 } else {
1668 // We don't have this stage in the list yet...
1669 assert(last_read_count < last_reads.size());
1670 ReadState &access = last_reads[last_read_count++];
1671 access.stage = usage_stage;
1672 access.barriers = 0;
1673 access.tag = tag;
1674 last_read_stages |= usage_stage;
1675 }
1676 } else {
1677 // Assume write
1678 // TODO determine what to do with READ-WRITE operations if any
1679 // Clobber last read and both sets of barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
1680 // if the last_reads/last_write were unsafe, we've reported them,
1681 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
1682 last_read_count = 0;
1683 last_read_stages = 0;
1684
1685 write_barriers = 0;
1686 write_dependency_chain = 0;
1687 write_tag = tag;
1688 last_write = usage_bit;
1689 }
1690}
John Zulauf5f13a792020-03-10 07:31:21 -06001691
John Zulauf9cb530d2019-09-30 14:14:10 -06001692void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
1693 // Execution Barriers only protect read operations
1694 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1695 ReadState &access = last_reads[read_index];
1696 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
1697 if (srcStageMask & (access.stage | access.barriers)) {
1698 access.barriers |= dstStageMask;
1699 }
1700 }
1701 if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask;
1702}
1703
John Zulauf36bcf6a2020-02-03 15:12:52 -07001704void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
1705 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001706 // Assuming we've applied the execution side of this barrier, we update just the write
1707 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07001708 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
1709 write_barriers |= dst_access_scope;
1710 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001711 }
1712}
1713
John Zulaufd1f85d42020-04-15 12:23:15 -06001714void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001715 auto *access_context = GetAccessContextNoInsert(command_buffer);
1716 if (access_context) {
1717 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06001718 }
1719}
1720
John Zulaufd1f85d42020-04-15 12:23:15 -06001721void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
1722 auto access_found = cb_access_state.find(command_buffer);
1723 if (access_found != cb_access_state.end()) {
1724 access_found->second->Reset();
1725 cb_access_state.erase(access_found);
1726 }
1727}
1728
John Zulauf540266b2020-04-06 18:54:53 -06001729void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07001730 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
1731 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06001732 const VkMemoryBarrier *pMemoryBarriers) {
1733 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07001734 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06001735 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06001736 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06001737}
1738
John Zulauf540266b2020-04-06 18:54:53 -06001739void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07001740 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
1741 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06001742 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001743 for (uint32_t index = 0; index < barrier_count; index++) {
locke-lunarg3c038002020-04-30 23:08:08 -06001744 auto barrier = barriers[index];
John Zulauf9cb530d2019-09-30 14:14:10 -06001745 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
1746 if (!buffer) continue;
locke-lunarg3c038002020-04-30 23:08:08 -06001747 barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size);
John Zulauf16adfc92020-04-08 10:28:33 -06001748 ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06001749 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
1750 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1751 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1752 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06001753 }
1754}
1755
John Zulauf540266b2020-04-06 18:54:53 -06001756void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
1757 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
1758 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06001759 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001760 for (uint32_t index = 0; index < barrier_count; index++) {
1761 const auto &barrier = barriers[index];
1762 const auto *image = Get<IMAGE_STATE>(barrier.image);
1763 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06001764 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06001765 bool layout_transition = barrier.oldLayout != barrier.newLayout;
1766 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
1767 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1768 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
1769 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001770 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001771}
1772
1773bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
1774 uint32_t regionCount, const VkBufferCopy *pRegions) const {
1775 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001776 const auto *cb_context = GetAccessContext(commandBuffer);
1777 assert(cb_context);
1778 if (!cb_context) return skip;
1779 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06001780
John Zulauf3d84f1b2020-03-09 13:33:25 -06001781 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06001782 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001783 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001784
1785 for (uint32_t region = 0; region < regionCount; region++) {
1786 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06001787 if (src_buffer) {
locke-lunarg3c038002020-04-30 23:08:08 -06001788 ResourceAccessRange src_range =
1789 MakeRange(copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06001790 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001791 if (hazard.hazard) {
1792 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06001793 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
1794 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1795 report_data->FormatHandle(srcBuffer).c_str(), region);
John Zulauf9cb530d2019-09-30 14:14:10 -06001796 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001797 }
John Zulauf16adfc92020-04-08 10:28:33 -06001798 if (dst_buffer && !skip) {
locke-lunarg3c038002020-04-30 23:08:08 -06001799 ResourceAccessRange dst_range =
1800 MakeRange(copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf355e49b2020-04-24 15:11:15 -06001801 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001802 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001803 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
1804 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1805 report_data->FormatHandle(dstBuffer).c_str(), region);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001806 }
1807 }
1808 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06001809 }
1810 return skip;
1811}
1812
1813void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
1814 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001815 auto *cb_context = GetAccessContext(commandBuffer);
1816 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001817 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001818 auto *context = cb_context->GetCurrentAccessContext();
1819
John Zulauf9cb530d2019-09-30 14:14:10 -06001820 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06001821 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06001822
1823 for (uint32_t region = 0; region < regionCount; region++) {
1824 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06001825 if (src_buffer) {
locke-lunarg3c038002020-04-30 23:08:08 -06001826 ResourceAccessRange src_range =
1827 MakeRange(copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06001828 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001829 }
John Zulauf16adfc92020-04-08 10:28:33 -06001830 if (dst_buffer) {
locke-lunarg3c038002020-04-30 23:08:08 -06001831 ResourceAccessRange dst_range =
1832 MakeRange(copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06001833 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001834 }
1835 }
1836}
1837
1838bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1839 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1840 const VkImageCopy *pRegions) const {
1841 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001842 const auto *cb_access_context = GetAccessContext(commandBuffer);
1843 assert(cb_access_context);
1844 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07001845
John Zulauf3d84f1b2020-03-09 13:33:25 -06001846 const auto *context = cb_access_context->GetCurrentAccessContext();
1847 assert(context);
1848 if (!context) return skip;
1849
1850 const auto *src_image = Get<IMAGE_STATE>(srcImage);
1851 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001852 for (uint32_t region = 0; region < regionCount; region++) {
1853 const auto &copy_region = pRegions[region];
1854 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001855 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001856 copy_region.srcOffset, copy_region.extent);
1857 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001858 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
1859 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1860 report_data->FormatHandle(srcImage).c_str(), region);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001861 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001862 }
1863
1864 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07001865 VkExtent3D dst_copy_extent =
1866 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001867 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07001868 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001869 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001870 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
1871 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1872 report_data->FormatHandle(dstImage).c_str(), region);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001873 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07001874 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07001875 }
1876 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001877
John Zulauf5c5e88d2019-12-26 11:22:02 -07001878 return skip;
1879}
1880
1881void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1882 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1883 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001884 auto *cb_access_context = GetAccessContext(commandBuffer);
1885 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001886 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001887 auto *context = cb_access_context->GetCurrentAccessContext();
1888 assert(context);
1889
John Zulauf5c5e88d2019-12-26 11:22:02 -07001890 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001891 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001892
1893 for (uint32_t region = 0; region < regionCount; region++) {
1894 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06001895 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001896 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
1897 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001898 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001899 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07001900 VkExtent3D dst_copy_extent =
1901 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001902 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
1903 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001904 }
1905 }
1906}
1907
1908bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1909 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1910 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1911 uint32_t bufferMemoryBarrierCount,
1912 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1913 uint32_t imageMemoryBarrierCount,
1914 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
1915 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001916 const auto *cb_access_context = GetAccessContext(commandBuffer);
1917 assert(cb_access_context);
1918 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07001919
John Zulauf3d84f1b2020-03-09 13:33:25 -06001920 const auto *context = cb_access_context->GetCurrentAccessContext();
1921 assert(context);
1922 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07001923
John Zulauf3d84f1b2020-03-09 13:33:25 -06001924 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07001925 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
1926 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07001927 // Validate Image Layout transitions
1928 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
1929 const auto &barrier = pImageMemoryBarriers[index];
1930 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
1931 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
1932 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06001933 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07001934 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06001935 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06001936 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
1937 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s", string_SyncHazard(hazard.hazard),
1938 index, report_data->FormatHandle(barrier.image).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07001939 }
1940 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001941
1942 return skip;
1943}
1944
1945void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1946 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1947 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1948 uint32_t bufferMemoryBarrierCount,
1949 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1950 uint32_t imageMemoryBarrierCount,
1951 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001952 auto *cb_access_context = GetAccessContext(commandBuffer);
1953 assert(cb_access_context);
1954 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06001955 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001956 auto access_context = cb_access_context->GetCurrentAccessContext();
1957 assert(access_context);
1958 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06001959
John Zulauf3d84f1b2020-03-09 13:33:25 -06001960 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07001961 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001962 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07001963 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
1964 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
1965 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001966 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
1967 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06001968 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001969 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001970
1971 // Apply these last in-case there operation is a superset of the other two and would clean them up...
John Zulauf3d84f1b2020-03-09 13:33:25 -06001972 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07001973 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06001974}
1975
1976void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
1977 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
1978 // The state tracker sets up the device state
1979 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
1980
John Zulauf5f13a792020-03-10 07:31:21 -06001981 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
1982 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06001983 // TODO: Find a good way to do this hooklessly.
1984 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
1985 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
1986 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
1987
John Zulaufd1f85d42020-04-15 12:23:15 -06001988 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
1989 sync_device_state->ResetCommandBufferCallback(command_buffer);
1990 });
1991 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
1992 sync_device_state->FreeCommandBufferCallback(command_buffer);
1993 });
John Zulauf9cb530d2019-09-30 14:14:10 -06001994}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001995
John Zulauf355e49b2020-04-24 15:11:15 -06001996bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
1997 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
1998 bool skip = false;
1999 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2000 auto cb_context = GetAccessContext(commandBuffer);
2001
2002 if (rp_state && cb_context) {
2003 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2004 }
2005
2006 return skip;
2007}
2008
2009bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2010 VkSubpassContents contents) const {
2011 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2012 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2013 subpass_begin_info.contents = contents;
2014 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
2015 return skip;
2016}
2017
2018bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2019 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2020 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2021 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
2022 return skip;
2023}
2024
2025bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2026 const VkRenderPassBeginInfo *pRenderPassBegin,
2027 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2028 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2029 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
2030 return skip;
2031}
2032
John Zulauf3d84f1b2020-03-09 13:33:25 -06002033void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
2034 VkResult result) {
2035 // The state tracker sets up the command buffer state
2036 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
2037
2038 // Create/initialize the structure that trackers accesses at the command buffer scope.
2039 auto cb_access_context = GetAccessContext(commandBuffer);
2040 assert(cb_access_context);
2041 cb_access_context->Reset();
2042}
2043
2044void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06002045 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002046 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002047 if (cb_context) {
2048 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002049 }
2050}
2051
2052void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2053 VkSubpassContents contents) {
2054 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2055 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2056 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002057 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002058}
2059
2060void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2061 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2062 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002063 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002064}
2065
2066void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2067 const VkRenderPassBeginInfo *pRenderPassBegin,
2068 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2069 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002070 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
2071}
2072
2073bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2074 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
2075 bool skip = false;
2076
2077 auto cb_context = GetAccessContext(commandBuffer);
2078 assert(cb_context);
2079 auto cb_state = cb_context->GetCommandBufferState();
2080 if (!cb_state) return skip;
2081
2082 auto rp_state = cb_state->activeRenderPass;
2083 if (!rp_state) return skip;
2084
2085 skip |= cb_context->ValidateNextSubpass(func_name);
2086
2087 return skip;
2088}
2089
2090bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
2091 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
2092 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2093 subpass_begin_info.contents = contents;
2094 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
2095 return skip;
2096}
2097
2098bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2099 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2100 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2101 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
2102 return skip;
2103}
2104
2105bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2106 const VkSubpassEndInfo *pSubpassEndInfo) const {
2107 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2108 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
2109 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002110}
2111
2112void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06002113 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002114 auto cb_context = GetAccessContext(commandBuffer);
2115 assert(cb_context);
2116 auto cb_state = cb_context->GetCommandBufferState();
2117 if (!cb_state) return;
2118
2119 auto rp_state = cb_state->activeRenderPass;
2120 if (!rp_state) return;
2121
John Zulauf355e49b2020-04-24 15:11:15 -06002122 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002123}
2124
2125void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
2126 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
2127 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2128 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002129 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002130}
2131
2132void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2133 const VkSubpassEndInfo *pSubpassEndInfo) {
2134 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002135 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002136}
2137
2138void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2139 const VkSubpassEndInfo *pSubpassEndInfo) {
2140 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002141 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002142}
2143
John Zulauf355e49b2020-04-24 15:11:15 -06002144bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
2145 const char *func_name) const {
2146 bool skip = false;
2147
2148 auto cb_context = GetAccessContext(commandBuffer);
2149 assert(cb_context);
2150 auto cb_state = cb_context->GetCommandBufferState();
2151 if (!cb_state) return skip;
2152
2153 auto rp_state = cb_state->activeRenderPass;
2154 if (!rp_state) return skip;
2155
2156 skip |= cb_context->ValidateEndRenderpass(func_name);
2157 return skip;
2158}
2159
2160bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2161 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
2162 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
2163 return skip;
2164}
2165
2166bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
2167 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2168 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2169 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
2170 return skip;
2171}
2172
2173bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
2174 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2175 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2176 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
2177 return skip;
2178}
2179
2180void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
2181 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06002182 // Resolve the all subpass contexts to the command buffer contexts
2183 auto cb_context = GetAccessContext(commandBuffer);
2184 assert(cb_context);
2185 auto cb_state = cb_context->GetCommandBufferState();
2186 if (!cb_state) return;
2187
locke-lunargaecf2152020-05-12 17:15:41 -06002188 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06002189 if (!rp_state) return;
2190
John Zulauf355e49b2020-04-24 15:11:15 -06002191 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06002192}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002193
2194void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
2195 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002196 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002197}
2198
2199void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
2200 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002201 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002202}
2203
2204void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
2205 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002206 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002207}
locke-lunarga19c71d2020-03-02 18:17:04 -07002208
2209bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2210 VkImageLayout dstImageLayout, uint32_t regionCount,
2211 const VkBufferImageCopy *pRegions) const {
2212 bool skip = false;
2213 const auto *cb_access_context = GetAccessContext(commandBuffer);
2214 assert(cb_access_context);
2215 if (!cb_access_context) return skip;
2216
2217 const auto *context = cb_access_context->GetCurrentAccessContext();
2218 assert(context);
2219 if (!context) return skip;
2220
2221 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07002222 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2223
2224 for (uint32_t region = 0; region < regionCount; region++) {
2225 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002226 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002227 ResourceAccessRange src_range =
2228 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002229 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002230 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002231 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002232 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
2233 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07002234 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region);
2235 }
2236 }
2237 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002238 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002239 copy_region.imageOffset, copy_region.imageExtent);
2240 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002241 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
2242 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07002243 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region);
2244 }
2245 if (skip) break;
2246 }
2247 if (skip) break;
2248 }
2249 return skip;
2250}
2251
2252void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2253 VkImageLayout dstImageLayout, uint32_t regionCount,
2254 const VkBufferImageCopy *pRegions) {
2255 auto *cb_access_context = GetAccessContext(commandBuffer);
2256 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002257 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002258 auto *context = cb_access_context->GetCurrentAccessContext();
2259 assert(context);
2260
2261 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06002262 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002263
2264 for (uint32_t region = 0; region < regionCount; region++) {
2265 const auto &copy_region = pRegions[region];
2266 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002267 ResourceAccessRange src_range =
2268 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002269 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002270 }
2271 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002272 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002273 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002274 }
2275 }
2276}
2277
2278bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
2279 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
2280 const VkBufferImageCopy *pRegions) const {
2281 bool skip = false;
2282 const auto *cb_access_context = GetAccessContext(commandBuffer);
2283 assert(cb_access_context);
2284 if (!cb_access_context) return skip;
2285
2286 const auto *context = cb_access_context->GetCurrentAccessContext();
2287 assert(context);
2288 if (!context) return skip;
2289
2290 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2291 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2292 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
2293 for (uint32_t region = 0; region < regionCount; region++) {
2294 const auto &copy_region = pRegions[region];
2295 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002296 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002297 copy_region.imageOffset, copy_region.imageExtent);
2298 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002299 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
2300 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07002301 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region);
2302 }
2303 }
2304 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06002305 ResourceAccessRange dst_range =
2306 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002307 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002308 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002309 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
2310 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07002311 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region);
2312 }
2313 }
2314 if (skip) break;
2315 }
2316 return skip;
2317}
2318
2319void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2320 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
2321 auto *cb_access_context = GetAccessContext(commandBuffer);
2322 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002323 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07002324 auto *context = cb_access_context->GetCurrentAccessContext();
2325 assert(context);
2326
2327 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002328 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2329 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06002330 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07002331
2332 for (uint32_t region = 0; region < regionCount; region++) {
2333 const auto &copy_region = pRegions[region];
2334 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002335 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002336 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002337 }
2338 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002339 ResourceAccessRange dst_range =
2340 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002341 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002342 }
2343 }
2344}
2345
2346bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2347 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2348 const VkImageBlit *pRegions, VkFilter filter) const {
2349 bool skip = false;
2350 const auto *cb_access_context = GetAccessContext(commandBuffer);
2351 assert(cb_access_context);
2352 if (!cb_access_context) return skip;
2353
2354 const auto *context = cb_access_context->GetCurrentAccessContext();
2355 assert(context);
2356 if (!context) return skip;
2357
2358 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2359 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2360
2361 for (uint32_t region = 0; region < regionCount; region++) {
2362 const auto &blit_region = pRegions[region];
2363 if (src_image) {
2364 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
2365 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
2366 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002367 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002368 blit_region.srcOffsets[0], extent);
2369 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002370 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
2371 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
2372 report_data->FormatHandle(srcImage).c_str(), region);
locke-lunarga19c71d2020-03-02 18:17:04 -07002373 }
2374 }
2375
2376 if (dst_image) {
2377 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
2378 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
2379 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002380 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002381 blit_region.dstOffsets[0], extent);
2382 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002383 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
2384 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
2385 report_data->FormatHandle(dstImage).c_str(), region);
locke-lunarga19c71d2020-03-02 18:17:04 -07002386 }
2387 if (skip) break;
2388 }
2389 }
2390
2391 return skip;
2392}
2393
2394void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2395 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2396 const VkImageBlit *pRegions, VkFilter filter) {
2397 auto *cb_access_context = GetAccessContext(commandBuffer);
2398 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002399 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002400 auto *context = cb_access_context->GetCurrentAccessContext();
2401 assert(context);
2402
2403 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002404 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002405
2406 for (uint32_t region = 0; region < regionCount; region++) {
2407 const auto &blit_region = pRegions[region];
2408 if (src_image) {
2409 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
2410 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
2411 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002412 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002413 blit_region.srcOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002414 }
2415 if (dst_image) {
2416 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
2417 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
2418 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002419 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002420 blit_region.dstOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002421 }
2422 }
2423}
locke-lunarg36ba2592020-04-03 09:42:04 -06002424
2425bool SyncValidator::DetectDescriptorSetHazard(const CMD_BUFFER_STATE &cmd, VkPipelineBindPoint pipelineBindPoint,
2426 const char *function) const {
2427 bool skip = false;
2428
2429 const auto last_bound_it = cmd.lastBound.find(pipelineBindPoint);
2430 if (last_bound_it == cmd.lastBound.cend()) {
2431 return skip;
2432 }
2433 auto const &state = last_bound_it->second;
2434 const auto *pPipe = state.pipeline_state;
2435 if (!pPipe) {
2436 return skip;
2437 }
2438
2439 const auto *cb_access_context = GetAccessContext(cmd.commandBuffer);
2440 assert(cb_access_context);
2441 if (!cb_access_context) return skip;
2442
2443 const auto *context = cb_access_context->GetCurrentAccessContext();
2444 assert(context);
2445 if (!context) return skip;
2446
2447 using DescriptorClass = cvdescriptorset::DescriptorClass;
2448 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
2449 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
2450 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
2451 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
2452
2453 for (const auto &set_binding_pair : pPipe->active_slots) {
2454 uint32_t setIndex = set_binding_pair.first;
2455 cvdescriptorset::DescriptorSet *descriptor_set = state.per_set[setIndex].bound_descriptor_set;
2456 for (auto binding_pair : set_binding_pair.second) {
2457 auto binding = binding_pair.first;
2458 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
2459 binding_pair.first);
2460 const auto descriptor_type = binding_it.GetType();
2461 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2462 auto array_idx = 0;
2463
2464 if (binding_it.IsVariableDescriptorCount()) {
2465 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2466 }
2467 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2468 uint32_t index = i - index_range.start;
2469 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2470 if (descriptor->GetClass() == DescriptorClass::ImageSampler || descriptor->GetClass() == DescriptorClass::Image) {
2471 const IMAGE_VIEW_STATE *img_view_state = nullptr;
2472 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
2473 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
2474 } else {
2475 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
2476 }
2477 if (!img_view_state) continue;
2478 SyncStageAccessIndex sync_index;
2479 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
2480 sync_index = SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
2481 } else {
2482 sync_index = SYNC_VERTEX_SHADER_SHADER_READ;
2483 }
2484 const IMAGE_STATE *img_state = img_view_state->image_state.get();
2485 auto hazard = context->DetectHazard(*img_state, sync_index, img_view_state->create_info.subresourceRange,
2486 {0, 0, 0}, img_state->createInfo.extent);
2487 if (hazard.hazard) {
2488 skip |= LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
2489 "%s: Hazard %s for %s in %s and %s binding #%d index %d", function,
2490 string_SyncHazard(hazard.hazard),
2491 report_data->FormatHandle(img_view_state->image_view).c_str(),
2492 report_data->FormatHandle(cmd.commandBuffer).c_str(),
2493 report_data->FormatHandle(descriptor_set->GetSet()).c_str(), binding, index);
2494 }
2495 } else if (descriptor->GetClass() == DescriptorClass::TexelBuffer) {
2496 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
2497 if (!buf_view_state) continue;
2498 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
2499 ResourceAccessRange range =
2500 MakeRange(buf_view_state->create_info.offset,
2501 GetRealWholeSize(buf_view_state->create_info.offset, buf_view_state->create_info.range,
2502 buf_state->createInfo.size));
2503 auto hazard = context->DetectHazard(*buf_state, SYNC_VERTEX_SHADER_SHADER_READ, range);
2504 if (hazard.hazard) {
2505 skip |= LogError(buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
2506 "%s: Hazard %s for %s in %s and %s binding #%d index %d", function,
2507 string_SyncHazard(hazard.hazard),
2508 report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
2509 report_data->FormatHandle(cmd.commandBuffer).c_str(),
2510 report_data->FormatHandle(descriptor_set->GetSet()).c_str(), binding, index);
2511 }
2512 } else if (descriptor->GetClass() == DescriptorClass::GeneralBuffer) {
2513 auto buf_state = static_cast<const BufferDescriptor *>(descriptor)->GetBufferState();
2514 if (!buf_state) continue;
2515 ResourceAccessRange range = MakeRange(0, buf_state->createInfo.size);
2516 SyncStageAccessIndex sync_index;
2517 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER ||
2518 descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
2519 sync_index = SYNC_VERTEX_SHADER_UNIFORM_READ;
2520 } else {
2521 sync_index = SYNC_VERTEX_SHADER_SHADER_READ;
2522 }
2523 auto hazard = context->DetectHazard(*buf_state, sync_index, range);
2524 if (hazard.hazard) {
2525 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
2526 "%s: Hazard %s for %s in %s and %s binding #%d index %d", function,
2527 string_SyncHazard(hazard.hazard), report_data->FormatHandle(buf_state->buffer).c_str(),
2528 report_data->FormatHandle(cmd.commandBuffer).c_str(),
2529 report_data->FormatHandle(descriptor_set->GetSet()).c_str(), binding, index);
2530 }
2531 }
2532 }
2533 }
2534 }
2535 return skip;
2536}
2537
2538void SyncValidator::UpdateDescriptorSetAccessState(const CMD_BUFFER_STATE &cmd, CMD_TYPE command,
2539 VkPipelineBindPoint pipelineBindPoint) {
2540 const auto last_bound_it = cmd.lastBound.find(pipelineBindPoint);
2541 if (last_bound_it == cmd.lastBound.cend()) {
2542 return;
2543 }
2544 auto const &state = last_bound_it->second;
2545 const auto *pPipe = state.pipeline_state;
2546 if (!pPipe) {
2547 return;
2548 }
2549
2550 auto *cb_access_context = GetAccessContext(cmd.commandBuffer);
2551 assert(cb_access_context);
2552 const auto tag = cb_access_context->NextCommandTag(command);
2553 auto *context = cb_access_context->GetCurrentAccessContext();
2554 assert(context);
2555
2556 using DescriptorClass = cvdescriptorset::DescriptorClass;
2557 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
2558 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
2559 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
2560 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
2561
2562 for (const auto &set_binding_pair : pPipe->active_slots) {
2563 uint32_t setIndex = set_binding_pair.first;
2564 const cvdescriptorset::DescriptorSet *descriptor_set = state.per_set[setIndex].bound_descriptor_set;
2565 for (auto binding_pair : set_binding_pair.second) {
2566 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
2567 binding_pair.first);
2568 const auto descriptor_type = binding_it.GetType();
2569 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2570 auto array_idx = 0;
2571
2572 if (binding_it.IsVariableDescriptorCount()) {
2573 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2574 }
2575
2576 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2577 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2578 if (descriptor->GetClass() == DescriptorClass::ImageSampler || descriptor->GetClass() == DescriptorClass::Image) {
2579 const IMAGE_VIEW_STATE *img_view_state = nullptr;
2580 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
2581 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
2582 } else {
2583 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
2584 }
2585 if (!img_view_state) continue;
2586 SyncStageAccessIndex sync_index;
2587 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
2588 sync_index = SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
2589 } else {
2590 sync_index = SYNC_VERTEX_SHADER_SHADER_READ;
2591 }
2592 const IMAGE_STATE *img_state = img_view_state->image_state.get();
2593 context->UpdateAccessState(*img_state, sync_index, img_view_state->create_info.subresourceRange, {0, 0, 0},
2594 img_state->createInfo.extent, tag);
2595
2596 } else if (descriptor->GetClass() == DescriptorClass::TexelBuffer) {
2597 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
2598 if (!buf_view_state) continue;
2599 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
2600 ResourceAccessRange range = MakeRange(buf_view_state->create_info.offset, buf_view_state->create_info.range);
2601 context->UpdateAccessState(*buf_state, SYNC_VERTEX_SHADER_SHADER_READ, range, tag);
2602
2603 } else if (descriptor->GetClass() == DescriptorClass::GeneralBuffer) {
2604 auto buf_state = static_cast<const BufferDescriptor *>(descriptor)->GetBufferState();
2605 if (!buf_state) continue;
2606 SyncStageAccessIndex sync_index;
2607 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER ||
2608 descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
2609 sync_index = SYNC_VERTEX_SHADER_UNIFORM_READ;
2610 } else {
2611 sync_index = SYNC_VERTEX_SHADER_SHADER_READ;
2612 }
2613 ResourceAccessRange range = MakeRange(0, buf_state->createInfo.size);
2614 context->UpdateAccessState(*buf_state, sync_index, range, tag);
2615 }
2616 }
2617 }
2618 }
2619}
2620
locke-lunarga4d39ea2020-05-22 14:17:29 -06002621bool SyncValidator::DetectVertexHazard(const CMD_BUFFER_STATE &cmd, uint32_t vertexCount, uint32_t firstVertex,
2622 const char *function) const {
2623 bool skip = false;
2624 const auto last_bound_it = cmd.lastBound.find(VK_PIPELINE_BIND_POINT_GRAPHICS);
2625 if (last_bound_it == cmd.lastBound.cend()) {
2626 return skip;
2627 }
2628 auto const &state = last_bound_it->second;
2629 const auto *pPipe = state.pipeline_state;
2630 if (!pPipe) {
2631 return skip;
2632 }
2633
2634 const auto *cb_access_context = GetAccessContext(cmd.commandBuffer);
2635 assert(cb_access_context);
2636 if (!cb_access_context) return skip;
2637
2638 const auto *context = cb_access_context->GetCurrentAccessContext();
2639 assert(context);
2640 if (!context) return skip;
2641
2642 const auto &binding_buffers = cmd.current_vertex_buffer_binding_info.vertex_buffer_bindings;
2643 const auto &binding_buffers_size = binding_buffers.size();
2644 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
2645
2646 for (size_t i = 0; i < binding_descriptions_size; ++i) {
2647 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
2648 if (binding_description.binding < binding_buffers_size) {
2649 const auto &binding_buffer = binding_buffers[binding_description.binding];
2650 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
2651
2652 auto *buf_state = Get<BUFFER_STATE>(binding_buffer.buffer);
2653 VkDeviceSize range_start = binding_buffer.offset + firstVertex * binding_description.stride;
2654 VkDeviceSize range_size = 0;
2655 if (vertexCount == UINT32_MAX) {
2656 range_size = buf_state->createInfo.size - range_start;
2657 } else {
2658 range_size = vertexCount * binding_description.stride;
2659 }
2660 ResourceAccessRange range = MakeRange(range_start, range_size);
2661 auto hazard = context->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
2662 if (hazard.hazard) {
2663 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s",
2664 function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(buf_state->buffer).c_str(),
2665 report_data->FormatHandle(cmd.commandBuffer).c_str());
2666 }
2667 }
2668 }
2669 return skip;
2670}
2671
2672void SyncValidator::UpdateVertexAccessState(const CMD_BUFFER_STATE &cmd, CMD_TYPE command, uint32_t vertexCount,
2673 uint32_t firstVertex) {
2674 const auto last_bound_it = cmd.lastBound.find(VK_PIPELINE_BIND_POINT_GRAPHICS);
2675 if (last_bound_it == cmd.lastBound.cend()) {
2676 return;
2677 }
2678 auto const &state = last_bound_it->second;
2679 const auto *pPipe = state.pipeline_state;
2680 if (!pPipe) {
2681 return;
2682 }
2683
2684 auto *cb_access_context = GetAccessContext(cmd.commandBuffer);
2685 assert(cb_access_context);
2686 const auto tag = cb_access_context->NextCommandTag(command);
2687 auto *context = cb_access_context->GetCurrentAccessContext();
2688 assert(context);
2689
2690 const auto &binding_buffers = cmd.current_vertex_buffer_binding_info.vertex_buffer_bindings;
2691 const auto &binding_buffers_size = binding_buffers.size();
2692 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
2693
2694 for (size_t i = 0; i < binding_descriptions_size; ++i) {
2695 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
2696 if (binding_description.binding < binding_buffers_size) {
2697 const auto &binding_buffer = binding_buffers[binding_description.binding];
2698 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
2699
2700 auto *buf_state = Get<BUFFER_STATE>(binding_buffer.buffer);
2701 VkDeviceSize range_start = binding_buffer.offset + firstVertex * binding_description.stride;
2702 VkDeviceSize range_size = 0;
2703 if (vertexCount == UINT32_MAX) {
2704 range_size = buf_state->createInfo.size - range_start;
2705 } else {
2706 range_size = vertexCount * binding_description.stride;
2707 }
2708 ResourceAccessRange range = MakeRange(range_start, range_size);
2709 context->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
2710 }
2711 }
2712}
2713
2714bool SyncValidator::DetectVertexIndexHazard(const CMD_BUFFER_STATE &cmd, uint32_t indexCount, uint32_t firstIndex,
2715 const char *function) const {
2716 bool skip = false;
2717 if (cmd.index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
2718
2719 const auto *cb_access_context = GetAccessContext(cmd.commandBuffer);
2720 assert(cb_access_context);
2721 if (!cb_access_context) return skip;
2722
2723 const auto *context = cb_access_context->GetCurrentAccessContext();
2724 assert(context);
2725 if (!context) return skip;
2726
2727 auto *index_buf_state = Get<BUFFER_STATE>(cmd.index_buffer_binding.buffer);
2728 const auto index_size = GetIndexAlignment(cmd.index_buffer_binding.index_type);
2729 VkDeviceSize range_start = cmd.index_buffer_binding.offset + firstIndex * index_size;
2730 VkDeviceSize range_size = indexCount * index_size;
2731 ResourceAccessRange range = MakeRange(range_start, range_size);
2732 auto hazard = context->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
2733 if (hazard.hazard) {
2734 skip |= LogError(index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s",
2735 function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(index_buf_state->buffer).c_str(),
2736 report_data->FormatHandle(cmd.commandBuffer).c_str());
2737 }
2738
2739 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2740 // We will detect more accurate range in the future.
2741 skip |= DetectVertexHazard(cmd, UINT32_MAX, 0, function);
2742 return skip;
2743}
2744
2745void SyncValidator::UpdateVertexIndexAccessState(const CMD_BUFFER_STATE &cmd, CMD_TYPE command, uint32_t indexCount,
2746 uint32_t firstIndex) {
2747 if (cmd.index_buffer_binding.buffer == VK_NULL_HANDLE) return;
2748
2749 auto *cb_access_context = GetAccessContext(cmd.commandBuffer);
2750 assert(cb_access_context);
2751 const auto tag = cb_access_context->NextCommandTag(command);
2752 auto *context = cb_access_context->GetCurrentAccessContext();
2753 assert(context);
2754
2755 auto *index_buf_state = Get<BUFFER_STATE>(cmd.index_buffer_binding.buffer);
2756 const auto index_size = GetIndexAlignment(cmd.index_buffer_binding.index_type);
2757 VkDeviceSize range_start = cmd.index_buffer_binding.offset + firstIndex * index_size;
2758 VkDeviceSize range_size = indexCount * index_size;
2759 ResourceAccessRange range = MakeRange(range_start, range_size);
2760 context->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
2761
2762 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2763 // We will detect more accurate range in the future.
2764 UpdateVertexAccessState(cmd, command, UINT32_MAX, 0);
2765}
2766
locke-lunarg36ba2592020-04-03 09:42:04 -06002767bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
2768 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
2769 return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
2770}
2771
2772void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
2773 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
2774 UpdateDescriptorSetAccessState(*cb_state, CMD_DISPATCH, VK_PIPELINE_BIND_POINT_COMPUTE);
2775}
locke-lunarge1a67022020-04-29 00:15:36 -06002776
2777bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
2778 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
2779 return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
2780}
2781
2782void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
2783 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
2784 UpdateDescriptorSetAccessState(*cb_state, CMD_DISPATCHINDIRECT, VK_PIPELINE_BIND_POINT_COMPUTE);
2785}
2786
2787bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2788 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06002789 bool skip = false;
locke-lunarge1a67022020-04-29 00:15:36 -06002790 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002791 skip |= DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
2792 skip |= DetectVertexHazard(*cb_state, vertexCount, firstVertex, "vkCmdDraw");
2793 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06002794}
2795
2796void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2797 uint32_t firstVertex, uint32_t firstInstance) {
2798 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
2799 UpdateDescriptorSetAccessState(*cb_state, CMD_DRAW, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002800 UpdateVertexAccessState(*cb_state, CMD_DRAW, vertexCount, firstVertex);
locke-lunarge1a67022020-04-29 00:15:36 -06002801}
2802
2803bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2804 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06002805 bool skip = false;
locke-lunarge1a67022020-04-29 00:15:36 -06002806 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002807 skip |= DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
2808 skip |= DetectVertexIndexHazard(*cb_state, indexCount, firstIndex, "vkCmdDrawIndexed");
2809 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06002810}
2811
2812void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2813 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2814 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
2815 UpdateDescriptorSetAccessState(*cb_state, CMD_DRAWINDEXED, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002816 UpdateVertexAccessState(*cb_state, CMD_DRAWINDEXED, indexCount, firstIndex);
locke-lunarge1a67022020-04-29 00:15:36 -06002817}
2818
2819bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2820 uint32_t drawCount, uint32_t stride) const {
2821 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
2822 return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
2823}
2824
2825void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2826 uint32_t drawCount, uint32_t stride) {
2827 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
2828 UpdateDescriptorSetAccessState(*cb_state, CMD_DRAWINDIRECT, VK_PIPELINE_BIND_POINT_GRAPHICS);
2829}
2830
2831bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2832 uint32_t drawCount, uint32_t stride) const {
2833 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
2834 return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
2835}
2836
2837void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2838 uint32_t drawCount, uint32_t stride) {
2839 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
2840 UpdateDescriptorSetAccessState(*cb_state, CMD_DRAWINDEXEDINDIRECT, VK_PIPELINE_BIND_POINT_GRAPHICS);
2841}
2842
2843bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2844 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2845 uint32_t stride) const {
2846 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
2847 return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirectCount");
2848}
2849
2850void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2851 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2852 uint32_t stride) {
2853 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
2854 UpdateDescriptorSetAccessState(*cb_state, CMD_DRAWINDIRECTCOUNT, VK_PIPELINE_BIND_POINT_GRAPHICS);
2855}
2856
2857bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2858 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2859 uint32_t maxDrawCount, uint32_t stride) const {
2860 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
2861 return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirectCountKHR");
2862}
2863
2864void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2865 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2866 uint32_t maxDrawCount, uint32_t stride) {
2867 PostCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
2868}
2869
2870bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2871 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2872 uint32_t maxDrawCount, uint32_t stride) const {
2873 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
2874 return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirectCountAMD");
2875}
2876
2877void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2878 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2879 uint32_t maxDrawCount, uint32_t stride) {
2880 PostCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
2881}
2882
2883bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2884 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2885 uint32_t maxDrawCount, uint32_t stride) const {
2886 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
2887 return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirectCount");
2888}
2889
2890void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2891 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2892 uint32_t maxDrawCount, uint32_t stride) {
2893 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
2894 UpdateDescriptorSetAccessState(*cb_state, CMD_DRAWINDEXEDINDIRECTCOUNT, VK_PIPELINE_BIND_POINT_GRAPHICS);
2895}
2896
2897bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
2898 VkDeviceSize offset, VkBuffer countBuffer,
2899 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2900 uint32_t stride) const {
2901 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
2902 return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirectCountKHR");
2903}
2904
2905void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2906 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2907 uint32_t maxDrawCount, uint32_t stride) {
2908 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
2909}
2910
2911bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
2912 VkDeviceSize offset, VkBuffer countBuffer,
2913 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2914 uint32_t stride) const {
2915 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
2916 return DetectDescriptorSetHazard(*cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirectCountAMD");
2917}
2918
2919void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2920 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2921 uint32_t maxDrawCount, uint32_t stride) {
2922 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
2923}
2924
2925bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
2926 const VkClearColorValue *pColor, uint32_t rangeCount,
2927 const VkImageSubresourceRange *pRanges) const {
2928 bool skip = false;
2929 const auto *cb_access_context = GetAccessContext(commandBuffer);
2930 assert(cb_access_context);
2931 if (!cb_access_context) return skip;
2932
2933 const auto *context = cb_access_context->GetCurrentAccessContext();
2934 assert(context);
2935 if (!context) return skip;
2936
2937 const auto *image_state = Get<IMAGE_STATE>(image);
2938
2939 for (uint32_t index = 0; index < rangeCount; index++) {
2940 const auto &range = pRanges[index];
2941 if (image_state) {
2942 auto hazard =
2943 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
2944 if (hazard.hazard) {
2945 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
2946 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32, string_SyncHazard(hazard.hazard),
2947 report_data->FormatHandle(image).c_str(), index);
2948 }
2949 }
2950 }
2951 return skip;
2952}
2953
2954void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
2955 const VkClearColorValue *pColor, uint32_t rangeCount,
2956 const VkImageSubresourceRange *pRanges) {
2957 auto *cb_access_context = GetAccessContext(commandBuffer);
2958 assert(cb_access_context);
2959 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
2960 auto *context = cb_access_context->GetCurrentAccessContext();
2961 assert(context);
2962
2963 const auto *image_state = Get<IMAGE_STATE>(image);
2964
2965 for (uint32_t index = 0; index < rangeCount; index++) {
2966 const auto &range = pRanges[index];
2967 if (image_state) {
2968 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
2969 tag);
2970 }
2971 }
2972}
2973
2974bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
2975 VkImageLayout imageLayout,
2976 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
2977 const VkImageSubresourceRange *pRanges) const {
2978 bool skip = false;
2979 const auto *cb_access_context = GetAccessContext(commandBuffer);
2980 assert(cb_access_context);
2981 if (!cb_access_context) return skip;
2982
2983 const auto *context = cb_access_context->GetCurrentAccessContext();
2984 assert(context);
2985 if (!context) return skip;
2986
2987 const auto *image_state = Get<IMAGE_STATE>(image);
2988
2989 for (uint32_t index = 0; index < rangeCount; index++) {
2990 const auto &range = pRanges[index];
2991 if (image_state) {
2992 auto hazard =
2993 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
2994 if (hazard.hazard) {
2995 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
2996 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32,
2997 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index);
2998 }
2999 }
3000 }
3001 return skip;
3002}
3003
3004void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3005 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3006 const VkImageSubresourceRange *pRanges) {
3007 auto *cb_access_context = GetAccessContext(commandBuffer);
3008 assert(cb_access_context);
3009 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
3010 auto *context = cb_access_context->GetCurrentAccessContext();
3011 assert(context);
3012
3013 const auto *image_state = Get<IMAGE_STATE>(image);
3014
3015 for (uint32_t index = 0; index < rangeCount; index++) {
3016 const auto &range = pRanges[index];
3017 if (image_state) {
3018 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3019 tag);
3020 }
3021 }
3022}
3023
3024bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
3025 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
3026 VkDeviceSize dstOffset, VkDeviceSize stride,
3027 VkQueryResultFlags flags) const {
3028 bool skip = false;
3029 const auto *cb_access_context = GetAccessContext(commandBuffer);
3030 assert(cb_access_context);
3031 if (!cb_access_context) return skip;
3032
3033 const auto *context = cb_access_context->GetCurrentAccessContext();
3034 assert(context);
3035 if (!context) return skip;
3036
3037 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3038
3039 if (dst_buffer) {
3040 // TODO: size is ERROR
3041 ResourceAccessRange range = MakeRange(dstOffset, dst_buffer->createInfo.size);
3042 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3043 if (hazard.hazard) {
3044 skip |=
3045 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s",
3046 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3047 }
3048 }
3049 return skip;
3050}
3051
3052void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
3053 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3054 VkDeviceSize stride, VkQueryResultFlags flags) {
3055 auto *cb_access_context = GetAccessContext(commandBuffer);
3056 assert(cb_access_context);
3057 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
3058 auto *context = cb_access_context->GetCurrentAccessContext();
3059 assert(context);
3060
3061 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3062
3063 if (dst_buffer) {
3064 // TODO: size is ERROR
3065 ResourceAccessRange range = MakeRange(dstOffset, dst_buffer->createInfo.size);
3066 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3067 }
3068}
3069
3070bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3071 VkDeviceSize size, uint32_t data) const {
3072 bool skip = false;
3073 const auto *cb_access_context = GetAccessContext(commandBuffer);
3074 assert(cb_access_context);
3075 if (!cb_access_context) return skip;
3076
3077 const auto *context = cb_access_context->GetCurrentAccessContext();
3078 assert(context);
3079 if (!context) return skip;
3080
3081 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3082
3083 if (dst_buffer) {
3084 ResourceAccessRange range = MakeRange(dstOffset, size);
3085 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3086 if (hazard.hazard) {
3087 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdFillBuffer: Hazard %s for dstBuffer %s",
3088 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3089 }
3090 }
3091 return skip;
3092}
3093
3094void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3095 VkDeviceSize size, uint32_t data) {
3096 auto *cb_access_context = GetAccessContext(commandBuffer);
3097 assert(cb_access_context);
3098 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
3099 auto *context = cb_access_context->GetCurrentAccessContext();
3100 assert(context);
3101
3102 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3103
3104 if (dst_buffer) {
3105 ResourceAccessRange range = MakeRange(dstOffset, size);
3106 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3107 }
3108}
3109
3110bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3111 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3112 const VkImageResolve *pRegions) const {
3113 bool skip = false;
3114 const auto *cb_access_context = GetAccessContext(commandBuffer);
3115 assert(cb_access_context);
3116 if (!cb_access_context) return skip;
3117
3118 const auto *context = cb_access_context->GetCurrentAccessContext();
3119 assert(context);
3120 if (!context) return skip;
3121
3122 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3123 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3124
3125 for (uint32_t region = 0; region < regionCount; region++) {
3126 const auto &resolve_region = pRegions[region];
3127 if (src_image) {
3128 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3129 resolve_region.srcOffset, resolve_region.extent);
3130 if (hazard.hazard) {
3131 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
3132 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
3133 report_data->FormatHandle(srcImage).c_str(), region);
3134 }
3135 }
3136
3137 if (dst_image) {
3138 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3139 resolve_region.dstOffset, resolve_region.extent);
3140 if (hazard.hazard) {
3141 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
3142 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
3143 report_data->FormatHandle(dstImage).c_str(), region);
3144 }
3145 if (skip) break;
3146 }
3147 }
3148
3149 return skip;
3150}
3151
3152void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3153 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3154 const VkImageResolve *pRegions) {
3155 auto *cb_access_context = GetAccessContext(commandBuffer);
3156 assert(cb_access_context);
3157 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
3158 auto *context = cb_access_context->GetCurrentAccessContext();
3159 assert(context);
3160
3161 auto *src_image = Get<IMAGE_STATE>(srcImage);
3162 auto *dst_image = Get<IMAGE_STATE>(dstImage);
3163
3164 for (uint32_t region = 0; region < regionCount; region++) {
3165 const auto &resolve_region = pRegions[region];
3166 if (src_image) {
3167 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3168 resolve_region.srcOffset, resolve_region.extent, tag);
3169 }
3170 if (dst_image) {
3171 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3172 resolve_region.dstOffset, resolve_region.extent, tag);
3173 }
3174 }
3175}
3176
3177bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3178 VkDeviceSize dataSize, const void *pData) const {
3179 bool skip = false;
3180 const auto *cb_access_context = GetAccessContext(commandBuffer);
3181 assert(cb_access_context);
3182 if (!cb_access_context) return skip;
3183
3184 const auto *context = cb_access_context->GetCurrentAccessContext();
3185 assert(context);
3186 if (!context) return skip;
3187
3188 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3189
3190 if (dst_buffer) {
3191 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3192 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3193 if (hazard.hazard) {
3194 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s",
3195 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3196 }
3197 }
3198 return skip;
3199}
3200
3201void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3202 VkDeviceSize dataSize, const void *pData) {
3203 auto *cb_access_context = GetAccessContext(commandBuffer);
3204 assert(cb_access_context);
3205 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
3206 auto *context = cb_access_context->GetCurrentAccessContext();
3207 assert(context);
3208
3209 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3210
3211 if (dst_buffer) {
3212 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3213 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3214 }
3215}