blob: 543d4e67dc4e8e35953a4f3ea7031337367b82d0 [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 {
locke-lunarg7077d502020-06-18 21:37:26 -06001635 bool skip = false;
1636 if (!current_renderpass_context_) return skip;
1637 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1638 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1639 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001640}
1641
1642void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg7077d502020-06-18 21:37:26 -06001643 if (current_renderpass_context_)
1644 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1645 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001646}
1647
John Zulauf355e49b2020-04-24 15:11:15 -06001648bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001649 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001650 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001651 skip |=
1652 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001653
1654 return skip;
1655}
1656
1657bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1658 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001659 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001660 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001661 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001662 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1663 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001664
1665 return skip;
1666}
1667
1668void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1669 assert(sync_state_);
1670 if (!cb_state_) return;
1671
1672 // Create an access context the current renderpass.
1673 render_pass_contexts_.emplace_back(&cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06001674 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf355e49b2020-04-24 15:11:15 -06001675 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001676 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001677}
1678
John Zulauf355e49b2020-04-24 15:11:15 -06001679void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001680 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001681 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001682 current_context_ = &current_renderpass_context_->CurrentContext();
1683}
1684
John Zulauf355e49b2020-04-24 15:11:15 -06001685void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001686 assert(current_renderpass_context_);
1687 if (!current_renderpass_context_) return;
1688
John Zulauf7635de32020-05-29 17:14:15 -06001689 current_renderpass_context_->RecordEndRenderPass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001690 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001691 current_renderpass_context_ = nullptr;
1692}
1693
locke-lunarg61870c22020-06-09 14:51:50 -06001694bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1695 const VkRect2D &render_area, const char *func_name) const {
1696 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001697 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001698 if (!pPipe ||
1699 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001700 return skip;
1701 }
1702 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001703 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1704 VkExtent3D extent = CastTo3D(render_area.extent);
1705 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001706
locke-lunarg44f9bb12020-06-10 14:43:57 -06001707 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001708 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1709 for (const auto location : list) {
1710 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1711 continue;
1712 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
1713 HazardResult hazard = external_context_->DetectHazard(
1714 img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent);
1715 if (hazard.hazard) {
1716 skip |= sync_state.LogError(
1717 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1718 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d", func_name,
1719 string_SyncHazard(hazard.hazard), sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1720 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass, location);
locke-lunarg61870c22020-06-09 14:51:50 -06001721 }
1722 }
1723 }
locke-lunarg37047832020-06-12 13:44:45 -06001724
1725 // PHASE1 TODO: Add layout based read/vs. write selection.
1726 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1727 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1728 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001729 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001730 bool depth_write = false, stencil_write = false;
1731
1732 // PHASE1 TODO: These validation should be in core_checks.
1733 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1734 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1735 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1736 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1737 depth_write = true;
1738 }
1739 // PHASE1 TODO: It needs to check if stencil is writable.
1740 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1741 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1742 // PHASE1 TODO: These validation should be in core_checks.
1743 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1744 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1745 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1746 stencil_write = true;
1747 }
1748
1749 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1750 if (depth_write) {
1751 HazardResult hazard =
1752 external_context_->DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1753 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
1754 if (hazard.hazard) {
1755 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1756 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment",
1757 func_name, string_SyncHazard(hazard.hazard),
1758 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1759 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass);
1760 }
1761 }
1762 if (stencil_write) {
1763 HazardResult hazard =
1764 external_context_->DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1765 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
1766 if (hazard.hazard) {
1767 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1768 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment",
1769 func_name, string_SyncHazard(hazard.hazard),
1770 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1771 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass);
1772 }
locke-lunarg61870c22020-06-09 14:51:50 -06001773 }
1774 }
1775 return skip;
1776}
1777
locke-lunarg96dc9632020-06-10 17:22:18 -06001778void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1779 const ResourceUsageTag &tag) {
1780 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001781 if (!pPipe ||
1782 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001783 return;
1784 }
1785 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001786 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1787 VkExtent3D extent = CastTo3D(render_area.extent);
1788 VkOffset3D offset = CastTo3D(render_area.offset);
1789
locke-lunarg44f9bb12020-06-10 14:43:57 -06001790 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001791 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1792 for (const auto location : list) {
1793 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1794 continue;
1795 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
1796 external_context_->UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset,
1797 extent, 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001798 }
1799 }
locke-lunarg37047832020-06-12 13:44:45 -06001800
1801 // PHASE1 TODO: Add layout based read/vs. write selection.
1802 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1803 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1804 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001805 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001806 bool depth_write = false, stencil_write = false;
1807
1808 // PHASE1 TODO: These validation should be in core_checks.
1809 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1810 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1811 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1812 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1813 depth_write = true;
1814 }
1815 // PHASE1 TODO: It needs to check if stencil is writable.
1816 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1817 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1818 // PHASE1 TODO: These validation should be in core_checks.
1819 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1820 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1821 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1822 stencil_write = true;
1823 }
1824
1825 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1826 if (depth_write) {
1827 external_context_->UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1828 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
1829 }
1830 if (stencil_write) {
1831 external_context_->UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1832 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
1833 }
locke-lunarg61870c22020-06-09 14:51:50 -06001834 }
1835}
1836
John Zulauf1507ee42020-05-18 11:33:09 -06001837bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1838 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001839 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001840 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001841 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1842 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001843 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1844 func_name);
1845
John Zulauf355e49b2020-04-24 15:11:15 -06001846 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06001847 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06001848 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1849 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1850 return skip;
1851}
1852bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
1853 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001854 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06001855 bool skip = false;
1856 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1857 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001858 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1859 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06001860 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001861 return skip;
1862}
1863
John Zulauf7635de32020-05-29 17:14:15 -06001864AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
1865 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
1866}
1867
1868bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
1869 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001870 bool skip = false;
1871
John Zulauf7635de32020-05-29 17:14:15 -06001872 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
1873 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
1874 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1875 // to apply and only copy then, if this proves a hot spot.
1876 std::unique_ptr<AccessContext> proxy_for_current;
1877
John Zulauf355e49b2020-04-24 15:11:15 -06001878 // Validate the "finalLayout" transitions to external
1879 // Get them from where there we're hidding in the extra entry.
1880 const auto &final_transitions = rp_state_->subpass_transitions.back();
1881 for (const auto &transition : final_transitions) {
1882 const auto &attach_view = attachment_views_[transition.attachment];
1883 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
1884 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06001885 auto *context = trackback.context;
1886
1887 if (transition.prev_pass == current_subpass_) {
1888 if (!proxy_for_current) {
1889 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
1890 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
1891 }
1892 context = proxy_for_current.get();
1893 }
1894
1895 auto hazard = context->DetectImageBarrierHazard(
John Zulauf355e49b2020-04-24 15:11:15 -06001896 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
1897 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
1898 if (hazard.hazard) {
1899 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
1900 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf1dae9192020-06-16 15:46:44 -06001901 " final image layout transition. Prior access %s.",
1902 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
1903 string_UsageTag(hazard.tag).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001904 }
1905 }
1906 return skip;
1907}
1908
1909void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
1910 // Add layout transitions...
1911 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
1912 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06001913 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06001914 for (const auto &transition : transitions) {
1915 const auto attachment_view = attachment_views_[transition.attachment];
1916 if (!attachment_view) continue;
1917 const auto image = attachment_view->image_state.get();
1918 if (!image) continue;
1919
1920 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -06001921 auto insert_pair = view_seen.insert(attachment_view);
1922 if (insert_pair.second) {
1923 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
1924 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
1925
1926 } else {
1927 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
1928 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
1929 auto barrier_to_transition = barrier->barrier;
1930 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
1931 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
1932 }
John Zulauf355e49b2020-04-24 15:11:15 -06001933 }
1934}
1935
John Zulauf1507ee42020-05-18 11:33:09 -06001936void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
1937 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
1938 auto &subpass_context = subpass_contexts_[current_subpass_];
1939 VkExtent3D extent = CastTo3D(render_area.extent);
1940 VkOffset3D offset = CastTo3D(render_area.offset);
1941
1942 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
1943 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
1944 if (attachment_views_[i] == nullptr) continue; // UNUSED
1945 const auto &view = *attachment_views_[i];
1946 const IMAGE_STATE *image = view.image_state.get();
1947 if (image == nullptr) continue;
1948
1949 const auto &ci = attachment_ci[i];
1950 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001951 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001952 const bool is_color = !(has_depth || has_stencil);
1953
1954 if (is_color) {
1955 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
1956 extent, tag);
1957 } else {
1958 auto update_range = view.normalized_subresource_range;
1959 if (has_depth) {
1960 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1961 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
1962 }
1963 if (has_stencil) {
1964 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1965 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
1966 tag);
1967 }
1968 }
1969 }
1970 }
1971}
1972
John Zulauf355e49b2020-04-24 15:11:15 -06001973void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
1974 VkQueueFlags queue_flags, const ResourceUsageTag &tag) {
1975 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06001976 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06001977 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
1978 // Add this for all subpasses here so that they exsist during next subpass validation
1979 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
1980 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context_);
1981 }
1982 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
1983
1984 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06001985 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001986}
John Zulauf1507ee42020-05-18 11:33:09 -06001987
1988void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001989 // Resolves are against *prior* subpass context and thus *before* the subpass increment
1990 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001991 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06001992
John Zulauf355e49b2020-04-24 15:11:15 -06001993 current_subpass_++;
1994 assert(current_subpass_ < subpass_contexts_.size());
1995 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06001996 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001997}
1998
John Zulauf7635de32020-05-29 17:14:15 -06001999void RenderPassAccessContext::RecordEndRenderPass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002000 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002001 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002002 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002003
John Zulauf355e49b2020-04-24 15:11:15 -06002004 // Export the accesses from the renderpass...
2005 external_context_->ResolveChildContexts(subpass_contexts_);
2006
2007 // Add the "finalLayout" transitions to external
2008 // Get them from where there we're hidding in the extra entry.
2009 const auto &final_transitions = rp_state_->subpass_transitions.back();
2010 for (const auto &transition : final_transitions) {
2011 const auto &attachment = attachment_views_[transition.attachment];
2012 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2013 assert(external_context_ == last_trackback.context);
2014 external_context_->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
2015 attachment->normalized_subresource_range, true, tag);
2016 }
2017}
2018
John Zulauf3d84f1b2020-03-09 13:33:25 -06002019SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2020 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2021 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2022 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2023 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2024 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2025 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2026}
2027
2028void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
2029 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
2030 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
2031}
2032
John Zulauf9cb530d2019-09-30 14:14:10 -06002033HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2034 HazardResult hazard;
2035 auto usage = FlagBit(usage_index);
2036 if (IsRead(usage)) {
John Zulaufc9201222020-05-13 15:13:03 -06002037 if (last_write && IsWriteHazard(usage)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002038 hazard.Set(READ_AFTER_WRITE, write_tag);
2039 }
2040 } else {
2041 // Assume write
2042 // TODO determine what to do with READ-WRITE usage states if any
2043 // Write-After-Write check -- if we have a previous write to test against
2044 if (last_write && IsWriteHazard(usage)) {
2045 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2046 } else {
John Zulauf69133422020-05-20 14:55:53 -06002047 // Look for casus belli for WAR
John Zulauf9cb530d2019-09-30 14:14:10 -06002048 const auto usage_stage = PipelineStageBit(usage_index);
2049 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2050 if (IsReadHazard(usage_stage, last_reads[read_index])) {
2051 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
2052 break;
2053 }
2054 }
2055 }
2056 }
2057 return hazard;
2058}
2059
John Zulauf69133422020-05-20 14:55:53 -06002060HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2061 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2062 HazardResult hazard;
2063 const auto usage = FlagBit(usage_index);
2064 const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good.
2065 if (IsRead(usage)) {
2066 if (!write_is_ordered && IsWriteHazard(usage)) {
2067 hazard.Set(READ_AFTER_WRITE, write_tag);
2068 }
2069 } else {
2070 if (!write_is_ordered && IsWriteHazard(usage)) {
2071 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2072 } else {
2073 const auto usage_stage = PipelineStageBit(usage_index);
2074 const auto unordered_reads = last_read_stages & ~ordering.exec_scope;
2075 if (unordered_reads) {
2076 // Look for any WAR hazards outside the ordered set of stages
2077 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2078 if (last_reads[read_index].stage & unordered_reads) {
2079 if (IsReadHazard(usage_stage, last_reads[read_index])) {
2080 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
2081 break;
2082 }
2083 }
2084 }
2085 }
2086 }
2087 }
2088 return hazard;
2089}
2090
John Zulauf2f952d22020-02-10 11:34:51 -07002091// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002092HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002093 HazardResult hazard;
2094 auto usage = FlagBit(usage_index);
2095 if (IsRead(usage)) {
2096 if (last_write != 0) {
2097 hazard.Set(READ_RACING_WRITE, write_tag);
2098 }
2099 } else {
2100 if (last_write != 0) {
2101 hazard.Set(WRITE_RACING_WRITE, write_tag);
2102 } else if (last_read_count > 0) {
2103 hazard.Set(WRITE_RACING_READ, last_reads[0].tag);
2104 }
2105 }
2106 return hazard;
2107}
2108
John Zulauf36bcf6a2020-02-03 15:12:52 -07002109HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2110 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002111 // Only supporting image layout transitions for now
2112 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2113 HazardResult hazard;
2114 if (last_write) {
2115 // If the previous write is *not* in the 1st access scope
2116 // *AND* the current barrier is not in the dependency chain
2117 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2118 // then the barrier access is unsafe (R/W after W)
John Zulauf36bcf6a2020-02-03 15:12:52 -07002119 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
John Zulauf0cb5be22020-01-23 12:18:22 -07002120 // TODO: Do we need a difference hazard name for this?
2121 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2122 }
John Zulauf355e49b2020-04-24 15:11:15 -06002123 }
2124 if (!hazard.hazard) {
2125 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002126 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002127 const auto &read_access = last_reads[read_index];
2128 // If the read stage is not in the src sync sync
2129 // *AND* not execution chained with an existing sync barrier (that's the or)
2130 // then the barrier access is unsafe (R/W after R)
2131 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
2132 hazard.Set(WRITE_AFTER_READ, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002133 break;
2134 }
2135 }
2136 }
2137 return hazard;
2138}
2139
John Zulauf5f13a792020-03-10 07:31:21 -06002140// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2141// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2142// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2143void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2144 if (write_tag.IsBefore(other.write_tag)) {
2145 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
2146 *this = other;
2147 } else if (!other.write_tag.IsBefore(write_tag)) {
2148 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2149 // dependency chaining logic or any stage expansion)
2150 write_barriers |= other.write_barriers;
2151
2152 // Merge that read states
2153 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2154 auto &other_read = other.last_reads[other_read_index];
2155 if (last_read_stages & other_read.stage) {
2156 // Merge in the barriers for read stages that exist in *both* this and other
2157 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
2158 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
2159 auto &my_read = last_reads[my_read_index];
2160 if (other_read.stage == my_read.stage) {
2161 if (my_read.tag.IsBefore(other_read.tag)) {
2162 my_read.tag = other_read.tag;
2163 }
2164 my_read.barriers |= other_read.barriers;
2165 break;
2166 }
2167 }
2168 } else {
2169 // The other read stage doesn't exist in this, so add it.
2170 last_reads[last_read_count] = other_read;
2171 last_read_count++;
2172 last_read_stages |= other_read.stage;
2173 }
2174 }
2175 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
2176 // it.
2177}
2178
John Zulauf9cb530d2019-09-30 14:14:10 -06002179void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2180 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2181 const auto usage_bit = FlagBit(usage_index);
2182 if (IsRead(usage_index)) {
2183 // Mulitple outstanding reads may be of interest and do dependency chains independently
2184 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2185 const auto usage_stage = PipelineStageBit(usage_index);
2186 if (usage_stage & last_read_stages) {
2187 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2188 ReadState &access = last_reads[read_index];
2189 if (access.stage == usage_stage) {
2190 access.barriers = 0;
2191 access.tag = tag;
2192 break;
2193 }
2194 }
2195 } else {
2196 // We don't have this stage in the list yet...
2197 assert(last_read_count < last_reads.size());
2198 ReadState &access = last_reads[last_read_count++];
2199 access.stage = usage_stage;
2200 access.barriers = 0;
2201 access.tag = tag;
2202 last_read_stages |= usage_stage;
2203 }
2204 } else {
2205 // Assume write
2206 // TODO determine what to do with READ-WRITE operations if any
2207 // Clobber last read and both sets of barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2208 // if the last_reads/last_write were unsafe, we've reported them,
2209 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
2210 last_read_count = 0;
2211 last_read_stages = 0;
2212
2213 write_barriers = 0;
2214 write_dependency_chain = 0;
2215 write_tag = tag;
2216 last_write = usage_bit;
2217 }
2218}
John Zulauf5f13a792020-03-10 07:31:21 -06002219
John Zulauf9cb530d2019-09-30 14:14:10 -06002220void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
2221 // Execution Barriers only protect read operations
2222 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2223 ReadState &access = last_reads[read_index];
2224 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2225 if (srcStageMask & (access.stage | access.barriers)) {
2226 access.barriers |= dstStageMask;
2227 }
2228 }
2229 if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask;
2230}
2231
John Zulauf36bcf6a2020-02-03 15:12:52 -07002232void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
2233 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002234 // Assuming we've applied the execution side of this barrier, we update just the write
2235 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07002236 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
2237 write_barriers |= dst_access_scope;
2238 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06002239 }
2240}
2241
John Zulaufd1f85d42020-04-15 12:23:15 -06002242void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002243 auto *access_context = GetAccessContextNoInsert(command_buffer);
2244 if (access_context) {
2245 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002246 }
2247}
2248
John Zulaufd1f85d42020-04-15 12:23:15 -06002249void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2250 auto access_found = cb_access_state.find(command_buffer);
2251 if (access_found != cb_access_state.end()) {
2252 access_found->second->Reset();
2253 cb_access_state.erase(access_found);
2254 }
2255}
2256
John Zulauf540266b2020-04-06 18:54:53 -06002257void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002258 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
2259 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002260 const VkMemoryBarrier *pMemoryBarriers) {
2261 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07002262 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002263 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002264 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002265}
2266
John Zulauf540266b2020-04-06 18:54:53 -06002267void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002268 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2269 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002270 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002271 for (uint32_t index = 0; index < barrier_count; index++) {
locke-lunarg3c038002020-04-30 23:08:08 -06002272 auto barrier = barriers[index];
John Zulauf9cb530d2019-09-30 14:14:10 -06002273 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2274 if (!buffer) continue;
locke-lunarg3c038002020-04-30 23:08:08 -06002275 barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002276 ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002277 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2278 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2279 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2280 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002281 }
2282}
2283
John Zulauf540266b2020-04-06 18:54:53 -06002284void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2285 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2286 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002287 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002288 for (uint32_t index = 0; index < barrier_count; index++) {
2289 const auto &barrier = barriers[index];
2290 const auto *image = Get<IMAGE_STATE>(barrier.image);
2291 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002292 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002293 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2294 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2295 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2296 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
2297 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002298 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002299}
2300
2301bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2302 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2303 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002304 const auto *cb_context = GetAccessContext(commandBuffer);
2305 assert(cb_context);
2306 if (!cb_context) return skip;
2307 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002308
John Zulauf3d84f1b2020-03-09 13:33:25 -06002309 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002310 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002311 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002312
2313 for (uint32_t region = 0; region < regionCount; region++) {
2314 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002315 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002316 ResourceAccessRange src_range = MakeRange(
2317 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002318 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002319 if (hazard.hazard) {
2320 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002321 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002322 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Prior access %s.",
2323 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
2324 string_UsageTag(hazard.tag).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002325 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002326 }
John Zulauf16adfc92020-04-08 10:28:33 -06002327 if (dst_buffer && !skip) {
locke-lunargff255f92020-05-13 18:53:52 -06002328 ResourceAccessRange dst_range = MakeRange(
2329 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf355e49b2020-04-24 15:11:15 -06002330 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002331 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002332 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002333 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Prior access %s.",
2334 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
2335 string_UsageTag(hazard.tag).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002336 }
2337 }
2338 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002339 }
2340 return skip;
2341}
2342
2343void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2344 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002345 auto *cb_context = GetAccessContext(commandBuffer);
2346 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002347 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002348 auto *context = cb_context->GetCurrentAccessContext();
2349
John Zulauf9cb530d2019-09-30 14:14:10 -06002350 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002351 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002352
2353 for (uint32_t region = 0; region < regionCount; region++) {
2354 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002355 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002356 ResourceAccessRange src_range = MakeRange(
2357 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002358 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002359 }
John Zulauf16adfc92020-04-08 10:28:33 -06002360 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002361 ResourceAccessRange dst_range = MakeRange(
2362 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002363 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002364 }
2365 }
2366}
2367
2368bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2369 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2370 const VkImageCopy *pRegions) const {
2371 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002372 const auto *cb_access_context = GetAccessContext(commandBuffer);
2373 assert(cb_access_context);
2374 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002375
John Zulauf3d84f1b2020-03-09 13:33:25 -06002376 const auto *context = cb_access_context->GetCurrentAccessContext();
2377 assert(context);
2378 if (!context) return skip;
2379
2380 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2381 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002382 for (uint32_t region = 0; region < regionCount; region++) {
2383 const auto &copy_region = pRegions[region];
2384 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002385 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002386 copy_region.srcOffset, copy_region.extent);
2387 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002388 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002389 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
2390 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
2391 string_UsageTag(hazard.tag).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002392 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002393 }
2394
2395 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002396 VkExtent3D dst_copy_extent =
2397 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002398 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002399 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002400 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002401 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002402 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
2403 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
2404 string_UsageTag(hazard.tag).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002405 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002406 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002407 }
2408 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002409
John Zulauf5c5e88d2019-12-26 11:22:02 -07002410 return skip;
2411}
2412
2413void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2414 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2415 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002416 auto *cb_access_context = GetAccessContext(commandBuffer);
2417 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002418 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002419 auto *context = cb_access_context->GetCurrentAccessContext();
2420 assert(context);
2421
John Zulauf5c5e88d2019-12-26 11:22:02 -07002422 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002423 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002424
2425 for (uint32_t region = 0; region < regionCount; region++) {
2426 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002427 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002428 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2429 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002430 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002431 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002432 VkExtent3D dst_copy_extent =
2433 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002434 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2435 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002436 }
2437 }
2438}
2439
2440bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2441 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2442 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2443 uint32_t bufferMemoryBarrierCount,
2444 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2445 uint32_t imageMemoryBarrierCount,
2446 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2447 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002448 const auto *cb_access_context = GetAccessContext(commandBuffer);
2449 assert(cb_access_context);
2450 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002451
John Zulauf3d84f1b2020-03-09 13:33:25 -06002452 const auto *context = cb_access_context->GetCurrentAccessContext();
2453 assert(context);
2454 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002455
John Zulauf3d84f1b2020-03-09 13:33:25 -06002456 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002457 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2458 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002459 // Validate Image Layout transitions
2460 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2461 const auto &barrier = pImageMemoryBarriers[index];
2462 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2463 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2464 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002465 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002466 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002467 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002468 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002469 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Prior access %s.",
2470 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
2471 string_UsageTag(hazard.tag).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002472 }
2473 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002474
2475 return skip;
2476}
2477
2478void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2479 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2480 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2481 uint32_t bufferMemoryBarrierCount,
2482 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2483 uint32_t imageMemoryBarrierCount,
2484 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002485 auto *cb_access_context = GetAccessContext(commandBuffer);
2486 assert(cb_access_context);
2487 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002488 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002489 auto access_context = cb_access_context->GetCurrentAccessContext();
2490 assert(access_context);
2491 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002492
John Zulauf3d84f1b2020-03-09 13:33:25 -06002493 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002494 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002495 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002496 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2497 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2498 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002499 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
2500 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002501 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06002502 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002503
2504 // 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 -06002505 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07002506 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06002507}
2508
2509void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
2510 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
2511 // The state tracker sets up the device state
2512 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
2513
John Zulauf5f13a792020-03-10 07:31:21 -06002514 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
2515 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06002516 // TODO: Find a good way to do this hooklessly.
2517 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
2518 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
2519 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
2520
John Zulaufd1f85d42020-04-15 12:23:15 -06002521 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2522 sync_device_state->ResetCommandBufferCallback(command_buffer);
2523 });
2524 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2525 sync_device_state->FreeCommandBufferCallback(command_buffer);
2526 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002527}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002528
John Zulauf355e49b2020-04-24 15:11:15 -06002529bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2530 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2531 bool skip = false;
2532 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2533 auto cb_context = GetAccessContext(commandBuffer);
2534
2535 if (rp_state && cb_context) {
2536 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2537 }
2538
2539 return skip;
2540}
2541
2542bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2543 VkSubpassContents contents) const {
2544 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2545 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2546 subpass_begin_info.contents = contents;
2547 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
2548 return skip;
2549}
2550
2551bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2552 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2553 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2554 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
2555 return skip;
2556}
2557
2558bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2559 const VkRenderPassBeginInfo *pRenderPassBegin,
2560 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2561 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2562 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
2563 return skip;
2564}
2565
John Zulauf3d84f1b2020-03-09 13:33:25 -06002566void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
2567 VkResult result) {
2568 // The state tracker sets up the command buffer state
2569 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
2570
2571 // Create/initialize the structure that trackers accesses at the command buffer scope.
2572 auto cb_access_context = GetAccessContext(commandBuffer);
2573 assert(cb_access_context);
2574 cb_access_context->Reset();
2575}
2576
2577void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06002578 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002579 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002580 if (cb_context) {
2581 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002582 }
2583}
2584
2585void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2586 VkSubpassContents contents) {
2587 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2588 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2589 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002590 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002591}
2592
2593void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2594 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2595 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002596 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002597}
2598
2599void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2600 const VkRenderPassBeginInfo *pRenderPassBegin,
2601 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2602 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002603 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
2604}
2605
2606bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2607 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
2608 bool skip = false;
2609
2610 auto cb_context = GetAccessContext(commandBuffer);
2611 assert(cb_context);
2612 auto cb_state = cb_context->GetCommandBufferState();
2613 if (!cb_state) return skip;
2614
2615 auto rp_state = cb_state->activeRenderPass;
2616 if (!rp_state) return skip;
2617
2618 skip |= cb_context->ValidateNextSubpass(func_name);
2619
2620 return skip;
2621}
2622
2623bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
2624 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
2625 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2626 subpass_begin_info.contents = contents;
2627 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
2628 return skip;
2629}
2630
2631bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2632 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2633 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2634 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
2635 return skip;
2636}
2637
2638bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2639 const VkSubpassEndInfo *pSubpassEndInfo) const {
2640 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2641 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
2642 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002643}
2644
2645void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06002646 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002647 auto cb_context = GetAccessContext(commandBuffer);
2648 assert(cb_context);
2649 auto cb_state = cb_context->GetCommandBufferState();
2650 if (!cb_state) return;
2651
2652 auto rp_state = cb_state->activeRenderPass;
2653 if (!rp_state) return;
2654
John Zulauf355e49b2020-04-24 15:11:15 -06002655 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002656}
2657
2658void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
2659 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
2660 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2661 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002662 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002663}
2664
2665void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2666 const VkSubpassEndInfo *pSubpassEndInfo) {
2667 StateTracker::PostCallRecordCmdNextSubpass2(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
2671void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2672 const VkSubpassEndInfo *pSubpassEndInfo) {
2673 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002674 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002675}
2676
John Zulauf355e49b2020-04-24 15:11:15 -06002677bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
2678 const char *func_name) const {
2679 bool skip = false;
2680
2681 auto cb_context = GetAccessContext(commandBuffer);
2682 assert(cb_context);
2683 auto cb_state = cb_context->GetCommandBufferState();
2684 if (!cb_state) return skip;
2685
2686 auto rp_state = cb_state->activeRenderPass;
2687 if (!rp_state) return skip;
2688
2689 skip |= cb_context->ValidateEndRenderpass(func_name);
2690 return skip;
2691}
2692
2693bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2694 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
2695 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
2696 return skip;
2697}
2698
2699bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
2700 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2701 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2702 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
2703 return skip;
2704}
2705
2706bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
2707 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2708 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2709 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
2710 return skip;
2711}
2712
2713void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
2714 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06002715 // Resolve the all subpass contexts to the command buffer contexts
2716 auto cb_context = GetAccessContext(commandBuffer);
2717 assert(cb_context);
2718 auto cb_state = cb_context->GetCommandBufferState();
2719 if (!cb_state) return;
2720
locke-lunargaecf2152020-05-12 17:15:41 -06002721 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06002722 if (!rp_state) return;
2723
John Zulauf355e49b2020-04-24 15:11:15 -06002724 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06002725}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002726
2727void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
2728 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002729 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002730}
2731
2732void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
2733 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002734 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002735}
2736
2737void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
2738 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002739 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002740}
locke-lunarga19c71d2020-03-02 18:17:04 -07002741
2742bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2743 VkImageLayout dstImageLayout, uint32_t regionCount,
2744 const VkBufferImageCopy *pRegions) const {
2745 bool skip = false;
2746 const auto *cb_access_context = GetAccessContext(commandBuffer);
2747 assert(cb_access_context);
2748 if (!cb_access_context) return skip;
2749
2750 const auto *context = cb_access_context->GetCurrentAccessContext();
2751 assert(context);
2752 if (!context) return skip;
2753
2754 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07002755 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2756
2757 for (uint32_t region = 0; region < regionCount; region++) {
2758 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002759 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002760 ResourceAccessRange src_range =
2761 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002762 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002763 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002764 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002765 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002766 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32 ". Prior access %s.",
2767 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
2768 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002769 }
2770 }
2771 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002772 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002773 copy_region.imageOffset, copy_region.imageExtent);
2774 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002775 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002776 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
2777 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
2778 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002779 }
2780 if (skip) break;
2781 }
2782 if (skip) break;
2783 }
2784 return skip;
2785}
2786
2787void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2788 VkImageLayout dstImageLayout, uint32_t regionCount,
2789 const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06002790 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07002791 auto *cb_access_context = GetAccessContext(commandBuffer);
2792 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002793 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002794 auto *context = cb_access_context->GetCurrentAccessContext();
2795 assert(context);
2796
2797 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06002798 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002799
2800 for (uint32_t region = 0; region < regionCount; region++) {
2801 const auto &copy_region = pRegions[region];
2802 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002803 ResourceAccessRange src_range =
2804 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002805 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002806 }
2807 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002808 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002809 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002810 }
2811 }
2812}
2813
2814bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
2815 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
2816 const VkBufferImageCopy *pRegions) const {
2817 bool skip = false;
2818 const auto *cb_access_context = GetAccessContext(commandBuffer);
2819 assert(cb_access_context);
2820 if (!cb_access_context) return skip;
2821
2822 const auto *context = cb_access_context->GetCurrentAccessContext();
2823 assert(context);
2824 if (!context) return skip;
2825
2826 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2827 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2828 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
2829 for (uint32_t region = 0; region < regionCount; region++) {
2830 const auto &copy_region = pRegions[region];
2831 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002832 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002833 copy_region.imageOffset, copy_region.imageExtent);
2834 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002835 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002836 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
2837 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
2838 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002839 }
2840 }
2841 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06002842 ResourceAccessRange dst_range =
2843 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002844 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002845 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002846 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002847 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Prior access %s.",
2848 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
2849 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002850 }
2851 }
2852 if (skip) break;
2853 }
2854 return skip;
2855}
2856
2857void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2858 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06002859 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07002860 auto *cb_access_context = GetAccessContext(commandBuffer);
2861 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002862 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07002863 auto *context = cb_access_context->GetCurrentAccessContext();
2864 assert(context);
2865
2866 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002867 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2868 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 -06002869 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07002870
2871 for (uint32_t region = 0; region < regionCount; region++) {
2872 const auto &copy_region = pRegions[region];
2873 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002874 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002875 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002876 }
2877 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002878 ResourceAccessRange dst_range =
2879 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002880 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002881 }
2882 }
2883}
2884
2885bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2886 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2887 const VkImageBlit *pRegions, VkFilter filter) const {
2888 bool skip = false;
2889 const auto *cb_access_context = GetAccessContext(commandBuffer);
2890 assert(cb_access_context);
2891 if (!cb_access_context) return skip;
2892
2893 const auto *context = cb_access_context->GetCurrentAccessContext();
2894 assert(context);
2895 if (!context) return skip;
2896
2897 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2898 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2899
2900 for (uint32_t region = 0; region < regionCount; region++) {
2901 const auto &blit_region = pRegions[region];
2902 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06002903 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
2904 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
2905 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
2906 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
2907 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
2908 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
2909 auto hazard =
2910 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07002911 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002912 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002913 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
2914 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
2915 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002916 }
2917 }
2918
2919 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06002920 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
2921 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
2922 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
2923 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
2924 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
2925 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
2926 auto hazard =
2927 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07002928 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002929 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002930 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
2931 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
2932 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002933 }
2934 if (skip) break;
2935 }
2936 }
2937
2938 return skip;
2939}
2940
2941void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2942 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2943 const VkImageBlit *pRegions, VkFilter filter) {
locke-lunarg8ec19162020-06-16 18:48:34 -06002944 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
2945 pRegions, filter);
locke-lunarga19c71d2020-03-02 18:17:04 -07002946 auto *cb_access_context = GetAccessContext(commandBuffer);
2947 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002948 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002949 auto *context = cb_access_context->GetCurrentAccessContext();
2950 assert(context);
2951
2952 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002953 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002954
2955 for (uint32_t region = 0; region < regionCount; region++) {
2956 const auto &blit_region = pRegions[region];
2957 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06002958 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
2959 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
2960 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
2961 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
2962 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
2963 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
2964 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002965 }
2966 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06002967 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
2968 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
2969 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
2970 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
2971 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
2972 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
2973 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002974 }
2975 }
2976}
locke-lunarg36ba2592020-04-03 09:42:04 -06002977
locke-lunarg61870c22020-06-09 14:51:50 -06002978bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
2979 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
2980 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06002981 bool skip = false;
2982 if (drawCount == 0) return skip;
2983
2984 const auto *buf_state = Get<BUFFER_STATE>(buffer);
2985 VkDeviceSize size = struct_size;
2986 if (drawCount == 1 || stride == size) {
2987 if (drawCount > 1) size *= drawCount;
2988 ResourceAccessRange range = MakeRange(offset, size);
2989 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
2990 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06002991 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
2992 "%s: Hazard %s for indirect %s in %s. Prior access %s.", function, string_SyncHazard(hazard.hazard),
2993 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
2994 string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06002995 }
2996 } else {
2997 for (uint32_t i = 0; i < drawCount; ++i) {
2998 ResourceAccessRange range = MakeRange(offset + i * stride, size);
2999 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3000 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003001 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
3002 "%s: Hazard %s for indirect %s in %s. Prior access %s.", function,
3003 string_SyncHazard(hazard.hazard), report_data->FormatHandle(buffer).c_str(),
3004 report_data->FormatHandle(commandBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003005 break;
3006 }
3007 }
3008 }
3009 return skip;
3010}
3011
locke-lunarg61870c22020-06-09 14:51:50 -06003012void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3013 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3014 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003015 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3016 VkDeviceSize size = struct_size;
3017 if (drawCount == 1 || stride == size) {
3018 if (drawCount > 1) size *= drawCount;
3019 ResourceAccessRange range = MakeRange(offset, size);
3020 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3021 } else {
3022 for (uint32_t i = 0; i < drawCount; ++i) {
3023 ResourceAccessRange range = MakeRange(offset + i * stride, size);
3024 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3025 }
3026 }
3027}
3028
locke-lunarg61870c22020-06-09 14:51:50 -06003029bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3030 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003031 bool skip = false;
3032
3033 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3034 ResourceAccessRange range = MakeRange(offset, 4);
3035 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3036 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003037 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
3038 "%s: Hazard %s for countBuffer %s in %s. Prior access %s.", function, string_SyncHazard(hazard.hazard),
3039 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3040 string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003041 }
3042 return skip;
3043}
3044
locke-lunarg61870c22020-06-09 14:51:50 -06003045void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003046 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3047 ResourceAccessRange range = MakeRange(offset, 4);
3048 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3049}
3050
locke-lunarg36ba2592020-04-03 09:42:04 -06003051bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003052 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003053 const auto *cb_access_context = GetAccessContext(commandBuffer);
3054 assert(cb_access_context);
3055 if (!cb_access_context) return skip;
3056
locke-lunarg61870c22020-06-09 14:51:50 -06003057 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003058 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003059}
3060
3061void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003062 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003063 auto *cb_access_context = GetAccessContext(commandBuffer);
3064 assert(cb_access_context);
3065 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003066
locke-lunarg61870c22020-06-09 14:51:50 -06003067 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003068}
locke-lunarge1a67022020-04-29 00:15:36 -06003069
3070bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003071 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003072 const auto *cb_access_context = GetAccessContext(commandBuffer);
3073 assert(cb_access_context);
3074 if (!cb_access_context) return skip;
3075
3076 const auto *context = cb_access_context->GetCurrentAccessContext();
3077 assert(context);
3078 if (!context) return skip;
3079
locke-lunarg61870c22020-06-09 14:51:50 -06003080 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3081 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3082 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003083 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003084}
3085
3086void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003087 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003088 auto *cb_access_context = GetAccessContext(commandBuffer);
3089 assert(cb_access_context);
3090 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3091 auto *context = cb_access_context->GetCurrentAccessContext();
3092 assert(context);
3093
locke-lunarg61870c22020-06-09 14:51:50 -06003094 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3095 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003096}
3097
3098bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3099 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003100 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003101 const auto *cb_access_context = GetAccessContext(commandBuffer);
3102 assert(cb_access_context);
3103 if (!cb_access_context) return skip;
3104
locke-lunarg61870c22020-06-09 14:51:50 -06003105 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3106 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3107 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003108 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003109}
3110
3111void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3112 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003113 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003114 auto *cb_access_context = GetAccessContext(commandBuffer);
3115 assert(cb_access_context);
3116 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003117
locke-lunarg61870c22020-06-09 14:51:50 -06003118 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3119 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3120 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003121}
3122
3123bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3124 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003125 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003126 const auto *cb_access_context = GetAccessContext(commandBuffer);
3127 assert(cb_access_context);
3128 if (!cb_access_context) return skip;
3129
locke-lunarg61870c22020-06-09 14:51:50 -06003130 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3131 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3132 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003133 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003134}
3135
3136void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3137 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003138 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003139 auto *cb_access_context = GetAccessContext(commandBuffer);
3140 assert(cb_access_context);
3141 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003142
locke-lunarg61870c22020-06-09 14:51:50 -06003143 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3144 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3145 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003146}
3147
3148bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3149 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003150 bool skip = false;
3151 if (drawCount == 0) return skip;
3152
locke-lunargff255f92020-05-13 18:53:52 -06003153 const auto *cb_access_context = GetAccessContext(commandBuffer);
3154 assert(cb_access_context);
3155 if (!cb_access_context) return skip;
3156
3157 const auto *context = cb_access_context->GetCurrentAccessContext();
3158 assert(context);
3159 if (!context) return skip;
3160
locke-lunarg61870c22020-06-09 14:51:50 -06003161 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3162 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3163 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3164 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003165
3166 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3167 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3168 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003169 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003170 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003171}
3172
3173void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3174 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003175 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003176 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003177 auto *cb_access_context = GetAccessContext(commandBuffer);
3178 assert(cb_access_context);
3179 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3180 auto *context = cb_access_context->GetCurrentAccessContext();
3181 assert(context);
3182
locke-lunarg61870c22020-06-09 14:51:50 -06003183 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3184 cb_access_context->RecordDrawSubpassAttachment(tag);
3185 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003186
3187 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3188 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3189 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003190 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003191}
3192
3193bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3194 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003195 bool skip = false;
3196 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003197 const auto *cb_access_context = GetAccessContext(commandBuffer);
3198 assert(cb_access_context);
3199 if (!cb_access_context) return skip;
3200
3201 const auto *context = cb_access_context->GetCurrentAccessContext();
3202 assert(context);
3203 if (!context) return skip;
3204
locke-lunarg61870c22020-06-09 14:51:50 -06003205 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3206 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3207 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3208 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003209
3210 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3211 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3212 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003213 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003214 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003215}
3216
3217void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3218 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003219 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003220 auto *cb_access_context = GetAccessContext(commandBuffer);
3221 assert(cb_access_context);
3222 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3223 auto *context = cb_access_context->GetCurrentAccessContext();
3224 assert(context);
3225
locke-lunarg61870c22020-06-09 14:51:50 -06003226 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3227 cb_access_context->RecordDrawSubpassAttachment(tag);
3228 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003229
3230 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3231 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3232 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003233 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003234}
3235
3236bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3237 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3238 uint32_t stride, const char *function) const {
3239 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003240 const auto *cb_access_context = GetAccessContext(commandBuffer);
3241 assert(cb_access_context);
3242 if (!cb_access_context) return skip;
3243
3244 const auto *context = cb_access_context->GetCurrentAccessContext();
3245 assert(context);
3246 if (!context) return skip;
3247
locke-lunarg61870c22020-06-09 14:51:50 -06003248 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3249 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3250 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3251 function);
3252 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003253
3254 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3255 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3256 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003257 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003258 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003259}
3260
3261bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3262 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3263 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003264 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3265 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003266}
3267
3268void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3269 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3270 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003271 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3272 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003273 auto *cb_access_context = GetAccessContext(commandBuffer);
3274 assert(cb_access_context);
3275 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3276 auto *context = cb_access_context->GetCurrentAccessContext();
3277 assert(context);
3278
locke-lunarg61870c22020-06-09 14:51:50 -06003279 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3280 cb_access_context->RecordDrawSubpassAttachment(tag);
3281 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3282 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003283
3284 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3285 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3286 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003287 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003288}
3289
3290bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3291 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3292 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003293 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3294 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003295}
3296
3297void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3298 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3299 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003300 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3301 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003302 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003303}
3304
3305bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3306 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3307 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003308 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3309 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003310}
3311
3312void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3313 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3314 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003315 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3316 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003317 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3318}
3319
3320bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3321 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3322 uint32_t stride, const char *function) const {
3323 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003324 const auto *cb_access_context = GetAccessContext(commandBuffer);
3325 assert(cb_access_context);
3326 if (!cb_access_context) return skip;
3327
3328 const auto *context = cb_access_context->GetCurrentAccessContext();
3329 assert(context);
3330 if (!context) return skip;
3331
locke-lunarg61870c22020-06-09 14:51:50 -06003332 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3333 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3334 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3335 stride, function);
3336 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003337
3338 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3339 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3340 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003341 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003342 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003343}
3344
3345bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3346 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3347 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003348 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3349 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003350}
3351
3352void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3353 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3354 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003355 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3356 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003357 auto *cb_access_context = GetAccessContext(commandBuffer);
3358 assert(cb_access_context);
3359 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3360 auto *context = cb_access_context->GetCurrentAccessContext();
3361 assert(context);
3362
locke-lunarg61870c22020-06-09 14:51:50 -06003363 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3364 cb_access_context->RecordDrawSubpassAttachment(tag);
3365 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3366 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003367
3368 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3369 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003370 // We will update the index and vertex buffer in SubmitQueue in the future.
3371 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003372}
3373
3374bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(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 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003380}
3381
3382void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(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::PreCallRecordCmdDrawIndexedIndirectCountKHR(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::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3391 VkDeviceSize offset, VkBuffer countBuffer,
3392 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3393 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003394 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3395 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003396}
3397
3398void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3399 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3400 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003401 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3402 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003403 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3404}
3405
3406bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3407 const VkClearColorValue *pColor, uint32_t rangeCount,
3408 const VkImageSubresourceRange *pRanges) const {
3409 bool skip = false;
3410 const auto *cb_access_context = GetAccessContext(commandBuffer);
3411 assert(cb_access_context);
3412 if (!cb_access_context) return skip;
3413
3414 const auto *context = cb_access_context->GetCurrentAccessContext();
3415 assert(context);
3416 if (!context) return skip;
3417
3418 const auto *image_state = Get<IMAGE_STATE>(image);
3419
3420 for (uint32_t index = 0; index < rangeCount; index++) {
3421 const auto &range = pRanges[index];
3422 if (image_state) {
3423 auto hazard =
3424 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3425 if (hazard.hazard) {
3426 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003427 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Prior access %s.",
3428 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
3429 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003430 }
3431 }
3432 }
3433 return skip;
3434}
3435
3436void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3437 const VkClearColorValue *pColor, uint32_t rangeCount,
3438 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003439 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003440 auto *cb_access_context = GetAccessContext(commandBuffer);
3441 assert(cb_access_context);
3442 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
3443 auto *context = cb_access_context->GetCurrentAccessContext();
3444 assert(context);
3445
3446 const auto *image_state = Get<IMAGE_STATE>(image);
3447
3448 for (uint32_t index = 0; index < rangeCount; index++) {
3449 const auto &range = pRanges[index];
3450 if (image_state) {
3451 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3452 tag);
3453 }
3454 }
3455}
3456
3457bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3458 VkImageLayout imageLayout,
3459 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3460 const VkImageSubresourceRange *pRanges) const {
3461 bool skip = false;
3462 const auto *cb_access_context = GetAccessContext(commandBuffer);
3463 assert(cb_access_context);
3464 if (!cb_access_context) return skip;
3465
3466 const auto *context = cb_access_context->GetCurrentAccessContext();
3467 assert(context);
3468 if (!context) return skip;
3469
3470 const auto *image_state = Get<IMAGE_STATE>(image);
3471
3472 for (uint32_t index = 0; index < rangeCount; index++) {
3473 const auto &range = pRanges[index];
3474 if (image_state) {
3475 auto hazard =
3476 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3477 if (hazard.hazard) {
3478 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003479 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Prior access %s.",
3480 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
3481 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003482 }
3483 }
3484 }
3485 return skip;
3486}
3487
3488void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3489 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3490 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003491 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003492 auto *cb_access_context = GetAccessContext(commandBuffer);
3493 assert(cb_access_context);
3494 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
3495 auto *context = cb_access_context->GetCurrentAccessContext();
3496 assert(context);
3497
3498 const auto *image_state = Get<IMAGE_STATE>(image);
3499
3500 for (uint32_t index = 0; index < rangeCount; index++) {
3501 const auto &range = pRanges[index];
3502 if (image_state) {
3503 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3504 tag);
3505 }
3506 }
3507}
3508
3509bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
3510 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
3511 VkDeviceSize dstOffset, VkDeviceSize stride,
3512 VkQueryResultFlags flags) const {
3513 bool skip = false;
3514 const auto *cb_access_context = GetAccessContext(commandBuffer);
3515 assert(cb_access_context);
3516 if (!cb_access_context) return skip;
3517
3518 const auto *context = cb_access_context->GetCurrentAccessContext();
3519 assert(context);
3520 if (!context) return skip;
3521
3522 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3523
3524 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003525 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003526 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3527 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003528 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3529 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Prior access %s.",
3530 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
3531 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003532 }
3533 }
locke-lunargff255f92020-05-13 18:53:52 -06003534
3535 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003536 return skip;
3537}
3538
3539void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
3540 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3541 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003542 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
3543 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06003544 auto *cb_access_context = GetAccessContext(commandBuffer);
3545 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06003546 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06003547 auto *context = cb_access_context->GetCurrentAccessContext();
3548 assert(context);
3549
3550 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3551
3552 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003553 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003554 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3555 }
locke-lunargff255f92020-05-13 18:53:52 -06003556
3557 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003558}
3559
3560bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3561 VkDeviceSize size, uint32_t data) const {
3562 bool skip = false;
3563 const auto *cb_access_context = GetAccessContext(commandBuffer);
3564 assert(cb_access_context);
3565 if (!cb_access_context) return skip;
3566
3567 const auto *context = cb_access_context->GetCurrentAccessContext();
3568 assert(context);
3569 if (!context) return skip;
3570
3571 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3572
3573 if (dst_buffer) {
3574 ResourceAccessRange range = MakeRange(dstOffset, size);
3575 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3576 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003577 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3578 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Prior access %s.", string_SyncHazard(hazard.hazard),
3579 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003580 }
3581 }
3582 return skip;
3583}
3584
3585void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3586 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003587 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06003588 auto *cb_access_context = GetAccessContext(commandBuffer);
3589 assert(cb_access_context);
3590 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
3591 auto *context = cb_access_context->GetCurrentAccessContext();
3592 assert(context);
3593
3594 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3595
3596 if (dst_buffer) {
3597 ResourceAccessRange range = MakeRange(dstOffset, size);
3598 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3599 }
3600}
3601
3602bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3603 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3604 const VkImageResolve *pRegions) const {
3605 bool skip = false;
3606 const auto *cb_access_context = GetAccessContext(commandBuffer);
3607 assert(cb_access_context);
3608 if (!cb_access_context) return skip;
3609
3610 const auto *context = cb_access_context->GetCurrentAccessContext();
3611 assert(context);
3612 if (!context) return skip;
3613
3614 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3615 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3616
3617 for (uint32_t region = 0; region < regionCount; region++) {
3618 const auto &resolve_region = pRegions[region];
3619 if (src_image) {
3620 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3621 resolve_region.srcOffset, resolve_region.extent);
3622 if (hazard.hazard) {
3623 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003624 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
3625 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
3626 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003627 }
3628 }
3629
3630 if (dst_image) {
3631 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3632 resolve_region.dstOffset, resolve_region.extent);
3633 if (hazard.hazard) {
3634 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003635 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
3636 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
3637 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003638 }
3639 if (skip) break;
3640 }
3641 }
3642
3643 return skip;
3644}
3645
3646void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3647 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3648 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003649 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3650 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06003651 auto *cb_access_context = GetAccessContext(commandBuffer);
3652 assert(cb_access_context);
3653 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
3654 auto *context = cb_access_context->GetCurrentAccessContext();
3655 assert(context);
3656
3657 auto *src_image = Get<IMAGE_STATE>(srcImage);
3658 auto *dst_image = Get<IMAGE_STATE>(dstImage);
3659
3660 for (uint32_t region = 0; region < regionCount; region++) {
3661 const auto &resolve_region = pRegions[region];
3662 if (src_image) {
3663 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3664 resolve_region.srcOffset, resolve_region.extent, tag);
3665 }
3666 if (dst_image) {
3667 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3668 resolve_region.dstOffset, resolve_region.extent, tag);
3669 }
3670 }
3671}
3672
3673bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3674 VkDeviceSize dataSize, const void *pData) const {
3675 bool skip = false;
3676 const auto *cb_access_context = GetAccessContext(commandBuffer);
3677 assert(cb_access_context);
3678 if (!cb_access_context) return skip;
3679
3680 const auto *context = cb_access_context->GetCurrentAccessContext();
3681 assert(context);
3682 if (!context) return skip;
3683
3684 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3685
3686 if (dst_buffer) {
3687 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3688 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3689 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003690 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3691 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Prior access %s.", string_SyncHazard(hazard.hazard),
3692 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003693 }
3694 }
3695 return skip;
3696}
3697
3698void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3699 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003700 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06003701 auto *cb_access_context = GetAccessContext(commandBuffer);
3702 assert(cb_access_context);
3703 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
3704 auto *context = cb_access_context->GetCurrentAccessContext();
3705 assert(context);
3706
3707 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3708
3709 if (dst_buffer) {
3710 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3711 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3712 }
3713}
locke-lunargff255f92020-05-13 18:53:52 -06003714
3715bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3716 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
3717 bool skip = false;
3718 const auto *cb_access_context = GetAccessContext(commandBuffer);
3719 assert(cb_access_context);
3720 if (!cb_access_context) return skip;
3721
3722 const auto *context = cb_access_context->GetCurrentAccessContext();
3723 assert(context);
3724 if (!context) return skip;
3725
3726 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3727
3728 if (dst_buffer) {
3729 ResourceAccessRange range = MakeRange(dstOffset, 4);
3730 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3731 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003732 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3733 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Prior access %s.",
3734 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
3735 string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003736 }
3737 }
3738 return skip;
3739}
3740
3741void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3742 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003743 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06003744 auto *cb_access_context = GetAccessContext(commandBuffer);
3745 assert(cb_access_context);
3746 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
3747 auto *context = cb_access_context->GetCurrentAccessContext();
3748 assert(context);
3749
3750 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3751
3752 if (dst_buffer) {
3753 ResourceAccessRange range = MakeRange(dstOffset, 4);
3754 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3755 }
3756}