blob: 81f0573044d6c07b50b2eb2033ca5504c40cb5b1 [file] [log] [blame]
locke-lunarg8ec19162020-06-16 18:48:34 -06001/* Copyright (c) 2019-2020 The Khronos Group Inc.
2 * Copyright (c) 2019-2020 Valve Corporation
3 * Copyright (c) 2019-2020 LunarG, Inc.
John Zulauf9cb530d2019-09-30 14:14:10 -06004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: John Zulauf <jzulauf@lunarg.com>
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 Zulauf1dae9192020-06-16 15:46:44 -060084static std::string string_UsageTag(const ResourceUsageTag &tag) {
85 std::stringstream out;
John Zulaufcc6fecb2020-06-17 15:24:54 -060086 out << "(command " << CommandTypeString(tag.command) << ", seq #" << (tag.index & 0xFFFFFFFF) << ", reset #"
87 << (tag.index >> 32) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -060088 return out.str();
89}
90
John Zulaufb027cdb2020-05-21 14:25:22 -060091static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
92static constexpr SyncStageAccessFlags kColorAttachmentAccessScope =
93 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
94 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
95 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT;
96static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
97 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
98static constexpr SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
99 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
100 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
101 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
102 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
103
104static constexpr SyncOrderingBarrier kColorAttachmentRasterOrder = {kColorAttachmentExecScope, kColorAttachmentAccessScope};
105static constexpr SyncOrderingBarrier kDepthStencilAttachmentRasterOrder = {kDepthStencilAttachmentExecScope,
106 kDepthStencilAttachmentAccessScope};
107static constexpr SyncOrderingBarrier kAttachmentRasterOrder = {kDepthStencilAttachmentExecScope | kColorAttachmentExecScope,
108 kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope};
John Zulauf7635de32020-05-29 17:14:15 -0600109// Sometimes we have an internal access conflict, and we using the kCurrentCommandTag to set and detect in temporary/proxy contexts
John Zulaufcc6fecb2020-06-17 15:24:54 -0600110static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600111
locke-lunarg3c038002020-04-30 23:08:08 -0600112inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
113 if (size == VK_WHOLE_SIZE) {
114 return (whole_size - offset);
115 }
116 return size;
117}
118
John Zulauf16adfc92020-04-08 10:28:33 -0600119template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600120static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600121 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
122}
123
John Zulauf355e49b2020-04-24 15:11:15 -0600124static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600125
John Zulauf0cb5be22020-01-23 12:18:22 -0700126// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
127VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
128 VkPipelineStageFlags expanded = stage_mask;
129 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
130 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
131 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
132 if (all_commands.first & queue_flags) {
133 expanded |= all_commands.second;
134 }
135 }
136 }
137 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
138 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
139 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
140 }
141 return expanded;
142}
143
John Zulauf36bcf6a2020-02-03 15:12:52 -0700144VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
145 std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
146 VkPipelineStageFlags unscanned = stage_mask;
147 VkPipelineStageFlags related = 0;
148 for (const auto entry : map) {
149 const auto stage = entry.first;
150 if (stage & unscanned) {
151 related = related | entry.second;
152 unscanned = unscanned & ~stage;
153 if (!unscanned) break;
154 }
155 }
156 return related;
157}
158
159VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
160 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
161}
162
163VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
164 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
165}
166
John Zulauf5c5e88d2019-12-26 11:22:02 -0700167static const ResourceAccessRange full_range(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700168
locke-lunargff255f92020-05-13 18:53:52 -0600169void GetBufferRange(VkDeviceSize &range_start, VkDeviceSize &range_size, VkDeviceSize offset, VkDeviceSize buf_whole_size,
170 uint32_t first_index, uint32_t count, VkDeviceSize stride) {
171 range_start = offset + first_index * stride;
172 range_size = 0;
173 if (count == UINT32_MAX) {
174 range_size = buf_whole_size - range_start;
175 } else {
176 range_size = count * stride;
177 }
178}
179
locke-lunarg654e3692020-06-04 17:19:15 -0600180SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
181 VkShaderStageFlagBits stage_flag) {
182 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
183 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
184 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
185 }
186 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
187 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
188 assert(0);
189 }
190 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
191 return stage_access->second.uniform_read;
192 }
193
194 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
195 // Because if write hazard happens, read hazard might or might not happen.
196 // But if write hazard doesn't happen, read hazard is impossible to happen.
197 if (descriptor_data.is_writable) {
198 return stage_access->second.shader_write;
199 }
200 return stage_access->second.shader_read;
201}
202
locke-lunarg37047832020-06-12 13:44:45 -0600203bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
204 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
205 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
206 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
207 ? true
208 : false;
209}
210
211bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
212 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
213 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
214 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
215 ? true
216 : false;
217}
218
John Zulauf355e49b2020-04-24 15:11:15 -0600219// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
220const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
221 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
222
John Zulauf7635de32020-05-29 17:14:15 -0600223// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
224// Used by both validation and record operations
225//
226// The signature for Action() reflect the needs of both uses.
227template <typename Action>
228void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
229 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
230 VkExtent3D extent = CastTo3D(render_area.extent);
231 VkOffset3D offset = CastTo3D(render_area.offset);
232 const auto &rp_ci = rp_state.createInfo;
233 const auto *attachment_ci = rp_ci.pAttachments;
234 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
235
236 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
237 const auto *color_attachments = subpass_ci.pColorAttachments;
238 const auto *color_resolve = subpass_ci.pResolveAttachments;
239 if (color_resolve && color_attachments) {
240 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
241 const auto &color_attach = color_attachments[i].attachment;
242 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
243 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
244 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
245 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
246 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
247 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
248 }
249 }
250 }
251
252 // Depth stencil resolve only if the extension is present
253 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
254 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
255 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
256 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
257 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
258 const auto src_ci = attachment_ci[src_at];
259 // The formats are required to match so we can pick either
260 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
261 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
262 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
263 VkImageAspectFlags aspect_mask = 0u;
264
265 // Figure out which aspects are actually touched during resolve operations
266 const char *aspect_string = nullptr;
267 if (resolve_depth && resolve_stencil) {
268 // Validate all aspects together
269 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
270 aspect_string = "depth/stencil";
271 } else if (resolve_depth) {
272 // Validate depth only
273 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
274 aspect_string = "depth";
275 } else if (resolve_stencil) {
276 // Validate all stencil only
277 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
278 aspect_string = "stencil";
279 }
280
281 if (aspect_mask) {
282 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
283 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kDepthStencilAttachmentRasterOrder, offset, extent,
284 aspect_mask);
285 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
286 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
287 }
288 }
289}
290
291// Action for validating resolve operations
292class ValidateResolveAction {
293 public:
294 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
295 const char *func_name)
296 : render_pass_(render_pass),
297 subpass_(subpass),
298 context_(context),
299 sync_state_(sync_state),
300 func_name_(func_name),
301 skip_(false) {}
302 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
303 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
304 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
305 HazardResult hazard;
306 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
307 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600308 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
309 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
310 " to resolve attachment %" PRIu32 ". Prior access %s.",
311 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
312 src_at, dst_at, string_UsageTag(hazard.tag).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600313 }
314 }
315 // Providing a mechanism for the constructing caller to get the result of the validation
316 bool GetSkip() const { return skip_; }
317
318 private:
319 VkRenderPass render_pass_;
320 const uint32_t subpass_;
321 const AccessContext &context_;
322 const SyncValidator &sync_state_;
323 const char *func_name_;
324 bool skip_;
325};
326
327// Update action for resolve operations
328class UpdateStateResolveAction {
329 public:
330 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
331 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
332 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
333 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
334 // Ignores validation only arguments...
335 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
336 }
337
338 private:
339 AccessContext &context_;
340 const ResourceUsageTag &tag_;
341};
342
John Zulauf540266b2020-04-06 18:54:53 -0600343AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
344 const std::vector<SubpassDependencyGraphNode> &dependencies,
345 const std::vector<AccessContext> &contexts, AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600346 Reset();
347 const auto &subpass_dep = dependencies[subpass];
348 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600349 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600350 for (const auto &prev_dep : subpass_dep.prev) {
351 assert(prev_dep.dependency);
352 const auto dep = *prev_dep.dependency;
John Zulauf540266b2020-04-06 18:54:53 -0600353 prev_.emplace_back(const_cast<AccessContext *>(&contexts[dep.srcSubpass]), queue_flags, dep);
John Zulauf355e49b2020-04-24 15:11:15 -0600354 prev_by_subpass_[dep.srcSubpass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700355 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600356
357 async_.reserve(subpass_dep.async.size());
358 for (const auto async_subpass : subpass_dep.async) {
John Zulauf540266b2020-04-06 18:54:53 -0600359 async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass]));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600360 }
John Zulaufe5da6e52020-03-18 15:32:18 -0600361 if (subpass_dep.barrier_from_external) {
362 src_external_ = TrackBack(external_context, queue_flags, *subpass_dep.barrier_from_external);
363 } else {
364 src_external_ = TrackBack();
365 }
366 if (subpass_dep.barrier_to_external) {
367 dst_external_ = TrackBack(this, queue_flags, *subpass_dep.barrier_to_external);
368 } else {
369 dst_external_ = TrackBack();
John Zulauf3d84f1b2020-03-09 13:33:25 -0600370 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700371}
372
John Zulauf5f13a792020-03-10 07:31:21 -0600373template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600374HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600375 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600376 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600377 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600378
379 HazardResult hazard;
380 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
381 hazard = detector.Detect(prev);
382 }
383 return hazard;
384}
385
John Zulauf3d84f1b2020-03-09 13:33:25 -0600386// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
387// the DAG of the contexts (for example subpasses)
388template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600389HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
390 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600391 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600392
John Zulauf355e49b2020-04-24 15:11:15 -0600393 if (static_cast<uint32_t>(options) | DetectOptions::kDetectAsync) {
394 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
395 // so we'll check these first
396 for (const auto &async_context : async_) {
397 hazard = async_context->DetectAsyncHazard(type, detector, range);
398 if (hazard.hazard) return hazard;
399 }
John Zulauf5f13a792020-03-10 07:31:21 -0600400 }
401
John Zulauf69133422020-05-20 14:55:53 -0600402 const bool detect_prev = (static_cast<uint32_t>(options) | DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600403
John Zulauf69133422020-05-20 14:55:53 -0600404 const auto &accesses = GetAccessStateMap(type);
405 const auto from = accesses.lower_bound(range);
406 const auto to = accesses.upper_bound(range);
407 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600408
John Zulauf69133422020-05-20 14:55:53 -0600409 for (auto pos = from; pos != to; ++pos) {
410 // Cover any leading gap, or gap between entries
411 if (detect_prev) {
412 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
413 // Cover any leading gap, or gap between entries
414 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600415 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600416 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600417 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600418 if (hazard.hazard) return hazard;
419 }
John Zulauf69133422020-05-20 14:55:53 -0600420 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
421 gap.begin = pos->first.end;
422 }
423
424 hazard = detector.Detect(pos);
425 if (hazard.hazard) return hazard;
426 }
427
428 if (detect_prev) {
429 // Detect in the trailing empty as needed
430 gap.end = range.end;
431 if (gap.non_empty()) {
432 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600433 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600434 }
435
436 return hazard;
437}
438
439// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
440template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600441HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600442 auto &accesses = GetAccessStateMap(type);
443 const auto from = accesses.lower_bound(range);
444 const auto to = accesses.upper_bound(range);
445
John Zulauf3d84f1b2020-03-09 13:33:25 -0600446 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600447 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
448 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600449 }
John Zulauf16adfc92020-04-08 10:28:33 -0600450
John Zulauf3d84f1b2020-03-09 13:33:25 -0600451 return hazard;
452}
453
John Zulauf355e49b2020-04-24 15:11:15 -0600454// Returns the last resolved entry
455static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
456 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
457 const SyncBarrier *barrier) {
458 auto at = entry;
459 for (auto pos = first; pos != last; ++pos) {
460 // Every member of the input iterator range must fit within the remaining portion of entry
461 assert(at->first.includes(pos->first));
462 assert(at != dest->end());
463 // Trim up at to the same size as the entry to resolve
464 at = sparse_container::split(at, *dest, pos->first);
465 auto access = pos->second;
466 if (barrier) {
467 access.ApplyBarrier(*barrier);
468 }
469 at->second.Resolve(access);
470 ++at; // Go to the remaining unused section of entry
471 }
472}
473
474void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const SyncBarrier *barrier,
475 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
476 bool recur_to_infill) const {
477 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
478 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf16adfc92020-04-08 10:28:33 -0600479 if (current->pos_B->valid) {
480 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600481 auto access = src_pos->second;
482 if (barrier) {
483 access.ApplyBarrier(*barrier);
484 }
John Zulauf16adfc92020-04-08 10:28:33 -0600485 if (current->pos_A->valid) {
486 current.trim_A();
John Zulauf355e49b2020-04-24 15:11:15 -0600487 current->pos_A->lower_bound->second.Resolve(access);
John Zulauf5f13a792020-03-10 07:31:21 -0600488 } else {
John Zulauf355e49b2020-04-24 15:11:15 -0600489 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, access));
490 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600491 }
John Zulauf16adfc92020-04-08 10:28:33 -0600492 } else {
493 // we have to descend to fill this gap
494 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600495 if (current->pos_A->valid) {
496 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
497 ResourceAccessRangeMap gap_map;
498 ResolvePreviousAccess(type, current->range, &gap_map, infill_state);
499 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier);
500 } else {
501 // There isn't anything in dest in current->range, so we can accumulate directly into it.
502 ResolvePreviousAccess(type, current->range, resolve_map, infill_state);
503 if (barrier) {
504 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
505 for (auto pos = resolve_map->lower_bound(current->range); pos != current->pos_A->lower_bound; ++pos) {
506 pos->second.ApplyBarrier(*barrier);
507 }
508 }
509 }
510 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
511 // iterator of the outer while.
512
513 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
514 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
515 // we stepped on the dest map
516 const auto seek_to = current->range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
517 current.invalidate_A(); // Changes current->range
518 current.seek(seek_to);
519 } else if (!current->pos_A->valid && infill_state) {
520 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
521 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
522 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600523 }
John Zulauf5f13a792020-03-10 07:31:21 -0600524 }
John Zulauf16adfc92020-04-08 10:28:33 -0600525 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600526 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600527}
528
John Zulauf355e49b2020-04-24 15:11:15 -0600529void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
530 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600531 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600532 if (range.non_empty() && infill_state) {
533 descent_map->insert(std::make_pair(range, *infill_state));
534 }
535 } else {
536 // Look for something to fill the gap further along.
537 for (const auto &prev_dep : prev_) {
John Zulauf355e49b2020-04-24 15:11:15 -0600538 prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600539 }
540
John Zulaufe5da6e52020-03-18 15:32:18 -0600541 if (src_external_.context) {
John Zulauf355e49b2020-04-24 15:11:15 -0600542 src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600543 }
544 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600545}
546
John Zulauf16adfc92020-04-08 10:28:33 -0600547AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600548 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600549}
550
551VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
552 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
553}
554
John Zulauf355e49b2020-04-24 15:11:15 -0600555static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600556
John Zulauf1507ee42020-05-18 11:33:09 -0600557static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
558 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
559 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
560 return stage_access;
561}
562static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
563 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
564 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
565 return stage_access;
566}
567
John Zulauf7635de32020-05-29 17:14:15 -0600568// Caller must manage returned pointer
569static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
570 uint32_t subpass, const VkRect2D &render_area,
571 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
572 auto *proxy = new AccessContext(context);
573 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600574 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600575 return proxy;
576}
577
John Zulauf540266b2020-04-06 18:54:53 -0600578void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600579 AddressType address_type, ResourceAccessRangeMap *descent_map,
580 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600581 if (!SimpleBinding(image_state)) return;
582
John Zulauf62f10592020-04-03 12:20:02 -0600583 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600584 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600585 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600586 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600587 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600588 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600589 }
590}
591
John Zulauf7635de32020-05-29 17:14:15 -0600592// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600593bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600594 const VkRect2D &render_area, uint32_t subpass,
595 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
596 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600597 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600598 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
599 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
600 // those affects have not been recorded yet.
601 //
602 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
603 // to apply and only copy then, if this proves a hot spot.
604 std::unique_ptr<AccessContext> proxy_for_prev;
605 TrackBack proxy_track_back;
606
John Zulauf355e49b2020-04-24 15:11:15 -0600607 const auto &transitions = rp_state.subpass_transitions[subpass];
608 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600609 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
610
611 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
612 if (prev_needs_proxy) {
613 if (!proxy_for_prev) {
614 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
615 render_area, attachment_views));
616 proxy_track_back = *track_back;
617 proxy_track_back.context = proxy_for_prev.get();
618 }
619 track_back = &proxy_track_back;
620 }
621 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600622 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600623 skip |= sync_state.LogError(
624 rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
625 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " image layout transition. Prior access %s.",
626 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment, string_UsageTag(hazard.tag).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600627 }
628 }
629 return skip;
630}
631
John Zulauf1507ee42020-05-18 11:33:09 -0600632bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600633 const VkRect2D &render_area, uint32_t subpass,
634 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
635 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600636 bool skip = false;
637 const auto *attachment_ci = rp_state.createInfo.pAttachments;
638 VkExtent3D extent = CastTo3D(render_area.extent);
639 VkOffset3D offset = CastTo3D(render_area.offset);
640 const auto external_access_scope = src_external_.barrier.dst_access_scope;
John Zulauf1507ee42020-05-18 11:33:09 -0600641
642 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
643 if (subpass == rp_state.attachment_first_subpass[i]) {
644 if (attachment_views[i] == nullptr) continue;
645 const IMAGE_VIEW_STATE &view = *attachment_views[i];
646 const IMAGE_STATE *image = view.image_state.get();
647 if (image == nullptr) continue;
648 const auto &ci = attachment_ci[i];
649 const bool is_transition = rp_state.attachment_first_is_transition[i];
650
651 // Need check in the following way
652 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
653 // vs. transition
654 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
655 // for each aspect loaded.
656
657 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600658 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600659 const bool is_color = !(has_depth || has_stencil);
660
661 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
662 const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U;
663 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
664 const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U;
665
John Zulaufaff20662020-06-01 14:07:58 -0600666 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600667 const char *aspect = nullptr;
668 if (is_transition) {
669 // For transition w
670 SyncHazard transition_hazard = SyncHazard::NONE;
671 bool checked_stencil = false;
672 if (load_mask) {
673 if ((load_mask & external_access_scope) != load_mask) {
674 transition_hazard =
675 SyncStageAccess::HasWrite(load_mask) ? SyncHazard::WRITE_AFTER_WRITE : SyncHazard::READ_AFTER_WRITE;
676 aspect = is_color ? "color" : "depth";
677 }
678 if (!transition_hazard && stencil_mask) {
679 if ((stencil_mask & external_access_scope) != stencil_mask) {
680 transition_hazard = SyncStageAccess::HasWrite(stencil_mask) ? SyncHazard::WRITE_AFTER_WRITE
681 : SyncHazard::READ_AFTER_WRITE;
682 aspect = "stencil";
683 checked_stencil = true;
684 }
685 }
686 }
687 if (transition_hazard) {
688 // Hazard vs. ILT
689 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
690 skip |=
691 sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
692 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
693 " aspect %s during load with loadOp %s.",
694 func_name, string_SyncHazard(transition_hazard), subpass, i, aspect, load_op_string);
695 }
696 } else {
697 auto hazard_range = view.normalized_subresource_range;
698 bool checked_stencil = false;
699 if (is_color) {
700 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
701 aspect = "color";
702 } else {
703 if (has_depth) {
704 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
705 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
706 aspect = "depth";
707 }
708 if (!hazard.hazard && has_stencil) {
709 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
710 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
711 aspect = "stencil";
712 checked_stencil = true;
713 }
714 }
715
716 if (hazard.hazard) {
717 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
718 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
719 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
720 " aspect %s during load with loadOp %s.",
721 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
722 }
723 }
724 }
725 }
726 return skip;
727}
728
John Zulaufaff20662020-06-01 14:07:58 -0600729// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
730// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
731// store is part of the same Next/End operation.
732// The latter is handled in layout transistion validation directly
733bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
734 const VkRect2D &render_area, uint32_t subpass,
735 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
736 const char *func_name) const {
737 bool skip = false;
738 const auto *attachment_ci = rp_state.createInfo.pAttachments;
739 VkExtent3D extent = CastTo3D(render_area.extent);
740 VkOffset3D offset = CastTo3D(render_area.offset);
741
742 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
743 if (subpass == rp_state.attachment_last_subpass[i]) {
744 if (attachment_views[i] == nullptr) continue;
745 const IMAGE_VIEW_STATE &view = *attachment_views[i];
746 const IMAGE_STATE *image = view.image_state.get();
747 if (image == nullptr) continue;
748 const auto &ci = attachment_ci[i];
749
750 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
751 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
752 // sake, we treat DONT_CARE as writing.
753 const bool has_depth = FormatHasDepth(ci.format);
754 const bool has_stencil = FormatHasStencil(ci.format);
755 const bool is_color = !(has_depth || has_stencil);
756 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
757 if (!has_stencil && !store_op_stores) continue;
758
759 HazardResult hazard;
760 const char *aspect = nullptr;
761 bool checked_stencil = false;
762 if (is_color) {
763 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
764 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
765 aspect = "color";
766 } else {
767 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
768 auto hazard_range = view.normalized_subresource_range;
769 if (has_depth && store_op_stores) {
770 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
771 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
772 kAttachmentRasterOrder, offset, extent);
773 aspect = "depth";
774 }
775 if (!hazard.hazard && has_stencil && stencil_op_stores) {
776 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
777 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
778 kAttachmentRasterOrder, offset, extent);
779 aspect = "stencil";
780 checked_stencil = true;
781 }
782 }
783
784 if (hazard.hazard) {
785 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
786 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -0600787 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
788 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
789 " %s aspect during store with %s %s. Prior access %s",
790 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
791 store_op_string, string_UsageTag(hazard.tag).c_str());
John Zulaufaff20662020-06-01 14:07:58 -0600792 }
793 }
794 }
795 return skip;
796}
797
John Zulaufb027cdb2020-05-21 14:25:22 -0600798bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
799 const VkRect2D &render_area,
800 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
801 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600802 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
803 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
804 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600805}
806
John Zulauf3d84f1b2020-03-09 13:33:25 -0600807class HazardDetector {
808 SyncStageAccessIndex usage_index_;
809
810 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600811 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600812 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
813 return pos->second.DetectAsyncHazard(usage_index_);
814 }
815 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
816};
817
John Zulauf69133422020-05-20 14:55:53 -0600818class HazardDetectorWithOrdering {
819 const SyncStageAccessIndex usage_index_;
820 const SyncOrderingBarrier &ordering_;
821
822 public:
823 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
824 return pos->second.DetectHazard(usage_index_, ordering_);
825 }
826 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
827 return pos->second.DetectAsyncHazard(usage_index_);
828 }
829 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
830 : usage_index_(usage), ordering_(ordering) {}
831};
832
John Zulauf16adfc92020-04-08 10:28:33 -0600833HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600834 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600835 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600836 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600837}
838
John Zulauf16adfc92020-04-08 10:28:33 -0600839HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600840 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600841 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600842 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600843}
844
John Zulauf69133422020-05-20 14:55:53 -0600845template <typename Detector>
846HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
847 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
848 const VkExtent3D &extent, DetectOptions options) const {
849 if (!SimpleBinding(image)) return HazardResult();
850 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
851 const auto address_type = ImageAddressType(image);
852 const auto base_address = ResourceBaseAddress(image);
853 for (; range_gen->non_empty(); ++range_gen) {
854 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
855 if (hazard.hazard) return hazard;
856 }
857 return HazardResult();
858}
859
John Zulauf540266b2020-04-06 18:54:53 -0600860HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
861 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
862 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700863 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
864 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -0600865 return DetectHazard(image, current_usage, subresource_range, offset, extent);
866}
867
868HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
869 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
870 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -0600871 HazardDetector detector(current_usage);
872 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
873}
874
875HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
876 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
877 const VkOffset3D &offset, const VkExtent3D &extent) const {
878 HazardDetectorWithOrdering detector(current_usage, ordering);
879 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -0600880}
881
John Zulaufb027cdb2020-05-21 14:25:22 -0600882// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
883// should have reported the issue regarding an invalid attachment entry
884HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
885 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
886 VkImageAspectFlags aspect_mask) const {
887 if (view != nullptr) {
888 const IMAGE_STATE *image = view->image_state.get();
889 if (image != nullptr) {
890 auto *detect_range = &view->normalized_subresource_range;
891 VkImageSubresourceRange masked_range;
892 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
893 masked_range = view->normalized_subresource_range;
894 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
895 detect_range = &masked_range;
896 }
897
898 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
899 if (detect_range->aspectMask) {
900 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
901 }
902 }
903 }
904 return HazardResult();
905}
John Zulauf3d84f1b2020-03-09 13:33:25 -0600906class BarrierHazardDetector {
907 public:
908 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
909 SyncStageAccessFlags src_access_scope)
910 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
911
John Zulauf5f13a792020-03-10 07:31:21 -0600912 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
913 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -0700914 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600915 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
916 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
917 return pos->second.DetectAsyncHazard(usage_index_);
918 }
919
920 private:
921 SyncStageAccessIndex usage_index_;
922 VkPipelineStageFlags src_exec_scope_;
923 SyncStageAccessFlags src_access_scope_;
924};
925
John Zulauf16adfc92020-04-08 10:28:33 -0600926HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -0600927 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600928 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600929 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -0600930 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700931}
932
John Zulauf16adfc92020-04-08 10:28:33 -0600933HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600934 SyncStageAccessFlags src_access_scope,
935 const VkImageSubresourceRange &subresource_range,
936 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -0600937 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
938 VkOffset3D zero_offset = {0, 0, 0};
939 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700940}
941
John Zulauf355e49b2020-04-24 15:11:15 -0600942HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
943 SyncStageAccessFlags src_stage_accesses,
944 const VkImageMemoryBarrier &barrier) const {
945 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
946 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
947 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
948}
949
John Zulauf9cb530d2019-09-30 14:14:10 -0600950template <typename Flags, typename Map>
951SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
952 SyncStageAccessFlags scope = 0;
953 for (const auto &bit_scope : map) {
954 if (flag_mask < bit_scope.first) break;
955
956 if (flag_mask & bit_scope.first) {
957 scope |= bit_scope.second;
958 }
959 }
960 return scope;
961}
962
963SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
964 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
965}
966
967SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
968 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
969}
970
971// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
972SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -0600973 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
974 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
975 // 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 -0600976 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
977}
978
979template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -0700980void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -0600981 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
982 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -0600983 auto pos = accesses->lower_bound(range);
984 if (pos == accesses->end() || !pos->first.intersects(range)) {
985 // The range is empty, fill it with a default value.
986 pos = action.Infill(accesses, pos, range);
987 } else if (range.begin < pos->first.begin) {
988 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -0700989 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -0600990 } else if (pos->first.begin < range.begin) {
991 // Trim the beginning if needed
992 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
993 ++pos;
994 }
995
996 const auto the_end = accesses->end();
997 while ((pos != the_end) && pos->first.intersects(range)) {
998 if (pos->first.end > range.end) {
999 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1000 }
1001
1002 pos = action(accesses, pos);
1003 if (pos == the_end) break;
1004
1005 auto next = pos;
1006 ++next;
1007 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1008 // Need to infill if next is disjoint
1009 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001010 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001011 next = action.Infill(accesses, next, new_range);
1012 }
1013 pos = next;
1014 }
1015}
1016
1017struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001018 using Iterator = ResourceAccessRangeMap::iterator;
1019 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001020 // this is only called on gaps, and never returns a gap.
1021 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001022 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001023 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001024 }
John Zulauf5f13a792020-03-10 07:31:21 -06001025
John Zulauf5c5e88d2019-12-26 11:22:02 -07001026 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001027 auto &access_state = pos->second;
1028 access_state.Update(usage, tag);
1029 return pos;
1030 }
1031
John Zulauf16adfc92020-04-08 10:28:33 -06001032 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001033 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001034 : type(type_), context(context_), usage(usage_), tag(tag_) {}
1035 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001036 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001037 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001038 const ResourceUsageTag &tag;
1039};
1040
1041struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001042 using Iterator = ResourceAccessRangeMap::iterator;
1043 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001044
John Zulauf5c5e88d2019-12-26 11:22:02 -07001045 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001046 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001047 access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001048 return pos;
1049 }
1050
John Zulauf36bcf6a2020-02-03 15:12:52 -07001051 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
1052 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
1053 : src_exec_scope(src_exec_scope_),
1054 src_access_scope(src_access_scope_),
1055 dst_exec_scope(dst_exec_scope_),
1056 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001057
John Zulauf36bcf6a2020-02-03 15:12:52 -07001058 VkPipelineStageFlags src_exec_scope;
1059 SyncStageAccessFlags src_access_scope;
1060 VkPipelineStageFlags dst_exec_scope;
1061 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001062};
1063
1064struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001065 using Iterator = ResourceAccessRangeMap::iterator;
1066 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001067
John Zulauf5c5e88d2019-12-26 11:22:02 -07001068 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001069 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001070 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001071
1072 for (const auto &functor : barrier_functor) {
1073 functor(accesses, pos);
1074 }
1075 return pos;
1076 }
1077
John Zulauf36bcf6a2020-02-03 15:12:52 -07001078 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
1079 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -06001080 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001081 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001082 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
1083 barrier_functor.reserve(memoryBarrierCount);
1084 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
1085 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -07001086 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
1087 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -06001088 }
1089 }
1090
John Zulauf36bcf6a2020-02-03 15:12:52 -07001091 const VkPipelineStageFlags src_exec_scope;
1092 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001093 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
1094};
1095
John Zulauf355e49b2020-04-24 15:11:15 -06001096void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1097 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001098 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1099 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001100}
1101
John Zulauf16adfc92020-04-08 10:28:33 -06001102void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001103 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001104 if (!SimpleBinding(buffer)) return;
1105 const auto base_address = ResourceBaseAddress(buffer);
1106 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1107}
John Zulauf355e49b2020-04-24 15:11:15 -06001108
John Zulauf540266b2020-04-06 18:54:53 -06001109void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001110 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001111 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001112 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001113 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001114 const auto address_type = ImageAddressType(image);
1115 const auto base_address = ResourceBaseAddress(image);
1116 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001117 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001118 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001119 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001120}
John Zulauf7635de32020-05-29 17:14:15 -06001121void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1122 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1123 if (view != nullptr) {
1124 const IMAGE_STATE *image = view->image_state.get();
1125 if (image != nullptr) {
1126 auto *update_range = &view->normalized_subresource_range;
1127 VkImageSubresourceRange masked_range;
1128 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1129 masked_range = view->normalized_subresource_range;
1130 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1131 update_range = &masked_range;
1132 }
1133 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1134 }
1135 }
1136}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001137
John Zulauf355e49b2020-04-24 15:11:15 -06001138void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1139 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1140 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001141 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1142 subresource.layerCount};
1143 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1144}
1145
John Zulauf540266b2020-04-06 18:54:53 -06001146template <typename Action>
1147void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001148 if (!SimpleBinding(buffer)) return;
1149 const auto base_address = ResourceBaseAddress(buffer);
1150 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001151}
1152
1153template <typename Action>
1154void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1155 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001156 if (!SimpleBinding(image)) return;
1157 const auto address_type = ImageAddressType(image);
1158 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001159
locke-lunargae26eac2020-04-16 15:29:05 -06001160 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001161 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001162
John Zulauf16adfc92020-04-08 10:28:33 -06001163 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001164 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001165 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001166 }
1167}
1168
John Zulauf7635de32020-05-29 17:14:15 -06001169void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1170 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1171 const ResourceUsageTag &tag) {
1172 UpdateStateResolveAction update(*this, tag);
1173 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1174}
1175
John Zulaufaff20662020-06-01 14:07:58 -06001176void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1177 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1178 const ResourceUsageTag &tag) {
1179 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1180 VkExtent3D extent = CastTo3D(render_area.extent);
1181 VkOffset3D offset = CastTo3D(render_area.offset);
1182
1183 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1184 if (rp_state.attachment_last_subpass[i] == subpass) {
1185 if (attachment_views[i] == nullptr) continue; // UNUSED
1186 const auto &view = *attachment_views[i];
1187 const IMAGE_STATE *image = view.image_state.get();
1188 if (image == nullptr) continue;
1189
1190 const auto &ci = attachment_ci[i];
1191 const bool has_depth = FormatHasDepth(ci.format);
1192 const bool has_stencil = FormatHasStencil(ci.format);
1193 const bool is_color = !(has_depth || has_stencil);
1194 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1195
1196 if (is_color && store_op_stores) {
1197 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1198 offset, extent, tag);
1199 } else {
1200 auto update_range = view.normalized_subresource_range;
1201 if (has_depth && store_op_stores) {
1202 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1203 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1204 tag);
1205 }
1206 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1207 if (has_stencil && stencil_op_stores) {
1208 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1209 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1210 tag);
1211 }
1212 }
1213 }
1214 }
1215}
1216
John Zulauf540266b2020-04-06 18:54:53 -06001217template <typename Action>
1218void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1219 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001220 for (const auto address_type : kAddressTypes) {
1221 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001222 }
1223}
1224
1225void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001226 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1227 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -06001228 for (const auto address_type : kAddressTypes) {
John Zulauf355e49b2020-04-24 15:11:15 -06001229 context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier,
1230 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001231 }
1232 }
1233}
1234
John Zulauf355e49b2020-04-24 15:11:15 -06001235void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1236 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1237 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
1238 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1239 UpdateMemoryAccess(image, subresource_range, barrier_action);
1240}
1241
John Zulauf7635de32020-05-29 17:14:15 -06001242// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001243void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1244 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1245 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
1246 bool layout_transition, const ResourceUsageTag &tag) {
1247 if (layout_transition) {
1248 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
1249 tag);
1250 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
1251 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -06001252 } else {
1253 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -06001254 }
John Zulauf355e49b2020-04-24 15:11:15 -06001255}
1256
John Zulauf7635de32020-05-29 17:14:15 -06001257// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001258void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
1259 const VkImageSubresourceRange &subresource_range, bool layout_transition,
1260 const ResourceUsageTag &tag) {
1261 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
1262 subresource_range, layout_transition, tag);
1263}
1264
1265// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001266HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001267 if (!attach_view) return HazardResult();
1268 const auto image_state = attach_view->image_state.get();
1269 if (!image_state) return HazardResult();
1270
John Zulauf355e49b2020-04-24 15:11:15 -06001271 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001272 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001273
1274 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulauf7635de32020-05-29 17:14:15 -06001275 auto hazard = track_back.context->DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope,
1276 track_back.barrier.src_access_scope,
1277 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001278 if (!hazard.hazard) {
1279 // The Async hazard check is against the current context's async set.
John Zulauf7635de32020-05-29 17:14:15 -06001280 hazard = DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope, track_back.barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001281 attach_view->normalized_subresource_range, kDetectAsync);
1282 }
1283 return hazard;
1284}
1285
1286// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1287bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1288
1289 const VkRenderPassBeginInfo *pRenderPassBegin,
1290 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1291 const char *func_name) const {
1292 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1293 bool skip = false;
1294 uint32_t subpass = 0;
1295 const auto &transitions = rp_state.subpass_transitions[subpass];
1296 if (transitions.size()) {
1297 const std::vector<AccessContext> empty_context_vector;
1298 // Create context we can use to validate against...
1299 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1300 const_cast<AccessContext *>(&cb_access_context_));
1301
1302 assert(pRenderPassBegin);
1303 if (nullptr == pRenderPassBegin) return skip;
1304
1305 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1306 assert(fb_state);
1307 if (nullptr == fb_state) return skip;
1308
1309 // Create a limited array of views (which we'll need to toss
1310 std::vector<const IMAGE_VIEW_STATE *> views;
1311 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
1312 const auto attachment_count = count_attachment.first;
1313 const auto *attachments = count_attachment.second;
1314 views.resize(attachment_count, nullptr);
1315 for (const auto &transition : transitions) {
1316 assert(transition.attachment < attachment_count);
1317 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
1318 }
1319
John Zulauf7635de32020-05-29 17:14:15 -06001320 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
1321 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001322 }
1323 return skip;
1324}
1325
locke-lunarg61870c22020-06-09 14:51:50 -06001326bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1327 const char *func_name) const {
1328 bool skip = false;
1329 const PIPELINE_STATE *pPipe = nullptr;
1330 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1331 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1332 if (!pPipe || !per_sets) {
1333 return skip;
1334 }
1335
1336 using DescriptorClass = cvdescriptorset::DescriptorClass;
1337 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1338 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1339 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1340 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1341
1342 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarg37047832020-06-12 13:44:45 -06001343 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001344 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1345 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001346 for (const auto &set_binding : stage_state.descriptor_uses) {
1347 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1348 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1349 set_binding.first.second);
1350 const auto descriptor_type = binding_it.GetType();
1351 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1352 auto array_idx = 0;
1353
1354 if (binding_it.IsVariableDescriptorCount()) {
1355 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1356 }
1357 SyncStageAccessIndex sync_index =
1358 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1359
1360 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1361 uint32_t index = i - index_range.start;
1362 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1363 switch (descriptor->GetClass()) {
1364 case DescriptorClass::ImageSampler:
1365 case DescriptorClass::Image: {
1366 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1367 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1368 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1369 } else {
1370 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1371 }
1372 if (!img_view_state) continue;
1373 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1374 VkExtent3D extent = {};
1375 VkOffset3D offset = {};
1376 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1377 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1378 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1379 } else {
1380 extent = img_state->createInfo.extent;
1381 }
1382 auto hazard = current_context_->DetectHazard(*img_state, sync_index,
1383 img_view_state->normalized_subresource_range, offset, extent);
1384 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06001385 skip |= sync_state_->LogError(
1386 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1387 "%s: Hazard %s for %s in %s, %s, and %s binding #%" PRIu32 " index %" PRIu32 ". Prior access %s.",
1388 func_name, string_SyncHazard(hazard.hazard),
1389 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1390 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1391 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1392 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(), set_binding.first.second,
1393 index, string_UsageTag(hazard.tag).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001394 }
1395 break;
1396 }
1397 case DescriptorClass::TexelBuffer: {
1398 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1399 if (!buf_view_state) continue;
1400 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1401 ResourceAccessRange range =
1402 MakeRange(buf_view_state->create_info.offset,
1403 GetRealWholeSize(buf_view_state->create_info.offset, buf_view_state->create_info.range,
1404 buf_state->createInfo.size));
1405 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
1406 if (hazard.hazard) {
1407 skip |=
1408 sync_state_->LogError(buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
1409 "%s: Hazard %s for %s in %s, %s, and %s binding #%d index %d", func_name,
1410 string_SyncHazard(hazard.hazard),
1411 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1412 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1413 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1414 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1415 set_binding.first.second, index);
1416 }
1417 break;
1418 }
1419 case DescriptorClass::GeneralBuffer: {
1420 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1421 auto buf_state = buffer_descriptor->GetBufferState();
1422 if (!buf_state) continue;
1423 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1424 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
1425 if (hazard.hazard) {
1426 skip |= sync_state_->LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
1427 "%s: Hazard %s for %s in %s, %s, and %s binding #%d index %d", func_name,
1428 string_SyncHazard(hazard.hazard),
1429 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
1430 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1431 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1432 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1433 set_binding.first.second, index);
1434 }
1435 break;
1436 }
1437 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1438 default:
1439 break;
1440 }
1441 }
1442 }
1443 }
1444 return skip;
1445}
1446
1447void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1448 const ResourceUsageTag &tag) {
1449 const PIPELINE_STATE *pPipe = nullptr;
1450 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1451 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1452 if (!pPipe || !per_sets) {
1453 return;
1454 }
1455
1456 using DescriptorClass = cvdescriptorset::DescriptorClass;
1457 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1458 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1459 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1460 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1461
1462 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001463 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
1464 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1465 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001466 for (const auto &set_binding : stage_state.descriptor_uses) {
1467 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1468 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1469 set_binding.first.second);
1470 const auto descriptor_type = binding_it.GetType();
1471 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1472 auto array_idx = 0;
1473
1474 if (binding_it.IsVariableDescriptorCount()) {
1475 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1476 }
1477 SyncStageAccessIndex sync_index =
1478 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1479
1480 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1481 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1482 switch (descriptor->GetClass()) {
1483 case DescriptorClass::ImageSampler:
1484 case DescriptorClass::Image: {
1485 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1486 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1487 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1488 } else {
1489 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1490 }
1491 if (!img_view_state) continue;
1492 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1493 VkExtent3D extent = {};
1494 VkOffset3D offset = {};
1495 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1496 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1497 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1498 } else {
1499 extent = img_state->createInfo.extent;
1500 }
1501 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1502 offset, extent, tag);
1503 break;
1504 }
1505 case DescriptorClass::TexelBuffer: {
1506 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1507 if (!buf_view_state) continue;
1508 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1509 ResourceAccessRange range =
1510 MakeRange(buf_view_state->create_info.offset, buf_view_state->create_info.range);
1511 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1512 break;
1513 }
1514 case DescriptorClass::GeneralBuffer: {
1515 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1516 auto buf_state = buffer_descriptor->GetBufferState();
1517 if (!buf_state) continue;
1518 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1519 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1520 break;
1521 }
1522 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1523 default:
1524 break;
1525 }
1526 }
1527 }
1528 }
1529}
1530
1531bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1532 bool skip = false;
1533 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1534 if (!pPipe) {
1535 return skip;
1536 }
1537
1538 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1539 const auto &binding_buffers_size = binding_buffers.size();
1540 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1541
1542 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1543 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1544 if (binding_description.binding < binding_buffers_size) {
1545 const auto &binding_buffer = binding_buffers[binding_description.binding];
1546 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1547
1548 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1549 VkDeviceSize range_start = 0;
1550 VkDeviceSize range_size = 0;
1551 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1552 binding_description.stride);
1553 ResourceAccessRange range = MakeRange(range_start, range_size);
1554 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1555 if (hazard.hazard) {
1556 skip |= sync_state_->LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
1557 "%s: Hazard %s for vertex %s in %s", func_name, string_SyncHazard(hazard.hazard),
1558 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
1559 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str());
1560 }
1561 }
1562 }
1563 return skip;
1564}
1565
1566void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1567 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1568 if (!pPipe) {
1569 return;
1570 }
1571 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1572 const auto &binding_buffers_size = binding_buffers.size();
1573 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1574
1575 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1576 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1577 if (binding_description.binding < binding_buffers_size) {
1578 const auto &binding_buffer = binding_buffers[binding_description.binding];
1579 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1580
1581 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1582 VkDeviceSize range_start = 0;
1583 VkDeviceSize range_size = 0;
1584 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1585 binding_description.stride);
1586 ResourceAccessRange range = MakeRange(range_start, range_size);
1587 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1588 }
1589 }
1590}
1591
1592bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1593 bool skip = false;
1594 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
1595
1596 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1597 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1598 VkDeviceSize range_start = 0;
1599 VkDeviceSize range_size = 0;
1600 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1601 indexCount, index_size);
1602 ResourceAccessRange range = MakeRange(range_start, range_size);
1603 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1604 if (hazard.hazard) {
1605 skip |= sync_state_->LogError(index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
1606 "%s: Hazard %s for index %s in %s", func_name, string_SyncHazard(hazard.hazard),
1607 sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
1608 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str());
1609 }
1610
1611 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1612 // We will detect more accurate range in the future.
1613 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1614 return skip;
1615}
1616
1617void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
1618 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return;
1619
1620 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1621 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1622 VkDeviceSize range_start = 0;
1623 VkDeviceSize range_size = 0;
1624 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1625 indexCount, index_size);
1626 ResourceAccessRange range = MakeRange(range_start, range_size);
1627 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1628
1629 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1630 // We will detect more accurate range in the future.
1631 RecordDrawVertex(UINT32_MAX, 0, tag);
1632}
1633
1634bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
1635 return current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1636 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1637}
1638
1639void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001640 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1641 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001642}
1643
John Zulauf355e49b2020-04-24 15:11:15 -06001644bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001645 bool skip = false;
John Zulauf1507ee42020-05-18 11:33:09 -06001646 skip |=
1647 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001648
1649 return skip;
1650}
1651
1652bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1653 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001654 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001655 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -06001656 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1657 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001658
1659 return skip;
1660}
1661
1662void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1663 assert(sync_state_);
1664 if (!cb_state_) return;
1665
1666 // Create an access context the current renderpass.
1667 render_pass_contexts_.emplace_back(&cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06001668 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf355e49b2020-04-24 15:11:15 -06001669 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001670 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001671}
1672
John Zulauf355e49b2020-04-24 15:11:15 -06001673void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001674 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001675 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001676 current_context_ = &current_renderpass_context_->CurrentContext();
1677}
1678
John Zulauf355e49b2020-04-24 15:11:15 -06001679void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001680 assert(current_renderpass_context_);
1681 if (!current_renderpass_context_) return;
1682
John Zulauf7635de32020-05-29 17:14:15 -06001683 current_renderpass_context_->RecordEndRenderPass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001684 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001685 current_renderpass_context_ = nullptr;
1686}
1687
locke-lunarg61870c22020-06-09 14:51:50 -06001688bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1689 const VkRect2D &render_area, const char *func_name) const {
1690 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001691 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001692 if (!pPipe ||
1693 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001694 return skip;
1695 }
1696 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001697 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1698 VkExtent3D extent = CastTo3D(render_area.extent);
1699 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001700
locke-lunarg44f9bb12020-06-10 14:43:57 -06001701 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001702 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1703 for (const auto location : list) {
1704 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1705 continue;
1706 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
1707 HazardResult hazard = external_context_->DetectHazard(
1708 img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent);
1709 if (hazard.hazard) {
1710 skip |= sync_state.LogError(
1711 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1712 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d", func_name,
1713 string_SyncHazard(hazard.hazard), sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1714 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass, location);
locke-lunarg61870c22020-06-09 14:51:50 -06001715 }
1716 }
1717 }
locke-lunarg37047832020-06-12 13:44:45 -06001718
1719 // PHASE1 TODO: Add layout based read/vs. write selection.
1720 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1721 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1722 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001723 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001724 bool depth_write = false, stencil_write = false;
1725
1726 // PHASE1 TODO: These validation should be in core_checks.
1727 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1728 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1729 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1730 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1731 depth_write = true;
1732 }
1733 // PHASE1 TODO: It needs to check if stencil is writable.
1734 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1735 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1736 // PHASE1 TODO: These validation should be in core_checks.
1737 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1738 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1739 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1740 stencil_write = true;
1741 }
1742
1743 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1744 if (depth_write) {
1745 HazardResult hazard =
1746 external_context_->DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1747 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
1748 if (hazard.hazard) {
1749 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1750 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment",
1751 func_name, string_SyncHazard(hazard.hazard),
1752 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1753 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass);
1754 }
1755 }
1756 if (stencil_write) {
1757 HazardResult hazard =
1758 external_context_->DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1759 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
1760 if (hazard.hazard) {
1761 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1762 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment",
1763 func_name, string_SyncHazard(hazard.hazard),
1764 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1765 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass);
1766 }
locke-lunarg61870c22020-06-09 14:51:50 -06001767 }
1768 }
1769 return skip;
1770}
1771
locke-lunarg96dc9632020-06-10 17:22:18 -06001772void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1773 const ResourceUsageTag &tag) {
1774 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001775 if (!pPipe ||
1776 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001777 return;
1778 }
1779 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001780 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1781 VkExtent3D extent = CastTo3D(render_area.extent);
1782 VkOffset3D offset = CastTo3D(render_area.offset);
1783
locke-lunarg44f9bb12020-06-10 14:43:57 -06001784 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001785 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1786 for (const auto location : list) {
1787 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1788 continue;
1789 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
1790 external_context_->UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset,
1791 extent, 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001792 }
1793 }
locke-lunarg37047832020-06-12 13:44:45 -06001794
1795 // PHASE1 TODO: Add layout based read/vs. write selection.
1796 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1797 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1798 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001799 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001800 bool depth_write = false, stencil_write = false;
1801
1802 // PHASE1 TODO: These validation should be in core_checks.
1803 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1804 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1805 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1806 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1807 depth_write = true;
1808 }
1809 // PHASE1 TODO: It needs to check if stencil is writable.
1810 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1811 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1812 // PHASE1 TODO: These validation should be in core_checks.
1813 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1814 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1815 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1816 stencil_write = true;
1817 }
1818
1819 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1820 if (depth_write) {
1821 external_context_->UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1822 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
1823 }
1824 if (stencil_write) {
1825 external_context_->UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1826 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
1827 }
locke-lunarg61870c22020-06-09 14:51:50 -06001828 }
1829}
1830
John Zulauf1507ee42020-05-18 11:33:09 -06001831bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1832 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001833 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001834 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001835 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1836 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001837 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1838 func_name);
1839
John Zulauf355e49b2020-04-24 15:11:15 -06001840 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06001841 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06001842 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1843 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1844 return skip;
1845}
1846bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
1847 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001848 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06001849 bool skip = false;
1850 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1851 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001852 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1853 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06001854 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001855 return skip;
1856}
1857
John Zulauf7635de32020-05-29 17:14:15 -06001858AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
1859 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
1860}
1861
1862bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
1863 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001864 bool skip = false;
1865
John Zulauf7635de32020-05-29 17:14:15 -06001866 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
1867 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
1868 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1869 // to apply and only copy then, if this proves a hot spot.
1870 std::unique_ptr<AccessContext> proxy_for_current;
1871
John Zulauf355e49b2020-04-24 15:11:15 -06001872 // Validate the "finalLayout" transitions to external
1873 // Get them from where there we're hidding in the extra entry.
1874 const auto &final_transitions = rp_state_->subpass_transitions.back();
1875 for (const auto &transition : final_transitions) {
1876 const auto &attach_view = attachment_views_[transition.attachment];
1877 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
1878 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06001879 auto *context = trackback.context;
1880
1881 if (transition.prev_pass == current_subpass_) {
1882 if (!proxy_for_current) {
1883 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
1884 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
1885 }
1886 context = proxy_for_current.get();
1887 }
1888
1889 auto hazard = context->DetectImageBarrierHazard(
John Zulauf355e49b2020-04-24 15:11:15 -06001890 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
1891 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
1892 if (hazard.hazard) {
1893 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
1894 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf1dae9192020-06-16 15:46:44 -06001895 " final image layout transition. Prior access %s.",
1896 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
1897 string_UsageTag(hazard.tag).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001898 }
1899 }
1900 return skip;
1901}
1902
1903void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
1904 // Add layout transitions...
1905 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
1906 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06001907 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06001908 for (const auto &transition : transitions) {
1909 const auto attachment_view = attachment_views_[transition.attachment];
1910 if (!attachment_view) continue;
1911 const auto image = attachment_view->image_state.get();
1912 if (!image) continue;
1913
1914 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -06001915 auto insert_pair = view_seen.insert(attachment_view);
1916 if (insert_pair.second) {
1917 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
1918 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
1919
1920 } else {
1921 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
1922 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
1923 auto barrier_to_transition = barrier->barrier;
1924 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
1925 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
1926 }
John Zulauf355e49b2020-04-24 15:11:15 -06001927 }
1928}
1929
John Zulauf1507ee42020-05-18 11:33:09 -06001930void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
1931 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
1932 auto &subpass_context = subpass_contexts_[current_subpass_];
1933 VkExtent3D extent = CastTo3D(render_area.extent);
1934 VkOffset3D offset = CastTo3D(render_area.offset);
1935
1936 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
1937 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
1938 if (attachment_views_[i] == nullptr) continue; // UNUSED
1939 const auto &view = *attachment_views_[i];
1940 const IMAGE_STATE *image = view.image_state.get();
1941 if (image == nullptr) continue;
1942
1943 const auto &ci = attachment_ci[i];
1944 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001945 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001946 const bool is_color = !(has_depth || has_stencil);
1947
1948 if (is_color) {
1949 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
1950 extent, tag);
1951 } else {
1952 auto update_range = view.normalized_subresource_range;
1953 if (has_depth) {
1954 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1955 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
1956 }
1957 if (has_stencil) {
1958 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1959 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
1960 tag);
1961 }
1962 }
1963 }
1964 }
1965}
1966
John Zulauf355e49b2020-04-24 15:11:15 -06001967void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
1968 VkQueueFlags queue_flags, const ResourceUsageTag &tag) {
1969 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06001970 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06001971 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
1972 // Add this for all subpasses here so that they exsist during next subpass validation
1973 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
1974 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context_);
1975 }
1976 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
1977
1978 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06001979 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001980}
John Zulauf1507ee42020-05-18 11:33:09 -06001981
1982void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001983 // Resolves are against *prior* subpass context and thus *before* the subpass increment
1984 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001985 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06001986
John Zulauf355e49b2020-04-24 15:11:15 -06001987 current_subpass_++;
1988 assert(current_subpass_ < subpass_contexts_.size());
1989 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06001990 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001991}
1992
John Zulauf7635de32020-05-29 17:14:15 -06001993void RenderPassAccessContext::RecordEndRenderPass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001994 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06001995 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001996 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06001997
John Zulauf355e49b2020-04-24 15:11:15 -06001998 // Export the accesses from the renderpass...
1999 external_context_->ResolveChildContexts(subpass_contexts_);
2000
2001 // Add the "finalLayout" transitions to external
2002 // Get them from where there we're hidding in the extra entry.
2003 const auto &final_transitions = rp_state_->subpass_transitions.back();
2004 for (const auto &transition : final_transitions) {
2005 const auto &attachment = attachment_views_[transition.attachment];
2006 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2007 assert(external_context_ == last_trackback.context);
2008 external_context_->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
2009 attachment->normalized_subresource_range, true, tag);
2010 }
2011}
2012
John Zulauf3d84f1b2020-03-09 13:33:25 -06002013SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2014 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2015 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2016 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2017 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2018 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2019 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2020}
2021
2022void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
2023 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
2024 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
2025}
2026
John Zulauf9cb530d2019-09-30 14:14:10 -06002027HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2028 HazardResult hazard;
2029 auto usage = FlagBit(usage_index);
2030 if (IsRead(usage)) {
John Zulaufc9201222020-05-13 15:13:03 -06002031 if (last_write && IsWriteHazard(usage)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002032 hazard.Set(READ_AFTER_WRITE, write_tag);
2033 }
2034 } else {
2035 // Assume write
2036 // TODO determine what to do with READ-WRITE usage states if any
2037 // Write-After-Write check -- if we have a previous write to test against
2038 if (last_write && IsWriteHazard(usage)) {
2039 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2040 } else {
John Zulauf69133422020-05-20 14:55:53 -06002041 // Look for casus belli for WAR
John Zulauf9cb530d2019-09-30 14:14:10 -06002042 const auto usage_stage = PipelineStageBit(usage_index);
2043 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2044 if (IsReadHazard(usage_stage, last_reads[read_index])) {
2045 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
2046 break;
2047 }
2048 }
2049 }
2050 }
2051 return hazard;
2052}
2053
John Zulauf69133422020-05-20 14:55:53 -06002054HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2055 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2056 HazardResult hazard;
2057 const auto usage = FlagBit(usage_index);
2058 const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good.
2059 if (IsRead(usage)) {
2060 if (!write_is_ordered && IsWriteHazard(usage)) {
2061 hazard.Set(READ_AFTER_WRITE, write_tag);
2062 }
2063 } else {
2064 if (!write_is_ordered && IsWriteHazard(usage)) {
2065 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2066 } else {
2067 const auto usage_stage = PipelineStageBit(usage_index);
2068 const auto unordered_reads = last_read_stages & ~ordering.exec_scope;
2069 if (unordered_reads) {
2070 // Look for any WAR hazards outside the ordered set of stages
2071 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2072 if (last_reads[read_index].stage & unordered_reads) {
2073 if (IsReadHazard(usage_stage, last_reads[read_index])) {
2074 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
2075 break;
2076 }
2077 }
2078 }
2079 }
2080 }
2081 }
2082 return hazard;
2083}
2084
John Zulauf2f952d22020-02-10 11:34:51 -07002085// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002086HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002087 HazardResult hazard;
2088 auto usage = FlagBit(usage_index);
2089 if (IsRead(usage)) {
2090 if (last_write != 0) {
2091 hazard.Set(READ_RACING_WRITE, write_tag);
2092 }
2093 } else {
2094 if (last_write != 0) {
2095 hazard.Set(WRITE_RACING_WRITE, write_tag);
2096 } else if (last_read_count > 0) {
2097 hazard.Set(WRITE_RACING_READ, last_reads[0].tag);
2098 }
2099 }
2100 return hazard;
2101}
2102
John Zulauf36bcf6a2020-02-03 15:12:52 -07002103HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2104 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002105 // Only supporting image layout transitions for now
2106 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2107 HazardResult hazard;
2108 if (last_write) {
2109 // If the previous write is *not* in the 1st access scope
2110 // *AND* the current barrier is not in the dependency chain
2111 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2112 // then the barrier access is unsafe (R/W after W)
John Zulauf36bcf6a2020-02-03 15:12:52 -07002113 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
John Zulauf0cb5be22020-01-23 12:18:22 -07002114 // TODO: Do we need a difference hazard name for this?
2115 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2116 }
John Zulauf355e49b2020-04-24 15:11:15 -06002117 }
2118 if (!hazard.hazard) {
2119 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002120 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002121 const auto &read_access = last_reads[read_index];
2122 // If the read stage is not in the src sync sync
2123 // *AND* not execution chained with an existing sync barrier (that's the or)
2124 // then the barrier access is unsafe (R/W after R)
2125 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
2126 hazard.Set(WRITE_AFTER_READ, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002127 break;
2128 }
2129 }
2130 }
2131 return hazard;
2132}
2133
John Zulauf5f13a792020-03-10 07:31:21 -06002134// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2135// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2136// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2137void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2138 if (write_tag.IsBefore(other.write_tag)) {
2139 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
2140 *this = other;
2141 } else if (!other.write_tag.IsBefore(write_tag)) {
2142 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2143 // dependency chaining logic or any stage expansion)
2144 write_barriers |= other.write_barriers;
2145
2146 // Merge that read states
2147 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2148 auto &other_read = other.last_reads[other_read_index];
2149 if (last_read_stages & other_read.stage) {
2150 // Merge in the barriers for read stages that exist in *both* this and other
2151 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
2152 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
2153 auto &my_read = last_reads[my_read_index];
2154 if (other_read.stage == my_read.stage) {
2155 if (my_read.tag.IsBefore(other_read.tag)) {
2156 my_read.tag = other_read.tag;
2157 }
2158 my_read.barriers |= other_read.barriers;
2159 break;
2160 }
2161 }
2162 } else {
2163 // The other read stage doesn't exist in this, so add it.
2164 last_reads[last_read_count] = other_read;
2165 last_read_count++;
2166 last_read_stages |= other_read.stage;
2167 }
2168 }
2169 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
2170 // it.
2171}
2172
John Zulauf9cb530d2019-09-30 14:14:10 -06002173void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2174 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2175 const auto usage_bit = FlagBit(usage_index);
2176 if (IsRead(usage_index)) {
2177 // Mulitple outstanding reads may be of interest and do dependency chains independently
2178 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2179 const auto usage_stage = PipelineStageBit(usage_index);
2180 if (usage_stage & last_read_stages) {
2181 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2182 ReadState &access = last_reads[read_index];
2183 if (access.stage == usage_stage) {
2184 access.barriers = 0;
2185 access.tag = tag;
2186 break;
2187 }
2188 }
2189 } else {
2190 // We don't have this stage in the list yet...
2191 assert(last_read_count < last_reads.size());
2192 ReadState &access = last_reads[last_read_count++];
2193 access.stage = usage_stage;
2194 access.barriers = 0;
2195 access.tag = tag;
2196 last_read_stages |= usage_stage;
2197 }
2198 } else {
2199 // Assume write
2200 // TODO determine what to do with READ-WRITE operations if any
2201 // Clobber last read and both sets of barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2202 // if the last_reads/last_write were unsafe, we've reported them,
2203 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
2204 last_read_count = 0;
2205 last_read_stages = 0;
2206
2207 write_barriers = 0;
2208 write_dependency_chain = 0;
2209 write_tag = tag;
2210 last_write = usage_bit;
2211 }
2212}
John Zulauf5f13a792020-03-10 07:31:21 -06002213
John Zulauf9cb530d2019-09-30 14:14:10 -06002214void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
2215 // Execution Barriers only protect read operations
2216 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2217 ReadState &access = last_reads[read_index];
2218 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2219 if (srcStageMask & (access.stage | access.barriers)) {
2220 access.barriers |= dstStageMask;
2221 }
2222 }
2223 if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask;
2224}
2225
John Zulauf36bcf6a2020-02-03 15:12:52 -07002226void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
2227 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002228 // Assuming we've applied the execution side of this barrier, we update just the write
2229 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07002230 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
2231 write_barriers |= dst_access_scope;
2232 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06002233 }
2234}
2235
John Zulaufd1f85d42020-04-15 12:23:15 -06002236void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002237 auto *access_context = GetAccessContextNoInsert(command_buffer);
2238 if (access_context) {
2239 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002240 }
2241}
2242
John Zulaufd1f85d42020-04-15 12:23:15 -06002243void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2244 auto access_found = cb_access_state.find(command_buffer);
2245 if (access_found != cb_access_state.end()) {
2246 access_found->second->Reset();
2247 cb_access_state.erase(access_found);
2248 }
2249}
2250
John Zulauf540266b2020-04-06 18:54:53 -06002251void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002252 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
2253 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002254 const VkMemoryBarrier *pMemoryBarriers) {
2255 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07002256 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002257 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002258 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002259}
2260
John Zulauf540266b2020-04-06 18:54:53 -06002261void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002262 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2263 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002264 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002265 for (uint32_t index = 0; index < barrier_count; index++) {
locke-lunarg3c038002020-04-30 23:08:08 -06002266 auto barrier = barriers[index];
John Zulauf9cb530d2019-09-30 14:14:10 -06002267 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2268 if (!buffer) continue;
locke-lunarg3c038002020-04-30 23:08:08 -06002269 barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002270 ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002271 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2272 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2273 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2274 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002275 }
2276}
2277
John Zulauf540266b2020-04-06 18:54:53 -06002278void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2279 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2280 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002281 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002282 for (uint32_t index = 0; index < barrier_count; index++) {
2283 const auto &barrier = barriers[index];
2284 const auto *image = Get<IMAGE_STATE>(barrier.image);
2285 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002286 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002287 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2288 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2289 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2290 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
2291 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002292 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002293}
2294
2295bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2296 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2297 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002298 const auto *cb_context = GetAccessContext(commandBuffer);
2299 assert(cb_context);
2300 if (!cb_context) return skip;
2301 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002302
John Zulauf3d84f1b2020-03-09 13:33:25 -06002303 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002304 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002305 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002306
2307 for (uint32_t region = 0; region < regionCount; region++) {
2308 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002309 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002310 ResourceAccessRange src_range = MakeRange(
2311 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002312 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002313 if (hazard.hazard) {
2314 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002315 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002316 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Prior access %s.",
2317 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
2318 string_UsageTag(hazard.tag).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002319 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002320 }
John Zulauf16adfc92020-04-08 10:28:33 -06002321 if (dst_buffer && !skip) {
locke-lunargff255f92020-05-13 18:53:52 -06002322 ResourceAccessRange dst_range = MakeRange(
2323 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf355e49b2020-04-24 15:11:15 -06002324 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002325 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002326 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002327 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Prior access %s.",
2328 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
2329 string_UsageTag(hazard.tag).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002330 }
2331 }
2332 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002333 }
2334 return skip;
2335}
2336
2337void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2338 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002339 auto *cb_context = GetAccessContext(commandBuffer);
2340 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002341 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002342 auto *context = cb_context->GetCurrentAccessContext();
2343
John Zulauf9cb530d2019-09-30 14:14:10 -06002344 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002345 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002346
2347 for (uint32_t region = 0; region < regionCount; region++) {
2348 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002349 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002350 ResourceAccessRange src_range = MakeRange(
2351 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002352 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002353 }
John Zulauf16adfc92020-04-08 10:28:33 -06002354 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002355 ResourceAccessRange dst_range = MakeRange(
2356 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002357 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002358 }
2359 }
2360}
2361
2362bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2363 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2364 const VkImageCopy *pRegions) const {
2365 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002366 const auto *cb_access_context = GetAccessContext(commandBuffer);
2367 assert(cb_access_context);
2368 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002369
John Zulauf3d84f1b2020-03-09 13:33:25 -06002370 const auto *context = cb_access_context->GetCurrentAccessContext();
2371 assert(context);
2372 if (!context) return skip;
2373
2374 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2375 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002376 for (uint32_t region = 0; region < regionCount; region++) {
2377 const auto &copy_region = pRegions[region];
2378 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002379 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002380 copy_region.srcOffset, copy_region.extent);
2381 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002382 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002383 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
2384 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
2385 string_UsageTag(hazard.tag).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002386 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002387 }
2388
2389 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002390 VkExtent3D dst_copy_extent =
2391 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002392 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002393 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002394 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002395 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002396 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
2397 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
2398 string_UsageTag(hazard.tag).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002399 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002400 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002401 }
2402 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002403
John Zulauf5c5e88d2019-12-26 11:22:02 -07002404 return skip;
2405}
2406
2407void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2408 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2409 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002410 auto *cb_access_context = GetAccessContext(commandBuffer);
2411 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002412 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002413 auto *context = cb_access_context->GetCurrentAccessContext();
2414 assert(context);
2415
John Zulauf5c5e88d2019-12-26 11:22:02 -07002416 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002417 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002418
2419 for (uint32_t region = 0; region < regionCount; region++) {
2420 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002421 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002422 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2423 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002424 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002425 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002426 VkExtent3D dst_copy_extent =
2427 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002428 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2429 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002430 }
2431 }
2432}
2433
2434bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2435 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2436 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2437 uint32_t bufferMemoryBarrierCount,
2438 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2439 uint32_t imageMemoryBarrierCount,
2440 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2441 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002442 const auto *cb_access_context = GetAccessContext(commandBuffer);
2443 assert(cb_access_context);
2444 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002445
John Zulauf3d84f1b2020-03-09 13:33:25 -06002446 const auto *context = cb_access_context->GetCurrentAccessContext();
2447 assert(context);
2448 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002449
John Zulauf3d84f1b2020-03-09 13:33:25 -06002450 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002451 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2452 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002453 // Validate Image Layout transitions
2454 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2455 const auto &barrier = pImageMemoryBarriers[index];
2456 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2457 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2458 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002459 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002460 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002461 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002462 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002463 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Prior access %s.",
2464 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
2465 string_UsageTag(hazard.tag).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002466 }
2467 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002468
2469 return skip;
2470}
2471
2472void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2473 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2474 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2475 uint32_t bufferMemoryBarrierCount,
2476 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2477 uint32_t imageMemoryBarrierCount,
2478 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002479 auto *cb_access_context = GetAccessContext(commandBuffer);
2480 assert(cb_access_context);
2481 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002482 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002483 auto access_context = cb_access_context->GetCurrentAccessContext();
2484 assert(access_context);
2485 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002486
John Zulauf3d84f1b2020-03-09 13:33:25 -06002487 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002488 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002489 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002490 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2491 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2492 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002493 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
2494 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002495 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06002496 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002497
2498 // 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 -06002499 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07002500 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06002501}
2502
2503void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
2504 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
2505 // The state tracker sets up the device state
2506 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
2507
John Zulauf5f13a792020-03-10 07:31:21 -06002508 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
2509 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06002510 // TODO: Find a good way to do this hooklessly.
2511 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
2512 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
2513 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
2514
John Zulaufd1f85d42020-04-15 12:23:15 -06002515 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2516 sync_device_state->ResetCommandBufferCallback(command_buffer);
2517 });
2518 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2519 sync_device_state->FreeCommandBufferCallback(command_buffer);
2520 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002521}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002522
John Zulauf355e49b2020-04-24 15:11:15 -06002523bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2524 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2525 bool skip = false;
2526 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2527 auto cb_context = GetAccessContext(commandBuffer);
2528
2529 if (rp_state && cb_context) {
2530 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2531 }
2532
2533 return skip;
2534}
2535
2536bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2537 VkSubpassContents contents) const {
2538 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2539 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2540 subpass_begin_info.contents = contents;
2541 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
2542 return skip;
2543}
2544
2545bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2546 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2547 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2548 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
2549 return skip;
2550}
2551
2552bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2553 const VkRenderPassBeginInfo *pRenderPassBegin,
2554 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2555 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2556 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
2557 return skip;
2558}
2559
John Zulauf3d84f1b2020-03-09 13:33:25 -06002560void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
2561 VkResult result) {
2562 // The state tracker sets up the command buffer state
2563 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
2564
2565 // Create/initialize the structure that trackers accesses at the command buffer scope.
2566 auto cb_access_context = GetAccessContext(commandBuffer);
2567 assert(cb_access_context);
2568 cb_access_context->Reset();
2569}
2570
2571void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06002572 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002573 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002574 if (cb_context) {
2575 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002576 }
2577}
2578
2579void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2580 VkSubpassContents contents) {
2581 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2582 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2583 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002584 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002585}
2586
2587void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2588 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2589 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002590 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002591}
2592
2593void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2594 const VkRenderPassBeginInfo *pRenderPassBegin,
2595 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2596 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002597 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
2598}
2599
2600bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2601 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
2602 bool skip = false;
2603
2604 auto cb_context = GetAccessContext(commandBuffer);
2605 assert(cb_context);
2606 auto cb_state = cb_context->GetCommandBufferState();
2607 if (!cb_state) return skip;
2608
2609 auto rp_state = cb_state->activeRenderPass;
2610 if (!rp_state) return skip;
2611
2612 skip |= cb_context->ValidateNextSubpass(func_name);
2613
2614 return skip;
2615}
2616
2617bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
2618 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
2619 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2620 subpass_begin_info.contents = contents;
2621 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
2622 return skip;
2623}
2624
2625bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2626 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2627 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2628 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
2629 return skip;
2630}
2631
2632bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2633 const VkSubpassEndInfo *pSubpassEndInfo) const {
2634 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2635 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
2636 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002637}
2638
2639void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06002640 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002641 auto cb_context = GetAccessContext(commandBuffer);
2642 assert(cb_context);
2643 auto cb_state = cb_context->GetCommandBufferState();
2644 if (!cb_state) return;
2645
2646 auto rp_state = cb_state->activeRenderPass;
2647 if (!rp_state) return;
2648
John Zulauf355e49b2020-04-24 15:11:15 -06002649 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002650}
2651
2652void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
2653 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
2654 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2655 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002656 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002657}
2658
2659void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2660 const VkSubpassEndInfo *pSubpassEndInfo) {
2661 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002662 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002663}
2664
2665void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2666 const VkSubpassEndInfo *pSubpassEndInfo) {
2667 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002668 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002669}
2670
John Zulauf355e49b2020-04-24 15:11:15 -06002671bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
2672 const char *func_name) const {
2673 bool skip = false;
2674
2675 auto cb_context = GetAccessContext(commandBuffer);
2676 assert(cb_context);
2677 auto cb_state = cb_context->GetCommandBufferState();
2678 if (!cb_state) return skip;
2679
2680 auto rp_state = cb_state->activeRenderPass;
2681 if (!rp_state) return skip;
2682
2683 skip |= cb_context->ValidateEndRenderpass(func_name);
2684 return skip;
2685}
2686
2687bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2688 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
2689 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
2690 return skip;
2691}
2692
2693bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
2694 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2695 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2696 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
2697 return skip;
2698}
2699
2700bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
2701 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2702 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2703 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
2704 return skip;
2705}
2706
2707void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
2708 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06002709 // Resolve the all subpass contexts to the command buffer contexts
2710 auto cb_context = GetAccessContext(commandBuffer);
2711 assert(cb_context);
2712 auto cb_state = cb_context->GetCommandBufferState();
2713 if (!cb_state) return;
2714
locke-lunargaecf2152020-05-12 17:15:41 -06002715 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06002716 if (!rp_state) return;
2717
John Zulauf355e49b2020-04-24 15:11:15 -06002718 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06002719}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002720
2721void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
2722 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002723 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002724}
2725
2726void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
2727 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002728 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002729}
2730
2731void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
2732 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002733 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002734}
locke-lunarga19c71d2020-03-02 18:17:04 -07002735
2736bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2737 VkImageLayout dstImageLayout, uint32_t regionCount,
2738 const VkBufferImageCopy *pRegions) const {
2739 bool skip = false;
2740 const auto *cb_access_context = GetAccessContext(commandBuffer);
2741 assert(cb_access_context);
2742 if (!cb_access_context) return skip;
2743
2744 const auto *context = cb_access_context->GetCurrentAccessContext();
2745 assert(context);
2746 if (!context) return skip;
2747
2748 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07002749 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2750
2751 for (uint32_t region = 0; region < regionCount; region++) {
2752 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002753 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002754 ResourceAccessRange src_range =
2755 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002756 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002757 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002758 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002759 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002760 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32 ". Prior access %s.",
2761 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
2762 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002763 }
2764 }
2765 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002766 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002767 copy_region.imageOffset, copy_region.imageExtent);
2768 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002769 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002770 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
2771 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
2772 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002773 }
2774 if (skip) break;
2775 }
2776 if (skip) break;
2777 }
2778 return skip;
2779}
2780
2781void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2782 VkImageLayout dstImageLayout, uint32_t regionCount,
2783 const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06002784 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07002785 auto *cb_access_context = GetAccessContext(commandBuffer);
2786 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002787 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002788 auto *context = cb_access_context->GetCurrentAccessContext();
2789 assert(context);
2790
2791 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06002792 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002793
2794 for (uint32_t region = 0; region < regionCount; region++) {
2795 const auto &copy_region = pRegions[region];
2796 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002797 ResourceAccessRange src_range =
2798 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002799 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002800 }
2801 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002802 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002803 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002804 }
2805 }
2806}
2807
2808bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
2809 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
2810 const VkBufferImageCopy *pRegions) const {
2811 bool skip = false;
2812 const auto *cb_access_context = GetAccessContext(commandBuffer);
2813 assert(cb_access_context);
2814 if (!cb_access_context) return skip;
2815
2816 const auto *context = cb_access_context->GetCurrentAccessContext();
2817 assert(context);
2818 if (!context) return skip;
2819
2820 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2821 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2822 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
2823 for (uint32_t region = 0; region < regionCount; region++) {
2824 const auto &copy_region = pRegions[region];
2825 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002826 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002827 copy_region.imageOffset, copy_region.imageExtent);
2828 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002829 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002830 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
2831 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
2832 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002833 }
2834 }
2835 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06002836 ResourceAccessRange dst_range =
2837 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002838 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002839 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002840 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002841 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Prior access %s.",
2842 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
2843 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002844 }
2845 }
2846 if (skip) break;
2847 }
2848 return skip;
2849}
2850
2851void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2852 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06002853 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07002854 auto *cb_access_context = GetAccessContext(commandBuffer);
2855 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002856 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07002857 auto *context = cb_access_context->GetCurrentAccessContext();
2858 assert(context);
2859
2860 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002861 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2862 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 -06002863 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07002864
2865 for (uint32_t region = 0; region < regionCount; region++) {
2866 const auto &copy_region = pRegions[region];
2867 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002868 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002869 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002870 }
2871 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002872 ResourceAccessRange dst_range =
2873 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002874 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002875 }
2876 }
2877}
2878
2879bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2880 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2881 const VkImageBlit *pRegions, VkFilter filter) const {
2882 bool skip = false;
2883 const auto *cb_access_context = GetAccessContext(commandBuffer);
2884 assert(cb_access_context);
2885 if (!cb_access_context) return skip;
2886
2887 const auto *context = cb_access_context->GetCurrentAccessContext();
2888 assert(context);
2889 if (!context) return skip;
2890
2891 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2892 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2893
2894 for (uint32_t region = 0; region < regionCount; region++) {
2895 const auto &blit_region = pRegions[region];
2896 if (src_image) {
2897 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
2898 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
2899 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002900 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002901 blit_region.srcOffsets[0], extent);
2902 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002903 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002904 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
2905 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
2906 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002907 }
2908 }
2909
2910 if (dst_image) {
2911 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
2912 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
2913 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002914 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002915 blit_region.dstOffsets[0], extent);
2916 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002917 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002918 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
2919 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
2920 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002921 }
2922 if (skip) break;
2923 }
2924 }
2925
2926 return skip;
2927}
2928
2929void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2930 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2931 const VkImageBlit *pRegions, VkFilter filter) {
locke-lunarg8ec19162020-06-16 18:48:34 -06002932 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
2933 pRegions, filter);
locke-lunarga19c71d2020-03-02 18:17:04 -07002934 auto *cb_access_context = GetAccessContext(commandBuffer);
2935 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002936 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002937 auto *context = cb_access_context->GetCurrentAccessContext();
2938 assert(context);
2939
2940 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002941 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002942
2943 for (uint32_t region = 0; region < regionCount; region++) {
2944 const auto &blit_region = pRegions[region];
2945 if (src_image) {
2946 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
2947 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
2948 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002949 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002950 blit_region.srcOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002951 }
2952 if (dst_image) {
2953 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
2954 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
2955 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002956 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002957 blit_region.dstOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002958 }
2959 }
2960}
locke-lunarg36ba2592020-04-03 09:42:04 -06002961
locke-lunarg61870c22020-06-09 14:51:50 -06002962bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
2963 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
2964 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06002965 bool skip = false;
2966 if (drawCount == 0) return skip;
2967
2968 const auto *buf_state = Get<BUFFER_STATE>(buffer);
2969 VkDeviceSize size = struct_size;
2970 if (drawCount == 1 || stride == size) {
2971 if (drawCount > 1) size *= drawCount;
2972 ResourceAccessRange range = MakeRange(offset, size);
2973 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
2974 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06002975 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
2976 "%s: Hazard %s for indirect %s in %s. Prior access %s.", function, string_SyncHazard(hazard.hazard),
2977 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
2978 string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06002979 }
2980 } else {
2981 for (uint32_t i = 0; i < drawCount; ++i) {
2982 ResourceAccessRange range = MakeRange(offset + i * stride, size);
2983 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
2984 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06002985 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
2986 "%s: Hazard %s for indirect %s in %s. Prior access %s.", function,
2987 string_SyncHazard(hazard.hazard), report_data->FormatHandle(buffer).c_str(),
2988 report_data->FormatHandle(commandBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06002989 break;
2990 }
2991 }
2992 }
2993 return skip;
2994}
2995
locke-lunarg61870c22020-06-09 14:51:50 -06002996void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
2997 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
2998 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06002999 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3000 VkDeviceSize size = struct_size;
3001 if (drawCount == 1 || stride == size) {
3002 if (drawCount > 1) size *= drawCount;
3003 ResourceAccessRange range = MakeRange(offset, size);
3004 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3005 } else {
3006 for (uint32_t i = 0; i < drawCount; ++i) {
3007 ResourceAccessRange range = MakeRange(offset + i * stride, size);
3008 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3009 }
3010 }
3011}
3012
locke-lunarg61870c22020-06-09 14:51:50 -06003013bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3014 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003015 bool skip = false;
3016
3017 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3018 ResourceAccessRange range = MakeRange(offset, 4);
3019 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3020 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003021 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
3022 "%s: Hazard %s for countBuffer %s in %s. Prior access %s.", function, string_SyncHazard(hazard.hazard),
3023 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3024 string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003025 }
3026 return skip;
3027}
3028
locke-lunarg61870c22020-06-09 14:51:50 -06003029void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003030 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3031 ResourceAccessRange range = MakeRange(offset, 4);
3032 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3033}
3034
locke-lunarg36ba2592020-04-03 09:42:04 -06003035bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003036 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003037 const auto *cb_access_context = GetAccessContext(commandBuffer);
3038 assert(cb_access_context);
3039 if (!cb_access_context) return skip;
3040
locke-lunarg61870c22020-06-09 14:51:50 -06003041 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003042 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003043}
3044
3045void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003046 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003047 auto *cb_access_context = GetAccessContext(commandBuffer);
3048 assert(cb_access_context);
3049 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003050
locke-lunarg61870c22020-06-09 14:51:50 -06003051 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003052}
locke-lunarge1a67022020-04-29 00:15:36 -06003053
3054bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003055 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003056 const auto *cb_access_context = GetAccessContext(commandBuffer);
3057 assert(cb_access_context);
3058 if (!cb_access_context) return skip;
3059
3060 const auto *context = cb_access_context->GetCurrentAccessContext();
3061 assert(context);
3062 if (!context) return skip;
3063
locke-lunarg61870c22020-06-09 14:51:50 -06003064 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3065 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3066 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003067 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003068}
3069
3070void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003071 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003072 auto *cb_access_context = GetAccessContext(commandBuffer);
3073 assert(cb_access_context);
3074 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3075 auto *context = cb_access_context->GetCurrentAccessContext();
3076 assert(context);
3077
locke-lunarg61870c22020-06-09 14:51:50 -06003078 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3079 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003080}
3081
3082bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3083 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003084 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003085 const auto *cb_access_context = GetAccessContext(commandBuffer);
3086 assert(cb_access_context);
3087 if (!cb_access_context) return skip;
3088
locke-lunarg61870c22020-06-09 14:51:50 -06003089 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3090 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3091 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003092 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003093}
3094
3095void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3096 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003097 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003098 auto *cb_access_context = GetAccessContext(commandBuffer);
3099 assert(cb_access_context);
3100 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003101
locke-lunarg61870c22020-06-09 14:51:50 -06003102 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3103 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3104 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003105}
3106
3107bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3108 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003109 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003110 const auto *cb_access_context = GetAccessContext(commandBuffer);
3111 assert(cb_access_context);
3112 if (!cb_access_context) return skip;
3113
locke-lunarg61870c22020-06-09 14:51:50 -06003114 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3115 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3116 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003117 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003118}
3119
3120void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3121 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003122 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003123 auto *cb_access_context = GetAccessContext(commandBuffer);
3124 assert(cb_access_context);
3125 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003126
locke-lunarg61870c22020-06-09 14:51:50 -06003127 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3128 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3129 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003130}
3131
3132bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3133 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003134 bool skip = false;
3135 if (drawCount == 0) return skip;
3136
locke-lunargff255f92020-05-13 18:53:52 -06003137 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
locke-lunarg61870c22020-06-09 14:51:50 -06003145 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3146 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3147 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3148 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003149
3150 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3151 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3152 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003153 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003154 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003155}
3156
3157void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3158 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003159 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003160 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003161 auto *cb_access_context = GetAccessContext(commandBuffer);
3162 assert(cb_access_context);
3163 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3164 auto *context = cb_access_context->GetCurrentAccessContext();
3165 assert(context);
3166
locke-lunarg61870c22020-06-09 14:51:50 -06003167 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3168 cb_access_context->RecordDrawSubpassAttachment(tag);
3169 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003170
3171 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3172 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3173 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003174 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003175}
3176
3177bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3178 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003179 bool skip = false;
3180 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003181 const auto *cb_access_context = GetAccessContext(commandBuffer);
3182 assert(cb_access_context);
3183 if (!cb_access_context) return skip;
3184
3185 const auto *context = cb_access_context->GetCurrentAccessContext();
3186 assert(context);
3187 if (!context) return skip;
3188
locke-lunarg61870c22020-06-09 14:51:50 -06003189 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3190 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3191 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3192 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003193
3194 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3195 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3196 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003197 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003198 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003199}
3200
3201void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3202 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003203 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003204 auto *cb_access_context = GetAccessContext(commandBuffer);
3205 assert(cb_access_context);
3206 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3207 auto *context = cb_access_context->GetCurrentAccessContext();
3208 assert(context);
3209
locke-lunarg61870c22020-06-09 14:51:50 -06003210 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3211 cb_access_context->RecordDrawSubpassAttachment(tag);
3212 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003213
3214 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3215 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3216 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003217 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003218}
3219
3220bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3221 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3222 uint32_t stride, const char *function) const {
3223 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003224 const auto *cb_access_context = GetAccessContext(commandBuffer);
3225 assert(cb_access_context);
3226 if (!cb_access_context) return skip;
3227
3228 const auto *context = cb_access_context->GetCurrentAccessContext();
3229 assert(context);
3230 if (!context) return skip;
3231
locke-lunarg61870c22020-06-09 14:51:50 -06003232 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3233 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3234 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3235 function);
3236 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003237
3238 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3239 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3240 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003241 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003242 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003243}
3244
3245bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3246 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3247 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003248 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3249 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003250}
3251
3252void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3253 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3254 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003255 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3256 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003257 auto *cb_access_context = GetAccessContext(commandBuffer);
3258 assert(cb_access_context);
3259 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3260 auto *context = cb_access_context->GetCurrentAccessContext();
3261 assert(context);
3262
locke-lunarg61870c22020-06-09 14:51:50 -06003263 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3264 cb_access_context->RecordDrawSubpassAttachment(tag);
3265 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3266 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003267
3268 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3269 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3270 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003271 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003272}
3273
3274bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3275 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3276 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003277 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3278 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003279}
3280
3281void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3282 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3283 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003284 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3285 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003286 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003287}
3288
3289bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3290 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3291 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003292 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3293 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003294}
3295
3296void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3297 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3298 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003299 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3300 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003301 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3302}
3303
3304bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3305 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3306 uint32_t stride, const char *function) const {
3307 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003308 const auto *cb_access_context = GetAccessContext(commandBuffer);
3309 assert(cb_access_context);
3310 if (!cb_access_context) return skip;
3311
3312 const auto *context = cb_access_context->GetCurrentAccessContext();
3313 assert(context);
3314 if (!context) return skip;
3315
locke-lunarg61870c22020-06-09 14:51:50 -06003316 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3317 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3318 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3319 stride, function);
3320 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003321
3322 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3323 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3324 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003325 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003326 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003327}
3328
3329bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3330 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3331 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003332 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3333 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003334}
3335
3336void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3337 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3338 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003339 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3340 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003341 auto *cb_access_context = GetAccessContext(commandBuffer);
3342 assert(cb_access_context);
3343 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3344 auto *context = cb_access_context->GetCurrentAccessContext();
3345 assert(context);
3346
locke-lunarg61870c22020-06-09 14:51:50 -06003347 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3348 cb_access_context->RecordDrawSubpassAttachment(tag);
3349 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3350 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003351
3352 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3353 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003354 // We will update the index and vertex buffer in SubmitQueue in the future.
3355 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003356}
3357
3358bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3359 VkDeviceSize offset, VkBuffer countBuffer,
3360 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3361 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003362 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3363 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003364}
3365
3366void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3367 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3368 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003369 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3370 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003371 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3372}
3373
3374bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3375 VkDeviceSize offset, VkBuffer countBuffer,
3376 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3377 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003378 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3379 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003380}
3381
3382void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3383 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3384 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003385 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3386 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003387 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3388}
3389
3390bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3391 const VkClearColorValue *pColor, uint32_t rangeCount,
3392 const VkImageSubresourceRange *pRanges) const {
3393 bool skip = false;
3394 const auto *cb_access_context = GetAccessContext(commandBuffer);
3395 assert(cb_access_context);
3396 if (!cb_access_context) return skip;
3397
3398 const auto *context = cb_access_context->GetCurrentAccessContext();
3399 assert(context);
3400 if (!context) return skip;
3401
3402 const auto *image_state = Get<IMAGE_STATE>(image);
3403
3404 for (uint32_t index = 0; index < rangeCount; index++) {
3405 const auto &range = pRanges[index];
3406 if (image_state) {
3407 auto hazard =
3408 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3409 if (hazard.hazard) {
3410 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003411 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Prior access %s.",
3412 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
3413 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003414 }
3415 }
3416 }
3417 return skip;
3418}
3419
3420void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3421 const VkClearColorValue *pColor, uint32_t rangeCount,
3422 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003423 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003424 auto *cb_access_context = GetAccessContext(commandBuffer);
3425 assert(cb_access_context);
3426 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
3427 auto *context = cb_access_context->GetCurrentAccessContext();
3428 assert(context);
3429
3430 const auto *image_state = Get<IMAGE_STATE>(image);
3431
3432 for (uint32_t index = 0; index < rangeCount; index++) {
3433 const auto &range = pRanges[index];
3434 if (image_state) {
3435 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3436 tag);
3437 }
3438 }
3439}
3440
3441bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3442 VkImageLayout imageLayout,
3443 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3444 const VkImageSubresourceRange *pRanges) const {
3445 bool skip = false;
3446 const auto *cb_access_context = GetAccessContext(commandBuffer);
3447 assert(cb_access_context);
3448 if (!cb_access_context) return skip;
3449
3450 const auto *context = cb_access_context->GetCurrentAccessContext();
3451 assert(context);
3452 if (!context) return skip;
3453
3454 const auto *image_state = Get<IMAGE_STATE>(image);
3455
3456 for (uint32_t index = 0; index < rangeCount; index++) {
3457 const auto &range = pRanges[index];
3458 if (image_state) {
3459 auto hazard =
3460 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3461 if (hazard.hazard) {
3462 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003463 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Prior access %s.",
3464 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
3465 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003466 }
3467 }
3468 }
3469 return skip;
3470}
3471
3472void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3473 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3474 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003475 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003476 auto *cb_access_context = GetAccessContext(commandBuffer);
3477 assert(cb_access_context);
3478 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
3479 auto *context = cb_access_context->GetCurrentAccessContext();
3480 assert(context);
3481
3482 const auto *image_state = Get<IMAGE_STATE>(image);
3483
3484 for (uint32_t index = 0; index < rangeCount; index++) {
3485 const auto &range = pRanges[index];
3486 if (image_state) {
3487 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3488 tag);
3489 }
3490 }
3491}
3492
3493bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
3494 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
3495 VkDeviceSize dstOffset, VkDeviceSize stride,
3496 VkQueryResultFlags flags) const {
3497 bool skip = false;
3498 const auto *cb_access_context = GetAccessContext(commandBuffer);
3499 assert(cb_access_context);
3500 if (!cb_access_context) return skip;
3501
3502 const auto *context = cb_access_context->GetCurrentAccessContext();
3503 assert(context);
3504 if (!context) return skip;
3505
3506 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3507
3508 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003509 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003510 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3511 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003512 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3513 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Prior access %s.",
3514 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
3515 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003516 }
3517 }
locke-lunargff255f92020-05-13 18:53:52 -06003518
3519 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003520 return skip;
3521}
3522
3523void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
3524 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3525 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003526 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
3527 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06003528 auto *cb_access_context = GetAccessContext(commandBuffer);
3529 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06003530 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06003531 auto *context = cb_access_context->GetCurrentAccessContext();
3532 assert(context);
3533
3534 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3535
3536 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003537 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003538 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3539 }
locke-lunargff255f92020-05-13 18:53:52 -06003540
3541 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003542}
3543
3544bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3545 VkDeviceSize size, uint32_t data) const {
3546 bool skip = false;
3547 const auto *cb_access_context = GetAccessContext(commandBuffer);
3548 assert(cb_access_context);
3549 if (!cb_access_context) return skip;
3550
3551 const auto *context = cb_access_context->GetCurrentAccessContext();
3552 assert(context);
3553 if (!context) return skip;
3554
3555 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3556
3557 if (dst_buffer) {
3558 ResourceAccessRange range = MakeRange(dstOffset, size);
3559 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3560 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003561 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3562 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Prior access %s.", string_SyncHazard(hazard.hazard),
3563 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003564 }
3565 }
3566 return skip;
3567}
3568
3569void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3570 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003571 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06003572 auto *cb_access_context = GetAccessContext(commandBuffer);
3573 assert(cb_access_context);
3574 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
3575 auto *context = cb_access_context->GetCurrentAccessContext();
3576 assert(context);
3577
3578 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3579
3580 if (dst_buffer) {
3581 ResourceAccessRange range = MakeRange(dstOffset, size);
3582 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3583 }
3584}
3585
3586bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3587 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3588 const VkImageResolve *pRegions) 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 *src_image = Get<IMAGE_STATE>(srcImage);
3599 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3600
3601 for (uint32_t region = 0; region < regionCount; region++) {
3602 const auto &resolve_region = pRegions[region];
3603 if (src_image) {
3604 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3605 resolve_region.srcOffset, resolve_region.extent);
3606 if (hazard.hazard) {
3607 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003608 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
3609 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
3610 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003611 }
3612 }
3613
3614 if (dst_image) {
3615 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3616 resolve_region.dstOffset, resolve_region.extent);
3617 if (hazard.hazard) {
3618 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003619 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
3620 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
3621 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003622 }
3623 if (skip) break;
3624 }
3625 }
3626
3627 return skip;
3628}
3629
3630void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3631 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3632 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003633 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3634 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06003635 auto *cb_access_context = GetAccessContext(commandBuffer);
3636 assert(cb_access_context);
3637 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
3638 auto *context = cb_access_context->GetCurrentAccessContext();
3639 assert(context);
3640
3641 auto *src_image = Get<IMAGE_STATE>(srcImage);
3642 auto *dst_image = Get<IMAGE_STATE>(dstImage);
3643
3644 for (uint32_t region = 0; region < regionCount; region++) {
3645 const auto &resolve_region = pRegions[region];
3646 if (src_image) {
3647 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3648 resolve_region.srcOffset, resolve_region.extent, tag);
3649 }
3650 if (dst_image) {
3651 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3652 resolve_region.dstOffset, resolve_region.extent, tag);
3653 }
3654 }
3655}
3656
3657bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3658 VkDeviceSize dataSize, const void *pData) const {
3659 bool skip = false;
3660 const auto *cb_access_context = GetAccessContext(commandBuffer);
3661 assert(cb_access_context);
3662 if (!cb_access_context) return skip;
3663
3664 const auto *context = cb_access_context->GetCurrentAccessContext();
3665 assert(context);
3666 if (!context) return skip;
3667
3668 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3669
3670 if (dst_buffer) {
3671 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3672 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3673 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003674 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3675 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Prior access %s.", string_SyncHazard(hazard.hazard),
3676 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003677 }
3678 }
3679 return skip;
3680}
3681
3682void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3683 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003684 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06003685 auto *cb_access_context = GetAccessContext(commandBuffer);
3686 assert(cb_access_context);
3687 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
3688 auto *context = cb_access_context->GetCurrentAccessContext();
3689 assert(context);
3690
3691 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3692
3693 if (dst_buffer) {
3694 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3695 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3696 }
3697}
locke-lunargff255f92020-05-13 18:53:52 -06003698
3699bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3700 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
3701 bool skip = false;
3702 const auto *cb_access_context = GetAccessContext(commandBuffer);
3703 assert(cb_access_context);
3704 if (!cb_access_context) return skip;
3705
3706 const auto *context = cb_access_context->GetCurrentAccessContext();
3707 assert(context);
3708 if (!context) return skip;
3709
3710 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3711
3712 if (dst_buffer) {
3713 ResourceAccessRange range = MakeRange(dstOffset, 4);
3714 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3715 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003716 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3717 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Prior access %s.",
3718 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
3719 string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003720 }
3721 }
3722 return skip;
3723}
3724
3725void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3726 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003727 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06003728 auto *cb_access_context = GetAccessContext(commandBuffer);
3729 assert(cb_access_context);
3730 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
3731 auto *context = cb_access_context->GetCurrentAccessContext();
3732 assert(context);
3733
3734 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3735
3736 if (dst_buffer) {
3737 ResourceAccessRange range = MakeRange(dstOffset, 4);
3738 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3739 }
3740}