blob: 3b59218a3371467cad31607f341c3abcda2117cb [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;
2439
2440 const auto last_bound_it = cmd.lastBound.find(pipelineBindPoint);
2441 if (last_bound_it == cmd.lastBound.cend()) {
2442 return skip;
2443 }
2444 auto const &state = last_bound_it->second;
2445 const auto *pPipe = state.pipeline_state;
2446 if (!pPipe) {
2447 return skip;
2448 }
2449
locke-lunarg36ba2592020-04-03 09:42:04 -06002450 using DescriptorClass = cvdescriptorset::DescriptorClass;
2451 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
2452 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
2453 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
2454 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
2455
2456 for (const auto &set_binding_pair : pPipe->active_slots) {
2457 uint32_t setIndex = set_binding_pair.first;
2458 cvdescriptorset::DescriptorSet *descriptor_set = state.per_set[setIndex].bound_descriptor_set;
2459 for (auto binding_pair : set_binding_pair.second) {
2460 auto binding = binding_pair.first;
2461 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
2462 binding_pair.first);
2463 const auto descriptor_type = binding_it.GetType();
2464 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2465 auto array_idx = 0;
2466
2467 if (binding_it.IsVariableDescriptorCount()) {
2468 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2469 }
2470 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2471 uint32_t index = i - index_range.start;
2472 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2473 if (descriptor->GetClass() == DescriptorClass::ImageSampler || descriptor->GetClass() == DescriptorClass::Image) {
2474 const IMAGE_VIEW_STATE *img_view_state = nullptr;
2475 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
2476 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
2477 } else {
2478 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
2479 }
2480 if (!img_view_state) continue;
2481 SyncStageAccessIndex sync_index;
2482 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
2483 sync_index = SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
2484 } else {
2485 sync_index = SYNC_VERTEX_SHADER_SHADER_READ;
2486 }
2487 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunargff255f92020-05-13 18:53:52 -06002488 auto hazard = context.DetectHazard(*img_state, sync_index, img_view_state->normalized_subresource_range,
locke-lunarg93d68af2020-05-12 17:18:03 -06002489 {0, 0, 0}, img_state->createInfo.extent);
locke-lunarg36ba2592020-04-03 09:42:04 -06002490 if (hazard.hazard) {
2491 skip |= LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
2492 "%s: Hazard %s for %s in %s and %s binding #%d index %d", function,
2493 string_SyncHazard(hazard.hazard),
2494 report_data->FormatHandle(img_view_state->image_view).c_str(),
2495 report_data->FormatHandle(cmd.commandBuffer).c_str(),
2496 report_data->FormatHandle(descriptor_set->GetSet()).c_str(), binding, index);
2497 }
2498 } else if (descriptor->GetClass() == DescriptorClass::TexelBuffer) {
2499 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
2500 if (!buf_view_state) continue;
2501 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
2502 ResourceAccessRange range =
2503 MakeRange(buf_view_state->create_info.offset,
2504 GetRealWholeSize(buf_view_state->create_info.offset, buf_view_state->create_info.range,
2505 buf_state->createInfo.size));
locke-lunargff255f92020-05-13 18:53:52 -06002506 auto hazard = context.DetectHazard(*buf_state, SYNC_VERTEX_SHADER_SHADER_READ, range);
locke-lunarg36ba2592020-04-03 09:42:04 -06002507 if (hazard.hazard) {
2508 skip |= LogError(buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
2509 "%s: Hazard %s for %s in %s and %s binding #%d index %d", function,
2510 string_SyncHazard(hazard.hazard),
2511 report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
2512 report_data->FormatHandle(cmd.commandBuffer).c_str(),
2513 report_data->FormatHandle(descriptor_set->GetSet()).c_str(), binding, index);
2514 }
2515 } else if (descriptor->GetClass() == DescriptorClass::GeneralBuffer) {
2516 auto buf_state = static_cast<const BufferDescriptor *>(descriptor)->GetBufferState();
2517 if (!buf_state) continue;
2518 ResourceAccessRange range = MakeRange(0, buf_state->createInfo.size);
2519 SyncStageAccessIndex sync_index;
2520 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER ||
2521 descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
2522 sync_index = SYNC_VERTEX_SHADER_UNIFORM_READ;
2523 } else {
2524 sync_index = SYNC_VERTEX_SHADER_SHADER_READ;
2525 }
locke-lunargff255f92020-05-13 18:53:52 -06002526 auto hazard = context.DetectHazard(*buf_state, sync_index, range);
locke-lunarg36ba2592020-04-03 09:42:04 -06002527 if (hazard.hazard) {
2528 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
2529 "%s: Hazard %s for %s in %s and %s binding #%d index %d", function,
2530 string_SyncHazard(hazard.hazard), report_data->FormatHandle(buf_state->buffer).c_str(),
2531 report_data->FormatHandle(cmd.commandBuffer).c_str(),
2532 report_data->FormatHandle(descriptor_set->GetSet()).c_str(), binding, index);
2533 }
2534 }
2535 }
2536 }
2537 }
2538 return skip;
2539}
2540
locke-lunargff255f92020-05-13 18:53:52 -06002541void SyncValidator::UpdateDescriptorSetAccessState(AccessContext &context, const ResourceUsageTag &tag, const CMD_BUFFER_STATE &cmd,
locke-lunarg36ba2592020-04-03 09:42:04 -06002542 VkPipelineBindPoint pipelineBindPoint) {
2543 const auto last_bound_it = cmd.lastBound.find(pipelineBindPoint);
2544 if (last_bound_it == cmd.lastBound.cend()) {
2545 return;
2546 }
2547 auto const &state = last_bound_it->second;
2548 const auto *pPipe = state.pipeline_state;
2549 if (!pPipe) {
2550 return;
2551 }
2552
locke-lunarg36ba2592020-04-03 09:42:04 -06002553 using DescriptorClass = cvdescriptorset::DescriptorClass;
2554 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
2555 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
2556 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
2557 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
2558
2559 for (const auto &set_binding_pair : pPipe->active_slots) {
2560 uint32_t setIndex = set_binding_pair.first;
2561 const cvdescriptorset::DescriptorSet *descriptor_set = state.per_set[setIndex].bound_descriptor_set;
2562 for (auto binding_pair : set_binding_pair.second) {
2563 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
2564 binding_pair.first);
2565 const auto descriptor_type = binding_it.GetType();
2566 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2567 auto array_idx = 0;
2568
2569 if (binding_it.IsVariableDescriptorCount()) {
2570 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2571 }
2572
2573 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2574 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2575 if (descriptor->GetClass() == DescriptorClass::ImageSampler || descriptor->GetClass() == DescriptorClass::Image) {
2576 const IMAGE_VIEW_STATE *img_view_state = nullptr;
2577 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
2578 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
2579 } else {
2580 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
2581 }
2582 if (!img_view_state) continue;
2583 SyncStageAccessIndex sync_index;
2584 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
2585 sync_index = SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
2586 } else {
2587 sync_index = SYNC_VERTEX_SHADER_SHADER_READ;
2588 }
2589 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunargff255f92020-05-13 18:53:52 -06002590 context.UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range, {0, 0, 0},
locke-lunarg93d68af2020-05-12 17:18:03 -06002591 img_state->createInfo.extent, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06002592
2593 } else if (descriptor->GetClass() == DescriptorClass::TexelBuffer) {
2594 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
2595 if (!buf_view_state) continue;
2596 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
2597 ResourceAccessRange range = MakeRange(buf_view_state->create_info.offset, buf_view_state->create_info.range);
locke-lunargff255f92020-05-13 18:53:52 -06002598 context.UpdateAccessState(*buf_state, SYNC_VERTEX_SHADER_SHADER_READ, range, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06002599
2600 } else if (descriptor->GetClass() == DescriptorClass::GeneralBuffer) {
2601 auto buf_state = static_cast<const BufferDescriptor *>(descriptor)->GetBufferState();
2602 if (!buf_state) continue;
2603 SyncStageAccessIndex sync_index;
2604 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER ||
2605 descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
2606 sync_index = SYNC_VERTEX_SHADER_UNIFORM_READ;
2607 } else {
2608 sync_index = SYNC_VERTEX_SHADER_SHADER_READ;
2609 }
2610 ResourceAccessRange range = MakeRange(0, buf_state->createInfo.size);
locke-lunargff255f92020-05-13 18:53:52 -06002611 context.UpdateAccessState(*buf_state, sync_index, range, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06002612 }
2613 }
2614 }
2615 }
2616}
2617
locke-lunargff255f92020-05-13 18:53:52 -06002618bool SyncValidator::DetectVertexHazard(const AccessContext &context, const CMD_BUFFER_STATE &cmd, uint32_t vertexCount,
2619 uint32_t firstVertex, const char *function) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06002620 bool skip = false;
2621 const auto last_bound_it = cmd.lastBound.find(VK_PIPELINE_BIND_POINT_GRAPHICS);
2622 if (last_bound_it == cmd.lastBound.cend()) {
2623 return skip;
2624 }
2625 auto const &state = last_bound_it->second;
2626 const auto *pPipe = state.pipeline_state;
2627 if (!pPipe) {
2628 return skip;
2629 }
2630
locke-lunarga4d39ea2020-05-22 14:17:29 -06002631 const auto &binding_buffers = cmd.current_vertex_buffer_binding_info.vertex_buffer_bindings;
2632 const auto &binding_buffers_size = binding_buffers.size();
2633 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
2634
2635 for (size_t i = 0; i < binding_descriptions_size; ++i) {
2636 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
2637 if (binding_description.binding < binding_buffers_size) {
2638 const auto &binding_buffer = binding_buffers[binding_description.binding];
2639 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
2640
2641 auto *buf_state = Get<BUFFER_STATE>(binding_buffer.buffer);
locke-lunargff255f92020-05-13 18:53:52 -06002642 VkDeviceSize range_start = 0;
locke-lunarga4d39ea2020-05-22 14:17:29 -06002643 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -06002644 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
2645 binding_description.stride);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002646 ResourceAccessRange range = MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -06002647 auto hazard = context.DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002648 if (hazard.hazard) {
2649 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s",
2650 function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(buf_state->buffer).c_str(),
2651 report_data->FormatHandle(cmd.commandBuffer).c_str());
2652 }
2653 }
2654 }
2655 return skip;
2656}
2657
locke-lunargff255f92020-05-13 18:53:52 -06002658void SyncValidator::UpdateVertexAccessState(AccessContext &context, const ResourceUsageTag &tag, const CMD_BUFFER_STATE &cmd,
2659 uint32_t vertexCount, uint32_t firstVertex) {
locke-lunarga4d39ea2020-05-22 14:17:29 -06002660 const auto last_bound_it = cmd.lastBound.find(VK_PIPELINE_BIND_POINT_GRAPHICS);
2661 if (last_bound_it == cmd.lastBound.cend()) {
2662 return;
2663 }
2664 auto const &state = last_bound_it->second;
2665 const auto *pPipe = state.pipeline_state;
2666 if (!pPipe) {
2667 return;
2668 }
2669
locke-lunarga4d39ea2020-05-22 14:17:29 -06002670 const auto &binding_buffers = cmd.current_vertex_buffer_binding_info.vertex_buffer_bindings;
2671 const auto &binding_buffers_size = binding_buffers.size();
2672 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
2673
2674 for (size_t i = 0; i < binding_descriptions_size; ++i) {
2675 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
2676 if (binding_description.binding < binding_buffers_size) {
2677 const auto &binding_buffer = binding_buffers[binding_description.binding];
2678 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
2679
2680 auto *buf_state = Get<BUFFER_STATE>(binding_buffer.buffer);
locke-lunargff255f92020-05-13 18:53:52 -06002681 VkDeviceSize range_start = 0;
locke-lunarga4d39ea2020-05-22 14:17:29 -06002682 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -06002683 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
2684 binding_description.stride);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002685 ResourceAccessRange range = MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -06002686 context.UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002687 }
2688 }
2689}
2690
locke-lunargff255f92020-05-13 18:53:52 -06002691bool SyncValidator::DetectVertexIndexHazard(const AccessContext &context, const CMD_BUFFER_STATE &cmd, uint32_t indexCount,
2692 uint32_t firstIndex, const char *function) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06002693 bool skip = false;
2694 if (cmd.index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
2695
locke-lunarga4d39ea2020-05-22 14:17:29 -06002696 auto *index_buf_state = Get<BUFFER_STATE>(cmd.index_buffer_binding.buffer);
2697 const auto index_size = GetIndexAlignment(cmd.index_buffer_binding.index_type);
locke-lunargff255f92020-05-13 18:53:52 -06002698 VkDeviceSize range_start = 0;
2699 VkDeviceSize range_size = 0;
2700 GetBufferRange(range_start, range_size, cmd.index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
2701 indexCount, index_size);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002702 ResourceAccessRange range = MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -06002703 auto hazard = context.DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002704 if (hazard.hazard) {
2705 skip |= LogError(index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s",
2706 function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(index_buf_state->buffer).c_str(),
2707 report_data->FormatHandle(cmd.commandBuffer).c_str());
2708 }
2709
2710 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2711 // We will detect more accurate range in the future.
locke-lunargff255f92020-05-13 18:53:52 -06002712 skip |= DetectVertexHazard(context, cmd, UINT32_MAX, 0, function);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002713 return skip;
2714}
2715
locke-lunargff255f92020-05-13 18:53:52 -06002716void SyncValidator::UpdateVertexIndexAccessState(AccessContext &context, const ResourceUsageTag &tag, const CMD_BUFFER_STATE &cmd,
2717 uint32_t indexCount, uint32_t firstIndex) {
locke-lunarga4d39ea2020-05-22 14:17:29 -06002718 if (cmd.index_buffer_binding.buffer == VK_NULL_HANDLE) return;
2719
locke-lunarga4d39ea2020-05-22 14:17:29 -06002720 auto *index_buf_state = Get<BUFFER_STATE>(cmd.index_buffer_binding.buffer);
2721 const auto index_size = GetIndexAlignment(cmd.index_buffer_binding.index_type);
locke-lunargff255f92020-05-13 18:53:52 -06002722 VkDeviceSize range_start = 0;
2723 VkDeviceSize range_size = 0;
2724 GetBufferRange(range_start, range_size, cmd.index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
2725 indexCount, index_size);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002726 ResourceAccessRange range = MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -06002727 context.UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002728
2729 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2730 // We will detect more accurate range in the future.
locke-lunargff255f92020-05-13 18:53:52 -06002731 UpdateVertexAccessState(context, tag, cmd, UINT32_MAX, 0);
locke-lunarga4d39ea2020-05-22 14:17:29 -06002732}
2733
locke-lunargff255f92020-05-13 18:53:52 -06002734bool SyncValidator::DetectSubpassAttachmentHazard(const AccessContext &context, const CMD_BUFFER_STATE &cmd,
2735 const char *function) const {
locke-lunarg93d68af2020-05-12 17:18:03 -06002736 bool skip = false;
2737
locke-lunarg93d68af2020-05-12 17:18:03 -06002738 const auto &subpass = cmd.activeRenderPass->createInfo.pSubpasses[cmd.activeSubpass];
2739 const auto *framebuffer = cmd.activeFramebuffer.get();
2740 VkExtent3D framebuffer_extent = {framebuffer->createInfo.width, framebuffer->createInfo.height, framebuffer->createInfo.layers};
2741
locke-lunargff255f92020-05-13 18:53:52 -06002742 auto dtct_fn = [&cmd, &function, &framebuffer, &framebuffer_extent, &context](
locke-lunarg93d68af2020-05-12 17:18:03 -06002743 const SyncValidator &this_, const safe_VkAttachmentReference2 &attachment_ref,
2744 const SyncStageAccessIndex sync_index, const std::string &attachment_desription) {
2745 if (attachment_ref.attachment == VK_ATTACHMENT_UNUSED) return false;
2746 auto attachment_index = attachment_ref.attachment;
2747 if (framebuffer->createInfo.attachmentCount > attachment_index) {
2748 const IMAGE_VIEW_STATE *img_view_state =
2749 this_.Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[attachment_index]);
2750 if (!img_view_state) return false;
2751 const IMAGE_STATE *img_state = img_view_state->image_state.get();
2752 HazardResult hazard;
2753 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
locke-lunargff255f92020-05-13 18:53:52 -06002754 hazard = context.DetectHazard(*img_state, sync_index, img_view_state->normalized_subresource_range, {0, 0, 0},
locke-lunarg93d68af2020-05-12 17:18:03 -06002755 framebuffer_extent);
2756 } else if (sync_index == SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE) {
2757 if (attachment_desription.compare("pDepthStencilAttachment") == 0) {
locke-lunargff255f92020-05-13 18:53:52 -06002758 hazard = context.DetectHazard(*img_state, sync_index, img_view_state->normalized_subresource_range,
locke-lunarg93d68af2020-05-12 17:18:03 -06002759 kDepthStencilAttachmentRasterOrder, {0, 0, 0}, framebuffer_extent);
2760 } else {
locke-lunargff255f92020-05-13 18:53:52 -06002761 hazard = context.DetectHazard(*img_state, sync_index, img_view_state->normalized_subresource_range,
locke-lunarg93d68af2020-05-12 17:18:03 -06002762 kColorAttachmentRasterOrder, {0, 0, 0}, framebuffer_extent);
2763 }
2764 }
2765
2766 if (hazard.hazard) {
2767 return this_.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
2768 "%s: Hazard %s for %s in %s, Subpass #%d, and %s", function, string_SyncHazard(hazard.hazard),
2769 this_.report_data->FormatHandle(img_view_state->image_view).c_str(),
2770 this_.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
2771 attachment_desription.c_str());
2772 }
2773 }
2774 return false;
2775 };
2776
2777 if (subpass.inputAttachmentCount && subpass.pInputAttachments) {
2778 for (uint32_t i = 0; i < subpass.inputAttachmentCount; ++i) {
2779 std::string attachment_desription = "pInputAttachments #" + std::to_string(i);
2780 skip |= dtct_fn(*this, subpass.pInputAttachments[i], SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, attachment_desription);
2781 }
2782 }
2783 if (subpass.colorAttachmentCount) {
2784 if (subpass.pColorAttachments) {
2785 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
2786 std::string attachment_desription = "pColorAttachments #" + std::to_string(i);
2787 skip |= dtct_fn(*this, subpass.pColorAttachments[i], SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
2788 attachment_desription);
2789 }
2790 }
2791 if (subpass.pResolveAttachments) {
2792 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
2793 std::string attachment_desription = "pResolveAttachments #" + std::to_string(i);
2794 skip |= dtct_fn(*this, subpass.pResolveAttachments[i], SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
2795 attachment_desription);
2796 }
2797 }
2798 }
2799 if (subpass.pDepthStencilAttachment) {
2800 skip |= dtct_fn(*this, *subpass.pDepthStencilAttachment, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
2801 "pDepthStencilAttachment");
2802 }
2803 return skip;
2804}
2805
locke-lunargff255f92020-05-13 18:53:52 -06002806void SyncValidator::UpdateSubpassAttachmentAccessState(AccessContext &context, const ResourceUsageTag &tag,
2807 const CMD_BUFFER_STATE &cmd) {
locke-lunarg93d68af2020-05-12 17:18:03 -06002808 const auto &subpass = cmd.activeRenderPass->createInfo.pSubpasses[cmd.activeSubpass];
2809 const auto *framebuffer = cmd.activeFramebuffer.get();
2810 VkExtent3D framebuffer_extent = {framebuffer->createInfo.width, framebuffer->createInfo.height, framebuffer->createInfo.layers};
2811
locke-lunargff255f92020-05-13 18:53:52 -06002812 auto updt_fn = [&framebuffer, &framebuffer_extent, &context, &tag](const SyncValidator &this_,
2813 const safe_VkAttachmentReference2 &attachment_ref,
2814 const SyncStageAccessIndex sync_index) {
locke-lunarg93d68af2020-05-12 17:18:03 -06002815 if (attachment_ref.attachment == VK_ATTACHMENT_UNUSED) return;
2816 auto attachment_index = attachment_ref.attachment;
2817 if (framebuffer->createInfo.attachmentCount > attachment_index) {
2818 const IMAGE_VIEW_STATE *img_view_state =
2819 this_.Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[attachment_index]);
2820 if (!img_view_state) return;
2821 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunargff255f92020-05-13 18:53:52 -06002822 context.UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range, {0, 0, 0},
locke-lunarg93d68af2020-05-12 17:18:03 -06002823 framebuffer_extent, tag);
2824 }
2825 };
2826
2827 if (subpass.inputAttachmentCount && subpass.pInputAttachments) {
2828 for (uint32_t i = 0; i < subpass.inputAttachmentCount; ++i) {
2829 updt_fn(*this, subpass.pInputAttachments[i], SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2830 }
2831 }
2832 if (subpass.colorAttachmentCount) {
2833 if (subpass.pColorAttachments) {
2834 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
2835 updt_fn(*this, subpass.pColorAttachments[i], SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE);
2836 }
2837 }
2838 if (subpass.pResolveAttachments) {
2839 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
2840 updt_fn(*this, subpass.pResolveAttachments[i], SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE);
2841 }
2842 }
2843 }
2844 if (subpass.pDepthStencilAttachment) {
2845 updt_fn(*this, *subpass.pDepthStencilAttachment, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE);
2846 }
2847}
2848
locke-lunargff255f92020-05-13 18:53:52 -06002849bool SyncValidator::DetectIndirectBufferHazard(const AccessContext &context, VkCommandBuffer commandBuffer,
2850 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
2851 const uint32_t drawCount, const uint32_t stride, const char *function) const {
2852 bool skip = false;
2853 if (drawCount == 0) return skip;
2854
2855 const auto *buf_state = Get<BUFFER_STATE>(buffer);
2856 VkDeviceSize size = struct_size;
2857 if (drawCount == 1 || stride == size) {
2858 if (drawCount > 1) size *= drawCount;
2859 ResourceAccessRange range = MakeRange(offset, size);
2860 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
2861 if (hazard.hazard) {
2862 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for indirect %s in %s",
2863 function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(buffer).c_str(),
2864 report_data->FormatHandle(commandBuffer).c_str());
2865 }
2866 } else {
2867 for (uint32_t i = 0; i < drawCount; ++i) {
2868 ResourceAccessRange range = MakeRange(offset + i * stride, size);
2869 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
2870 if (hazard.hazard) {
2871 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for indirect %s in %s",
2872 function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(buffer).c_str(),
2873 report_data->FormatHandle(commandBuffer).c_str());
2874 break;
2875 }
2876 }
2877 }
2878 return skip;
2879}
2880
2881void SyncValidator::UpdateIndirectBufferAccessState(AccessContext &context, const ResourceUsageTag &tag,
2882 const VkDeviceSize struct_size, const VkBuffer buffer,
2883 const VkDeviceSize offset, const uint32_t drawCount, uint32_t stride) {
2884 const auto *buf_state = Get<BUFFER_STATE>(buffer);
2885 VkDeviceSize size = struct_size;
2886 if (drawCount == 1 || stride == size) {
2887 if (drawCount > 1) size *= drawCount;
2888 ResourceAccessRange range = MakeRange(offset, size);
2889 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
2890 } else {
2891 for (uint32_t i = 0; i < drawCount; ++i) {
2892 ResourceAccessRange range = MakeRange(offset + i * stride, size);
2893 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
2894 }
2895 }
2896}
2897
2898bool SyncValidator::DetectCountBufferHazard(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
2899 VkDeviceSize offset, const char *function) const {
2900 bool skip = false;
2901
2902 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
2903 ResourceAccessRange range = MakeRange(offset, 4);
2904 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
2905 if (hazard.hazard) {
2906 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for countBuffer %s in %s",
2907 function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(buffer).c_str(),
2908 report_data->FormatHandle(commandBuffer).c_str());
2909 }
2910 return skip;
2911}
2912
2913void SyncValidator::UpdateCountBufferAccessState(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer,
2914 VkDeviceSize offset) {
2915 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
2916 ResourceAccessRange range = MakeRange(offset, 4);
2917 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
2918}
2919
locke-lunarg36ba2592020-04-03 09:42:04 -06002920bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06002921 bool skip = false;
2922 ;
locke-lunarg36ba2592020-04-03 09:42:04 -06002923 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06002924 const auto *cb_access_context = GetAccessContext(commandBuffer);
2925 assert(cb_access_context);
2926 if (!cb_access_context) return skip;
2927
2928 const auto *context = cb_access_context->GetCurrentAccessContext();
2929 assert(context);
2930 if (!context) return skip;
2931
2932 skip |= DetectDescriptorSetHazard(*context, *cb_state, VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
2933 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06002934}
2935
2936void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
2937 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06002938 auto *cb_access_context = GetAccessContext(commandBuffer);
2939 assert(cb_access_context);
2940 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
2941 auto *context = cb_access_context->GetCurrentAccessContext();
2942 assert(context);
2943
2944 UpdateDescriptorSetAccessState(*context, tag, *cb_state, VK_PIPELINE_BIND_POINT_COMPUTE);
locke-lunarg36ba2592020-04-03 09:42:04 -06002945}
locke-lunarge1a67022020-04-29 00:15:36 -06002946
2947bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06002948 bool skip = false;
locke-lunarge1a67022020-04-29 00:15:36 -06002949 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06002950 const auto *cb_access_context = GetAccessContext(commandBuffer);
2951 assert(cb_access_context);
2952 if (!cb_access_context) return skip;
2953
2954 const auto *context = cb_access_context->GetCurrentAccessContext();
2955 assert(context);
2956 if (!context) return skip;
2957
2958 skip |= DetectDescriptorSetHazard(*context, *cb_state, VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
2959 skip |= DetectIndirectBufferHazard(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
2960 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
2961 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06002962}
2963
2964void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
2965 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06002966 auto *cb_access_context = GetAccessContext(commandBuffer);
2967 assert(cb_access_context);
2968 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
2969 auto *context = cb_access_context->GetCurrentAccessContext();
2970 assert(context);
2971
2972 UpdateDescriptorSetAccessState(*context, tag, *cb_state, VK_PIPELINE_BIND_POINT_COMPUTE);
2973 UpdateIndirectBufferAccessState(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
2974 sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06002975}
2976
2977bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2978 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06002979 bool skip = false;
locke-lunarge1a67022020-04-29 00:15:36 -06002980 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06002981 const auto *cb_access_context = GetAccessContext(commandBuffer);
2982 assert(cb_access_context);
2983 if (!cb_access_context) return skip;
2984
2985 const auto *context = cb_access_context->GetCurrentAccessContext();
2986 assert(context);
2987 if (!context) return skip;
2988
2989 skip |= DetectDescriptorSetHazard(*context, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
2990 skip |= DetectVertexHazard(*context, *cb_state, vertexCount, firstVertex, "vkCmdDraw");
2991 skip |= DetectSubpassAttachmentHazard(*context, *cb_state, "vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06002992 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06002993}
2994
2995void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2996 uint32_t firstVertex, uint32_t firstInstance) {
2997 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06002998 auto *cb_access_context = GetAccessContext(commandBuffer);
2999 assert(cb_access_context);
3000 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
3001 auto *context = cb_access_context->GetCurrentAccessContext();
3002 assert(context);
3003
3004 UpdateDescriptorSetAccessState(*context, tag, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS);
3005 UpdateVertexAccessState(*context, tag, *cb_state, vertexCount, firstVertex);
3006 UpdateSubpassAttachmentAccessState(*context, tag, *cb_state);
locke-lunarge1a67022020-04-29 00:15:36 -06003007}
3008
3009bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3010 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003011 bool skip = false;
locke-lunarge1a67022020-04-29 00:15:36 -06003012 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06003013 const auto *cb_access_context = GetAccessContext(commandBuffer);
3014 assert(cb_access_context);
3015 if (!cb_access_context) return skip;
3016
3017 const auto *context = cb_access_context->GetCurrentAccessContext();
3018 assert(context);
3019 if (!context) return skip;
3020
3021 skip |= DetectDescriptorSetHazard(*context, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3022 skip |= DetectVertexIndexHazard(*context, *cb_state, indexCount, firstIndex, "vkCmdDrawIndexed");
3023 skip |= DetectSubpassAttachmentHazard(*context, *cb_state, "vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003024 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003025}
3026
3027void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3028 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
3029 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06003030 auto *cb_access_context = GetAccessContext(commandBuffer);
3031 assert(cb_access_context);
3032 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
3033 auto *context = cb_access_context->GetCurrentAccessContext();
3034 assert(context);
3035
3036 UpdateDescriptorSetAccessState(*context, tag, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS);
3037 UpdateVertexAccessState(*context, tag, *cb_state, indexCount, firstIndex);
3038 UpdateSubpassAttachmentAccessState(*context, tag, *cb_state);
locke-lunarge1a67022020-04-29 00:15:36 -06003039}
3040
3041bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3042 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003043 bool skip = false;
3044 if (drawCount == 0) return skip;
3045
locke-lunarge1a67022020-04-29 00:15:36 -06003046 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06003047 const auto *cb_access_context = GetAccessContext(commandBuffer);
3048 assert(cb_access_context);
3049 if (!cb_access_context) return skip;
3050
3051 const auto *context = cb_access_context->GetCurrentAccessContext();
3052 assert(context);
3053 if (!context) return skip;
3054
3055 skip |= DetectDescriptorSetHazard(*context, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3056 skip |= DetectSubpassAttachmentHazard(*context, *cb_state, "vkCmdDrawIndirect");
3057 skip |= DetectIndirectBufferHazard(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3058 "vkCmdDrawIndirect");
3059
3060 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3061 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3062 // We will validate the vertex buffer in SubmitQueue in the future.
3063 skip |= DetectVertexHazard(*context, *cb_state, UINT32_MAX, 0, "vkCmdDrawIndirect");
3064 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003065}
3066
3067void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3068 uint32_t drawCount, uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003069 if (drawCount == 0) return;
locke-lunarge1a67022020-04-29 00:15:36 -06003070 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06003071 auto *cb_access_context = GetAccessContext(commandBuffer);
3072 assert(cb_access_context);
3073 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3074 auto *context = cb_access_context->GetCurrentAccessContext();
3075 assert(context);
3076
3077 UpdateDescriptorSetAccessState(*context, tag, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS);
3078 UpdateSubpassAttachmentAccessState(*context, tag, *cb_state);
3079 UpdateSubpassAttachmentAccessState(*context, tag, *cb_state);
3080 UpdateIndirectBufferAccessState(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
3081
3082 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3083 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3084 // We will record the vertex buffer in SubmitQueue in the future.
3085 UpdateVertexAccessState(*context, tag, *cb_state, UINT32_MAX, 0);
locke-lunarge1a67022020-04-29 00:15:36 -06003086}
3087
3088bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3089 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003090 bool skip = false;
3091 if (drawCount == 0) return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003092 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06003093 const auto *cb_access_context = GetAccessContext(commandBuffer);
3094 assert(cb_access_context);
3095 if (!cb_access_context) return skip;
3096
3097 const auto *context = cb_access_context->GetCurrentAccessContext();
3098 assert(context);
3099 if (!context) return skip;
3100
3101 skip |= DetectDescriptorSetHazard(*context, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3102 skip |= DetectSubpassAttachmentHazard(*context, *cb_state, "vkCmdDrawIndexedIndirect");
3103 skip |= DetectIndirectBufferHazard(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount,
3104 stride, "vkCmdDrawIndexedIndirect");
3105
3106 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3107 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3108 // We will validate the index and vertex buffer in SubmitQueue in the future.
3109 skip |= DetectVertexIndexHazard(*context, *cb_state, UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
3110 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003111}
3112
3113void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3114 uint32_t drawCount, uint32_t stride) {
3115 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06003116 auto *cb_access_context = GetAccessContext(commandBuffer);
3117 assert(cb_access_context);
3118 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3119 auto *context = cb_access_context->GetCurrentAccessContext();
3120 assert(context);
3121
3122 UpdateDescriptorSetAccessState(*context, tag, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS);
3123 UpdateSubpassAttachmentAccessState(*context, tag, *cb_state);
3124 UpdateIndirectBufferAccessState(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
3125
3126 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3127 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3128 // We will record the index and vertex buffer in SubmitQueue in the future.
3129 UpdateVertexIndexAccessState(*context, tag, *cb_state, UINT32_MAX, 0);
3130}
3131
3132bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3133 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3134 uint32_t stride, const char *function) const {
3135 bool skip = false;
3136 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
3137 const auto *cb_access_context = GetAccessContext(commandBuffer);
3138 assert(cb_access_context);
3139 if (!cb_access_context) return skip;
3140
3141 const auto *context = cb_access_context->GetCurrentAccessContext();
3142 assert(context);
3143 if (!context) return skip;
3144
3145 skip |= DetectDescriptorSetHazard(*context, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3146 skip |= DetectSubpassAttachmentHazard(*context, *cb_state, function);
3147 skip |= DetectIndirectBufferHazard(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3148 function);
3149 skip |= DetectCountBufferHazard(*context, commandBuffer, countBuffer, countBufferOffset, function);
3150
3151 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3152 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3153 // We will validate the vertex buffer in SubmitQueue in the future.
3154 skip |= DetectVertexHazard(*context, *cb_state, UINT32_MAX, 0, "vkCmdDrawIndirectCount");
3155 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003156}
3157
3158bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3159 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3160 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003161 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3162 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003163}
3164
3165void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3166 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3167 uint32_t stride) {
3168 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06003169 auto *cb_access_context = GetAccessContext(commandBuffer);
3170 assert(cb_access_context);
3171 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3172 auto *context = cb_access_context->GetCurrentAccessContext();
3173 assert(context);
3174
3175 UpdateDescriptorSetAccessState(*context, tag, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS);
3176 UpdateSubpassAttachmentAccessState(*context, tag, *cb_state);
3177 UpdateIndirectBufferAccessState(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3178 UpdateCountBufferAccessState(*context, tag, countBuffer, countBufferOffset);
3179
3180 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3181 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3182 // We will record the vertex buffer in SubmitQueue in the future.
3183 UpdateVertexAccessState(*context, tag, *cb_state, UINT32_MAX, 0);
locke-lunarge1a67022020-04-29 00:15:36 -06003184}
3185
3186bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3187 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3188 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003189 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3190 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003191}
3192
3193void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3194 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3195 uint32_t maxDrawCount, uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003196 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003197}
3198
3199bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3200 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3201 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003202 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3203 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003204}
3205
3206void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3207 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3208 uint32_t maxDrawCount, uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003209 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3210}
3211
3212bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3213 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3214 uint32_t stride, const char *function) const {
3215 bool skip = false;
3216 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
3217 const auto *cb_access_context = GetAccessContext(commandBuffer);
3218 assert(cb_access_context);
3219 if (!cb_access_context) return skip;
3220
3221 const auto *context = cb_access_context->GetCurrentAccessContext();
3222 assert(context);
3223 if (!context) return skip;
3224
3225 skip |= DetectDescriptorSetHazard(*context, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3226 skip |= DetectSubpassAttachmentHazard(*context, *cb_state, function);
3227 skip |= DetectIndirectBufferHazard(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3228 stride, function);
3229 skip |= DetectCountBufferHazard(*context, commandBuffer, countBuffer, countBufferOffset, function);
3230
3231 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3232 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3233 // We will validate the index and vertex buffer in SubmitQueue in the future.
3234 skip |= DetectVertexIndexHazard(*context, *cb_state, UINT32_MAX, 0, "vkCmdDrawIndexedIndirectCount");
3235 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003236}
3237
3238bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3239 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3240 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003241 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3242 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003243}
3244
3245void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3246 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3247 uint32_t maxDrawCount, uint32_t stride) {
3248 const auto *cb_state = Get<CMD_BUFFER_STATE>(commandBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06003249 auto *cb_access_context = GetAccessContext(commandBuffer);
3250 assert(cb_access_context);
3251 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3252 auto *context = cb_access_context->GetCurrentAccessContext();
3253 assert(context);
3254
3255 UpdateDescriptorSetAccessState(*context, tag, *cb_state, VK_PIPELINE_BIND_POINT_GRAPHICS);
3256 UpdateSubpassAttachmentAccessState(*context, tag, *cb_state);
3257 UpdateIndirectBufferAccessState(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3258 UpdateCountBufferAccessState(*context, tag, countBuffer, countBufferOffset);
3259
3260 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3261 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3262 UpdateVertexIndexAccessState(*context, tag, *cb_state, UINT32_MAX, 0);
locke-lunarge1a67022020-04-29 00:15:36 -06003263}
3264
3265bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3266 VkDeviceSize offset, VkBuffer countBuffer,
3267 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3268 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003269 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3270 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003271}
3272
3273void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3274 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3275 uint32_t maxDrawCount, uint32_t stride) {
3276 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3277}
3278
3279bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3280 VkDeviceSize offset, VkBuffer countBuffer,
3281 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3282 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003283 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3284 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003285}
3286
3287void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3288 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3289 uint32_t maxDrawCount, uint32_t stride) {
3290 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3291}
3292
3293bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3294 const VkClearColorValue *pColor, uint32_t rangeCount,
3295 const VkImageSubresourceRange *pRanges) const {
3296 bool skip = false;
3297 const auto *cb_access_context = GetAccessContext(commandBuffer);
3298 assert(cb_access_context);
3299 if (!cb_access_context) return skip;
3300
3301 const auto *context = cb_access_context->GetCurrentAccessContext();
3302 assert(context);
3303 if (!context) return skip;
3304
3305 const auto *image_state = Get<IMAGE_STATE>(image);
3306
3307 for (uint32_t index = 0; index < rangeCount; index++) {
3308 const auto &range = pRanges[index];
3309 if (image_state) {
3310 auto hazard =
3311 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3312 if (hazard.hazard) {
3313 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
3314 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32, string_SyncHazard(hazard.hazard),
3315 report_data->FormatHandle(image).c_str(), index);
3316 }
3317 }
3318 }
3319 return skip;
3320}
3321
3322void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3323 const VkClearColorValue *pColor, uint32_t rangeCount,
3324 const VkImageSubresourceRange *pRanges) {
3325 auto *cb_access_context = GetAccessContext(commandBuffer);
3326 assert(cb_access_context);
3327 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
3328 auto *context = cb_access_context->GetCurrentAccessContext();
3329 assert(context);
3330
3331 const auto *image_state = Get<IMAGE_STATE>(image);
3332
3333 for (uint32_t index = 0; index < rangeCount; index++) {
3334 const auto &range = pRanges[index];
3335 if (image_state) {
3336 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3337 tag);
3338 }
3339 }
3340}
3341
3342bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3343 VkImageLayout imageLayout,
3344 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3345 const VkImageSubresourceRange *pRanges) const {
3346 bool skip = false;
3347 const auto *cb_access_context = GetAccessContext(commandBuffer);
3348 assert(cb_access_context);
3349 if (!cb_access_context) return skip;
3350
3351 const auto *context = cb_access_context->GetCurrentAccessContext();
3352 assert(context);
3353 if (!context) return skip;
3354
3355 const auto *image_state = Get<IMAGE_STATE>(image);
3356
3357 for (uint32_t index = 0; index < rangeCount; index++) {
3358 const auto &range = pRanges[index];
3359 if (image_state) {
3360 auto hazard =
3361 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3362 if (hazard.hazard) {
3363 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
3364 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32,
3365 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index);
3366 }
3367 }
3368 }
3369 return skip;
3370}
3371
3372void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3373 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3374 const VkImageSubresourceRange *pRanges) {
3375 auto *cb_access_context = GetAccessContext(commandBuffer);
3376 assert(cb_access_context);
3377 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
3378 auto *context = cb_access_context->GetCurrentAccessContext();
3379 assert(context);
3380
3381 const auto *image_state = Get<IMAGE_STATE>(image);
3382
3383 for (uint32_t index = 0; index < rangeCount; index++) {
3384 const auto &range = pRanges[index];
3385 if (image_state) {
3386 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3387 tag);
3388 }
3389 }
3390}
3391
3392bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
3393 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
3394 VkDeviceSize dstOffset, VkDeviceSize stride,
3395 VkQueryResultFlags flags) const {
3396 bool skip = false;
3397 const auto *cb_access_context = GetAccessContext(commandBuffer);
3398 assert(cb_access_context);
3399 if (!cb_access_context) return skip;
3400
3401 const auto *context = cb_access_context->GetCurrentAccessContext();
3402 assert(context);
3403 if (!context) return skip;
3404
3405 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3406
3407 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003408 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003409 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3410 if (hazard.hazard) {
3411 skip |=
3412 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s",
3413 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3414 }
3415 }
locke-lunargff255f92020-05-13 18:53:52 -06003416
3417 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003418 return skip;
3419}
3420
3421void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
3422 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3423 VkDeviceSize stride, VkQueryResultFlags flags) {
3424 auto *cb_access_context = GetAccessContext(commandBuffer);
3425 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06003426 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06003427 auto *context = cb_access_context->GetCurrentAccessContext();
3428 assert(context);
3429
3430 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3431
3432 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003433 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003434 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3435 }
locke-lunargff255f92020-05-13 18:53:52 -06003436
3437 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003438}
3439
3440bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3441 VkDeviceSize size, uint32_t data) const {
3442 bool skip = false;
3443 const auto *cb_access_context = GetAccessContext(commandBuffer);
3444 assert(cb_access_context);
3445 if (!cb_access_context) return skip;
3446
3447 const auto *context = cb_access_context->GetCurrentAccessContext();
3448 assert(context);
3449 if (!context) return skip;
3450
3451 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3452
3453 if (dst_buffer) {
3454 ResourceAccessRange range = MakeRange(dstOffset, size);
3455 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3456 if (hazard.hazard) {
3457 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdFillBuffer: Hazard %s for dstBuffer %s",
3458 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3459 }
3460 }
3461 return skip;
3462}
3463
3464void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3465 VkDeviceSize size, uint32_t data) {
3466 auto *cb_access_context = GetAccessContext(commandBuffer);
3467 assert(cb_access_context);
3468 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
3469 auto *context = cb_access_context->GetCurrentAccessContext();
3470 assert(context);
3471
3472 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3473
3474 if (dst_buffer) {
3475 ResourceAccessRange range = MakeRange(dstOffset, size);
3476 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3477 }
3478}
3479
3480bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3481 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3482 const VkImageResolve *pRegions) const {
3483 bool skip = false;
3484 const auto *cb_access_context = GetAccessContext(commandBuffer);
3485 assert(cb_access_context);
3486 if (!cb_access_context) return skip;
3487
3488 const auto *context = cb_access_context->GetCurrentAccessContext();
3489 assert(context);
3490 if (!context) return skip;
3491
3492 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3493 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3494
3495 for (uint32_t region = 0; region < regionCount; region++) {
3496 const auto &resolve_region = pRegions[region];
3497 if (src_image) {
3498 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3499 resolve_region.srcOffset, resolve_region.extent);
3500 if (hazard.hazard) {
3501 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
3502 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
3503 report_data->FormatHandle(srcImage).c_str(), region);
3504 }
3505 }
3506
3507 if (dst_image) {
3508 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3509 resolve_region.dstOffset, resolve_region.extent);
3510 if (hazard.hazard) {
3511 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
3512 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
3513 report_data->FormatHandle(dstImage).c_str(), region);
3514 }
3515 if (skip) break;
3516 }
3517 }
3518
3519 return skip;
3520}
3521
3522void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3523 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3524 const VkImageResolve *pRegions) {
3525 auto *cb_access_context = GetAccessContext(commandBuffer);
3526 assert(cb_access_context);
3527 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
3528 auto *context = cb_access_context->GetCurrentAccessContext();
3529 assert(context);
3530
3531 auto *src_image = Get<IMAGE_STATE>(srcImage);
3532 auto *dst_image = Get<IMAGE_STATE>(dstImage);
3533
3534 for (uint32_t region = 0; region < regionCount; region++) {
3535 const auto &resolve_region = pRegions[region];
3536 if (src_image) {
3537 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3538 resolve_region.srcOffset, resolve_region.extent, tag);
3539 }
3540 if (dst_image) {
3541 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3542 resolve_region.dstOffset, resolve_region.extent, tag);
3543 }
3544 }
3545}
3546
3547bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3548 VkDeviceSize dataSize, const void *pData) const {
3549 bool skip = false;
3550 const auto *cb_access_context = GetAccessContext(commandBuffer);
3551 assert(cb_access_context);
3552 if (!cb_access_context) return skip;
3553
3554 const auto *context = cb_access_context->GetCurrentAccessContext();
3555 assert(context);
3556 if (!context) return skip;
3557
3558 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3559
3560 if (dst_buffer) {
3561 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3562 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3563 if (hazard.hazard) {
3564 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s",
3565 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3566 }
3567 }
3568 return skip;
3569}
3570
3571void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3572 VkDeviceSize dataSize, const void *pData) {
3573 auto *cb_access_context = GetAccessContext(commandBuffer);
3574 assert(cb_access_context);
3575 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
3576 auto *context = cb_access_context->GetCurrentAccessContext();
3577 assert(context);
3578
3579 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3580
3581 if (dst_buffer) {
3582 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3583 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3584 }
3585}
locke-lunargff255f92020-05-13 18:53:52 -06003586
3587bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3588 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
3589 bool skip = false;
3590 const auto *cb_access_context = GetAccessContext(commandBuffer);
3591 assert(cb_access_context);
3592 if (!cb_access_context) return skip;
3593
3594 const auto *context = cb_access_context->GetCurrentAccessContext();
3595 assert(context);
3596 if (!context) return skip;
3597
3598 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3599
3600 if (dst_buffer) {
3601 ResourceAccessRange range = MakeRange(dstOffset, 4);
3602 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3603 if (hazard.hazard) {
3604 skip |=
3605 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s",
3606 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3607 }
3608 }
3609 return skip;
3610}
3611
3612void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3613 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
3614 auto *cb_access_context = GetAccessContext(commandBuffer);
3615 assert(cb_access_context);
3616 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
3617 auto *context = cb_access_context->GetCurrentAccessContext();
3618 assert(context);
3619
3620 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3621
3622 if (dst_buffer) {
3623 ResourceAccessRange range = MakeRange(dstOffset, 4);
3624 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3625 }
3626}