blob: d7407a676d19bbcf70721af4d61e28562d69496e [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
locke-lunargff255f92020-05-13 18:53:52 -0600162void GetBufferRange(VkDeviceSize &range_start, VkDeviceSize &range_size, VkDeviceSize offset, VkDeviceSize buf_whole_size,
163 uint32_t first_index, uint32_t count, VkDeviceSize stride) {
164 range_start = offset + first_index * stride;
165 range_size = 0;
166 if (count == UINT32_MAX) {
167 range_size = buf_whole_size - range_start;
168 } else {
169 range_size = count * stride;
170 }
171}
172
John Zulauf355e49b2020-04-24 15:11:15 -0600173// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
174const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
175 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
176
John Zulauf7635de32020-05-29 17:14:15 -0600177// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
178// Used by both validation and record operations
179//
180// The signature for Action() reflect the needs of both uses.
181template <typename Action>
182void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
183 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
184 VkExtent3D extent = CastTo3D(render_area.extent);
185 VkOffset3D offset = CastTo3D(render_area.offset);
186 const auto &rp_ci = rp_state.createInfo;
187 const auto *attachment_ci = rp_ci.pAttachments;
188 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
189
190 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
191 const auto *color_attachments = subpass_ci.pColorAttachments;
192 const auto *color_resolve = subpass_ci.pResolveAttachments;
193 if (color_resolve && color_attachments) {
194 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
195 const auto &color_attach = color_attachments[i].attachment;
196 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
197 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
198 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
199 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
200 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
201 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
202 }
203 }
204 }
205
206 // Depth stencil resolve only if the extension is present
207 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
208 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
209 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
210 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
211 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
212 const auto src_ci = attachment_ci[src_at];
213 // The formats are required to match so we can pick either
214 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
215 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
216 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
217 VkImageAspectFlags aspect_mask = 0u;
218
219 // Figure out which aspects are actually touched during resolve operations
220 const char *aspect_string = nullptr;
221 if (resolve_depth && resolve_stencil) {
222 // Validate all aspects together
223 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
224 aspect_string = "depth/stencil";
225 } else if (resolve_depth) {
226 // Validate depth only
227 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
228 aspect_string = "depth";
229 } else if (resolve_stencil) {
230 // Validate all stencil only
231 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
232 aspect_string = "stencil";
233 }
234
235 if (aspect_mask) {
236 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
237 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kDepthStencilAttachmentRasterOrder, offset, extent,
238 aspect_mask);
239 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
240 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
241 }
242 }
243}
244
245// Action for validating resolve operations
246class ValidateResolveAction {
247 public:
248 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
249 const char *func_name)
250 : render_pass_(render_pass),
251 subpass_(subpass),
252 context_(context),
253 sync_state_(sync_state),
254 func_name_(func_name),
255 skip_(false) {}
256 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
257 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
258 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
259 HazardResult hazard;
260 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
261 if (hazard.hazard) {
262 skip_ |= sync_state_.LogError(
263 render_pass_, string_SyncHazardVUID(hazard.hazard),
264 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32 " to resolve attachment %" PRIu32 ".",
265 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name, src_at, dst_at);
266 }
267 }
268 // Providing a mechanism for the constructing caller to get the result of the validation
269 bool GetSkip() const { return skip_; }
270
271 private:
272 VkRenderPass render_pass_;
273 const uint32_t subpass_;
274 const AccessContext &context_;
275 const SyncValidator &sync_state_;
276 const char *func_name_;
277 bool skip_;
278};
279
280// Update action for resolve operations
281class UpdateStateResolveAction {
282 public:
283 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
284 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
285 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
286 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
287 // Ignores validation only arguments...
288 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
289 }
290
291 private:
292 AccessContext &context_;
293 const ResourceUsageTag &tag_;
294};
295
John Zulauf540266b2020-04-06 18:54:53 -0600296AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
297 const std::vector<SubpassDependencyGraphNode> &dependencies,
298 const std::vector<AccessContext> &contexts, AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600299 Reset();
300 const auto &subpass_dep = dependencies[subpass];
301 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600302 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600303 for (const auto &prev_dep : subpass_dep.prev) {
304 assert(prev_dep.dependency);
305 const auto dep = *prev_dep.dependency;
John Zulauf540266b2020-04-06 18:54:53 -0600306 prev_.emplace_back(const_cast<AccessContext *>(&contexts[dep.srcSubpass]), queue_flags, dep);
John Zulauf355e49b2020-04-24 15:11:15 -0600307 prev_by_subpass_[dep.srcSubpass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700308 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600309
310 async_.reserve(subpass_dep.async.size());
311 for (const auto async_subpass : subpass_dep.async) {
John Zulauf540266b2020-04-06 18:54:53 -0600312 async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass]));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600313 }
John Zulaufe5da6e52020-03-18 15:32:18 -0600314 if (subpass_dep.barrier_from_external) {
315 src_external_ = TrackBack(external_context, queue_flags, *subpass_dep.barrier_from_external);
316 } else {
317 src_external_ = TrackBack();
318 }
319 if (subpass_dep.barrier_to_external) {
320 dst_external_ = TrackBack(this, queue_flags, *subpass_dep.barrier_to_external);
321 } else {
322 dst_external_ = TrackBack();
John Zulauf3d84f1b2020-03-09 13:33:25 -0600323 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700324}
325
John Zulauf5f13a792020-03-10 07:31:21 -0600326template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600327HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600328 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600329 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600330 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600331
332 HazardResult hazard;
333 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
334 hazard = detector.Detect(prev);
335 }
336 return hazard;
337}
338
John Zulauf3d84f1b2020-03-09 13:33:25 -0600339// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
340// the DAG of the contexts (for example subpasses)
341template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600342HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
343 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600344 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600345
John Zulauf355e49b2020-04-24 15:11:15 -0600346 if (static_cast<uint32_t>(options) | DetectOptions::kDetectAsync) {
347 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
348 // so we'll check these first
349 for (const auto &async_context : async_) {
350 hazard = async_context->DetectAsyncHazard(type, detector, range);
351 if (hazard.hazard) return hazard;
352 }
John Zulauf5f13a792020-03-10 07:31:21 -0600353 }
354
John Zulauf69133422020-05-20 14:55:53 -0600355 const bool detect_prev = (static_cast<uint32_t>(options) | DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600356
John Zulauf69133422020-05-20 14:55:53 -0600357 const auto &accesses = GetAccessStateMap(type);
358 const auto from = accesses.lower_bound(range);
359 const auto to = accesses.upper_bound(range);
360 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600361
John Zulauf69133422020-05-20 14:55:53 -0600362 for (auto pos = from; pos != to; ++pos) {
363 // Cover any leading gap, or gap between entries
364 if (detect_prev) {
365 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
366 // Cover any leading gap, or gap between entries
367 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600368 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600369 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600370 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600371 if (hazard.hazard) return hazard;
372 }
John Zulauf69133422020-05-20 14:55:53 -0600373 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
374 gap.begin = pos->first.end;
375 }
376
377 hazard = detector.Detect(pos);
378 if (hazard.hazard) return hazard;
379 }
380
381 if (detect_prev) {
382 // Detect in the trailing empty as needed
383 gap.end = range.end;
384 if (gap.non_empty()) {
385 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600386 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600387 }
388
389 return hazard;
390}
391
392// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
393template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600394HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600395 auto &accesses = GetAccessStateMap(type);
396 const auto from = accesses.lower_bound(range);
397 const auto to = accesses.upper_bound(range);
398
John Zulauf3d84f1b2020-03-09 13:33:25 -0600399 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600400 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
401 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600402 }
John Zulauf16adfc92020-04-08 10:28:33 -0600403
John Zulauf3d84f1b2020-03-09 13:33:25 -0600404 return hazard;
405}
406
John Zulauf355e49b2020-04-24 15:11:15 -0600407// Returns the last resolved entry
408static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
409 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
410 const SyncBarrier *barrier) {
411 auto at = entry;
412 for (auto pos = first; pos != last; ++pos) {
413 // Every member of the input iterator range must fit within the remaining portion of entry
414 assert(at->first.includes(pos->first));
415 assert(at != dest->end());
416 // Trim up at to the same size as the entry to resolve
417 at = sparse_container::split(at, *dest, pos->first);
418 auto access = pos->second;
419 if (barrier) {
420 access.ApplyBarrier(*barrier);
421 }
422 at->second.Resolve(access);
423 ++at; // Go to the remaining unused section of entry
424 }
425}
426
427void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const SyncBarrier *barrier,
428 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
429 bool recur_to_infill) const {
430 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
431 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf16adfc92020-04-08 10:28:33 -0600432 if (current->pos_B->valid) {
433 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600434 auto access = src_pos->second;
435 if (barrier) {
436 access.ApplyBarrier(*barrier);
437 }
John Zulauf16adfc92020-04-08 10:28:33 -0600438 if (current->pos_A->valid) {
439 current.trim_A();
John Zulauf355e49b2020-04-24 15:11:15 -0600440 current->pos_A->lower_bound->second.Resolve(access);
John Zulauf5f13a792020-03-10 07:31:21 -0600441 } else {
John Zulauf355e49b2020-04-24 15:11:15 -0600442 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, access));
443 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600444 }
John Zulauf16adfc92020-04-08 10:28:33 -0600445 } else {
446 // we have to descend to fill this gap
447 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600448 if (current->pos_A->valid) {
449 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
450 ResourceAccessRangeMap gap_map;
451 ResolvePreviousAccess(type, current->range, &gap_map, infill_state);
452 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier);
453 } else {
454 // There isn't anything in dest in current->range, so we can accumulate directly into it.
455 ResolvePreviousAccess(type, current->range, resolve_map, infill_state);
456 if (barrier) {
457 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
458 for (auto pos = resolve_map->lower_bound(current->range); pos != current->pos_A->lower_bound; ++pos) {
459 pos->second.ApplyBarrier(*barrier);
460 }
461 }
462 }
463 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
464 // iterator of the outer while.
465
466 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
467 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
468 // we stepped on the dest map
469 const auto seek_to = current->range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
470 current.invalidate_A(); // Changes current->range
471 current.seek(seek_to);
472 } else if (!current->pos_A->valid && infill_state) {
473 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
474 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
475 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600476 }
John Zulauf5f13a792020-03-10 07:31:21 -0600477 }
John Zulauf16adfc92020-04-08 10:28:33 -0600478 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600479 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600480}
481
John Zulauf355e49b2020-04-24 15:11:15 -0600482void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
483 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600484 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600485 if (range.non_empty() && infill_state) {
486 descent_map->insert(std::make_pair(range, *infill_state));
487 }
488 } else {
489 // Look for something to fill the gap further along.
490 for (const auto &prev_dep : prev_) {
John Zulauf355e49b2020-04-24 15:11:15 -0600491 prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600492 }
493
John Zulaufe5da6e52020-03-18 15:32:18 -0600494 if (src_external_.context) {
John Zulauf355e49b2020-04-24 15:11:15 -0600495 src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600496 }
497 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600498}
499
John Zulauf16adfc92020-04-08 10:28:33 -0600500AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600501 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600502}
503
504VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
505 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
506}
507
John Zulauf355e49b2020-04-24 15:11:15 -0600508static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600509
John Zulauf1507ee42020-05-18 11:33:09 -0600510static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
511 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
512 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
513 return stage_access;
514}
515static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
516 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
517 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
518 return stage_access;
519}
520
John Zulauf7635de32020-05-29 17:14:15 -0600521// Caller must manage returned pointer
522static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
523 uint32_t subpass, const VkRect2D &render_area,
524 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
525 auto *proxy = new AccessContext(context);
526 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600527 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600528 return proxy;
529}
530
John Zulauf540266b2020-04-06 18:54:53 -0600531void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600532 AddressType address_type, ResourceAccessRangeMap *descent_map,
533 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600534 if (!SimpleBinding(image_state)) return;
535
John Zulauf62f10592020-04-03 12:20:02 -0600536 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600537 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600538 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600539 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600540 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600541 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600542 }
543}
544
John Zulauf7635de32020-05-29 17:14:15 -0600545// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600546bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600547 const VkRect2D &render_area, uint32_t subpass,
548 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
549 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600550 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600551 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
552 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
553 // those affects have not been recorded yet.
554 //
555 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
556 // to apply and only copy then, if this proves a hot spot.
557 std::unique_ptr<AccessContext> proxy_for_prev;
558 TrackBack proxy_track_back;
559
John Zulauf355e49b2020-04-24 15:11:15 -0600560 const auto &transitions = rp_state.subpass_transitions[subpass];
561 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600562 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
563
564 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
565 if (prev_needs_proxy) {
566 if (!proxy_for_prev) {
567 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
568 render_area, attachment_views));
569 proxy_track_back = *track_back;
570 proxy_track_back.context = proxy_for_prev.get();
571 }
572 track_back = &proxy_track_back;
573 }
574 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600575 if (hazard.hazard) {
576 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
577 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " image layout transition.",
578 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment);
579 }
580 }
581 return skip;
582}
583
John Zulauf1507ee42020-05-18 11:33:09 -0600584bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600585 const VkRect2D &render_area, uint32_t subpass,
586 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
587 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600588 bool skip = false;
589 const auto *attachment_ci = rp_state.createInfo.pAttachments;
590 VkExtent3D extent = CastTo3D(render_area.extent);
591 VkOffset3D offset = CastTo3D(render_area.offset);
592 const auto external_access_scope = src_external_.barrier.dst_access_scope;
John Zulauf1507ee42020-05-18 11:33:09 -0600593
594 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
595 if (subpass == rp_state.attachment_first_subpass[i]) {
596 if (attachment_views[i] == nullptr) continue;
597 const IMAGE_VIEW_STATE &view = *attachment_views[i];
598 const IMAGE_STATE *image = view.image_state.get();
599 if (image == nullptr) continue;
600 const auto &ci = attachment_ci[i];
601 const bool is_transition = rp_state.attachment_first_is_transition[i];
602
603 // Need check in the following way
604 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
605 // vs. transition
606 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
607 // for each aspect loaded.
608
609 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600610 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600611 const bool is_color = !(has_depth || has_stencil);
612
613 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
614 const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U;
615 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
616 const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U;
617
John Zulaufaff20662020-06-01 14:07:58 -0600618 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600619 const char *aspect = nullptr;
620 if (is_transition) {
621 // For transition w
622 SyncHazard transition_hazard = SyncHazard::NONE;
623 bool checked_stencil = false;
624 if (load_mask) {
625 if ((load_mask & external_access_scope) != load_mask) {
626 transition_hazard =
627 SyncStageAccess::HasWrite(load_mask) ? SyncHazard::WRITE_AFTER_WRITE : SyncHazard::READ_AFTER_WRITE;
628 aspect = is_color ? "color" : "depth";
629 }
630 if (!transition_hazard && stencil_mask) {
631 if ((stencil_mask & external_access_scope) != stencil_mask) {
632 transition_hazard = SyncStageAccess::HasWrite(stencil_mask) ? SyncHazard::WRITE_AFTER_WRITE
633 : SyncHazard::READ_AFTER_WRITE;
634 aspect = "stencil";
635 checked_stencil = true;
636 }
637 }
638 }
639 if (transition_hazard) {
640 // Hazard vs. ILT
641 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
642 skip |=
643 sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
644 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
645 " aspect %s during load with loadOp %s.",
646 func_name, string_SyncHazard(transition_hazard), subpass, i, aspect, load_op_string);
647 }
648 } else {
649 auto hazard_range = view.normalized_subresource_range;
650 bool checked_stencil = false;
651 if (is_color) {
652 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
653 aspect = "color";
654 } else {
655 if (has_depth) {
656 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
657 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
658 aspect = "depth";
659 }
660 if (!hazard.hazard && has_stencil) {
661 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
662 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
663 aspect = "stencil";
664 checked_stencil = true;
665 }
666 }
667
668 if (hazard.hazard) {
669 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
670 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
671 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
672 " aspect %s during load with loadOp %s.",
673 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
674 }
675 }
676 }
677 }
678 return skip;
679}
680
John Zulaufaff20662020-06-01 14:07:58 -0600681// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
682// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
683// store is part of the same Next/End operation.
684// The latter is handled in layout transistion validation directly
685bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
686 const VkRect2D &render_area, uint32_t subpass,
687 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
688 const char *func_name) const {
689 bool skip = false;
690 const auto *attachment_ci = rp_state.createInfo.pAttachments;
691 VkExtent3D extent = CastTo3D(render_area.extent);
692 VkOffset3D offset = CastTo3D(render_area.offset);
693
694 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
695 if (subpass == rp_state.attachment_last_subpass[i]) {
696 if (attachment_views[i] == nullptr) continue;
697 const IMAGE_VIEW_STATE &view = *attachment_views[i];
698 const IMAGE_STATE *image = view.image_state.get();
699 if (image == nullptr) continue;
700 const auto &ci = attachment_ci[i];
701
702 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
703 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
704 // sake, we treat DONT_CARE as writing.
705 const bool has_depth = FormatHasDepth(ci.format);
706 const bool has_stencil = FormatHasStencil(ci.format);
707 const bool is_color = !(has_depth || has_stencil);
708 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
709 if (!has_stencil && !store_op_stores) continue;
710
711 HazardResult hazard;
712 const char *aspect = nullptr;
713 bool checked_stencil = false;
714 if (is_color) {
715 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
716 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
717 aspect = "color";
718 } else {
719 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
720 auto hazard_range = view.normalized_subresource_range;
721 if (has_depth && store_op_stores) {
722 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
723 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
724 kAttachmentRasterOrder, offset, extent);
725 aspect = "depth";
726 }
727 if (!hazard.hazard && has_stencil && stencil_op_stores) {
728 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
729 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
730 kAttachmentRasterOrder, offset, extent);
731 aspect = "stencil";
732 checked_stencil = true;
733 }
734 }
735
736 if (hazard.hazard) {
737 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
738 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
739 skip |= sync_state.LogError(
740 rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
741 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " %s aspect during store with %s %s.", func_name,
742 string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string, store_op_string);
743 }
744 }
745 }
746 return skip;
747}
748
John Zulaufb027cdb2020-05-21 14:25:22 -0600749bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
750 const VkRect2D &render_area,
751 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
752 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600753 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
754 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
755 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600756}
757
John Zulauf3d84f1b2020-03-09 13:33:25 -0600758class HazardDetector {
759 SyncStageAccessIndex usage_index_;
760
761 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600762 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600763 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
764 return pos->second.DetectAsyncHazard(usage_index_);
765 }
766 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
767};
768
John Zulauf69133422020-05-20 14:55:53 -0600769class HazardDetectorWithOrdering {
770 const SyncStageAccessIndex usage_index_;
771 const SyncOrderingBarrier &ordering_;
772
773 public:
774 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
775 return pos->second.DetectHazard(usage_index_, ordering_);
776 }
777 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
778 return pos->second.DetectAsyncHazard(usage_index_);
779 }
780 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
781 : usage_index_(usage), ordering_(ordering) {}
782};
783
John Zulauf16adfc92020-04-08 10:28:33 -0600784HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600785 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600786 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600787 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600788}
789
John Zulauf16adfc92020-04-08 10:28:33 -0600790HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600791 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600792 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600793 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600794}
795
John Zulauf69133422020-05-20 14:55:53 -0600796template <typename Detector>
797HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
798 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
799 const VkExtent3D &extent, DetectOptions options) const {
800 if (!SimpleBinding(image)) return HazardResult();
801 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
802 const auto address_type = ImageAddressType(image);
803 const auto base_address = ResourceBaseAddress(image);
804 for (; range_gen->non_empty(); ++range_gen) {
805 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
806 if (hazard.hazard) return hazard;
807 }
808 return HazardResult();
809}
810
John Zulauf540266b2020-04-06 18:54:53 -0600811HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
812 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
813 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700814 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
815 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -0600816 return DetectHazard(image, current_usage, subresource_range, offset, extent);
817}
818
819HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
820 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
821 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -0600822 HazardDetector detector(current_usage);
823 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
824}
825
826HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
827 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
828 const VkOffset3D &offset, const VkExtent3D &extent) const {
829 HazardDetectorWithOrdering detector(current_usage, ordering);
830 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -0600831}
832
John Zulaufb027cdb2020-05-21 14:25:22 -0600833// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
834// should have reported the issue regarding an invalid attachment entry
835HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
836 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
837 VkImageAspectFlags aspect_mask) const {
838 if (view != nullptr) {
839 const IMAGE_STATE *image = view->image_state.get();
840 if (image != nullptr) {
841 auto *detect_range = &view->normalized_subresource_range;
842 VkImageSubresourceRange masked_range;
843 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
844 masked_range = view->normalized_subresource_range;
845 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
846 detect_range = &masked_range;
847 }
848
849 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
850 if (detect_range->aspectMask) {
851 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
852 }
853 }
854 }
855 return HazardResult();
856}
John Zulauf3d84f1b2020-03-09 13:33:25 -0600857class BarrierHazardDetector {
858 public:
859 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
860 SyncStageAccessFlags src_access_scope)
861 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
862
John Zulauf5f13a792020-03-10 07:31:21 -0600863 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
864 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -0700865 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600866 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
867 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
868 return pos->second.DetectAsyncHazard(usage_index_);
869 }
870
871 private:
872 SyncStageAccessIndex usage_index_;
873 VkPipelineStageFlags src_exec_scope_;
874 SyncStageAccessFlags src_access_scope_;
875};
876
John Zulauf16adfc92020-04-08 10:28:33 -0600877HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -0600878 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600879 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600880 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -0600881 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700882}
883
John Zulauf16adfc92020-04-08 10:28:33 -0600884HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600885 SyncStageAccessFlags src_access_scope,
886 const VkImageSubresourceRange &subresource_range,
887 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -0600888 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
889 VkOffset3D zero_offset = {0, 0, 0};
890 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700891}
892
John Zulauf355e49b2020-04-24 15:11:15 -0600893HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
894 SyncStageAccessFlags src_stage_accesses,
895 const VkImageMemoryBarrier &barrier) const {
896 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
897 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
898 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
899}
900
John Zulauf9cb530d2019-09-30 14:14:10 -0600901template <typename Flags, typename Map>
902SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
903 SyncStageAccessFlags scope = 0;
904 for (const auto &bit_scope : map) {
905 if (flag_mask < bit_scope.first) break;
906
907 if (flag_mask & bit_scope.first) {
908 scope |= bit_scope.second;
909 }
910 }
911 return scope;
912}
913
914SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
915 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
916}
917
918SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
919 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
920}
921
922// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
923SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -0600924 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
925 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
926 // 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 -0600927 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
928}
929
930template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -0700931void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -0600932 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
933 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -0600934 auto pos = accesses->lower_bound(range);
935 if (pos == accesses->end() || !pos->first.intersects(range)) {
936 // The range is empty, fill it with a default value.
937 pos = action.Infill(accesses, pos, range);
938 } else if (range.begin < pos->first.begin) {
939 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -0700940 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -0600941 } else if (pos->first.begin < range.begin) {
942 // Trim the beginning if needed
943 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
944 ++pos;
945 }
946
947 const auto the_end = accesses->end();
948 while ((pos != the_end) && pos->first.intersects(range)) {
949 if (pos->first.end > range.end) {
950 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
951 }
952
953 pos = action(accesses, pos);
954 if (pos == the_end) break;
955
956 auto next = pos;
957 ++next;
958 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
959 // Need to infill if next is disjoint
960 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -0700961 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -0600962 next = action.Infill(accesses, next, new_range);
963 }
964 pos = next;
965 }
966}
967
968struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700969 using Iterator = ResourceAccessRangeMap::iterator;
970 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600971 // this is only called on gaps, and never returns a gap.
972 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -0600973 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600974 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -0600975 }
John Zulauf5f13a792020-03-10 07:31:21 -0600976
John Zulauf5c5e88d2019-12-26 11:22:02 -0700977 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -0600978 auto &access_state = pos->second;
979 access_state.Update(usage, tag);
980 return pos;
981 }
982
John Zulauf16adfc92020-04-08 10:28:33 -0600983 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -0600984 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -0600985 : type(type_), context(context_), usage(usage_), tag(tag_) {}
986 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -0600987 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -0600988 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -0600989 const ResourceUsageTag &tag;
990};
991
992struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700993 using Iterator = ResourceAccessRangeMap::iterator;
994 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -0600995
John Zulauf5c5e88d2019-12-26 11:22:02 -0700996 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -0600997 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -0700998 access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -0600999 return pos;
1000 }
1001
John Zulauf36bcf6a2020-02-03 15:12:52 -07001002 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
1003 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
1004 : src_exec_scope(src_exec_scope_),
1005 src_access_scope(src_access_scope_),
1006 dst_exec_scope(dst_exec_scope_),
1007 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001008
John Zulauf36bcf6a2020-02-03 15:12:52 -07001009 VkPipelineStageFlags src_exec_scope;
1010 SyncStageAccessFlags src_access_scope;
1011 VkPipelineStageFlags dst_exec_scope;
1012 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001013};
1014
1015struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001016 using Iterator = ResourceAccessRangeMap::iterator;
1017 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001018
John Zulauf5c5e88d2019-12-26 11:22:02 -07001019 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001020 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001021 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001022
1023 for (const auto &functor : barrier_functor) {
1024 functor(accesses, pos);
1025 }
1026 return pos;
1027 }
1028
John Zulauf36bcf6a2020-02-03 15:12:52 -07001029 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
1030 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -06001031 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001032 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001033 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
1034 barrier_functor.reserve(memoryBarrierCount);
1035 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
1036 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -07001037 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
1038 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -06001039 }
1040 }
1041
John Zulauf36bcf6a2020-02-03 15:12:52 -07001042 const VkPipelineStageFlags src_exec_scope;
1043 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001044 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
1045};
1046
John Zulauf355e49b2020-04-24 15:11:15 -06001047void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1048 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001049 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1050 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001051}
1052
John Zulauf16adfc92020-04-08 10:28:33 -06001053void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001054 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001055 if (!SimpleBinding(buffer)) return;
1056 const auto base_address = ResourceBaseAddress(buffer);
1057 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1058}
John Zulauf355e49b2020-04-24 15:11:15 -06001059
John Zulauf540266b2020-04-06 18:54:53 -06001060void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001061 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001062 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001063 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001064 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001065 const auto address_type = ImageAddressType(image);
1066 const auto base_address = ResourceBaseAddress(image);
1067 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001068 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001069 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001070 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001071}
John Zulauf7635de32020-05-29 17:14:15 -06001072void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1073 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1074 if (view != nullptr) {
1075 const IMAGE_STATE *image = view->image_state.get();
1076 if (image != nullptr) {
1077 auto *update_range = &view->normalized_subresource_range;
1078 VkImageSubresourceRange masked_range;
1079 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1080 masked_range = view->normalized_subresource_range;
1081 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1082 update_range = &masked_range;
1083 }
1084 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1085 }
1086 }
1087}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001088
John Zulauf355e49b2020-04-24 15:11:15 -06001089void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1090 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1091 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001092 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1093 subresource.layerCount};
1094 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1095}
1096
John Zulauf540266b2020-04-06 18:54:53 -06001097template <typename Action>
1098void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001099 if (!SimpleBinding(buffer)) return;
1100 const auto base_address = ResourceBaseAddress(buffer);
1101 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001102}
1103
1104template <typename Action>
1105void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1106 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001107 if (!SimpleBinding(image)) return;
1108 const auto address_type = ImageAddressType(image);
1109 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001110
locke-lunargae26eac2020-04-16 15:29:05 -06001111 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001112 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001113
John Zulauf16adfc92020-04-08 10:28:33 -06001114 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001115 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001116 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001117 }
1118}
1119
John Zulauf7635de32020-05-29 17:14:15 -06001120void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1121 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1122 const ResourceUsageTag &tag) {
1123 UpdateStateResolveAction update(*this, tag);
1124 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1125}
1126
John Zulaufaff20662020-06-01 14:07:58 -06001127void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1128 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1129 const ResourceUsageTag &tag) {
1130 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1131 VkExtent3D extent = CastTo3D(render_area.extent);
1132 VkOffset3D offset = CastTo3D(render_area.offset);
1133
1134 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1135 if (rp_state.attachment_last_subpass[i] == subpass) {
1136 if (attachment_views[i] == nullptr) continue; // UNUSED
1137 const auto &view = *attachment_views[i];
1138 const IMAGE_STATE *image = view.image_state.get();
1139 if (image == nullptr) continue;
1140
1141 const auto &ci = attachment_ci[i];
1142 const bool has_depth = FormatHasDepth(ci.format);
1143 const bool has_stencil = FormatHasStencil(ci.format);
1144 const bool is_color = !(has_depth || has_stencil);
1145 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1146
1147 if (is_color && store_op_stores) {
1148 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1149 offset, extent, tag);
1150 } else {
1151 auto update_range = view.normalized_subresource_range;
1152 if (has_depth && store_op_stores) {
1153 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1154 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1155 tag);
1156 }
1157 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1158 if (has_stencil && stencil_op_stores) {
1159 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1160 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1161 tag);
1162 }
1163 }
1164 }
1165 }
1166}
1167
John Zulauf540266b2020-04-06 18:54:53 -06001168template <typename Action>
1169void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1170 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001171 for (const auto address_type : kAddressTypes) {
1172 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001173 }
1174}
1175
1176void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001177 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1178 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -06001179 for (const auto address_type : kAddressTypes) {
John Zulauf355e49b2020-04-24 15:11:15 -06001180 context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier,
1181 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001182 }
1183 }
1184}
1185
John Zulauf355e49b2020-04-24 15:11:15 -06001186void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1187 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1188 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
1189 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1190 UpdateMemoryAccess(image, subresource_range, barrier_action);
1191}
1192
John Zulauf7635de32020-05-29 17:14:15 -06001193// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001194void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1195 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1196 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
1197 bool layout_transition, const ResourceUsageTag &tag) {
1198 if (layout_transition) {
1199 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
1200 tag);
1201 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
1202 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -06001203 } else {
1204 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -06001205 }
John Zulauf355e49b2020-04-24 15:11:15 -06001206}
1207
John Zulauf7635de32020-05-29 17:14:15 -06001208// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001209void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
1210 const VkImageSubresourceRange &subresource_range, bool layout_transition,
1211 const ResourceUsageTag &tag) {
1212 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
1213 subresource_range, layout_transition, tag);
1214}
1215
1216// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001217HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001218 if (!attach_view) return HazardResult();
1219 const auto image_state = attach_view->image_state.get();
1220 if (!image_state) return HazardResult();
1221
John Zulauf355e49b2020-04-24 15:11:15 -06001222 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001223 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001224
1225 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulauf7635de32020-05-29 17:14:15 -06001226 auto hazard = track_back.context->DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope,
1227 track_back.barrier.src_access_scope,
1228 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001229 if (!hazard.hazard) {
1230 // The Async hazard check is against the current context's async set.
John Zulauf7635de32020-05-29 17:14:15 -06001231 hazard = DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope, track_back.barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001232 attach_view->normalized_subresource_range, kDetectAsync);
1233 }
1234 return hazard;
1235}
1236
1237// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1238bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1239
1240 const VkRenderPassBeginInfo *pRenderPassBegin,
1241 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1242 const char *func_name) const {
1243 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1244 bool skip = false;
1245 uint32_t subpass = 0;
1246 const auto &transitions = rp_state.subpass_transitions[subpass];
1247 if (transitions.size()) {
1248 const std::vector<AccessContext> empty_context_vector;
1249 // Create context we can use to validate against...
1250 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1251 const_cast<AccessContext *>(&cb_access_context_));
1252
1253 assert(pRenderPassBegin);
1254 if (nullptr == pRenderPassBegin) return skip;
1255
1256 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1257 assert(fb_state);
1258 if (nullptr == fb_state) return skip;
1259
1260 // Create a limited array of views (which we'll need to toss
1261 std::vector<const IMAGE_VIEW_STATE *> views;
1262 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
1263 const auto attachment_count = count_attachment.first;
1264 const auto *attachments = count_attachment.second;
1265 views.resize(attachment_count, nullptr);
1266 for (const auto &transition : transitions) {
1267 assert(transition.attachment < attachment_count);
1268 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
1269 }
1270
John Zulauf7635de32020-05-29 17:14:15 -06001271 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
1272 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001273 }
1274 return skip;
1275}
1276
1277bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001278 bool skip = false;
John Zulauf1507ee42020-05-18 11:33:09 -06001279 skip |=
1280 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001281
1282 return skip;
1283}
1284
1285bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1286 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001287 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001288 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -06001289 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1290 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001291
1292 return skip;
1293}
1294
1295void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1296 assert(sync_state_);
1297 if (!cb_state_) return;
1298
1299 // Create an access context the current renderpass.
1300 render_pass_contexts_.emplace_back(&cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06001301 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf355e49b2020-04-24 15:11:15 -06001302 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001303 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001304}
1305
John Zulauf355e49b2020-04-24 15:11:15 -06001306void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001307 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001308 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001309 current_context_ = &current_renderpass_context_->CurrentContext();
1310}
1311
John Zulauf355e49b2020-04-24 15:11:15 -06001312void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001313 assert(current_renderpass_context_);
1314 if (!current_renderpass_context_) return;
1315
John Zulauf7635de32020-05-29 17:14:15 -06001316 current_renderpass_context_->RecordEndRenderPass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001317 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001318 current_renderpass_context_ = nullptr;
1319}
1320
John Zulauf1507ee42020-05-18 11:33:09 -06001321bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1322 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001323 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001324 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001325 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1326 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001327 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1328 func_name);
1329
John Zulauf355e49b2020-04-24 15:11:15 -06001330 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06001331 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06001332 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1333 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1334 return skip;
1335}
1336bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
1337 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001338 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06001339 bool skip = false;
1340 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1341 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001342 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1343 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06001344 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001345 return skip;
1346}
1347
John Zulauf7635de32020-05-29 17:14:15 -06001348AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
1349 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
1350}
1351
1352bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
1353 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001354 bool skip = false;
1355
John Zulauf7635de32020-05-29 17:14:15 -06001356 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
1357 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
1358 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1359 // to apply and only copy then, if this proves a hot spot.
1360 std::unique_ptr<AccessContext> proxy_for_current;
1361
John Zulauf355e49b2020-04-24 15:11:15 -06001362 // Validate the "finalLayout" transitions to external
1363 // Get them from where there we're hidding in the extra entry.
1364 const auto &final_transitions = rp_state_->subpass_transitions.back();
1365 for (const auto &transition : final_transitions) {
1366 const auto &attach_view = attachment_views_[transition.attachment];
1367 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
1368 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06001369 auto *context = trackback.context;
1370
1371 if (transition.prev_pass == current_subpass_) {
1372 if (!proxy_for_current) {
1373 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
1374 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
1375 }
1376 context = proxy_for_current.get();
1377 }
1378
1379 auto hazard = context->DetectImageBarrierHazard(
John Zulauf355e49b2020-04-24 15:11:15 -06001380 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
1381 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
1382 if (hazard.hazard) {
1383 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
1384 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
1385 " final image layout transition.",
1386 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment);
1387 }
1388 }
1389 return skip;
1390}
1391
1392void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
1393 // Add layout transitions...
1394 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
1395 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06001396 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06001397 for (const auto &transition : transitions) {
1398 const auto attachment_view = attachment_views_[transition.attachment];
1399 if (!attachment_view) continue;
1400 const auto image = attachment_view->image_state.get();
1401 if (!image) continue;
1402
1403 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -06001404 auto insert_pair = view_seen.insert(attachment_view);
1405 if (insert_pair.second) {
1406 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
1407 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
1408
1409 } else {
1410 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
1411 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
1412 auto barrier_to_transition = barrier->barrier;
1413 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
1414 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
1415 }
John Zulauf355e49b2020-04-24 15:11:15 -06001416 }
1417}
1418
John Zulauf1507ee42020-05-18 11:33:09 -06001419void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
1420 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
1421 auto &subpass_context = subpass_contexts_[current_subpass_];
1422 VkExtent3D extent = CastTo3D(render_area.extent);
1423 VkOffset3D offset = CastTo3D(render_area.offset);
1424
1425 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
1426 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
1427 if (attachment_views_[i] == nullptr) continue; // UNUSED
1428 const auto &view = *attachment_views_[i];
1429 const IMAGE_STATE *image = view.image_state.get();
1430 if (image == nullptr) continue;
1431
1432 const auto &ci = attachment_ci[i];
1433 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001434 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001435 const bool is_color = !(has_depth || has_stencil);
1436
1437 if (is_color) {
1438 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
1439 extent, tag);
1440 } else {
1441 auto update_range = view.normalized_subresource_range;
1442 if (has_depth) {
1443 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1444 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
1445 }
1446 if (has_stencil) {
1447 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1448 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
1449 tag);
1450 }
1451 }
1452 }
1453 }
1454}
1455
John Zulauf355e49b2020-04-24 15:11:15 -06001456void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
1457 VkQueueFlags queue_flags, const ResourceUsageTag &tag) {
1458 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06001459 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06001460 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
1461 // Add this for all subpasses here so that they exsist during next subpass validation
1462 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
1463 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context_);
1464 }
1465 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
1466
1467 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06001468 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001469}
John Zulauf1507ee42020-05-18 11:33:09 -06001470
1471void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001472 // Resolves are against *prior* subpass context and thus *before* the subpass increment
1473 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 current_subpass_++;
1477 assert(current_subpass_ < subpass_contexts_.size());
1478 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06001479 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001480}
1481
John Zulauf7635de32020-05-29 17:14:15 -06001482void RenderPassAccessContext::RecordEndRenderPass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001483 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06001484 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001485 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06001486
John Zulauf355e49b2020-04-24 15:11:15 -06001487 // Export the accesses from the renderpass...
1488 external_context_->ResolveChildContexts(subpass_contexts_);
1489
1490 // Add the "finalLayout" transitions to external
1491 // Get them from where there we're hidding in the extra entry.
1492 const auto &final_transitions = rp_state_->subpass_transitions.back();
1493 for (const auto &transition : final_transitions) {
1494 const auto &attachment = attachment_views_[transition.attachment];
1495 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
1496 assert(external_context_ == last_trackback.context);
1497 external_context_->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
1498 attachment->normalized_subresource_range, true, tag);
1499 }
1500}
1501
John Zulauf3d84f1b2020-03-09 13:33:25 -06001502SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
1503 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
1504 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
1505 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
1506 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
1507 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
1508 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
1509}
1510
1511void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
1512 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
1513 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
1514}
1515
John Zulauf9cb530d2019-09-30 14:14:10 -06001516HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
1517 HazardResult hazard;
1518 auto usage = FlagBit(usage_index);
1519 if (IsRead(usage)) {
John Zulaufc9201222020-05-13 15:13:03 -06001520 if (last_write && IsWriteHazard(usage)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001521 hazard.Set(READ_AFTER_WRITE, write_tag);
1522 }
1523 } else {
1524 // Assume write
1525 // TODO determine what to do with READ-WRITE usage states if any
1526 // Write-After-Write check -- if we have a previous write to test against
1527 if (last_write && IsWriteHazard(usage)) {
1528 hazard.Set(WRITE_AFTER_WRITE, write_tag);
1529 } else {
John Zulauf69133422020-05-20 14:55:53 -06001530 // Look for casus belli for WAR
John Zulauf9cb530d2019-09-30 14:14:10 -06001531 const auto usage_stage = PipelineStageBit(usage_index);
1532 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1533 if (IsReadHazard(usage_stage, last_reads[read_index])) {
1534 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
1535 break;
1536 }
1537 }
1538 }
1539 }
1540 return hazard;
1541}
1542
John Zulauf69133422020-05-20 14:55:53 -06001543HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
1544 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
1545 HazardResult hazard;
1546 const auto usage = FlagBit(usage_index);
1547 const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good.
1548 if (IsRead(usage)) {
1549 if (!write_is_ordered && IsWriteHazard(usage)) {
1550 hazard.Set(READ_AFTER_WRITE, write_tag);
1551 }
1552 } else {
1553 if (!write_is_ordered && IsWriteHazard(usage)) {
1554 hazard.Set(WRITE_AFTER_WRITE, write_tag);
1555 } else {
1556 const auto usage_stage = PipelineStageBit(usage_index);
1557 const auto unordered_reads = last_read_stages & ~ordering.exec_scope;
1558 if (unordered_reads) {
1559 // Look for any WAR hazards outside the ordered set of stages
1560 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1561 if (last_reads[read_index].stage & unordered_reads) {
1562 if (IsReadHazard(usage_stage, last_reads[read_index])) {
1563 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
1564 break;
1565 }
1566 }
1567 }
1568 }
1569 }
1570 }
1571 return hazard;
1572}
1573
John Zulauf2f952d22020-02-10 11:34:51 -07001574// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06001575HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07001576 HazardResult hazard;
1577 auto usage = FlagBit(usage_index);
1578 if (IsRead(usage)) {
1579 if (last_write != 0) {
1580 hazard.Set(READ_RACING_WRITE, write_tag);
1581 }
1582 } else {
1583 if (last_write != 0) {
1584 hazard.Set(WRITE_RACING_WRITE, write_tag);
1585 } else if (last_read_count > 0) {
1586 hazard.Set(WRITE_RACING_READ, last_reads[0].tag);
1587 }
1588 }
1589 return hazard;
1590}
1591
John Zulauf36bcf6a2020-02-03 15:12:52 -07001592HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
1593 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07001594 // Only supporting image layout transitions for now
1595 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
1596 HazardResult hazard;
1597 if (last_write) {
1598 // If the previous write is *not* in the 1st access scope
1599 // *AND* the current barrier is not in the dependency chain
1600 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
1601 // then the barrier access is unsafe (R/W after W)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001602 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
John Zulauf0cb5be22020-01-23 12:18:22 -07001603 // TODO: Do we need a difference hazard name for this?
1604 hazard.Set(WRITE_AFTER_WRITE, write_tag);
1605 }
John Zulauf355e49b2020-04-24 15:11:15 -06001606 }
1607 if (!hazard.hazard) {
1608 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07001609 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07001610 const auto &read_access = last_reads[read_index];
1611 // If the read stage is not in the src sync sync
1612 // *AND* not execution chained with an existing sync barrier (that's the or)
1613 // then the barrier access is unsafe (R/W after R)
1614 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
1615 hazard.Set(WRITE_AFTER_READ, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07001616 break;
1617 }
1618 }
1619 }
1620 return hazard;
1621}
1622
John Zulauf5f13a792020-03-10 07:31:21 -06001623// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
1624// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
1625// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
1626void ResourceAccessState::Resolve(const ResourceAccessState &other) {
1627 if (write_tag.IsBefore(other.write_tag)) {
1628 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
1629 *this = other;
1630 } else if (!other.write_tag.IsBefore(write_tag)) {
1631 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
1632 // dependency chaining logic or any stage expansion)
1633 write_barriers |= other.write_barriers;
1634
1635 // Merge that read states
1636 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
1637 auto &other_read = other.last_reads[other_read_index];
1638 if (last_read_stages & other_read.stage) {
1639 // Merge in the barriers for read stages that exist in *both* this and other
1640 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
1641 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
1642 auto &my_read = last_reads[my_read_index];
1643 if (other_read.stage == my_read.stage) {
1644 if (my_read.tag.IsBefore(other_read.tag)) {
1645 my_read.tag = other_read.tag;
1646 }
1647 my_read.barriers |= other_read.barriers;
1648 break;
1649 }
1650 }
1651 } else {
1652 // The other read stage doesn't exist in this, so add it.
1653 last_reads[last_read_count] = other_read;
1654 last_read_count++;
1655 last_read_stages |= other_read.stage;
1656 }
1657 }
1658 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
1659 // it.
1660}
1661
John Zulauf9cb530d2019-09-30 14:14:10 -06001662void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
1663 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
1664 const auto usage_bit = FlagBit(usage_index);
1665 if (IsRead(usage_index)) {
1666 // Mulitple outstanding reads may be of interest and do dependency chains independently
1667 // However, for purposes of barrier tracking, only one read per pipeline stage matters
1668 const auto usage_stage = PipelineStageBit(usage_index);
1669 if (usage_stage & last_read_stages) {
1670 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1671 ReadState &access = last_reads[read_index];
1672 if (access.stage == usage_stage) {
1673 access.barriers = 0;
1674 access.tag = tag;
1675 break;
1676 }
1677 }
1678 } else {
1679 // We don't have this stage in the list yet...
1680 assert(last_read_count < last_reads.size());
1681 ReadState &access = last_reads[last_read_count++];
1682 access.stage = usage_stage;
1683 access.barriers = 0;
1684 access.tag = tag;
1685 last_read_stages |= usage_stage;
1686 }
1687 } else {
1688 // Assume write
1689 // TODO determine what to do with READ-WRITE operations if any
1690 // Clobber last read and both sets of barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
1691 // if the last_reads/last_write were unsafe, we've reported them,
1692 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
1693 last_read_count = 0;
1694 last_read_stages = 0;
1695
1696 write_barriers = 0;
1697 write_dependency_chain = 0;
1698 write_tag = tag;
1699 last_write = usage_bit;
1700 }
1701}
John Zulauf5f13a792020-03-10 07:31:21 -06001702
John Zulauf9cb530d2019-09-30 14:14:10 -06001703void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
1704 // Execution Barriers only protect read operations
1705 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1706 ReadState &access = last_reads[read_index];
1707 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
1708 if (srcStageMask & (access.stage | access.barriers)) {
1709 access.barriers |= dstStageMask;
1710 }
1711 }
1712 if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask;
1713}
1714
John Zulauf36bcf6a2020-02-03 15:12:52 -07001715void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
1716 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001717 // Assuming we've applied the execution side of this barrier, we update just the write
1718 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07001719 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
1720 write_barriers |= dst_access_scope;
1721 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001722 }
1723}
1724
John Zulaufd1f85d42020-04-15 12:23:15 -06001725void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001726 auto *access_context = GetAccessContextNoInsert(command_buffer);
1727 if (access_context) {
1728 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06001729 }
1730}
1731
John Zulaufd1f85d42020-04-15 12:23:15 -06001732void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
1733 auto access_found = cb_access_state.find(command_buffer);
1734 if (access_found != cb_access_state.end()) {
1735 access_found->second->Reset();
1736 cb_access_state.erase(access_found);
1737 }
1738}
1739
John Zulauf540266b2020-04-06 18:54:53 -06001740void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07001741 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
1742 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06001743 const VkMemoryBarrier *pMemoryBarriers) {
1744 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07001745 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06001746 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06001747 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06001748}
1749
John Zulauf540266b2020-04-06 18:54:53 -06001750void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07001751 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
1752 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06001753 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001754 for (uint32_t index = 0; index < barrier_count; index++) {
locke-lunarg3c038002020-04-30 23:08:08 -06001755 auto barrier = barriers[index];
John Zulauf9cb530d2019-09-30 14:14:10 -06001756 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
1757 if (!buffer) continue;
locke-lunarg3c038002020-04-30 23:08:08 -06001758 barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size);
John Zulauf16adfc92020-04-08 10:28:33 -06001759 ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06001760 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
1761 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1762 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1763 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06001764 }
1765}
1766
John Zulauf540266b2020-04-06 18:54:53 -06001767void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
1768 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
1769 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06001770 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001771 for (uint32_t index = 0; index < barrier_count; index++) {
1772 const auto &barrier = barriers[index];
1773 const auto *image = Get<IMAGE_STATE>(barrier.image);
1774 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06001775 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06001776 bool layout_transition = barrier.oldLayout != barrier.newLayout;
1777 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
1778 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
1779 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
1780 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001781 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001782}
1783
1784bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
1785 uint32_t regionCount, const VkBufferCopy *pRegions) const {
1786 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001787 const auto *cb_context = GetAccessContext(commandBuffer);
1788 assert(cb_context);
1789 if (!cb_context) return skip;
1790 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06001791
John Zulauf3d84f1b2020-03-09 13:33:25 -06001792 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06001793 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001794 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001795
1796 for (uint32_t region = 0; region < regionCount; region++) {
1797 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06001798 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06001799 ResourceAccessRange src_range = MakeRange(
1800 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06001801 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001802 if (hazard.hazard) {
1803 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06001804 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
1805 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1806 report_data->FormatHandle(srcBuffer).c_str(), region);
John Zulauf9cb530d2019-09-30 14:14:10 -06001807 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001808 }
John Zulauf16adfc92020-04-08 10:28:33 -06001809 if (dst_buffer && !skip) {
locke-lunargff255f92020-05-13 18:53:52 -06001810 ResourceAccessRange dst_range = MakeRange(
1811 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf355e49b2020-04-24 15:11:15 -06001812 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001813 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001814 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
1815 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1816 report_data->FormatHandle(dstBuffer).c_str(), region);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001817 }
1818 }
1819 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06001820 }
1821 return skip;
1822}
1823
1824void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
1825 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001826 auto *cb_context = GetAccessContext(commandBuffer);
1827 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001828 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001829 auto *context = cb_context->GetCurrentAccessContext();
1830
John Zulauf9cb530d2019-09-30 14:14:10 -06001831 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06001832 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06001833
1834 for (uint32_t region = 0; region < regionCount; region++) {
1835 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06001836 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06001837 ResourceAccessRange src_range = MakeRange(
1838 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06001839 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001840 }
John Zulauf16adfc92020-04-08 10:28:33 -06001841 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06001842 ResourceAccessRange dst_range = MakeRange(
1843 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06001844 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001845 }
1846 }
1847}
1848
1849bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1850 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1851 const VkImageCopy *pRegions) const {
1852 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001853 const auto *cb_access_context = GetAccessContext(commandBuffer);
1854 assert(cb_access_context);
1855 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07001856
John Zulauf3d84f1b2020-03-09 13:33:25 -06001857 const auto *context = cb_access_context->GetCurrentAccessContext();
1858 assert(context);
1859 if (!context) return skip;
1860
1861 const auto *src_image = Get<IMAGE_STATE>(srcImage);
1862 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001863 for (uint32_t region = 0; region < regionCount; region++) {
1864 const auto &copy_region = pRegions[region];
1865 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001866 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001867 copy_region.srcOffset, copy_region.extent);
1868 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001869 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
1870 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1871 report_data->FormatHandle(srcImage).c_str(), region);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001872 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001873 }
1874
1875 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07001876 VkExtent3D dst_copy_extent =
1877 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001878 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07001879 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001880 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06001881 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
1882 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
1883 report_data->FormatHandle(dstImage).c_str(), region);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001884 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07001885 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07001886 }
1887 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001888
John Zulauf5c5e88d2019-12-26 11:22:02 -07001889 return skip;
1890}
1891
1892void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1893 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
1894 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001895 auto *cb_access_context = GetAccessContext(commandBuffer);
1896 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06001897 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001898 auto *context = cb_access_context->GetCurrentAccessContext();
1899 assert(context);
1900
John Zulauf5c5e88d2019-12-26 11:22:02 -07001901 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001902 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001903
1904 for (uint32_t region = 0; region < regionCount; region++) {
1905 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06001906 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06001907 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
1908 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001909 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001910 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07001911 VkExtent3D dst_copy_extent =
1912 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001913 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
1914 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001915 }
1916 }
1917}
1918
1919bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1920 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1921 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1922 uint32_t bufferMemoryBarrierCount,
1923 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1924 uint32_t imageMemoryBarrierCount,
1925 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
1926 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001927 const auto *cb_access_context = GetAccessContext(commandBuffer);
1928 assert(cb_access_context);
1929 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07001930
John Zulauf3d84f1b2020-03-09 13:33:25 -06001931 const auto *context = cb_access_context->GetCurrentAccessContext();
1932 assert(context);
1933 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07001934
John Zulauf3d84f1b2020-03-09 13:33:25 -06001935 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07001936 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
1937 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07001938 // Validate Image Layout transitions
1939 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
1940 const auto &barrier = pImageMemoryBarriers[index];
1941 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
1942 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
1943 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06001944 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07001945 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06001946 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06001947 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
1948 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s", string_SyncHazard(hazard.hazard),
1949 index, report_data->FormatHandle(barrier.image).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07001950 }
1951 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001952
1953 return skip;
1954}
1955
1956void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1957 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1958 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1959 uint32_t bufferMemoryBarrierCount,
1960 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1961 uint32_t imageMemoryBarrierCount,
1962 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001963 auto *cb_access_context = GetAccessContext(commandBuffer);
1964 assert(cb_access_context);
1965 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06001966 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001967 auto access_context = cb_access_context->GetCurrentAccessContext();
1968 assert(access_context);
1969 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06001970
John Zulauf3d84f1b2020-03-09 13:33:25 -06001971 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07001972 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001973 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07001974 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
1975 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
1976 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001977 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
1978 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06001979 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001980 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001981
1982 // 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 -06001983 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07001984 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06001985}
1986
1987void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
1988 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
1989 // The state tracker sets up the device state
1990 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
1991
John Zulauf5f13a792020-03-10 07:31:21 -06001992 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
1993 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06001994 // TODO: Find a good way to do this hooklessly.
1995 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
1996 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
1997 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
1998
John Zulaufd1f85d42020-04-15 12:23:15 -06001999 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2000 sync_device_state->ResetCommandBufferCallback(command_buffer);
2001 });
2002 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2003 sync_device_state->FreeCommandBufferCallback(command_buffer);
2004 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002005}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002006
John Zulauf355e49b2020-04-24 15:11:15 -06002007bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2008 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2009 bool skip = false;
2010 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2011 auto cb_context = GetAccessContext(commandBuffer);
2012
2013 if (rp_state && cb_context) {
2014 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2015 }
2016
2017 return skip;
2018}
2019
2020bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2021 VkSubpassContents contents) const {
2022 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2023 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2024 subpass_begin_info.contents = contents;
2025 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
2026 return skip;
2027}
2028
2029bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2030 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2031 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2032 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
2033 return skip;
2034}
2035
2036bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2037 const VkRenderPassBeginInfo *pRenderPassBegin,
2038 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2039 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2040 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
2041 return skip;
2042}
2043
John Zulauf3d84f1b2020-03-09 13:33:25 -06002044void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
2045 VkResult result) {
2046 // The state tracker sets up the command buffer state
2047 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
2048
2049 // Create/initialize the structure that trackers accesses at the command buffer scope.
2050 auto cb_access_context = GetAccessContext(commandBuffer);
2051 assert(cb_access_context);
2052 cb_access_context->Reset();
2053}
2054
2055void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06002056 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002057 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002058 if (cb_context) {
2059 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002060 }
2061}
2062
2063void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2064 VkSubpassContents contents) {
2065 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2066 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2067 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002068 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002069}
2070
2071void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2072 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2073 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002074 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002075}
2076
2077void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2078 const VkRenderPassBeginInfo *pRenderPassBegin,
2079 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2080 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002081 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
2082}
2083
2084bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2085 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
2086 bool skip = false;
2087
2088 auto cb_context = GetAccessContext(commandBuffer);
2089 assert(cb_context);
2090 auto cb_state = cb_context->GetCommandBufferState();
2091 if (!cb_state) return skip;
2092
2093 auto rp_state = cb_state->activeRenderPass;
2094 if (!rp_state) return skip;
2095
2096 skip |= cb_context->ValidateNextSubpass(func_name);
2097
2098 return skip;
2099}
2100
2101bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
2102 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
2103 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2104 subpass_begin_info.contents = contents;
2105 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
2106 return skip;
2107}
2108
2109bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2110 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2111 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2112 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
2113 return skip;
2114}
2115
2116bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2117 const VkSubpassEndInfo *pSubpassEndInfo) const {
2118 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2119 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
2120 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002121}
2122
2123void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06002124 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002125 auto cb_context = GetAccessContext(commandBuffer);
2126 assert(cb_context);
2127 auto cb_state = cb_context->GetCommandBufferState();
2128 if (!cb_state) return;
2129
2130 auto rp_state = cb_state->activeRenderPass;
2131 if (!rp_state) return;
2132
John Zulauf355e49b2020-04-24 15:11:15 -06002133 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002134}
2135
2136void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
2137 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
2138 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2139 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002140 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002141}
2142
2143void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2144 const VkSubpassEndInfo *pSubpassEndInfo) {
2145 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002146 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002147}
2148
2149void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2150 const VkSubpassEndInfo *pSubpassEndInfo) {
2151 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002152 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002153}
2154
John Zulauf355e49b2020-04-24 15:11:15 -06002155bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
2156 const char *func_name) const {
2157 bool skip = false;
2158
2159 auto cb_context = GetAccessContext(commandBuffer);
2160 assert(cb_context);
2161 auto cb_state = cb_context->GetCommandBufferState();
2162 if (!cb_state) return skip;
2163
2164 auto rp_state = cb_state->activeRenderPass;
2165 if (!rp_state) return skip;
2166
2167 skip |= cb_context->ValidateEndRenderpass(func_name);
2168 return skip;
2169}
2170
2171bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2172 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
2173 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
2174 return skip;
2175}
2176
2177bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
2178 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2179 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2180 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
2181 return skip;
2182}
2183
2184bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
2185 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2186 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2187 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
2188 return skip;
2189}
2190
2191void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
2192 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06002193 // Resolve the all subpass contexts to the command buffer contexts
2194 auto cb_context = GetAccessContext(commandBuffer);
2195 assert(cb_context);
2196 auto cb_state = cb_context->GetCommandBufferState();
2197 if (!cb_state) return;
2198
locke-lunargaecf2152020-05-12 17:15:41 -06002199 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06002200 if (!rp_state) return;
2201
John Zulauf355e49b2020-04-24 15:11:15 -06002202 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06002203}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002204
2205void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
2206 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002207 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002208}
2209
2210void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
2211 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002212 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002213}
2214
2215void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
2216 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002217 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002218}
locke-lunarga19c71d2020-03-02 18:17:04 -07002219
2220bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2221 VkImageLayout dstImageLayout, uint32_t regionCount,
2222 const VkBufferImageCopy *pRegions) const {
2223 bool skip = false;
2224 const auto *cb_access_context = GetAccessContext(commandBuffer);
2225 assert(cb_access_context);
2226 if (!cb_access_context) return skip;
2227
2228 const auto *context = cb_access_context->GetCurrentAccessContext();
2229 assert(context);
2230 if (!context) return skip;
2231
2232 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07002233 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2234
2235 for (uint32_t region = 0; region < regionCount; region++) {
2236 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002237 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002238 ResourceAccessRange src_range =
2239 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002240 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002241 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002242 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002243 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
2244 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07002245 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region);
2246 }
2247 }
2248 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002249 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002250 copy_region.imageOffset, copy_region.imageExtent);
2251 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002252 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
2253 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07002254 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region);
2255 }
2256 if (skip) break;
2257 }
2258 if (skip) break;
2259 }
2260 return skip;
2261}
2262
2263void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2264 VkImageLayout dstImageLayout, uint32_t regionCount,
2265 const VkBufferImageCopy *pRegions) {
2266 auto *cb_access_context = GetAccessContext(commandBuffer);
2267 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002268 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002269 auto *context = cb_access_context->GetCurrentAccessContext();
2270 assert(context);
2271
2272 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06002273 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002274
2275 for (uint32_t region = 0; region < regionCount; region++) {
2276 const auto &copy_region = pRegions[region];
2277 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002278 ResourceAccessRange src_range =
2279 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002280 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002281 }
2282 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002283 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002284 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002285 }
2286 }
2287}
2288
2289bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
2290 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
2291 const VkBufferImageCopy *pRegions) const {
2292 bool skip = false;
2293 const auto *cb_access_context = GetAccessContext(commandBuffer);
2294 assert(cb_access_context);
2295 if (!cb_access_context) return skip;
2296
2297 const auto *context = cb_access_context->GetCurrentAccessContext();
2298 assert(context);
2299 if (!context) return skip;
2300
2301 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2302 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2303 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
2304 for (uint32_t region = 0; region < regionCount; region++) {
2305 const auto &copy_region = pRegions[region];
2306 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002307 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002308 copy_region.imageOffset, copy_region.imageExtent);
2309 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002310 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
2311 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07002312 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region);
2313 }
2314 }
2315 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06002316 ResourceAccessRange dst_range =
2317 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002318 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002319 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002320 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
2321 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07002322 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region);
2323 }
2324 }
2325 if (skip) break;
2326 }
2327 return skip;
2328}
2329
2330void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2331 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
2332 auto *cb_access_context = GetAccessContext(commandBuffer);
2333 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002334 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07002335 auto *context = cb_access_context->GetCurrentAccessContext();
2336 assert(context);
2337
2338 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002339 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2340 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 -06002341 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07002342
2343 for (uint32_t region = 0; region < regionCount; region++) {
2344 const auto &copy_region = pRegions[region];
2345 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002346 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002347 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002348 }
2349 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002350 ResourceAccessRange dst_range =
2351 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002352 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002353 }
2354 }
2355}
2356
2357bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2358 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2359 const VkImageBlit *pRegions, VkFilter filter) const {
2360 bool skip = false;
2361 const auto *cb_access_context = GetAccessContext(commandBuffer);
2362 assert(cb_access_context);
2363 if (!cb_access_context) return skip;
2364
2365 const auto *context = cb_access_context->GetCurrentAccessContext();
2366 assert(context);
2367 if (!context) return skip;
2368
2369 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2370 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2371
2372 for (uint32_t region = 0; region < regionCount; region++) {
2373 const auto &blit_region = pRegions[region];
2374 if (src_image) {
2375 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
2376 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
2377 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002378 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002379 blit_region.srcOffsets[0], extent);
2380 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002381 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
2382 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
2383 report_data->FormatHandle(srcImage).c_str(), region);
locke-lunarga19c71d2020-03-02 18:17:04 -07002384 }
2385 }
2386
2387 if (dst_image) {
2388 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
2389 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
2390 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002391 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002392 blit_region.dstOffsets[0], extent);
2393 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002394 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
2395 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
2396 report_data->FormatHandle(dstImage).c_str(), region);
locke-lunarga19c71d2020-03-02 18:17:04 -07002397 }
2398 if (skip) break;
2399 }
2400 }
2401
2402 return skip;
2403}
2404
2405void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2406 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2407 const VkImageBlit *pRegions, VkFilter filter) {
2408 auto *cb_access_context = GetAccessContext(commandBuffer);
2409 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002410 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002411 auto *context = cb_access_context->GetCurrentAccessContext();
2412 assert(context);
2413
2414 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002415 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002416
2417 for (uint32_t region = 0; region < regionCount; region++) {
2418 const auto &blit_region = pRegions[region];
2419 if (src_image) {
2420 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
2421 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
2422 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002423 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002424 blit_region.srcOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002425 }
2426 if (dst_image) {
2427 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
2428 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
2429 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002430 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002431 blit_region.dstOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002432 }
2433 }
2434}
locke-lunarg36ba2592020-04-03 09:42:04 -06002435
locke-lunargff255f92020-05-13 18:53:52 -06002436bool SyncValidator::DetectDescriptorSetHazard(const AccessContext &context, const CMD_BUFFER_STATE &cmd,
2437 VkPipelineBindPoint pipelineBindPoint, const char *function) const {
locke-lunarg36ba2592020-04-03 09:42:04 -06002438 bool skip = false;
locke-lunarg3e127c72020-06-09 17:45:28 -06002439 const PIPELINE_STATE *pPipe = nullptr;
2440 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
2441 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(cmd, pipelineBindPoint, &pPipe, &per_sets);
2442 if (!pPipe || !per_sets) {
locke-lunarg36ba2592020-04-03 09:42:04 -06002443 return skip;
2444 }
2445
locke-lunarg36ba2592020-04-03 09:42:04 -06002446 using DescriptorClass = cvdescriptorset::DescriptorClass;
2447 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
2448 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
2449 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
2450 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
2451
2452 for (const auto &set_binding_pair : pPipe->active_slots) {
2453 uint32_t setIndex = set_binding_pair.first;
locke-lunarg3e127c72020-06-09 17:45:28 -06002454 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[setIndex].bound_descriptor_set;
locke-lunarg36ba2592020-04-03 09:42:04 -06002455 for (auto binding_pair : set_binding_pair.second) {
2456 auto binding = binding_pair.first;
2457 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
2458 binding_pair.first);
2459 const auto descriptor_type = binding_it.GetType();
2460 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2461 auto array_idx = 0;
2462
2463 if (binding_it.IsVariableDescriptorCount()) {
2464 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2465 }
2466 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2467 uint32_t index = i - index_range.start;
2468 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2469 if (descriptor->GetClass() == DescriptorClass::ImageSampler || descriptor->GetClass() == DescriptorClass::Image) {
2470 const IMAGE_VIEW_STATE *img_view_state = nullptr;
2471 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
2472 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
2473 } else {
2474 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
2475 }
2476 if (!img_view_state) continue;
2477 SyncStageAccessIndex sync_index;
2478 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
2479 sync_index = SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
2480 } else {
2481 sync_index = SYNC_VERTEX_SHADER_SHADER_READ;
2482 }
2483 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunargff255f92020-05-13 18:53:52 -06002484 auto hazard = context.DetectHazard(*img_state, sync_index, img_view_state->normalized_subresource_range,
locke-lunarg93d68af2020-05-12 17:18:03 -06002485 {0, 0, 0}, img_state->createInfo.extent);
locke-lunarg36ba2592020-04-03 09:42:04 -06002486 if (hazard.hazard) {
2487 skip |= LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
2488 "%s: Hazard %s for %s in %s and %s binding #%d index %d", function,
2489 string_SyncHazard(hazard.hazard),
2490 report_data->FormatHandle(img_view_state->image_view).c_str(),
2491 report_data->FormatHandle(cmd.commandBuffer).c_str(),
2492 report_data->FormatHandle(descriptor_set->GetSet()).c_str(), binding, index);
2493 }
2494 } else if (descriptor->GetClass() == DescriptorClass::TexelBuffer) {
2495 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
2496 if (!buf_view_state) continue;
2497 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
2498 ResourceAccessRange range =
2499 MakeRange(buf_view_state->create_info.offset,
2500 GetRealWholeSize(buf_view_state->create_info.offset, buf_view_state->create_info.range,
2501 buf_state->createInfo.size));
locke-lunargff255f92020-05-13 18:53:52 -06002502 auto hazard = context.DetectHazard(*buf_state, SYNC_VERTEX_SHADER_SHADER_READ, range);
locke-lunarg36ba2592020-04-03 09:42:04 -06002503 if (hazard.hazard) {
2504 skip |= LogError(buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
2505 "%s: Hazard %s for %s in %s and %s binding #%d index %d", function,
2506 string_SyncHazard(hazard.hazard),
2507 report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
2508 report_data->FormatHandle(cmd.commandBuffer).c_str(),
2509 report_data->FormatHandle(descriptor_set->GetSet()).c_str(), binding, index);
2510 }
2511 } else if (descriptor->GetClass() == DescriptorClass::GeneralBuffer) {
2512 auto buf_state = static_cast<const BufferDescriptor *>(descriptor)->GetBufferState();
2513 if (!buf_state) continue;
2514 ResourceAccessRange range = MakeRange(0, buf_state->createInfo.size);
2515 SyncStageAccessIndex sync_index;
2516 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER ||
2517 descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
2518 sync_index = SYNC_VERTEX_SHADER_UNIFORM_READ;
2519 } else {
2520 sync_index = SYNC_VERTEX_SHADER_SHADER_READ;
2521 }
locke-lunargff255f92020-05-13 18:53:52 -06002522 auto hazard = context.DetectHazard(*buf_state, sync_index, range);
locke-lunarg36ba2592020-04-03 09:42:04 -06002523 if (hazard.hazard) {
2524 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
2525 "%s: Hazard %s for %s in %s and %s binding #%d index %d", function,
2526 string_SyncHazard(hazard.hazard), report_data->FormatHandle(buf_state->buffer).c_str(),
2527 report_data->FormatHandle(cmd.commandBuffer).c_str(),
2528 report_data->FormatHandle(descriptor_set->GetSet()).c_str(), binding, index);
2529 }
2530 }
2531 }
2532 }
2533 }
2534 return skip;
2535}
2536
locke-lunargff255f92020-05-13 18:53:52 -06002537void SyncValidator::UpdateDescriptorSetAccessState(AccessContext &context, const ResourceUsageTag &tag, const CMD_BUFFER_STATE &cmd,
locke-lunarg36ba2592020-04-03 09:42:04 -06002538 VkPipelineBindPoint pipelineBindPoint) {
locke-lunarg3e127c72020-06-09 17:45:28 -06002539 const PIPELINE_STATE *pPipe = nullptr;
2540 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
2541 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(cmd, pipelineBindPoint, &pPipe, &per_sets);
2542 if (!pPipe || !per_sets) {
locke-lunarg36ba2592020-04-03 09:42:04 -06002543 return;
2544 }
2545
locke-lunarg36ba2592020-04-03 09:42:04 -06002546 using DescriptorClass = cvdescriptorset::DescriptorClass;
2547 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
2548 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
2549 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
2550 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
2551
2552 for (const auto &set_binding_pair : pPipe->active_slots) {
2553 uint32_t setIndex = set_binding_pair.first;
locke-lunarg3e127c72020-06-09 17:45:28 -06002554 const cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[setIndex].bound_descriptor_set;
locke-lunarg36ba2592020-04-03 09:42:04 -06002555 for (auto binding_pair : set_binding_pair.second) {
2556 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
2557 binding_pair.first);
2558 const auto descriptor_type = binding_it.GetType();
2559 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2560 auto array_idx = 0;
2561
2562 if (binding_it.IsVariableDescriptorCount()) {
2563 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2564 }
2565
2566 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2567 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2568 if (descriptor->GetClass() == DescriptorClass::ImageSampler || descriptor->GetClass() == DescriptorClass::Image) {
2569 const IMAGE_VIEW_STATE *img_view_state = nullptr;
2570 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
2571 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
2572 } else {
2573 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
2574 }
2575 if (!img_view_state) continue;
2576 SyncStageAccessIndex sync_index;
2577 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
2578 sync_index = SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
2579 } else {
2580 sync_index = SYNC_VERTEX_SHADER_SHADER_READ;
2581 }
2582 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunargff255f92020-05-13 18:53:52 -06002583 context.UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range, {0, 0, 0},
locke-lunarg93d68af2020-05-12 17:18:03 -06002584 img_state->createInfo.extent, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06002585
2586 } else if (descriptor->GetClass() == DescriptorClass::TexelBuffer) {
2587 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
2588 if (!buf_view_state) continue;
2589 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
2590 ResourceAccessRange range = MakeRange(buf_view_state->create_info.offset, buf_view_state->create_info.range);
locke-lunargff255f92020-05-13 18:53:52 -06002591 context.UpdateAccessState(*buf_state, SYNC_VERTEX_SHADER_SHADER_READ, range, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06002592
2593 } else if (descriptor->GetClass() == DescriptorClass::GeneralBuffer) {
2594 auto buf_state = static_cast<const BufferDescriptor *>(descriptor)->GetBufferState();
2595 if (!buf_state) continue;
2596 SyncStageAccessIndex sync_index;
2597 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER ||
2598 descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
2599 sync_index = SYNC_VERTEX_SHADER_UNIFORM_READ;
2600 } else {
2601 sync_index = SYNC_VERTEX_SHADER_SHADER_READ;
2602 }
2603 ResourceAccessRange range = MakeRange(0, buf_state->createInfo.size);
locke-lunargff255f92020-05-13 18:53:52 -06002604 context.UpdateAccessState(*buf_state, sync_index, range, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06002605 }
2606 }
2607 }
2608 }
2609}
2610
locke-lunargff255f92020-05-13 18:53:52 -06002611bool SyncValidator::DetectVertexHazard(const AccessContext &context, const CMD_BUFFER_STATE &cmd, uint32_t vertexCount,
2612 uint32_t firstVertex, const char *function) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06002613 bool skip = false;
locke-lunarg3e127c72020-06-09 17:45:28 -06002614 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002615 if (!pPipe) {
2616 return skip;
2617 }
2618
locke-lunarga4d39ea2020-05-22 14:17:29 -06002619 const auto &binding_buffers = cmd.current_vertex_buffer_binding_info.vertex_buffer_bindings;
2620 const auto &binding_buffers_size = binding_buffers.size();
2621 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
2622
2623 for (size_t i = 0; i < binding_descriptions_size; ++i) {
2624 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
2625 if (binding_description.binding < binding_buffers_size) {
2626 const auto &binding_buffer = binding_buffers[binding_description.binding];
2627 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
2628
2629 auto *buf_state = Get<BUFFER_STATE>(binding_buffer.buffer);
locke-lunargff255f92020-05-13 18:53:52 -06002630 VkDeviceSize range_start = 0;
locke-lunarga4d39ea2020-05-22 14:17:29 -06002631 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -06002632 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
2633 binding_description.stride);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002634 ResourceAccessRange range = MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -06002635 auto hazard = context.DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002636 if (hazard.hazard) {
2637 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s",
2638 function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(buf_state->buffer).c_str(),
2639 report_data->FormatHandle(cmd.commandBuffer).c_str());
2640 }
2641 }
2642 }
2643 return skip;
2644}
2645
locke-lunargff255f92020-05-13 18:53:52 -06002646void SyncValidator::UpdateVertexAccessState(AccessContext &context, const ResourceUsageTag &tag, const CMD_BUFFER_STATE &cmd,
2647 uint32_t vertexCount, uint32_t firstVertex) {
locke-lunarg3e127c72020-06-09 17:45:28 -06002648 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002649 if (!pPipe) {
2650 return;
2651 }
locke-lunarga4d39ea2020-05-22 14:17:29 -06002652 const auto &binding_buffers = cmd.current_vertex_buffer_binding_info.vertex_buffer_bindings;
2653 const auto &binding_buffers_size = binding_buffers.size();
2654 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
2655
2656 for (size_t i = 0; i < binding_descriptions_size; ++i) {
2657 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
2658 if (binding_description.binding < binding_buffers_size) {
2659 const auto &binding_buffer = binding_buffers[binding_description.binding];
2660 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
2661
2662 auto *buf_state = Get<BUFFER_STATE>(binding_buffer.buffer);
locke-lunargff255f92020-05-13 18:53:52 -06002663 VkDeviceSize range_start = 0;
locke-lunarga4d39ea2020-05-22 14:17:29 -06002664 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -06002665 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
2666 binding_description.stride);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002667 ResourceAccessRange range = MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -06002668 context.UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002669 }
2670 }
2671}
2672
locke-lunargff255f92020-05-13 18:53:52 -06002673bool SyncValidator::DetectVertexIndexHazard(const AccessContext &context, const CMD_BUFFER_STATE &cmd, uint32_t indexCount,
2674 uint32_t firstIndex, const char *function) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06002675 bool skip = false;
2676 if (cmd.index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
2677
locke-lunarga4d39ea2020-05-22 14:17:29 -06002678 auto *index_buf_state = Get<BUFFER_STATE>(cmd.index_buffer_binding.buffer);
2679 const auto index_size = GetIndexAlignment(cmd.index_buffer_binding.index_type);
locke-lunargff255f92020-05-13 18:53:52 -06002680 VkDeviceSize range_start = 0;
2681 VkDeviceSize range_size = 0;
2682 GetBufferRange(range_start, range_size, cmd.index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
2683 indexCount, index_size);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002684 ResourceAccessRange range = MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -06002685 auto hazard = context.DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002686 if (hazard.hazard) {
2687 skip |= LogError(index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s",
2688 function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(index_buf_state->buffer).c_str(),
2689 report_data->FormatHandle(cmd.commandBuffer).c_str());
2690 }
2691
2692 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2693 // We will detect more accurate range in the future.
locke-lunargff255f92020-05-13 18:53:52 -06002694 skip |= DetectVertexHazard(context, cmd, UINT32_MAX, 0, function);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002695 return skip;
2696}
2697
locke-lunargff255f92020-05-13 18:53:52 -06002698void SyncValidator::UpdateVertexIndexAccessState(AccessContext &context, const ResourceUsageTag &tag, const CMD_BUFFER_STATE &cmd,
2699 uint32_t indexCount, uint32_t firstIndex) {
locke-lunarga4d39ea2020-05-22 14:17:29 -06002700 if (cmd.index_buffer_binding.buffer == VK_NULL_HANDLE) return;
2701
locke-lunarga4d39ea2020-05-22 14:17:29 -06002702 auto *index_buf_state = Get<BUFFER_STATE>(cmd.index_buffer_binding.buffer);
2703 const auto index_size = GetIndexAlignment(cmd.index_buffer_binding.index_type);
locke-lunargff255f92020-05-13 18:53:52 -06002704 VkDeviceSize range_start = 0;
2705 VkDeviceSize range_size = 0;
2706 GetBufferRange(range_start, range_size, cmd.index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
2707 indexCount, index_size);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002708 ResourceAccessRange range = MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -06002709 context.UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002710
2711 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2712 // We will detect more accurate range in the future.
locke-lunargff255f92020-05-13 18:53:52 -06002713 UpdateVertexAccessState(context, tag, cmd, UINT32_MAX, 0);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002714}
2715
locke-lunargff255f92020-05-13 18:53:52 -06002716bool SyncValidator::DetectSubpassAttachmentHazard(const AccessContext &context, const CMD_BUFFER_STATE &cmd,
2717 const char *function) const {
locke-lunarg93d68af2020-05-12 17:18:03 -06002718 bool skip = false;
2719
locke-lunarg93d68af2020-05-12 17:18:03 -06002720 const auto &subpass = cmd.activeRenderPass->createInfo.pSubpasses[cmd.activeSubpass];
2721 const auto *framebuffer = cmd.activeFramebuffer.get();
2722 VkExtent3D framebuffer_extent = {framebuffer->createInfo.width, framebuffer->createInfo.height, framebuffer->createInfo.layers};
2723
locke-lunargff255f92020-05-13 18:53:52 -06002724 auto dtct_fn = [&cmd, &function, &framebuffer, &framebuffer_extent, &context](
locke-lunarg93d68af2020-05-12 17:18:03 -06002725 const SyncValidator &this_, const safe_VkAttachmentReference2 &attachment_ref,
2726 const SyncStageAccessIndex sync_index, const std::string &attachment_desription) {
2727 if (attachment_ref.attachment == VK_ATTACHMENT_UNUSED) return false;
2728 auto attachment_index = attachment_ref.attachment;
2729 if (framebuffer->createInfo.attachmentCount > attachment_index) {
2730 const IMAGE_VIEW_STATE *img_view_state =
2731 this_.Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[attachment_index]);
2732 if (!img_view_state) return false;
2733 const IMAGE_STATE *img_state = img_view_state->image_state.get();
2734 HazardResult hazard;
2735 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
locke-lunargff255f92020-05-13 18:53:52 -06002736 hazard = context.DetectHazard(*img_state, sync_index, img_view_state->normalized_subresource_range, {0, 0, 0},
locke-lunarg93d68af2020-05-12 17:18:03 -06002737 framebuffer_extent);
2738 } else if (sync_index == SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE) {
2739 if (attachment_desription.compare("pDepthStencilAttachment") == 0) {
locke-lunargff255f92020-05-13 18:53:52 -06002740 hazard = context.DetectHazard(*img_state, sync_index, img_view_state->normalized_subresource_range,
locke-lunarg93d68af2020-05-12 17:18:03 -06002741 kDepthStencilAttachmentRasterOrder, {0, 0, 0}, framebuffer_extent);
2742 } else {
locke-lunargff255f92020-05-13 18:53:52 -06002743 hazard = context.DetectHazard(*img_state, sync_index, img_view_state->normalized_subresource_range,
locke-lunarg93d68af2020-05-12 17:18:03 -06002744 kColorAttachmentRasterOrder, {0, 0, 0}, framebuffer_extent);
2745 }
2746 }
2747
2748 if (hazard.hazard) {
2749 return this_.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
2750 "%s: Hazard %s for %s in %s, Subpass #%d, and %s", function, string_SyncHazard(hazard.hazard),
2751 this_.report_data->FormatHandle(img_view_state->image_view).c_str(),
2752 this_.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
2753 attachment_desription.c_str());
2754 }
2755 }
2756 return false;
2757 };
2758
2759 if (subpass.inputAttachmentCount && subpass.pInputAttachments) {
2760 for (uint32_t i = 0; i < subpass.inputAttachmentCount; ++i) {
2761 std::string attachment_desription = "pInputAttachments #" + std::to_string(i);
2762 skip |= dtct_fn(*this, subpass.pInputAttachments[i], SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, attachment_desription);
2763 }
2764 }
2765 if (subpass.colorAttachmentCount) {
2766 if (subpass.pColorAttachments) {
2767 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
2768 std::string attachment_desription = "pColorAttachments #" + std::to_string(i);
2769 skip |= dtct_fn(*this, subpass.pColorAttachments[i], SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
2770 attachment_desription);
2771 }
2772 }
2773 if (subpass.pResolveAttachments) {
2774 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
2775 std::string attachment_desription = "pResolveAttachments #" + std::to_string(i);
2776 skip |= dtct_fn(*this, subpass.pResolveAttachments[i], SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
2777 attachment_desription);
2778 }
2779 }
2780 }
2781 if (subpass.pDepthStencilAttachment) {
2782 skip |= dtct_fn(*this, *subpass.pDepthStencilAttachment, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
2783 "pDepthStencilAttachment");
2784 }
2785 return skip;
2786}
2787
locke-lunargff255f92020-05-13 18:53:52 -06002788void SyncValidator::UpdateSubpassAttachmentAccessState(AccessContext &context, const ResourceUsageTag &tag,
2789 const CMD_BUFFER_STATE &cmd) {
locke-lunarg93d68af2020-05-12 17:18:03 -06002790 const auto &subpass = cmd.activeRenderPass->createInfo.pSubpasses[cmd.activeSubpass];
2791 const auto *framebuffer = cmd.activeFramebuffer.get();
2792 VkExtent3D framebuffer_extent = {framebuffer->createInfo.width, framebuffer->createInfo.height, framebuffer->createInfo.layers};
2793
locke-lunargff255f92020-05-13 18:53:52 -06002794 auto updt_fn = [&framebuffer, &framebuffer_extent, &context, &tag](const SyncValidator &this_,
2795 const safe_VkAttachmentReference2 &attachment_ref,
2796 const SyncStageAccessIndex sync_index) {
locke-lunarg93d68af2020-05-12 17:18:03 -06002797 if (attachment_ref.attachment == VK_ATTACHMENT_UNUSED) return;
2798 auto attachment_index = attachment_ref.attachment;
2799 if (framebuffer->createInfo.attachmentCount > attachment_index) {
2800 const IMAGE_VIEW_STATE *img_view_state =
2801 this_.Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[attachment_index]);
2802 if (!img_view_state) return;
2803 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunargff255f92020-05-13 18:53:52 -06002804 context.UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range, {0, 0, 0},
locke-lunarg93d68af2020-05-12 17:18:03 -06002805 framebuffer_extent, tag);
2806 }
2807 };
2808
2809 if (subpass.inputAttachmentCount && subpass.pInputAttachments) {
2810 for (uint32_t i = 0; i < subpass.inputAttachmentCount; ++i) {
2811 updt_fn(*this, subpass.pInputAttachments[i], SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2812 }
2813 }
2814 if (subpass.colorAttachmentCount) {
2815 if (subpass.pColorAttachments) {
2816 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
2817 updt_fn(*this, subpass.pColorAttachments[i], SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE);
2818 }
2819 }
2820 if (subpass.pResolveAttachments) {
2821 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
2822 updt_fn(*this, subpass.pResolveAttachments[i], SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE);
2823 }
2824 }
2825 }
2826 if (subpass.pDepthStencilAttachment) {
2827 updt_fn(*this, *subpass.pDepthStencilAttachment, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE);
2828 }
2829}
2830
locke-lunargff255f92020-05-13 18:53:52 -06002831bool SyncValidator::DetectIndirectBufferHazard(const AccessContext &context, VkCommandBuffer commandBuffer,
2832 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
2833 const uint32_t drawCount, const uint32_t stride, const char *function) const {
2834 bool skip = false;
2835 if (drawCount == 0) return skip;
2836
2837 const auto *buf_state = Get<BUFFER_STATE>(buffer);
2838 VkDeviceSize size = struct_size;
2839 if (drawCount == 1 || stride == size) {
2840 if (drawCount > 1) size *= drawCount;
2841 ResourceAccessRange range = MakeRange(offset, size);
2842 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
2843 if (hazard.hazard) {
2844 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for indirect %s in %s",
2845 function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(buffer).c_str(),
2846 report_data->FormatHandle(commandBuffer).c_str());
2847 }
2848 } else {
2849 for (uint32_t i = 0; i < drawCount; ++i) {
2850 ResourceAccessRange range = MakeRange(offset + i * stride, size);
2851 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
2852 if (hazard.hazard) {
2853 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for indirect %s in %s",
2854 function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(buffer).c_str(),
2855 report_data->FormatHandle(commandBuffer).c_str());
2856 break;
2857 }
2858 }
2859 }
2860 return skip;
2861}
2862
2863void SyncValidator::UpdateIndirectBufferAccessState(AccessContext &context, const ResourceUsageTag &tag,
2864 const VkDeviceSize struct_size, const VkBuffer buffer,
2865 const VkDeviceSize offset, const uint32_t drawCount, uint32_t stride) {
2866 const auto *buf_state = Get<BUFFER_STATE>(buffer);
2867 VkDeviceSize size = struct_size;
2868 if (drawCount == 1 || stride == size) {
2869 if (drawCount > 1) size *= drawCount;
2870 ResourceAccessRange range = MakeRange(offset, size);
2871 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
2872 } else {
2873 for (uint32_t i = 0; i < drawCount; ++i) {
2874 ResourceAccessRange range = MakeRange(offset + i * stride, size);
2875 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
2876 }
2877 }
2878}
2879
2880bool SyncValidator::DetectCountBufferHazard(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
2881 VkDeviceSize offset, const char *function) const {
2882 bool skip = false;
2883
2884 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
2885 ResourceAccessRange range = MakeRange(offset, 4);
2886 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
2887 if (hazard.hazard) {
2888 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for countBuffer %s in %s",
2889 function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(buffer).c_str(),
2890 report_data->FormatHandle(commandBuffer).c_str());
2891 }
2892 return skip;
2893}
2894
2895void SyncValidator::UpdateCountBufferAccessState(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer,
2896 VkDeviceSize offset) {
2897 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
2898 ResourceAccessRange range = MakeRange(offset, 4);
2899 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
2900}
2901
locke-lunarg36ba2592020-04-03 09:42:04 -06002902bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06002903 bool skip = false;
2904 ;
locke-lunarg36ba2592020-04-03 09:42:04 -06002905 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06002906 const auto *cb_access_context = GetAccessContext(commandBuffer);
2907 assert(cb_access_context);
2908 if (!cb_access_context) return skip;
2909
2910 const auto *context = cb_access_context->GetCurrentAccessContext();
2911 assert(context);
2912 if (!context) return skip;
2913
2914 skip |= DetectDescriptorSetHazard(*context, *cb_state, VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
2915 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06002916}
2917
2918void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
2919 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06002920 auto *cb_access_context = GetAccessContext(commandBuffer);
2921 assert(cb_access_context);
2922 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
2923 auto *context = cb_access_context->GetCurrentAccessContext();
2924 assert(context);
2925
2926 UpdateDescriptorSetAccessState(*context, tag, *cb_state, VK_PIPELINE_BIND_POINT_COMPUTE);
locke-lunarg36ba2592020-04-03 09:42:04 -06002927}
locke-lunarge1a67022020-04-29 00:15:36 -06002928
2929bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06002930 bool skip = false;
locke-lunarge1a67022020-04-29 00:15:36 -06002931 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06002932 const auto *cb_access_context = GetAccessContext(commandBuffer);
2933 assert(cb_access_context);
2934 if (!cb_access_context) return skip;
2935
2936 const auto *context = cb_access_context->GetCurrentAccessContext();
2937 assert(context);
2938 if (!context) return skip;
2939
2940 skip |= DetectDescriptorSetHazard(*context, *cb_state, VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
2941 skip |= DetectIndirectBufferHazard(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
2942 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
2943 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06002944}
2945
2946void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
2947 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06002948 auto *cb_access_context = GetAccessContext(commandBuffer);
2949 assert(cb_access_context);
2950 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
2951 auto *context = cb_access_context->GetCurrentAccessContext();
2952 assert(context);
2953
2954 UpdateDescriptorSetAccessState(*context, tag, *cb_state, VK_PIPELINE_BIND_POINT_COMPUTE);
2955 UpdateIndirectBufferAccessState(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
2956 sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06002957}
2958
2959bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2960 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06002961 bool skip = false;
locke-lunarge1a67022020-04-29 00:15:36 -06002962 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06002963 const auto *cb_access_context = GetAccessContext(commandBuffer);
2964 assert(cb_access_context);
2965 if (!cb_access_context) return skip;
2966
2967 const auto *context = cb_access_context->GetCurrentAccessContext();
2968 assert(context);
2969 if (!context) return skip;
2970
2971 skip |= DetectDescriptorSetHazard(*context, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
2972 skip |= DetectVertexHazard(*context, *cb_state, vertexCount, firstVertex, "vkCmdDraw");
2973 skip |= DetectSubpassAttachmentHazard(*context, *cb_state, "vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06002974 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06002975}
2976
2977void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2978 uint32_t firstVertex, uint32_t firstInstance) {
2979 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06002980 auto *cb_access_context = GetAccessContext(commandBuffer);
2981 assert(cb_access_context);
2982 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
2983 auto *context = cb_access_context->GetCurrentAccessContext();
2984 assert(context);
2985
2986 UpdateDescriptorSetAccessState(*context, tag, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS);
2987 UpdateVertexAccessState(*context, tag, *cb_state, vertexCount, firstVertex);
2988 UpdateSubpassAttachmentAccessState(*context, tag, *cb_state);
locke-lunarge1a67022020-04-29 00:15:36 -06002989}
2990
2991bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2992 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06002993 bool skip = false;
locke-lunarge1a67022020-04-29 00:15:36 -06002994 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06002995 const auto *cb_access_context = GetAccessContext(commandBuffer);
2996 assert(cb_access_context);
2997 if (!cb_access_context) return skip;
2998
2999 const auto *context = cb_access_context->GetCurrentAccessContext();
3000 assert(context);
3001 if (!context) return skip;
3002
3003 skip |= DetectDescriptorSetHazard(*context, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3004 skip |= DetectVertexIndexHazard(*context, *cb_state, indexCount, firstIndex, "vkCmdDrawIndexed");
3005 skip |= DetectSubpassAttachmentHazard(*context, *cb_state, "vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003006 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003007}
3008
3009void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3010 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
3011 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06003012 auto *cb_access_context = GetAccessContext(commandBuffer);
3013 assert(cb_access_context);
3014 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
3015 auto *context = cb_access_context->GetCurrentAccessContext();
3016 assert(context);
3017
3018 UpdateDescriptorSetAccessState(*context, tag, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS);
3019 UpdateVertexAccessState(*context, tag, *cb_state, indexCount, firstIndex);
3020 UpdateSubpassAttachmentAccessState(*context, tag, *cb_state);
locke-lunarge1a67022020-04-29 00:15:36 -06003021}
3022
3023bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3024 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003025 bool skip = false;
3026 if (drawCount == 0) return skip;
3027
locke-lunarge1a67022020-04-29 00:15:36 -06003028 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06003029 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 skip |= DetectDescriptorSetHazard(*context, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3038 skip |= DetectSubpassAttachmentHazard(*context, *cb_state, "vkCmdDrawIndirect");
3039 skip |= DetectIndirectBufferHazard(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3040 "vkCmdDrawIndirect");
3041
3042 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3043 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3044 // We will validate the vertex buffer in SubmitQueue in the future.
3045 skip |= DetectVertexHazard(*context, *cb_state, UINT32_MAX, 0, "vkCmdDrawIndirect");
3046 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003047}
3048
3049void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3050 uint32_t drawCount, uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003051 if (drawCount == 0) return;
locke-lunarge1a67022020-04-29 00:15:36 -06003052 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06003053 auto *cb_access_context = GetAccessContext(commandBuffer);
3054 assert(cb_access_context);
3055 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3056 auto *context = cb_access_context->GetCurrentAccessContext();
3057 assert(context);
3058
3059 UpdateDescriptorSetAccessState(*context, tag, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS);
3060 UpdateSubpassAttachmentAccessState(*context, tag, *cb_state);
3061 UpdateSubpassAttachmentAccessState(*context, tag, *cb_state);
3062 UpdateIndirectBufferAccessState(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
3063
3064 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3065 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3066 // We will record the vertex buffer in SubmitQueue in the future.
3067 UpdateVertexAccessState(*context, tag, *cb_state, UINT32_MAX, 0);
locke-lunarge1a67022020-04-29 00:15:36 -06003068}
3069
3070bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3071 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003072 bool skip = false;
3073 if (drawCount == 0) return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003074 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06003075 const auto *cb_access_context = GetAccessContext(commandBuffer);
3076 assert(cb_access_context);
3077 if (!cb_access_context) return skip;
3078
3079 const auto *context = cb_access_context->GetCurrentAccessContext();
3080 assert(context);
3081 if (!context) return skip;
3082
3083 skip |= DetectDescriptorSetHazard(*context, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3084 skip |= DetectSubpassAttachmentHazard(*context, *cb_state, "vkCmdDrawIndexedIndirect");
3085 skip |= DetectIndirectBufferHazard(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount,
3086 stride, "vkCmdDrawIndexedIndirect");
3087
3088 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3089 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3090 // We will validate the index and vertex buffer in SubmitQueue in the future.
3091 skip |= DetectVertexIndexHazard(*context, *cb_state, UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
3092 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003093}
3094
3095void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3096 uint32_t drawCount, uint32_t stride) {
3097 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06003098 auto *cb_access_context = GetAccessContext(commandBuffer);
3099 assert(cb_access_context);
3100 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3101 auto *context = cb_access_context->GetCurrentAccessContext();
3102 assert(context);
3103
3104 UpdateDescriptorSetAccessState(*context, tag, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS);
3105 UpdateSubpassAttachmentAccessState(*context, tag, *cb_state);
3106 UpdateIndirectBufferAccessState(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
3107
3108 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3109 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3110 // We will record the index and vertex buffer in SubmitQueue in the future.
3111 UpdateVertexIndexAccessState(*context, tag, *cb_state, UINT32_MAX, 0);
3112}
3113
3114bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3115 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3116 uint32_t stride, const char *function) const {
3117 bool skip = false;
3118 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
3119 const auto *cb_access_context = GetAccessContext(commandBuffer);
3120 assert(cb_access_context);
3121 if (!cb_access_context) return skip;
3122
3123 const auto *context = cb_access_context->GetCurrentAccessContext();
3124 assert(context);
3125 if (!context) return skip;
3126
3127 skip |= DetectDescriptorSetHazard(*context, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3128 skip |= DetectSubpassAttachmentHazard(*context, *cb_state, function);
3129 skip |= DetectIndirectBufferHazard(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3130 function);
3131 skip |= DetectCountBufferHazard(*context, commandBuffer, countBuffer, countBufferOffset, function);
3132
3133 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3134 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3135 // We will validate the vertex buffer in SubmitQueue in the future.
3136 skip |= DetectVertexHazard(*context, *cb_state, UINT32_MAX, 0, "vkCmdDrawIndirectCount");
3137 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003138}
3139
3140bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3141 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3142 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003143 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3144 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003145}
3146
3147void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3148 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3149 uint32_t stride) {
3150 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06003151 auto *cb_access_context = GetAccessContext(commandBuffer);
3152 assert(cb_access_context);
3153 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3154 auto *context = cb_access_context->GetCurrentAccessContext();
3155 assert(context);
3156
3157 UpdateDescriptorSetAccessState(*context, tag, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS);
3158 UpdateSubpassAttachmentAccessState(*context, tag, *cb_state);
3159 UpdateIndirectBufferAccessState(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3160 UpdateCountBufferAccessState(*context, tag, countBuffer, countBufferOffset);
3161
3162 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3163 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3164 // We will record the vertex buffer in SubmitQueue in the future.
3165 UpdateVertexAccessState(*context, tag, *cb_state, UINT32_MAX, 0);
locke-lunarge1a67022020-04-29 00:15:36 -06003166}
3167
3168bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3169 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3170 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003171 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3172 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003173}
3174
3175void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3176 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3177 uint32_t maxDrawCount, uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003178 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003179}
3180
3181bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3182 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3183 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003184 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3185 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003186}
3187
3188void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3189 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3190 uint32_t maxDrawCount, uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003191 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3192}
3193
3194bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3195 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3196 uint32_t stride, const char *function) const {
3197 bool skip = false;
3198 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
3199 const auto *cb_access_context = GetAccessContext(commandBuffer);
3200 assert(cb_access_context);
3201 if (!cb_access_context) return skip;
3202
3203 const auto *context = cb_access_context->GetCurrentAccessContext();
3204 assert(context);
3205 if (!context) return skip;
3206
3207 skip |= DetectDescriptorSetHazard(*context, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3208 skip |= DetectSubpassAttachmentHazard(*context, *cb_state, function);
3209 skip |= DetectIndirectBufferHazard(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3210 stride, function);
3211 skip |= DetectCountBufferHazard(*context, commandBuffer, countBuffer, countBufferOffset, function);
3212
3213 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3214 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3215 // We will validate the index and vertex buffer in SubmitQueue in the future.
3216 skip |= DetectVertexIndexHazard(*context, *cb_state, UINT32_MAX, 0, "vkCmdDrawIndexedIndirectCount");
3217 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003218}
3219
3220bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3221 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3222 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003223 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3224 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003225}
3226
3227void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3228 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3229 uint32_t maxDrawCount, uint32_t stride) {
3230 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06003231 auto *cb_access_context = GetAccessContext(commandBuffer);
3232 assert(cb_access_context);
3233 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3234 auto *context = cb_access_context->GetCurrentAccessContext();
3235 assert(context);
3236
3237 UpdateDescriptorSetAccessState(*context, tag, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS);
3238 UpdateSubpassAttachmentAccessState(*context, tag, *cb_state);
3239 UpdateIndirectBufferAccessState(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3240 UpdateCountBufferAccessState(*context, tag, countBuffer, countBufferOffset);
3241
3242 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3243 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3244 UpdateVertexIndexAccessState(*context, tag, *cb_state, UINT32_MAX, 0);
locke-lunarge1a67022020-04-29 00:15:36 -06003245}
3246
3247bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3248 VkDeviceSize offset, VkBuffer countBuffer,
3249 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3250 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003251 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3252 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003253}
3254
3255void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3256 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3257 uint32_t maxDrawCount, uint32_t stride) {
3258 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3259}
3260
3261bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3262 VkDeviceSize offset, VkBuffer countBuffer,
3263 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3264 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003265 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3266 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003267}
3268
3269void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3270 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3271 uint32_t maxDrawCount, uint32_t stride) {
3272 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3273}
3274
3275bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3276 const VkClearColorValue *pColor, uint32_t rangeCount,
3277 const VkImageSubresourceRange *pRanges) const {
3278 bool skip = false;
3279 const auto *cb_access_context = GetAccessContext(commandBuffer);
3280 assert(cb_access_context);
3281 if (!cb_access_context) return skip;
3282
3283 const auto *context = cb_access_context->GetCurrentAccessContext();
3284 assert(context);
3285 if (!context) return skip;
3286
3287 const auto *image_state = Get<IMAGE_STATE>(image);
3288
3289 for (uint32_t index = 0; index < rangeCount; index++) {
3290 const auto &range = pRanges[index];
3291 if (image_state) {
3292 auto hazard =
3293 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3294 if (hazard.hazard) {
3295 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
3296 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32, string_SyncHazard(hazard.hazard),
3297 report_data->FormatHandle(image).c_str(), index);
3298 }
3299 }
3300 }
3301 return skip;
3302}
3303
3304void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3305 const VkClearColorValue *pColor, uint32_t rangeCount,
3306 const VkImageSubresourceRange *pRanges) {
3307 auto *cb_access_context = GetAccessContext(commandBuffer);
3308 assert(cb_access_context);
3309 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
3310 auto *context = cb_access_context->GetCurrentAccessContext();
3311 assert(context);
3312
3313 const auto *image_state = Get<IMAGE_STATE>(image);
3314
3315 for (uint32_t index = 0; index < rangeCount; index++) {
3316 const auto &range = pRanges[index];
3317 if (image_state) {
3318 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3319 tag);
3320 }
3321 }
3322}
3323
3324bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3325 VkImageLayout imageLayout,
3326 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3327 const VkImageSubresourceRange *pRanges) const {
3328 bool skip = false;
3329 const auto *cb_access_context = GetAccessContext(commandBuffer);
3330 assert(cb_access_context);
3331 if (!cb_access_context) return skip;
3332
3333 const auto *context = cb_access_context->GetCurrentAccessContext();
3334 assert(context);
3335 if (!context) return skip;
3336
3337 const auto *image_state = Get<IMAGE_STATE>(image);
3338
3339 for (uint32_t index = 0; index < rangeCount; index++) {
3340 const auto &range = pRanges[index];
3341 if (image_state) {
3342 auto hazard =
3343 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3344 if (hazard.hazard) {
3345 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
3346 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32,
3347 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index);
3348 }
3349 }
3350 }
3351 return skip;
3352}
3353
3354void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3355 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3356 const VkImageSubresourceRange *pRanges) {
3357 auto *cb_access_context = GetAccessContext(commandBuffer);
3358 assert(cb_access_context);
3359 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
3360 auto *context = cb_access_context->GetCurrentAccessContext();
3361 assert(context);
3362
3363 const auto *image_state = Get<IMAGE_STATE>(image);
3364
3365 for (uint32_t index = 0; index < rangeCount; index++) {
3366 const auto &range = pRanges[index];
3367 if (image_state) {
3368 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3369 tag);
3370 }
3371 }
3372}
3373
3374bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
3375 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
3376 VkDeviceSize dstOffset, VkDeviceSize stride,
3377 VkQueryResultFlags flags) const {
3378 bool skip = false;
3379 const auto *cb_access_context = GetAccessContext(commandBuffer);
3380 assert(cb_access_context);
3381 if (!cb_access_context) return skip;
3382
3383 const auto *context = cb_access_context->GetCurrentAccessContext();
3384 assert(context);
3385 if (!context) return skip;
3386
3387 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3388
3389 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003390 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003391 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3392 if (hazard.hazard) {
3393 skip |=
3394 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s",
3395 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3396 }
3397 }
locke-lunargff255f92020-05-13 18:53:52 -06003398
3399 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003400 return skip;
3401}
3402
3403void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
3404 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3405 VkDeviceSize stride, VkQueryResultFlags flags) {
3406 auto *cb_access_context = GetAccessContext(commandBuffer);
3407 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06003408 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06003409 auto *context = cb_access_context->GetCurrentAccessContext();
3410 assert(context);
3411
3412 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3413
3414 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003415 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003416 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3417 }
locke-lunargff255f92020-05-13 18:53:52 -06003418
3419 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003420}
3421
3422bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3423 VkDeviceSize size, uint32_t data) const {
3424 bool skip = false;
3425 const auto *cb_access_context = GetAccessContext(commandBuffer);
3426 assert(cb_access_context);
3427 if (!cb_access_context) return skip;
3428
3429 const auto *context = cb_access_context->GetCurrentAccessContext();
3430 assert(context);
3431 if (!context) return skip;
3432
3433 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3434
3435 if (dst_buffer) {
3436 ResourceAccessRange range = MakeRange(dstOffset, size);
3437 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3438 if (hazard.hazard) {
3439 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdFillBuffer: Hazard %s for dstBuffer %s",
3440 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3441 }
3442 }
3443 return skip;
3444}
3445
3446void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3447 VkDeviceSize size, uint32_t data) {
3448 auto *cb_access_context = GetAccessContext(commandBuffer);
3449 assert(cb_access_context);
3450 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
3451 auto *context = cb_access_context->GetCurrentAccessContext();
3452 assert(context);
3453
3454 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3455
3456 if (dst_buffer) {
3457 ResourceAccessRange range = MakeRange(dstOffset, size);
3458 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3459 }
3460}
3461
3462bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3463 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3464 const VkImageResolve *pRegions) const {
3465 bool skip = false;
3466 const auto *cb_access_context = GetAccessContext(commandBuffer);
3467 assert(cb_access_context);
3468 if (!cb_access_context) return skip;
3469
3470 const auto *context = cb_access_context->GetCurrentAccessContext();
3471 assert(context);
3472 if (!context) return skip;
3473
3474 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3475 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3476
3477 for (uint32_t region = 0; region < regionCount; region++) {
3478 const auto &resolve_region = pRegions[region];
3479 if (src_image) {
3480 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3481 resolve_region.srcOffset, resolve_region.extent);
3482 if (hazard.hazard) {
3483 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
3484 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
3485 report_data->FormatHandle(srcImage).c_str(), region);
3486 }
3487 }
3488
3489 if (dst_image) {
3490 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3491 resolve_region.dstOffset, resolve_region.extent);
3492 if (hazard.hazard) {
3493 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
3494 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
3495 report_data->FormatHandle(dstImage).c_str(), region);
3496 }
3497 if (skip) break;
3498 }
3499 }
3500
3501 return skip;
3502}
3503
3504void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3505 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3506 const VkImageResolve *pRegions) {
3507 auto *cb_access_context = GetAccessContext(commandBuffer);
3508 assert(cb_access_context);
3509 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
3510 auto *context = cb_access_context->GetCurrentAccessContext();
3511 assert(context);
3512
3513 auto *src_image = Get<IMAGE_STATE>(srcImage);
3514 auto *dst_image = Get<IMAGE_STATE>(dstImage);
3515
3516 for (uint32_t region = 0; region < regionCount; region++) {
3517 const auto &resolve_region = pRegions[region];
3518 if (src_image) {
3519 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3520 resolve_region.srcOffset, resolve_region.extent, tag);
3521 }
3522 if (dst_image) {
3523 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3524 resolve_region.dstOffset, resolve_region.extent, tag);
3525 }
3526 }
3527}
3528
3529bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3530 VkDeviceSize dataSize, const void *pData) const {
3531 bool skip = false;
3532 const auto *cb_access_context = GetAccessContext(commandBuffer);
3533 assert(cb_access_context);
3534 if (!cb_access_context) return skip;
3535
3536 const auto *context = cb_access_context->GetCurrentAccessContext();
3537 assert(context);
3538 if (!context) return skip;
3539
3540 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3541
3542 if (dst_buffer) {
3543 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3544 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3545 if (hazard.hazard) {
3546 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s",
3547 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3548 }
3549 }
3550 return skip;
3551}
3552
3553void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3554 VkDeviceSize dataSize, const void *pData) {
3555 auto *cb_access_context = GetAccessContext(commandBuffer);
3556 assert(cb_access_context);
3557 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
3558 auto *context = cb_access_context->GetCurrentAccessContext();
3559 assert(context);
3560
3561 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3562
3563 if (dst_buffer) {
3564 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3565 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3566 }
3567}
locke-lunargff255f92020-05-13 18:53:52 -06003568
3569bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3570 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
3571 bool skip = false;
3572 const auto *cb_access_context = GetAccessContext(commandBuffer);
3573 assert(cb_access_context);
3574 if (!cb_access_context) return skip;
3575
3576 const auto *context = cb_access_context->GetCurrentAccessContext();
3577 assert(context);
3578 if (!context) return skip;
3579
3580 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3581
3582 if (dst_buffer) {
3583 ResourceAccessRange range = MakeRange(dstOffset, 4);
3584 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3585 if (hazard.hazard) {
3586 skip |=
3587 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s",
3588 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3589 }
3590 }
3591 return skip;
3592}
3593
3594void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3595 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
3596 auto *cb_access_context = GetAccessContext(commandBuffer);
3597 assert(cb_access_context);
3598 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
3599 auto *context = cb_access_context->GetCurrentAccessContext();
3600 assert(context);
3601
3602 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3603
3604 if (dst_buffer) {
3605 ResourceAccessRange range = MakeRange(dstOffset, 4);
3606 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3607 }
3608}