blob: bb4e69b36d61783adf3d5adf010b60c1f2d57f52 [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,
John Zulauf1a224292020-06-30 14:52:13 -0600345 const std::vector<AccessContext> &contexts, const 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 Zulauf1a224292020-06-30 14:52:13 -0600393 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600394 // 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 Zulauf1a224292020-06-30 14:52:13 -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 {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600477 if (!range.non_empty()) return;
478
John Zulauf355e49b2020-04-24 15:11:15 -0600479 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
480 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600481 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600482 if (current->pos_B->valid) {
483 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600484 auto access = src_pos->second;
485 if (barrier) {
486 access.ApplyBarrier(*barrier);
487 }
John Zulauf16adfc92020-04-08 10:28:33 -0600488 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600489 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
490 trimmed->second.Resolve(access);
491 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600492 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600493 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600494 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600495 }
John Zulauf16adfc92020-04-08 10:28:33 -0600496 } else {
497 // we have to descend to fill this gap
498 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600499 if (current->pos_A->valid) {
500 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
501 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600502 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulauf355e49b2020-04-24 15:11:15 -0600503 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier);
504 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600505 // There isn't anything in dest in current)range, so we can accumulate directly into it.
506 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulauf355e49b2020-04-24 15:11:15 -0600507 if (barrier) {
508 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
John Zulauf3bcab5e2020-06-19 14:42:32 -0600509 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
John Zulauf355e49b2020-04-24 15:11:15 -0600510 pos->second.ApplyBarrier(*barrier);
511 }
512 }
513 }
514 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
515 // iterator of the outer while.
516
517 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
518 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
519 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600520 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
521 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600522 current.seek(seek_to);
523 } else if (!current->pos_A->valid && infill_state) {
524 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
525 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
526 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600527 }
John Zulauf5f13a792020-03-10 07:31:21 -0600528 }
John Zulauf16adfc92020-04-08 10:28:33 -0600529 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600530 }
John Zulauf1a224292020-06-30 14:52:13 -0600531
532 // Infill if range goes passed both the current and resolve map prior contents
533 if (recur_to_infill && (current->range.end < range.end)) {
534 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
535 ResourceAccessRangeMap gap_map;
536 const auto the_end = resolve_map->end();
537 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
538 for (auto &access : gap_map) {
539 access.second.ApplyBarrier(*barrier);
540 resolve_map->insert(the_end, access);
541 }
542 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600543}
544
John Zulauf355e49b2020-04-24 15:11:15 -0600545void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
546 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600547 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600548 if (range.non_empty() && infill_state) {
549 descent_map->insert(std::make_pair(range, *infill_state));
550 }
551 } else {
552 // Look for something to fill the gap further along.
553 for (const auto &prev_dep : prev_) {
John Zulauf355e49b2020-04-24 15:11:15 -0600554 prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600555 }
556
John Zulaufe5da6e52020-03-18 15:32:18 -0600557 if (src_external_.context) {
John Zulauf355e49b2020-04-24 15:11:15 -0600558 src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600559 }
560 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600561}
562
John Zulauf16adfc92020-04-08 10:28:33 -0600563AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600564 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600565}
566
567VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
568 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
569}
570
John Zulauf355e49b2020-04-24 15:11:15 -0600571static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600572
John Zulauf1507ee42020-05-18 11:33:09 -0600573static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
574 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
575 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
576 return stage_access;
577}
578static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
579 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
580 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
581 return stage_access;
582}
583
John Zulauf7635de32020-05-29 17:14:15 -0600584// Caller must manage returned pointer
585static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
586 uint32_t subpass, const VkRect2D &render_area,
587 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
588 auto *proxy = new AccessContext(context);
589 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600590 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600591 return proxy;
592}
593
John Zulauf540266b2020-04-06 18:54:53 -0600594void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600595 AddressType address_type, ResourceAccessRangeMap *descent_map,
596 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600597 if (!SimpleBinding(image_state)) return;
598
John Zulauf62f10592020-04-03 12:20:02 -0600599 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600600 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600601 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600602 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600603 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600604 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600605 }
606}
607
John Zulauf7635de32020-05-29 17:14:15 -0600608// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600609bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600610 const VkRect2D &render_area, uint32_t subpass,
611 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
612 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600613 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600614 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
615 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
616 // those affects have not been recorded yet.
617 //
618 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
619 // to apply and only copy then, if this proves a hot spot.
620 std::unique_ptr<AccessContext> proxy_for_prev;
621 TrackBack proxy_track_back;
622
John Zulauf355e49b2020-04-24 15:11:15 -0600623 const auto &transitions = rp_state.subpass_transitions[subpass];
624 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600625 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
626
627 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
628 if (prev_needs_proxy) {
629 if (!proxy_for_prev) {
630 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
631 render_area, attachment_views));
632 proxy_track_back = *track_back;
633 proxy_track_back.context = proxy_for_prev.get();
634 }
635 track_back = &proxy_track_back;
636 }
637 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600638 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600639 skip |= sync_state.LogError(
640 rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
641 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " image layout transition. Prior access %s.",
642 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment, string_UsageTag(hazard.tag).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600643 }
644 }
645 return skip;
646}
647
John Zulauf1507ee42020-05-18 11:33:09 -0600648bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600649 const VkRect2D &render_area, uint32_t subpass,
650 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
651 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600652 bool skip = false;
653 const auto *attachment_ci = rp_state.createInfo.pAttachments;
654 VkExtent3D extent = CastTo3D(render_area.extent);
655 VkOffset3D offset = CastTo3D(render_area.offset);
656 const auto external_access_scope = src_external_.barrier.dst_access_scope;
John Zulauf1507ee42020-05-18 11:33:09 -0600657
658 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
659 if (subpass == rp_state.attachment_first_subpass[i]) {
660 if (attachment_views[i] == nullptr) continue;
661 const IMAGE_VIEW_STATE &view = *attachment_views[i];
662 const IMAGE_STATE *image = view.image_state.get();
663 if (image == nullptr) continue;
664 const auto &ci = attachment_ci[i];
665 const bool is_transition = rp_state.attachment_first_is_transition[i];
666
667 // Need check in the following way
668 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
669 // vs. transition
670 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
671 // for each aspect loaded.
672
673 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600674 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600675 const bool is_color = !(has_depth || has_stencil);
676
677 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
678 const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U;
679 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
680 const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U;
681
John Zulaufaff20662020-06-01 14:07:58 -0600682 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600683 const char *aspect = nullptr;
684 if (is_transition) {
685 // For transition w
686 SyncHazard transition_hazard = SyncHazard::NONE;
687 bool checked_stencil = false;
688 if (load_mask) {
689 if ((load_mask & external_access_scope) != load_mask) {
690 transition_hazard =
691 SyncStageAccess::HasWrite(load_mask) ? SyncHazard::WRITE_AFTER_WRITE : SyncHazard::READ_AFTER_WRITE;
692 aspect = is_color ? "color" : "depth";
693 }
694 if (!transition_hazard && stencil_mask) {
695 if ((stencil_mask & external_access_scope) != stencil_mask) {
696 transition_hazard = SyncStageAccess::HasWrite(stencil_mask) ? SyncHazard::WRITE_AFTER_WRITE
697 : SyncHazard::READ_AFTER_WRITE;
698 aspect = "stencil";
699 checked_stencil = true;
700 }
701 }
702 }
703 if (transition_hazard) {
704 // Hazard vs. ILT
705 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
706 skip |=
707 sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
708 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
709 " aspect %s during load with loadOp %s.",
710 func_name, string_SyncHazard(transition_hazard), subpass, i, aspect, load_op_string);
711 }
712 } else {
713 auto hazard_range = view.normalized_subresource_range;
714 bool checked_stencil = false;
715 if (is_color) {
716 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
717 aspect = "color";
718 } else {
719 if (has_depth) {
720 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
721 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
722 aspect = "depth";
723 }
724 if (!hazard.hazard && has_stencil) {
725 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
726 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
727 aspect = "stencil";
728 checked_stencil = true;
729 }
730 }
731
732 if (hazard.hazard) {
733 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
734 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
735 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
locke-lunarg88dbb542020-06-23 22:05:42 -0600736 " aspect %s during load with loadOp %s. Prior access %s.",
737 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
738 string_UsageTag(hazard.tag).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -0600739 }
740 }
741 }
742 }
743 return skip;
744}
745
John Zulaufaff20662020-06-01 14:07:58 -0600746// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
747// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
748// store is part of the same Next/End operation.
749// The latter is handled in layout transistion validation directly
750bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
751 const VkRect2D &render_area, uint32_t subpass,
752 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
753 const char *func_name) const {
754 bool skip = false;
755 const auto *attachment_ci = rp_state.createInfo.pAttachments;
756 VkExtent3D extent = CastTo3D(render_area.extent);
757 VkOffset3D offset = CastTo3D(render_area.offset);
758
759 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
760 if (subpass == rp_state.attachment_last_subpass[i]) {
761 if (attachment_views[i] == nullptr) continue;
762 const IMAGE_VIEW_STATE &view = *attachment_views[i];
763 const IMAGE_STATE *image = view.image_state.get();
764 if (image == nullptr) continue;
765 const auto &ci = attachment_ci[i];
766
767 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
768 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
769 // sake, we treat DONT_CARE as writing.
770 const bool has_depth = FormatHasDepth(ci.format);
771 const bool has_stencil = FormatHasStencil(ci.format);
772 const bool is_color = !(has_depth || has_stencil);
773 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
774 if (!has_stencil && !store_op_stores) continue;
775
776 HazardResult hazard;
777 const char *aspect = nullptr;
778 bool checked_stencil = false;
779 if (is_color) {
780 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
781 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
782 aspect = "color";
783 } else {
784 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
785 auto hazard_range = view.normalized_subresource_range;
786 if (has_depth && store_op_stores) {
787 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
788 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
789 kAttachmentRasterOrder, offset, extent);
790 aspect = "depth";
791 }
792 if (!hazard.hazard && has_stencil && stencil_op_stores) {
793 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
794 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
795 kAttachmentRasterOrder, offset, extent);
796 aspect = "stencil";
797 checked_stencil = true;
798 }
799 }
800
801 if (hazard.hazard) {
802 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
803 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -0600804 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
805 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
806 " %s aspect during store with %s %s. Prior access %s",
807 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
808 store_op_string, string_UsageTag(hazard.tag).c_str());
John Zulaufaff20662020-06-01 14:07:58 -0600809 }
810 }
811 }
812 return skip;
813}
814
John Zulaufb027cdb2020-05-21 14:25:22 -0600815bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
816 const VkRect2D &render_area,
817 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
818 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600819 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
820 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
821 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600822}
823
John Zulauf3d84f1b2020-03-09 13:33:25 -0600824class HazardDetector {
825 SyncStageAccessIndex usage_index_;
826
827 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600828 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600829 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
830 return pos->second.DetectAsyncHazard(usage_index_);
831 }
832 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
833};
834
John Zulauf69133422020-05-20 14:55:53 -0600835class HazardDetectorWithOrdering {
836 const SyncStageAccessIndex usage_index_;
837 const SyncOrderingBarrier &ordering_;
838
839 public:
840 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
841 return pos->second.DetectHazard(usage_index_, ordering_);
842 }
843 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
844 return pos->second.DetectAsyncHazard(usage_index_);
845 }
846 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
847 : usage_index_(usage), ordering_(ordering) {}
848};
849
John Zulauf16adfc92020-04-08 10:28:33 -0600850HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600851 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600852 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600853 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600854}
855
John Zulauf16adfc92020-04-08 10:28:33 -0600856HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600857 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600858 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600859 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600860}
861
John Zulauf69133422020-05-20 14:55:53 -0600862template <typename Detector>
863HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
864 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
865 const VkExtent3D &extent, DetectOptions options) const {
866 if (!SimpleBinding(image)) return HazardResult();
867 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
868 const auto address_type = ImageAddressType(image);
869 const auto base_address = ResourceBaseAddress(image);
870 for (; range_gen->non_empty(); ++range_gen) {
871 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
872 if (hazard.hazard) return hazard;
873 }
874 return HazardResult();
875}
876
John Zulauf540266b2020-04-06 18:54:53 -0600877HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
878 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
879 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700880 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
881 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -0600882 return DetectHazard(image, current_usage, subresource_range, offset, extent);
883}
884
885HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
886 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
887 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -0600888 HazardDetector detector(current_usage);
889 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
890}
891
892HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
893 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
894 const VkOffset3D &offset, const VkExtent3D &extent) const {
895 HazardDetectorWithOrdering detector(current_usage, ordering);
896 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -0600897}
898
John Zulaufb027cdb2020-05-21 14:25:22 -0600899// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
900// should have reported the issue regarding an invalid attachment entry
901HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
902 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
903 VkImageAspectFlags aspect_mask) const {
904 if (view != nullptr) {
905 const IMAGE_STATE *image = view->image_state.get();
906 if (image != nullptr) {
907 auto *detect_range = &view->normalized_subresource_range;
908 VkImageSubresourceRange masked_range;
909 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
910 masked_range = view->normalized_subresource_range;
911 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
912 detect_range = &masked_range;
913 }
914
915 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
916 if (detect_range->aspectMask) {
917 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
918 }
919 }
920 }
921 return HazardResult();
922}
John Zulauf3d84f1b2020-03-09 13:33:25 -0600923class BarrierHazardDetector {
924 public:
925 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
926 SyncStageAccessFlags src_access_scope)
927 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
928
John Zulauf5f13a792020-03-10 07:31:21 -0600929 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
930 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -0700931 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600932 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
933 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
934 return pos->second.DetectAsyncHazard(usage_index_);
935 }
936
937 private:
938 SyncStageAccessIndex usage_index_;
939 VkPipelineStageFlags src_exec_scope_;
940 SyncStageAccessFlags src_access_scope_;
941};
942
John Zulauf16adfc92020-04-08 10:28:33 -0600943HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -0600944 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600945 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600946 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -0600947 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700948}
949
John Zulauf16adfc92020-04-08 10:28:33 -0600950HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600951 SyncStageAccessFlags src_access_scope,
952 const VkImageSubresourceRange &subresource_range,
953 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -0600954 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
955 VkOffset3D zero_offset = {0, 0, 0};
956 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700957}
958
John Zulauf355e49b2020-04-24 15:11:15 -0600959HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
960 SyncStageAccessFlags src_stage_accesses,
961 const VkImageMemoryBarrier &barrier) const {
962 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
963 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
964 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
965}
966
John Zulauf9cb530d2019-09-30 14:14:10 -0600967template <typename Flags, typename Map>
968SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
969 SyncStageAccessFlags scope = 0;
970 for (const auto &bit_scope : map) {
971 if (flag_mask < bit_scope.first) break;
972
973 if (flag_mask & bit_scope.first) {
974 scope |= bit_scope.second;
975 }
976 }
977 return scope;
978}
979
980SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
981 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
982}
983
984SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
985 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
986}
987
988// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
989SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -0600990 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
991 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
992 // 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 -0600993 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
994}
995
996template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -0700997void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -0600998 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
999 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -06001000 auto pos = accesses->lower_bound(range);
1001 if (pos == accesses->end() || !pos->first.intersects(range)) {
1002 // The range is empty, fill it with a default value.
1003 pos = action.Infill(accesses, pos, range);
1004 } else if (range.begin < pos->first.begin) {
1005 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001006 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001007 } else if (pos->first.begin < range.begin) {
1008 // Trim the beginning if needed
1009 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1010 ++pos;
1011 }
1012
1013 const auto the_end = accesses->end();
1014 while ((pos != the_end) && pos->first.intersects(range)) {
1015 if (pos->first.end > range.end) {
1016 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1017 }
1018
1019 pos = action(accesses, pos);
1020 if (pos == the_end) break;
1021
1022 auto next = pos;
1023 ++next;
1024 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1025 // Need to infill if next is disjoint
1026 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001027 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001028 next = action.Infill(accesses, next, new_range);
1029 }
1030 pos = next;
1031 }
1032}
1033
1034struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001035 using Iterator = ResourceAccessRangeMap::iterator;
1036 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001037 // this is only called on gaps, and never returns a gap.
1038 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001039 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001040 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001041 }
John Zulauf5f13a792020-03-10 07:31:21 -06001042
John Zulauf5c5e88d2019-12-26 11:22:02 -07001043 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001044 auto &access_state = pos->second;
1045 access_state.Update(usage, tag);
1046 return pos;
1047 }
1048
John Zulauf16adfc92020-04-08 10:28:33 -06001049 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001050 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001051 : type(type_), context(context_), usage(usage_), tag(tag_) {}
1052 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001053 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001054 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001055 const ResourceUsageTag &tag;
1056};
1057
1058struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001059 using Iterator = ResourceAccessRangeMap::iterator;
1060 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001061
John Zulauf5c5e88d2019-12-26 11:22:02 -07001062 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001063 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001064 access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001065 return pos;
1066 }
1067
John Zulauf36bcf6a2020-02-03 15:12:52 -07001068 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
1069 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
1070 : src_exec_scope(src_exec_scope_),
1071 src_access_scope(src_access_scope_),
1072 dst_exec_scope(dst_exec_scope_),
1073 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001074
John Zulauf36bcf6a2020-02-03 15:12:52 -07001075 VkPipelineStageFlags src_exec_scope;
1076 SyncStageAccessFlags src_access_scope;
1077 VkPipelineStageFlags dst_exec_scope;
1078 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001079};
1080
1081struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001082 using Iterator = ResourceAccessRangeMap::iterator;
1083 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001084
John Zulauf5c5e88d2019-12-26 11:22:02 -07001085 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001086 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001087 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001088
1089 for (const auto &functor : barrier_functor) {
1090 functor(accesses, pos);
1091 }
1092 return pos;
1093 }
1094
John Zulauf36bcf6a2020-02-03 15:12:52 -07001095 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
1096 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -06001097 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001098 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001099 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
1100 barrier_functor.reserve(memoryBarrierCount);
1101 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
1102 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -07001103 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
1104 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -06001105 }
1106 }
1107
John Zulauf36bcf6a2020-02-03 15:12:52 -07001108 const VkPipelineStageFlags src_exec_scope;
1109 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001110 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
1111};
1112
John Zulauf355e49b2020-04-24 15:11:15 -06001113void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1114 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001115 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1116 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001117}
1118
John Zulauf16adfc92020-04-08 10:28:33 -06001119void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001120 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001121 if (!SimpleBinding(buffer)) return;
1122 const auto base_address = ResourceBaseAddress(buffer);
1123 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1124}
John Zulauf355e49b2020-04-24 15:11:15 -06001125
John Zulauf540266b2020-04-06 18:54:53 -06001126void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001127 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001128 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001129 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001130 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001131 const auto address_type = ImageAddressType(image);
1132 const auto base_address = ResourceBaseAddress(image);
1133 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001134 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001135 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001136 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001137}
John Zulauf7635de32020-05-29 17:14:15 -06001138void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1139 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1140 if (view != nullptr) {
1141 const IMAGE_STATE *image = view->image_state.get();
1142 if (image != nullptr) {
1143 auto *update_range = &view->normalized_subresource_range;
1144 VkImageSubresourceRange masked_range;
1145 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1146 masked_range = view->normalized_subresource_range;
1147 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1148 update_range = &masked_range;
1149 }
1150 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1151 }
1152 }
1153}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001154
John Zulauf355e49b2020-04-24 15:11:15 -06001155void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1156 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1157 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001158 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1159 subresource.layerCount};
1160 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1161}
1162
John Zulauf540266b2020-04-06 18:54:53 -06001163template <typename Action>
1164void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001165 if (!SimpleBinding(buffer)) return;
1166 const auto base_address = ResourceBaseAddress(buffer);
1167 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001168}
1169
1170template <typename Action>
1171void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1172 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001173 if (!SimpleBinding(image)) return;
1174 const auto address_type = ImageAddressType(image);
1175 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001176
locke-lunargae26eac2020-04-16 15:29:05 -06001177 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001178 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001179
John Zulauf16adfc92020-04-08 10:28:33 -06001180 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001181 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001182 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001183 }
1184}
1185
John Zulauf7635de32020-05-29 17:14:15 -06001186void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1187 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1188 const ResourceUsageTag &tag) {
1189 UpdateStateResolveAction update(*this, tag);
1190 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1191}
1192
John Zulaufaff20662020-06-01 14:07:58 -06001193void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1194 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1195 const ResourceUsageTag &tag) {
1196 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1197 VkExtent3D extent = CastTo3D(render_area.extent);
1198 VkOffset3D offset = CastTo3D(render_area.offset);
1199
1200 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1201 if (rp_state.attachment_last_subpass[i] == subpass) {
1202 if (attachment_views[i] == nullptr) continue; // UNUSED
1203 const auto &view = *attachment_views[i];
1204 const IMAGE_STATE *image = view.image_state.get();
1205 if (image == nullptr) continue;
1206
1207 const auto &ci = attachment_ci[i];
1208 const bool has_depth = FormatHasDepth(ci.format);
1209 const bool has_stencil = FormatHasStencil(ci.format);
1210 const bool is_color = !(has_depth || has_stencil);
1211 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1212
1213 if (is_color && store_op_stores) {
1214 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1215 offset, extent, tag);
1216 } else {
1217 auto update_range = view.normalized_subresource_range;
1218 if (has_depth && store_op_stores) {
1219 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1220 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1221 tag);
1222 }
1223 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1224 if (has_stencil && stencil_op_stores) {
1225 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1226 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1227 tag);
1228 }
1229 }
1230 }
1231 }
1232}
1233
John Zulauf540266b2020-04-06 18:54:53 -06001234template <typename Action>
1235void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1236 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001237 for (const auto address_type : kAddressTypes) {
1238 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001239 }
1240}
1241
1242void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001243 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1244 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -06001245 for (const auto address_type : kAddressTypes) {
John Zulauf355e49b2020-04-24 15:11:15 -06001246 context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier,
1247 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001248 }
1249 }
1250}
1251
John Zulauf355e49b2020-04-24 15:11:15 -06001252void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1253 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1254 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
1255 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1256 UpdateMemoryAccess(image, subresource_range, barrier_action);
1257}
1258
John Zulauf7635de32020-05-29 17:14:15 -06001259// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001260void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1261 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1262 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
1263 bool layout_transition, const ResourceUsageTag &tag) {
1264 if (layout_transition) {
1265 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
1266 tag);
1267 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
1268 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -06001269 } else {
1270 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -06001271 }
John Zulauf355e49b2020-04-24 15:11:15 -06001272}
1273
John Zulauf7635de32020-05-29 17:14:15 -06001274// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001275void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
1276 const VkImageSubresourceRange &subresource_range, bool layout_transition,
1277 const ResourceUsageTag &tag) {
1278 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
1279 subresource_range, layout_transition, tag);
1280}
1281
1282// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001283HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001284 if (!attach_view) return HazardResult();
1285 const auto image_state = attach_view->image_state.get();
1286 if (!image_state) return HazardResult();
1287
John Zulauf355e49b2020-04-24 15:11:15 -06001288 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001289 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001290
1291 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulauf7635de32020-05-29 17:14:15 -06001292 auto hazard = track_back.context->DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope,
1293 track_back.barrier.src_access_scope,
1294 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001295 if (!hazard.hazard) {
1296 // The Async hazard check is against the current context's async set.
John Zulauf7635de32020-05-29 17:14:15 -06001297 hazard = DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope, track_back.barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001298 attach_view->normalized_subresource_range, kDetectAsync);
1299 }
1300 return hazard;
1301}
1302
1303// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1304bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1305
1306 const VkRenderPassBeginInfo *pRenderPassBegin,
1307 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1308 const char *func_name) const {
1309 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1310 bool skip = false;
1311 uint32_t subpass = 0;
1312 const auto &transitions = rp_state.subpass_transitions[subpass];
1313 if (transitions.size()) {
1314 const std::vector<AccessContext> empty_context_vector;
1315 // Create context we can use to validate against...
1316 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1317 const_cast<AccessContext *>(&cb_access_context_));
1318
1319 assert(pRenderPassBegin);
1320 if (nullptr == pRenderPassBegin) return skip;
1321
1322 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1323 assert(fb_state);
1324 if (nullptr == fb_state) return skip;
1325
1326 // Create a limited array of views (which we'll need to toss
1327 std::vector<const IMAGE_VIEW_STATE *> views;
1328 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
1329 const auto attachment_count = count_attachment.first;
1330 const auto *attachments = count_attachment.second;
1331 views.resize(attachment_count, nullptr);
1332 for (const auto &transition : transitions) {
1333 assert(transition.attachment < attachment_count);
1334 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
1335 }
1336
John Zulauf7635de32020-05-29 17:14:15 -06001337 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
1338 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001339 }
1340 return skip;
1341}
1342
locke-lunarg61870c22020-06-09 14:51:50 -06001343bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1344 const char *func_name) const {
1345 bool skip = false;
1346 const PIPELINE_STATE *pPipe = nullptr;
1347 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1348 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1349 if (!pPipe || !per_sets) {
1350 return skip;
1351 }
1352
1353 using DescriptorClass = cvdescriptorset::DescriptorClass;
1354 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1355 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1356 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1357 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1358
1359 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarg37047832020-06-12 13:44:45 -06001360 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001361 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1362 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001363 for (const auto &set_binding : stage_state.descriptor_uses) {
1364 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1365 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1366 set_binding.first.second);
1367 const auto descriptor_type = binding_it.GetType();
1368 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1369 auto array_idx = 0;
1370
1371 if (binding_it.IsVariableDescriptorCount()) {
1372 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1373 }
1374 SyncStageAccessIndex sync_index =
1375 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1376
1377 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1378 uint32_t index = i - index_range.start;
1379 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1380 switch (descriptor->GetClass()) {
1381 case DescriptorClass::ImageSampler:
1382 case DescriptorClass::Image: {
1383 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1384 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1385 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1386 } else {
1387 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1388 }
1389 if (!img_view_state) continue;
1390 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1391 VkExtent3D extent = {};
1392 VkOffset3D offset = {};
1393 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1394 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1395 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1396 } else {
1397 extent = img_state->createInfo.extent;
1398 }
1399 auto hazard = current_context_->DetectHazard(*img_state, sync_index,
1400 img_view_state->normalized_subresource_range, offset, extent);
1401 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06001402 skip |= sync_state_->LogError(
1403 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1404 "%s: Hazard %s for %s in %s, %s, and %s binding #%" PRIu32 " index %" PRIu32 ". Prior access %s.",
1405 func_name, string_SyncHazard(hazard.hazard),
1406 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1407 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1408 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1409 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(), set_binding.first.second,
1410 index, string_UsageTag(hazard.tag).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001411 }
1412 break;
1413 }
1414 case DescriptorClass::TexelBuffer: {
1415 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1416 if (!buf_view_state) continue;
1417 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1418 ResourceAccessRange range =
1419 MakeRange(buf_view_state->create_info.offset,
1420 GetRealWholeSize(buf_view_state->create_info.offset, buf_view_state->create_info.range,
1421 buf_state->createInfo.size));
1422 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
1423 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001424 skip |= sync_state_->LogError(
1425 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
1426 "%s: Hazard %s for %s in %s, %s, and %s binding #%d index %d. Prior access %s.", func_name,
1427 string_SyncHazard(hazard.hazard),
1428 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1429 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1430 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1431 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(), set_binding.first.second,
1432 index, string_UsageTag(hazard.tag).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001433 }
1434 break;
1435 }
1436 case DescriptorClass::GeneralBuffer: {
1437 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1438 auto buf_state = buffer_descriptor->GetBufferState();
1439 if (!buf_state) continue;
1440 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1441 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
1442 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001443 skip |= sync_state_->LogError(
1444 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
1445 "%s: Hazard %s for %s in %s, %s, and %s binding #%d index %d. Prior access %s.", func_name,
1446 string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
1447 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1448 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1449 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(), set_binding.first.second,
1450 index, string_UsageTag(hazard.tag).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001451 }
1452 break;
1453 }
1454 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1455 default:
1456 break;
1457 }
1458 }
1459 }
1460 }
1461 return skip;
1462}
1463
1464void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1465 const ResourceUsageTag &tag) {
1466 const PIPELINE_STATE *pPipe = nullptr;
1467 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1468 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1469 if (!pPipe || !per_sets) {
1470 return;
1471 }
1472
1473 using DescriptorClass = cvdescriptorset::DescriptorClass;
1474 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1475 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1476 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1477 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1478
1479 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001480 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
1481 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1482 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001483 for (const auto &set_binding : stage_state.descriptor_uses) {
1484 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1485 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1486 set_binding.first.second);
1487 const auto descriptor_type = binding_it.GetType();
1488 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1489 auto array_idx = 0;
1490
1491 if (binding_it.IsVariableDescriptorCount()) {
1492 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1493 }
1494 SyncStageAccessIndex sync_index =
1495 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1496
1497 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1498 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1499 switch (descriptor->GetClass()) {
1500 case DescriptorClass::ImageSampler:
1501 case DescriptorClass::Image: {
1502 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1503 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1504 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1505 } else {
1506 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1507 }
1508 if (!img_view_state) continue;
1509 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1510 VkExtent3D extent = {};
1511 VkOffset3D offset = {};
1512 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1513 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1514 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1515 } else {
1516 extent = img_state->createInfo.extent;
1517 }
1518 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1519 offset, extent, tag);
1520 break;
1521 }
1522 case DescriptorClass::TexelBuffer: {
1523 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1524 if (!buf_view_state) continue;
1525 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1526 ResourceAccessRange range =
1527 MakeRange(buf_view_state->create_info.offset, buf_view_state->create_info.range);
1528 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1529 break;
1530 }
1531 case DescriptorClass::GeneralBuffer: {
1532 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1533 auto buf_state = buffer_descriptor->GetBufferState();
1534 if (!buf_state) continue;
1535 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1536 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1537 break;
1538 }
1539 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1540 default:
1541 break;
1542 }
1543 }
1544 }
1545 }
1546}
1547
1548bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1549 bool skip = false;
1550 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1551 if (!pPipe) {
1552 return skip;
1553 }
1554
1555 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1556 const auto &binding_buffers_size = binding_buffers.size();
1557 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1558
1559 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1560 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1561 if (binding_description.binding < binding_buffers_size) {
1562 const auto &binding_buffer = binding_buffers[binding_description.binding];
1563 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1564
1565 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1566 VkDeviceSize range_start = 0;
1567 VkDeviceSize range_size = 0;
1568 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1569 binding_description.stride);
1570 ResourceAccessRange range = MakeRange(range_start, range_size);
1571 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1572 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001573 skip |= sync_state_->LogError(
1574 buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Prior access %s.",
1575 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
1576 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001577 }
1578 }
1579 }
1580 return skip;
1581}
1582
1583void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1584 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1585 if (!pPipe) {
1586 return;
1587 }
1588 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1589 const auto &binding_buffers_size = binding_buffers.size();
1590 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1591
1592 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1593 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1594 if (binding_description.binding < binding_buffers_size) {
1595 const auto &binding_buffer = binding_buffers[binding_description.binding];
1596 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1597
1598 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1599 VkDeviceSize range_start = 0;
1600 VkDeviceSize range_size = 0;
1601 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1602 binding_description.stride);
1603 ResourceAccessRange range = MakeRange(range_start, range_size);
1604 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1605 }
1606 }
1607}
1608
1609bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1610 bool skip = false;
1611 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
1612
1613 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1614 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1615 VkDeviceSize range_start = 0;
1616 VkDeviceSize range_size = 0;
1617 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1618 indexCount, index_size);
1619 ResourceAccessRange range = MakeRange(range_start, range_size);
1620 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1621 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001622 skip |= sync_state_->LogError(
1623 index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Prior access %s.",
1624 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
1625 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001626 }
1627
1628 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1629 // We will detect more accurate range in the future.
1630 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1631 return skip;
1632}
1633
1634void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
1635 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return;
1636
1637 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1638 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1639 VkDeviceSize range_start = 0;
1640 VkDeviceSize range_size = 0;
1641 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1642 indexCount, index_size);
1643 ResourceAccessRange range = MakeRange(range_start, range_size);
1644 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1645
1646 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1647 // We will detect more accurate range in the future.
1648 RecordDrawVertex(UINT32_MAX, 0, tag);
1649}
1650
1651bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001652 bool skip = false;
1653 if (!current_renderpass_context_) return skip;
1654 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1655 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1656 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001657}
1658
1659void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg7077d502020-06-18 21:37:26 -06001660 if (current_renderpass_context_)
1661 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1662 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001663}
1664
John Zulauf355e49b2020-04-24 15:11:15 -06001665bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001666 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001667 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001668 skip |=
1669 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001670
1671 return skip;
1672}
1673
1674bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1675 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001676 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001677 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001678 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001679 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1680 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001681
1682 return skip;
1683}
1684
1685void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1686 assert(sync_state_);
1687 if (!cb_state_) return;
1688
1689 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001690 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001691 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001692 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001693 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001694}
1695
John Zulauf355e49b2020-04-24 15:11:15 -06001696void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001697 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001698 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001699 current_context_ = &current_renderpass_context_->CurrentContext();
1700}
1701
John Zulauf355e49b2020-04-24 15:11:15 -06001702void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001703 assert(current_renderpass_context_);
1704 if (!current_renderpass_context_) return;
1705
John Zulauf1a224292020-06-30 14:52:13 -06001706 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001707 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001708 current_renderpass_context_ = nullptr;
1709}
1710
locke-lunarg61870c22020-06-09 14:51:50 -06001711bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1712 const VkRect2D &render_area, const char *func_name) const {
1713 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001714 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001715 if (!pPipe ||
1716 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001717 return skip;
1718 }
1719 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001720 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1721 VkExtent3D extent = CastTo3D(render_area.extent);
1722 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001723
John Zulauf1a224292020-06-30 14:52:13 -06001724 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001725 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001726 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1727 for (const auto location : list) {
1728 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1729 continue;
1730 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001731 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1732 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001733 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001734 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1735 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Prior access %s.",
1736 func_name, string_SyncHazard(hazard.hazard),
1737 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1738 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
1739 location, string_UsageTag(hazard.tag).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001740 }
1741 }
1742 }
locke-lunarg37047832020-06-12 13:44:45 -06001743
1744 // PHASE1 TODO: Add layout based read/vs. write selection.
1745 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1746 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1747 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001748 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001749 bool depth_write = false, stencil_write = false;
1750
1751 // PHASE1 TODO: These validation should be in core_checks.
1752 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1753 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1754 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1755 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1756 depth_write = true;
1757 }
1758 // PHASE1 TODO: It needs to check if stencil is writable.
1759 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1760 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1761 // PHASE1 TODO: These validation should be in core_checks.
1762 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1763 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1764 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1765 stencil_write = true;
1766 }
1767
1768 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1769 if (depth_write) {
1770 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001771 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1772 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001773 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001774 skip |= sync_state.LogError(
1775 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1776 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Prior access %s.",
1777 func_name, string_SyncHazard(hazard.hazard),
1778 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1779 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
1780 string_UsageTag(hazard.tag).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001781 }
1782 }
1783 if (stencil_write) {
1784 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001785 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1786 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001787 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001788 skip |= sync_state.LogError(
1789 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1790 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Prior access %s.",
1791 func_name, string_SyncHazard(hazard.hazard),
1792 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1793 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
1794 string_UsageTag(hazard.tag).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001795 }
locke-lunarg61870c22020-06-09 14:51:50 -06001796 }
1797 }
1798 return skip;
1799}
1800
locke-lunarg96dc9632020-06-10 17:22:18 -06001801void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1802 const ResourceUsageTag &tag) {
1803 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001804 if (!pPipe ||
1805 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001806 return;
1807 }
1808 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001809 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1810 VkExtent3D extent = CastTo3D(render_area.extent);
1811 VkOffset3D offset = CastTo3D(render_area.offset);
1812
John Zulauf1a224292020-06-30 14:52:13 -06001813 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001814 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001815 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1816 for (const auto location : list) {
1817 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1818 continue;
1819 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001820 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
1821 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001822 }
1823 }
locke-lunarg37047832020-06-12 13:44:45 -06001824
1825 // PHASE1 TODO: Add layout based read/vs. write selection.
1826 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1827 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1828 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001829 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001830 bool depth_write = false, stencil_write = false;
1831
1832 // PHASE1 TODO: These validation should be in core_checks.
1833 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1834 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1835 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1836 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1837 depth_write = true;
1838 }
1839 // PHASE1 TODO: It needs to check if stencil is writable.
1840 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1841 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1842 // PHASE1 TODO: These validation should be in core_checks.
1843 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1844 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1845 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1846 stencil_write = true;
1847 }
1848
1849 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1850 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001851 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1852 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001853 }
1854 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001855 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1856 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001857 }
locke-lunarg61870c22020-06-09 14:51:50 -06001858 }
1859}
1860
John Zulauf1507ee42020-05-18 11:33:09 -06001861bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1862 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001863 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001864 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001865 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1866 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001867 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1868 func_name);
1869
John Zulauf355e49b2020-04-24 15:11:15 -06001870 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06001871 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06001872 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1873 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1874 return skip;
1875}
1876bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
1877 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001878 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06001879 bool skip = false;
1880 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1881 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001882 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1883 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06001884 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001885 return skip;
1886}
1887
John Zulauf7635de32020-05-29 17:14:15 -06001888AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
1889 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
1890}
1891
1892bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
1893 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001894 bool skip = false;
1895
John Zulauf7635de32020-05-29 17:14:15 -06001896 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
1897 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
1898 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1899 // to apply and only copy then, if this proves a hot spot.
1900 std::unique_ptr<AccessContext> proxy_for_current;
1901
John Zulauf355e49b2020-04-24 15:11:15 -06001902 // Validate the "finalLayout" transitions to external
1903 // Get them from where there we're hidding in the extra entry.
1904 const auto &final_transitions = rp_state_->subpass_transitions.back();
1905 for (const auto &transition : final_transitions) {
1906 const auto &attach_view = attachment_views_[transition.attachment];
1907 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
1908 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06001909 auto *context = trackback.context;
1910
1911 if (transition.prev_pass == current_subpass_) {
1912 if (!proxy_for_current) {
1913 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
1914 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
1915 }
1916 context = proxy_for_current.get();
1917 }
1918
1919 auto hazard = context->DetectImageBarrierHazard(
John Zulauf355e49b2020-04-24 15:11:15 -06001920 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
1921 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
1922 if (hazard.hazard) {
1923 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
1924 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf1dae9192020-06-16 15:46:44 -06001925 " final image layout transition. Prior access %s.",
1926 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
1927 string_UsageTag(hazard.tag).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001928 }
1929 }
1930 return skip;
1931}
1932
1933void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
1934 // Add layout transitions...
1935 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
1936 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06001937 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06001938 for (const auto &transition : transitions) {
1939 const auto attachment_view = attachment_views_[transition.attachment];
1940 if (!attachment_view) continue;
1941 const auto image = attachment_view->image_state.get();
1942 if (!image) continue;
1943
1944 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -06001945 auto insert_pair = view_seen.insert(attachment_view);
1946 if (insert_pair.second) {
1947 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
1948 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
1949
1950 } else {
1951 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
1952 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
1953 auto barrier_to_transition = barrier->barrier;
1954 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
1955 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
1956 }
John Zulauf355e49b2020-04-24 15:11:15 -06001957 }
1958}
1959
John Zulauf1507ee42020-05-18 11:33:09 -06001960void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
1961 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
1962 auto &subpass_context = subpass_contexts_[current_subpass_];
1963 VkExtent3D extent = CastTo3D(render_area.extent);
1964 VkOffset3D offset = CastTo3D(render_area.offset);
1965
1966 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
1967 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
1968 if (attachment_views_[i] == nullptr) continue; // UNUSED
1969 const auto &view = *attachment_views_[i];
1970 const IMAGE_STATE *image = view.image_state.get();
1971 if (image == nullptr) continue;
1972
1973 const auto &ci = attachment_ci[i];
1974 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001975 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001976 const bool is_color = !(has_depth || has_stencil);
1977
1978 if (is_color) {
1979 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
1980 extent, tag);
1981 } else {
1982 auto update_range = view.normalized_subresource_range;
1983 if (has_depth) {
1984 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1985 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
1986 }
1987 if (has_stencil) {
1988 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1989 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
1990 tag);
1991 }
1992 }
1993 }
1994 }
1995}
1996
John Zulauf355e49b2020-04-24 15:11:15 -06001997void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06001998 const AccessContext *external_context, VkQueueFlags queue_flags,
1999 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002000 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002001 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002002 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2003 // Add this for all subpasses here so that they exsist during next subpass validation
2004 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002005 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002006 }
2007 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2008
2009 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002010 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002011}
John Zulauf1507ee42020-05-18 11:33:09 -06002012
2013void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002014 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2015 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002016 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002017
John Zulauf355e49b2020-04-24 15:11:15 -06002018 current_subpass_++;
2019 assert(current_subpass_ < subpass_contexts_.size());
2020 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002021 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002022}
2023
John Zulauf1a224292020-06-30 14:52:13 -06002024void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2025 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002026 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002027 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002028 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002029
John Zulauf355e49b2020-04-24 15:11:15 -06002030 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002031 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002032
2033 // Add the "finalLayout" transitions to external
2034 // Get them from where there we're hidding in the extra entry.
2035 const auto &final_transitions = rp_state_->subpass_transitions.back();
2036 for (const auto &transition : final_transitions) {
2037 const auto &attachment = attachment_views_[transition.attachment];
2038 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulauf1a224292020-06-30 14:52:13 -06002039 assert(external_context == last_trackback.context);
2040 external_context->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
2041 attachment->normalized_subresource_range, true, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002042 }
2043}
2044
John Zulauf3d84f1b2020-03-09 13:33:25 -06002045SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2046 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2047 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2048 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2049 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2050 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2051 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2052}
2053
2054void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
2055 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
2056 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
2057}
2058
John Zulauf9cb530d2019-09-30 14:14:10 -06002059HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2060 HazardResult hazard;
2061 auto usage = FlagBit(usage_index);
2062 if (IsRead(usage)) {
John Zulaufc9201222020-05-13 15:13:03 -06002063 if (last_write && IsWriteHazard(usage)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002064 hazard.Set(READ_AFTER_WRITE, write_tag);
2065 }
2066 } else {
2067 // Assume write
2068 // TODO determine what to do with READ-WRITE usage states if any
2069 // Write-After-Write check -- if we have a previous write to test against
2070 if (last_write && IsWriteHazard(usage)) {
2071 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2072 } else {
John Zulauf69133422020-05-20 14:55:53 -06002073 // Look for casus belli for WAR
John Zulauf9cb530d2019-09-30 14:14:10 -06002074 const auto usage_stage = PipelineStageBit(usage_index);
2075 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2076 if (IsReadHazard(usage_stage, last_reads[read_index])) {
2077 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
2078 break;
2079 }
2080 }
2081 }
2082 }
2083 return hazard;
2084}
2085
John Zulauf69133422020-05-20 14:55:53 -06002086HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2087 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2088 HazardResult hazard;
2089 const auto usage = FlagBit(usage_index);
2090 const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good.
2091 if (IsRead(usage)) {
2092 if (!write_is_ordered && IsWriteHazard(usage)) {
2093 hazard.Set(READ_AFTER_WRITE, write_tag);
2094 }
2095 } else {
2096 if (!write_is_ordered && IsWriteHazard(usage)) {
2097 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2098 } else {
2099 const auto usage_stage = PipelineStageBit(usage_index);
2100 const auto unordered_reads = last_read_stages & ~ordering.exec_scope;
2101 if (unordered_reads) {
2102 // Look for any WAR hazards outside the ordered set of stages
2103 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2104 if (last_reads[read_index].stage & unordered_reads) {
2105 if (IsReadHazard(usage_stage, last_reads[read_index])) {
2106 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
2107 break;
2108 }
2109 }
2110 }
2111 }
2112 }
2113 }
2114 return hazard;
2115}
2116
John Zulauf2f952d22020-02-10 11:34:51 -07002117// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002118HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002119 HazardResult hazard;
2120 auto usage = FlagBit(usage_index);
2121 if (IsRead(usage)) {
2122 if (last_write != 0) {
2123 hazard.Set(READ_RACING_WRITE, write_tag);
2124 }
2125 } else {
2126 if (last_write != 0) {
2127 hazard.Set(WRITE_RACING_WRITE, write_tag);
2128 } else if (last_read_count > 0) {
2129 hazard.Set(WRITE_RACING_READ, last_reads[0].tag);
2130 }
2131 }
2132 return hazard;
2133}
2134
John Zulauf36bcf6a2020-02-03 15:12:52 -07002135HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2136 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002137 // Only supporting image layout transitions for now
2138 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2139 HazardResult hazard;
2140 if (last_write) {
2141 // If the previous write is *not* in the 1st access scope
2142 // *AND* the current barrier is not in the dependency chain
2143 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2144 // then the barrier access is unsafe (R/W after W)
John Zulauf36bcf6a2020-02-03 15:12:52 -07002145 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
John Zulauf0cb5be22020-01-23 12:18:22 -07002146 // TODO: Do we need a difference hazard name for this?
2147 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2148 }
John Zulauf355e49b2020-04-24 15:11:15 -06002149 }
2150 if (!hazard.hazard) {
2151 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002152 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002153 const auto &read_access = last_reads[read_index];
2154 // If the read stage is not in the src sync sync
2155 // *AND* not execution chained with an existing sync barrier (that's the or)
2156 // then the barrier access is unsafe (R/W after R)
2157 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
2158 hazard.Set(WRITE_AFTER_READ, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002159 break;
2160 }
2161 }
2162 }
2163 return hazard;
2164}
2165
John Zulauf5f13a792020-03-10 07:31:21 -06002166// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2167// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2168// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2169void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2170 if (write_tag.IsBefore(other.write_tag)) {
2171 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
2172 *this = other;
2173 } else if (!other.write_tag.IsBefore(write_tag)) {
2174 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2175 // dependency chaining logic or any stage expansion)
2176 write_barriers |= other.write_barriers;
2177
2178 // Merge that read states
2179 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2180 auto &other_read = other.last_reads[other_read_index];
2181 if (last_read_stages & other_read.stage) {
2182 // Merge in the barriers for read stages that exist in *both* this and other
2183 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
2184 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
2185 auto &my_read = last_reads[my_read_index];
2186 if (other_read.stage == my_read.stage) {
2187 if (my_read.tag.IsBefore(other_read.tag)) {
2188 my_read.tag = other_read.tag;
2189 }
2190 my_read.barriers |= other_read.barriers;
2191 break;
2192 }
2193 }
2194 } else {
2195 // The other read stage doesn't exist in this, so add it.
2196 last_reads[last_read_count] = other_read;
2197 last_read_count++;
2198 last_read_stages |= other_read.stage;
2199 }
2200 }
2201 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
2202 // it.
2203}
2204
John Zulauf9cb530d2019-09-30 14:14:10 -06002205void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2206 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2207 const auto usage_bit = FlagBit(usage_index);
2208 if (IsRead(usage_index)) {
2209 // Mulitple outstanding reads may be of interest and do dependency chains independently
2210 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2211 const auto usage_stage = PipelineStageBit(usage_index);
2212 if (usage_stage & last_read_stages) {
2213 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2214 ReadState &access = last_reads[read_index];
2215 if (access.stage == usage_stage) {
2216 access.barriers = 0;
2217 access.tag = tag;
2218 break;
2219 }
2220 }
2221 } else {
2222 // We don't have this stage in the list yet...
2223 assert(last_read_count < last_reads.size());
2224 ReadState &access = last_reads[last_read_count++];
2225 access.stage = usage_stage;
2226 access.barriers = 0;
2227 access.tag = tag;
2228 last_read_stages |= usage_stage;
2229 }
2230 } else {
2231 // Assume write
2232 // TODO determine what to do with READ-WRITE operations if any
2233 // Clobber last read and both sets of barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2234 // if the last_reads/last_write were unsafe, we've reported them,
2235 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
2236 last_read_count = 0;
2237 last_read_stages = 0;
2238
2239 write_barriers = 0;
2240 write_dependency_chain = 0;
2241 write_tag = tag;
2242 last_write = usage_bit;
2243 }
2244}
John Zulauf5f13a792020-03-10 07:31:21 -06002245
John Zulauf9cb530d2019-09-30 14:14:10 -06002246void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
2247 // Execution Barriers only protect read operations
2248 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2249 ReadState &access = last_reads[read_index];
2250 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2251 if (srcStageMask & (access.stage | access.barriers)) {
2252 access.barriers |= dstStageMask;
2253 }
2254 }
2255 if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask;
2256}
2257
John Zulauf36bcf6a2020-02-03 15:12:52 -07002258void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
2259 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002260 // Assuming we've applied the execution side of this barrier, we update just the write
2261 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07002262 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
2263 write_barriers |= dst_access_scope;
2264 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06002265 }
2266}
2267
John Zulaufd1f85d42020-04-15 12:23:15 -06002268void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002269 auto *access_context = GetAccessContextNoInsert(command_buffer);
2270 if (access_context) {
2271 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002272 }
2273}
2274
John Zulaufd1f85d42020-04-15 12:23:15 -06002275void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2276 auto access_found = cb_access_state.find(command_buffer);
2277 if (access_found != cb_access_state.end()) {
2278 access_found->second->Reset();
2279 cb_access_state.erase(access_found);
2280 }
2281}
2282
John Zulauf540266b2020-04-06 18:54:53 -06002283void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002284 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
2285 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002286 const VkMemoryBarrier *pMemoryBarriers) {
2287 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07002288 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002289 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002290 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002291}
2292
John Zulauf540266b2020-04-06 18:54:53 -06002293void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002294 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2295 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002296 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002297 for (uint32_t index = 0; index < barrier_count; index++) {
locke-lunarg3c038002020-04-30 23:08:08 -06002298 auto barrier = barriers[index];
John Zulauf9cb530d2019-09-30 14:14:10 -06002299 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2300 if (!buffer) continue;
locke-lunarg3c038002020-04-30 23:08:08 -06002301 barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002302 ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002303 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2304 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2305 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2306 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002307 }
2308}
2309
John Zulauf540266b2020-04-06 18:54:53 -06002310void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2311 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2312 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002313 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002314 for (uint32_t index = 0; index < barrier_count; index++) {
2315 const auto &barrier = barriers[index];
2316 const auto *image = Get<IMAGE_STATE>(barrier.image);
2317 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002318 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002319 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2320 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2321 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2322 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
2323 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002324 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002325}
2326
2327bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2328 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2329 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002330 const auto *cb_context = GetAccessContext(commandBuffer);
2331 assert(cb_context);
2332 if (!cb_context) return skip;
2333 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002334
John Zulauf3d84f1b2020-03-09 13:33:25 -06002335 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002336 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002337 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002338
2339 for (uint32_t region = 0; region < regionCount; region++) {
2340 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002341 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002342 ResourceAccessRange src_range = MakeRange(
2343 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002344 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002345 if (hazard.hazard) {
2346 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002347 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002348 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Prior access %s.",
2349 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
2350 string_UsageTag(hazard.tag).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002351 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002352 }
John Zulauf16adfc92020-04-08 10:28:33 -06002353 if (dst_buffer && !skip) {
locke-lunargff255f92020-05-13 18:53:52 -06002354 ResourceAccessRange dst_range = MakeRange(
2355 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf355e49b2020-04-24 15:11:15 -06002356 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002357 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002358 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002359 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Prior access %s.",
2360 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
2361 string_UsageTag(hazard.tag).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002362 }
2363 }
2364 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002365 }
2366 return skip;
2367}
2368
2369void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2370 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002371 auto *cb_context = GetAccessContext(commandBuffer);
2372 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002373 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002374 auto *context = cb_context->GetCurrentAccessContext();
2375
John Zulauf9cb530d2019-09-30 14:14:10 -06002376 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002377 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002378
2379 for (uint32_t region = 0; region < regionCount; region++) {
2380 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002381 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002382 ResourceAccessRange src_range = MakeRange(
2383 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002384 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002385 }
John Zulauf16adfc92020-04-08 10:28:33 -06002386 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002387 ResourceAccessRange dst_range = MakeRange(
2388 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002389 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002390 }
2391 }
2392}
2393
2394bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2395 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2396 const VkImageCopy *pRegions) const {
2397 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002398 const auto *cb_access_context = GetAccessContext(commandBuffer);
2399 assert(cb_access_context);
2400 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002401
John Zulauf3d84f1b2020-03-09 13:33:25 -06002402 const auto *context = cb_access_context->GetCurrentAccessContext();
2403 assert(context);
2404 if (!context) return skip;
2405
2406 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2407 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002408 for (uint32_t region = 0; region < regionCount; region++) {
2409 const auto &copy_region = pRegions[region];
2410 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002411 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002412 copy_region.srcOffset, copy_region.extent);
2413 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002414 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002415 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
2416 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
2417 string_UsageTag(hazard.tag).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002418 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002419 }
2420
2421 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002422 VkExtent3D dst_copy_extent =
2423 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002424 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002425 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002426 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002427 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002428 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
2429 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
2430 string_UsageTag(hazard.tag).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002431 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002432 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002433 }
2434 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002435
John Zulauf5c5e88d2019-12-26 11:22:02 -07002436 return skip;
2437}
2438
2439void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2440 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2441 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002442 auto *cb_access_context = GetAccessContext(commandBuffer);
2443 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002444 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002445 auto *context = cb_access_context->GetCurrentAccessContext();
2446 assert(context);
2447
John Zulauf5c5e88d2019-12-26 11:22:02 -07002448 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002449 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002450
2451 for (uint32_t region = 0; region < regionCount; region++) {
2452 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002453 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002454 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2455 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002456 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002457 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002458 VkExtent3D dst_copy_extent =
2459 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002460 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2461 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002462 }
2463 }
2464}
2465
2466bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2467 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2468 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2469 uint32_t bufferMemoryBarrierCount,
2470 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2471 uint32_t imageMemoryBarrierCount,
2472 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2473 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002474 const auto *cb_access_context = GetAccessContext(commandBuffer);
2475 assert(cb_access_context);
2476 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002477
John Zulauf3d84f1b2020-03-09 13:33:25 -06002478 const auto *context = cb_access_context->GetCurrentAccessContext();
2479 assert(context);
2480 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002481
John Zulauf3d84f1b2020-03-09 13:33:25 -06002482 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002483 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2484 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002485 // Validate Image Layout transitions
2486 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2487 const auto &barrier = pImageMemoryBarriers[index];
2488 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2489 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2490 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002491 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002492 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002493 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002494 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002495 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Prior access %s.",
2496 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
2497 string_UsageTag(hazard.tag).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002498 }
2499 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002500
2501 return skip;
2502}
2503
2504void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2505 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2506 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2507 uint32_t bufferMemoryBarrierCount,
2508 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2509 uint32_t imageMemoryBarrierCount,
2510 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002511 auto *cb_access_context = GetAccessContext(commandBuffer);
2512 assert(cb_access_context);
2513 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002514 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002515 auto access_context = cb_access_context->GetCurrentAccessContext();
2516 assert(access_context);
2517 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002518
John Zulauf3d84f1b2020-03-09 13:33:25 -06002519 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002520 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002521 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002522 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2523 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2524 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002525 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
2526 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002527 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06002528 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002529
2530 // 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 -06002531 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07002532 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06002533}
2534
2535void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
2536 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
2537 // The state tracker sets up the device state
2538 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
2539
John Zulauf5f13a792020-03-10 07:31:21 -06002540 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
2541 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06002542 // TODO: Find a good way to do this hooklessly.
2543 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
2544 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
2545 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
2546
John Zulaufd1f85d42020-04-15 12:23:15 -06002547 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2548 sync_device_state->ResetCommandBufferCallback(command_buffer);
2549 });
2550 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2551 sync_device_state->FreeCommandBufferCallback(command_buffer);
2552 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002553}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002554
John Zulauf355e49b2020-04-24 15:11:15 -06002555bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2556 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2557 bool skip = false;
2558 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2559 auto cb_context = GetAccessContext(commandBuffer);
2560
2561 if (rp_state && cb_context) {
2562 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2563 }
2564
2565 return skip;
2566}
2567
2568bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2569 VkSubpassContents contents) const {
2570 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2571 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2572 subpass_begin_info.contents = contents;
2573 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
2574 return skip;
2575}
2576
2577bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2578 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2579 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2580 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
2581 return skip;
2582}
2583
2584bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2585 const VkRenderPassBeginInfo *pRenderPassBegin,
2586 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2587 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2588 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
2589 return skip;
2590}
2591
John Zulauf3d84f1b2020-03-09 13:33:25 -06002592void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
2593 VkResult result) {
2594 // The state tracker sets up the command buffer state
2595 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
2596
2597 // Create/initialize the structure that trackers accesses at the command buffer scope.
2598 auto cb_access_context = GetAccessContext(commandBuffer);
2599 assert(cb_access_context);
2600 cb_access_context->Reset();
2601}
2602
2603void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06002604 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002605 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002606 if (cb_context) {
2607 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002608 }
2609}
2610
2611void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2612 VkSubpassContents contents) {
2613 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2614 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2615 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002616 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002617}
2618
2619void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2620 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2621 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002622 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002623}
2624
2625void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2626 const VkRenderPassBeginInfo *pRenderPassBegin,
2627 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2628 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002629 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
2630}
2631
2632bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2633 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
2634 bool skip = false;
2635
2636 auto cb_context = GetAccessContext(commandBuffer);
2637 assert(cb_context);
2638 auto cb_state = cb_context->GetCommandBufferState();
2639 if (!cb_state) return skip;
2640
2641 auto rp_state = cb_state->activeRenderPass;
2642 if (!rp_state) return skip;
2643
2644 skip |= cb_context->ValidateNextSubpass(func_name);
2645
2646 return skip;
2647}
2648
2649bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
2650 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
2651 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2652 subpass_begin_info.contents = contents;
2653 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
2654 return skip;
2655}
2656
2657bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2658 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2659 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2660 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
2661 return skip;
2662}
2663
2664bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2665 const VkSubpassEndInfo *pSubpassEndInfo) const {
2666 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2667 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
2668 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002669}
2670
2671void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06002672 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002673 auto cb_context = GetAccessContext(commandBuffer);
2674 assert(cb_context);
2675 auto cb_state = cb_context->GetCommandBufferState();
2676 if (!cb_state) return;
2677
2678 auto rp_state = cb_state->activeRenderPass;
2679 if (!rp_state) return;
2680
John Zulauf355e49b2020-04-24 15:11:15 -06002681 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002682}
2683
2684void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
2685 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
2686 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2687 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002688 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002689}
2690
2691void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2692 const VkSubpassEndInfo *pSubpassEndInfo) {
2693 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002694 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002695}
2696
2697void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2698 const VkSubpassEndInfo *pSubpassEndInfo) {
2699 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002700 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002701}
2702
John Zulauf355e49b2020-04-24 15:11:15 -06002703bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
2704 const char *func_name) const {
2705 bool skip = false;
2706
2707 auto cb_context = GetAccessContext(commandBuffer);
2708 assert(cb_context);
2709 auto cb_state = cb_context->GetCommandBufferState();
2710 if (!cb_state) return skip;
2711
2712 auto rp_state = cb_state->activeRenderPass;
2713 if (!rp_state) return skip;
2714
2715 skip |= cb_context->ValidateEndRenderpass(func_name);
2716 return skip;
2717}
2718
2719bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2720 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
2721 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
2722 return skip;
2723}
2724
2725bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
2726 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2727 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2728 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
2729 return skip;
2730}
2731
2732bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
2733 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2734 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2735 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
2736 return skip;
2737}
2738
2739void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
2740 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06002741 // Resolve the all subpass contexts to the command buffer contexts
2742 auto cb_context = GetAccessContext(commandBuffer);
2743 assert(cb_context);
2744 auto cb_state = cb_context->GetCommandBufferState();
2745 if (!cb_state) return;
2746
locke-lunargaecf2152020-05-12 17:15:41 -06002747 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06002748 if (!rp_state) return;
2749
John Zulauf355e49b2020-04-24 15:11:15 -06002750 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06002751}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002752
2753void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002754 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06002755 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002756}
2757
2758void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06002759 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06002760 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002761}
2762
2763void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06002764 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06002765 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002766}
locke-lunarga19c71d2020-03-02 18:17:04 -07002767
2768bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2769 VkImageLayout dstImageLayout, uint32_t regionCount,
2770 const VkBufferImageCopy *pRegions) const {
2771 bool skip = false;
2772 const auto *cb_access_context = GetAccessContext(commandBuffer);
2773 assert(cb_access_context);
2774 if (!cb_access_context) return skip;
2775
2776 const auto *context = cb_access_context->GetCurrentAccessContext();
2777 assert(context);
2778 if (!context) return skip;
2779
2780 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07002781 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2782
2783 for (uint32_t region = 0; region < regionCount; region++) {
2784 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002785 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002786 ResourceAccessRange src_range =
2787 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002788 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002789 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002790 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002791 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002792 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32 ". Prior access %s.",
2793 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
2794 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002795 }
2796 }
2797 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002798 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002799 copy_region.imageOffset, copy_region.imageExtent);
2800 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002801 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002802 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
2803 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
2804 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002805 }
2806 if (skip) break;
2807 }
2808 if (skip) break;
2809 }
2810 return skip;
2811}
2812
2813void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2814 VkImageLayout dstImageLayout, uint32_t regionCount,
2815 const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06002816 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07002817 auto *cb_access_context = GetAccessContext(commandBuffer);
2818 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002819 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002820 auto *context = cb_access_context->GetCurrentAccessContext();
2821 assert(context);
2822
2823 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06002824 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002825
2826 for (uint32_t region = 0; region < regionCount; region++) {
2827 const auto &copy_region = pRegions[region];
2828 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002829 ResourceAccessRange src_range =
2830 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002831 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002832 }
2833 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002834 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002835 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002836 }
2837 }
2838}
2839
2840bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
2841 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
2842 const VkBufferImageCopy *pRegions) const {
2843 bool skip = false;
2844 const auto *cb_access_context = GetAccessContext(commandBuffer);
2845 assert(cb_access_context);
2846 if (!cb_access_context) return skip;
2847
2848 const auto *context = cb_access_context->GetCurrentAccessContext();
2849 assert(context);
2850 if (!context) return skip;
2851
2852 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2853 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2854 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
2855 for (uint32_t region = 0; region < regionCount; region++) {
2856 const auto &copy_region = pRegions[region];
2857 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002858 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002859 copy_region.imageOffset, copy_region.imageExtent);
2860 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002861 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002862 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
2863 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
2864 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002865 }
2866 }
2867 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06002868 ResourceAccessRange dst_range =
2869 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002870 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002871 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002872 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002873 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Prior access %s.",
2874 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
2875 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002876 }
2877 }
2878 if (skip) break;
2879 }
2880 return skip;
2881}
2882
2883void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2884 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06002885 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07002886 auto *cb_access_context = GetAccessContext(commandBuffer);
2887 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002888 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07002889 auto *context = cb_access_context->GetCurrentAccessContext();
2890 assert(context);
2891
2892 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002893 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2894 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 -06002895 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07002896
2897 for (uint32_t region = 0; region < regionCount; region++) {
2898 const auto &copy_region = pRegions[region];
2899 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002900 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002901 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002902 }
2903 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002904 ResourceAccessRange dst_range =
2905 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002906 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002907 }
2908 }
2909}
2910
2911bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2912 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2913 const VkImageBlit *pRegions, VkFilter filter) const {
2914 bool skip = false;
2915 const auto *cb_access_context = GetAccessContext(commandBuffer);
2916 assert(cb_access_context);
2917 if (!cb_access_context) return skip;
2918
2919 const auto *context = cb_access_context->GetCurrentAccessContext();
2920 assert(context);
2921 if (!context) return skip;
2922
2923 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2924 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2925
2926 for (uint32_t region = 0; region < regionCount; region++) {
2927 const auto &blit_region = pRegions[region];
2928 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06002929 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
2930 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
2931 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
2932 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
2933 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
2934 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
2935 auto hazard =
2936 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07002937 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002938 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002939 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
2940 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
2941 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002942 }
2943 }
2944
2945 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06002946 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
2947 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
2948 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
2949 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
2950 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
2951 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
2952 auto hazard =
2953 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07002954 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002955 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002956 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
2957 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
2958 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002959 }
2960 if (skip) break;
2961 }
2962 }
2963
2964 return skip;
2965}
2966
2967void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2968 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2969 const VkImageBlit *pRegions, VkFilter filter) {
locke-lunarg8ec19162020-06-16 18:48:34 -06002970 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
2971 pRegions, filter);
locke-lunarga19c71d2020-03-02 18:17:04 -07002972 auto *cb_access_context = GetAccessContext(commandBuffer);
2973 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002974 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002975 auto *context = cb_access_context->GetCurrentAccessContext();
2976 assert(context);
2977
2978 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002979 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002980
2981 for (uint32_t region = 0; region < regionCount; region++) {
2982 const auto &blit_region = pRegions[region];
2983 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06002984 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
2985 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
2986 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
2987 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
2988 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
2989 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
2990 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002991 }
2992 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06002993 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
2994 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
2995 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
2996 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
2997 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
2998 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
2999 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003000 }
3001 }
3002}
locke-lunarg36ba2592020-04-03 09:42:04 -06003003
locke-lunarg61870c22020-06-09 14:51:50 -06003004bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3005 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3006 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003007 bool skip = false;
3008 if (drawCount == 0) return skip;
3009
3010 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3011 VkDeviceSize size = struct_size;
3012 if (drawCount == 1 || stride == size) {
3013 if (drawCount > 1) size *= drawCount;
3014 ResourceAccessRange range = MakeRange(offset, size);
3015 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3016 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003017 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
3018 "%s: Hazard %s for indirect %s in %s. Prior access %s.", function, string_SyncHazard(hazard.hazard),
3019 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3020 string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003021 }
3022 } else {
3023 for (uint32_t i = 0; i < drawCount; ++i) {
3024 ResourceAccessRange range = MakeRange(offset + i * stride, size);
3025 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3026 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003027 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
3028 "%s: Hazard %s for indirect %s in %s. Prior access %s.", function,
3029 string_SyncHazard(hazard.hazard), report_data->FormatHandle(buffer).c_str(),
3030 report_data->FormatHandle(commandBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003031 break;
3032 }
3033 }
3034 }
3035 return skip;
3036}
3037
locke-lunarg61870c22020-06-09 14:51:50 -06003038void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3039 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3040 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003041 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3042 VkDeviceSize size = struct_size;
3043 if (drawCount == 1 || stride == size) {
3044 if (drawCount > 1) size *= drawCount;
3045 ResourceAccessRange range = MakeRange(offset, size);
3046 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3047 } else {
3048 for (uint32_t i = 0; i < drawCount; ++i) {
3049 ResourceAccessRange range = MakeRange(offset + i * stride, size);
3050 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3051 }
3052 }
3053}
3054
locke-lunarg61870c22020-06-09 14:51:50 -06003055bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3056 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003057 bool skip = false;
3058
3059 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3060 ResourceAccessRange range = MakeRange(offset, 4);
3061 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3062 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003063 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
3064 "%s: Hazard %s for countBuffer %s in %s. Prior access %s.", function, string_SyncHazard(hazard.hazard),
3065 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3066 string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003067 }
3068 return skip;
3069}
3070
locke-lunarg61870c22020-06-09 14:51:50 -06003071void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003072 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3073 ResourceAccessRange range = MakeRange(offset, 4);
3074 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3075}
3076
locke-lunarg36ba2592020-04-03 09:42:04 -06003077bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003078 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003079 const auto *cb_access_context = GetAccessContext(commandBuffer);
3080 assert(cb_access_context);
3081 if (!cb_access_context) return skip;
3082
locke-lunarg61870c22020-06-09 14:51:50 -06003083 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003084 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003085}
3086
3087void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003088 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003089 auto *cb_access_context = GetAccessContext(commandBuffer);
3090 assert(cb_access_context);
3091 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003092
locke-lunarg61870c22020-06-09 14:51:50 -06003093 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003094}
locke-lunarge1a67022020-04-29 00:15:36 -06003095
3096bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003097 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003098 const auto *cb_access_context = GetAccessContext(commandBuffer);
3099 assert(cb_access_context);
3100 if (!cb_access_context) return skip;
3101
3102 const auto *context = cb_access_context->GetCurrentAccessContext();
3103 assert(context);
3104 if (!context) return skip;
3105
locke-lunarg61870c22020-06-09 14:51:50 -06003106 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3107 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3108 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003109 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003110}
3111
3112void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003113 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
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_DISPATCHINDIRECT);
3117 auto *context = cb_access_context->GetCurrentAccessContext();
3118 assert(context);
3119
locke-lunarg61870c22020-06-09 14:51:50 -06003120 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3121 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003122}
3123
3124bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3125 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003126 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003127 const auto *cb_access_context = GetAccessContext(commandBuffer);
3128 assert(cb_access_context);
3129 if (!cb_access_context) return skip;
3130
locke-lunarg61870c22020-06-09 14:51:50 -06003131 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3132 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3133 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003134 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003135}
3136
3137void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3138 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003139 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003140 auto *cb_access_context = GetAccessContext(commandBuffer);
3141 assert(cb_access_context);
3142 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003143
locke-lunarg61870c22020-06-09 14:51:50 -06003144 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3145 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3146 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003147}
3148
3149bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3150 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003151 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003152 const auto *cb_access_context = GetAccessContext(commandBuffer);
3153 assert(cb_access_context);
3154 if (!cb_access_context) return skip;
3155
locke-lunarg61870c22020-06-09 14:51:50 -06003156 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3157 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3158 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003159 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003160}
3161
3162void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3163 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003164 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003165 auto *cb_access_context = GetAccessContext(commandBuffer);
3166 assert(cb_access_context);
3167 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003168
locke-lunarg61870c22020-06-09 14:51:50 -06003169 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3170 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3171 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003172}
3173
3174bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3175 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003176 bool skip = false;
3177 if (drawCount == 0) return skip;
3178
locke-lunargff255f92020-05-13 18:53:52 -06003179 const auto *cb_access_context = GetAccessContext(commandBuffer);
3180 assert(cb_access_context);
3181 if (!cb_access_context) return skip;
3182
3183 const auto *context = cb_access_context->GetCurrentAccessContext();
3184 assert(context);
3185 if (!context) return skip;
3186
locke-lunarg61870c22020-06-09 14:51:50 -06003187 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3188 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3189 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3190 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003191
3192 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3193 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3194 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003195 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003196 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003197}
3198
3199void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3200 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003201 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003202 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003203 auto *cb_access_context = GetAccessContext(commandBuffer);
3204 assert(cb_access_context);
3205 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3206 auto *context = cb_access_context->GetCurrentAccessContext();
3207 assert(context);
3208
locke-lunarg61870c22020-06-09 14:51:50 -06003209 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3210 cb_access_context->RecordDrawSubpassAttachment(tag);
3211 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003212
3213 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3214 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3215 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003216 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003217}
3218
3219bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3220 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003221 bool skip = false;
3222 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003223 const auto *cb_access_context = GetAccessContext(commandBuffer);
3224 assert(cb_access_context);
3225 if (!cb_access_context) return skip;
3226
3227 const auto *context = cb_access_context->GetCurrentAccessContext();
3228 assert(context);
3229 if (!context) return skip;
3230
locke-lunarg61870c22020-06-09 14:51:50 -06003231 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3232 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3233 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3234 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003235
3236 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3237 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3238 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003239 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003240 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003241}
3242
3243void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3244 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003245 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003246 auto *cb_access_context = GetAccessContext(commandBuffer);
3247 assert(cb_access_context);
3248 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3249 auto *context = cb_access_context->GetCurrentAccessContext();
3250 assert(context);
3251
locke-lunarg61870c22020-06-09 14:51:50 -06003252 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3253 cb_access_context->RecordDrawSubpassAttachment(tag);
3254 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003255
3256 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3257 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3258 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003259 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003260}
3261
3262bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3263 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3264 uint32_t stride, const char *function) const {
3265 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003266 const auto *cb_access_context = GetAccessContext(commandBuffer);
3267 assert(cb_access_context);
3268 if (!cb_access_context) return skip;
3269
3270 const auto *context = cb_access_context->GetCurrentAccessContext();
3271 assert(context);
3272 if (!context) return skip;
3273
locke-lunarg61870c22020-06-09 14:51:50 -06003274 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3275 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3276 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3277 function);
3278 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003279
3280 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3281 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3282 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003283 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003284 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003285}
3286
3287bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3288 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3289 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003290 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3291 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003292}
3293
3294void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3295 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3296 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003297 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3298 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003299 auto *cb_access_context = GetAccessContext(commandBuffer);
3300 assert(cb_access_context);
3301 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3302 auto *context = cb_access_context->GetCurrentAccessContext();
3303 assert(context);
3304
locke-lunarg61870c22020-06-09 14:51:50 -06003305 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3306 cb_access_context->RecordDrawSubpassAttachment(tag);
3307 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3308 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003309
3310 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3311 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3312 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003313 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003314}
3315
3316bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3317 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3318 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003319 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3320 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003321}
3322
3323void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3324 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3325 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003326 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3327 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003328 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003329}
3330
3331bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3332 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3333 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003334 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3335 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003336}
3337
3338void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3339 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3340 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003341 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3342 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003343 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3344}
3345
3346bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3347 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3348 uint32_t stride, const char *function) const {
3349 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003350 const auto *cb_access_context = GetAccessContext(commandBuffer);
3351 assert(cb_access_context);
3352 if (!cb_access_context) return skip;
3353
3354 const auto *context = cb_access_context->GetCurrentAccessContext();
3355 assert(context);
3356 if (!context) return skip;
3357
locke-lunarg61870c22020-06-09 14:51:50 -06003358 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3359 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3360 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3361 stride, function);
3362 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003363
3364 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3365 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3366 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003367 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003368 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003369}
3370
3371bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3372 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3373 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003374 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3375 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003376}
3377
3378void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3379 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3380 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003381 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3382 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003383 auto *cb_access_context = GetAccessContext(commandBuffer);
3384 assert(cb_access_context);
3385 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3386 auto *context = cb_access_context->GetCurrentAccessContext();
3387 assert(context);
3388
locke-lunarg61870c22020-06-09 14:51:50 -06003389 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3390 cb_access_context->RecordDrawSubpassAttachment(tag);
3391 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3392 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003393
3394 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3395 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003396 // We will update the index and vertex buffer in SubmitQueue in the future.
3397 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003398}
3399
3400bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3401 VkDeviceSize offset, VkBuffer countBuffer,
3402 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3403 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003404 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3405 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003406}
3407
3408void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3409 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3410 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003411 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3412 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003413 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3414}
3415
3416bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3417 VkDeviceSize offset, VkBuffer countBuffer,
3418 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3419 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003420 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3421 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003422}
3423
3424void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3425 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3426 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003427 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3428 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003429 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3430}
3431
3432bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3433 const VkClearColorValue *pColor, uint32_t rangeCount,
3434 const VkImageSubresourceRange *pRanges) const {
3435 bool skip = false;
3436 const auto *cb_access_context = GetAccessContext(commandBuffer);
3437 assert(cb_access_context);
3438 if (!cb_access_context) return skip;
3439
3440 const auto *context = cb_access_context->GetCurrentAccessContext();
3441 assert(context);
3442 if (!context) return skip;
3443
3444 const auto *image_state = Get<IMAGE_STATE>(image);
3445
3446 for (uint32_t index = 0; index < rangeCount; index++) {
3447 const auto &range = pRanges[index];
3448 if (image_state) {
3449 auto hazard =
3450 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3451 if (hazard.hazard) {
3452 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003453 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Prior access %s.",
3454 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
3455 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003456 }
3457 }
3458 }
3459 return skip;
3460}
3461
3462void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3463 const VkClearColorValue *pColor, uint32_t rangeCount,
3464 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003465 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003466 auto *cb_access_context = GetAccessContext(commandBuffer);
3467 assert(cb_access_context);
3468 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
3469 auto *context = cb_access_context->GetCurrentAccessContext();
3470 assert(context);
3471
3472 const auto *image_state = Get<IMAGE_STATE>(image);
3473
3474 for (uint32_t index = 0; index < rangeCount; index++) {
3475 const auto &range = pRanges[index];
3476 if (image_state) {
3477 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3478 tag);
3479 }
3480 }
3481}
3482
3483bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3484 VkImageLayout imageLayout,
3485 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3486 const VkImageSubresourceRange *pRanges) const {
3487 bool skip = false;
3488 const auto *cb_access_context = GetAccessContext(commandBuffer);
3489 assert(cb_access_context);
3490 if (!cb_access_context) return skip;
3491
3492 const auto *context = cb_access_context->GetCurrentAccessContext();
3493 assert(context);
3494 if (!context) return skip;
3495
3496 const auto *image_state = Get<IMAGE_STATE>(image);
3497
3498 for (uint32_t index = 0; index < rangeCount; index++) {
3499 const auto &range = pRanges[index];
3500 if (image_state) {
3501 auto hazard =
3502 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3503 if (hazard.hazard) {
3504 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003505 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Prior access %s.",
3506 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
3507 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003508 }
3509 }
3510 }
3511 return skip;
3512}
3513
3514void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3515 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3516 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003517 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003518 auto *cb_access_context = GetAccessContext(commandBuffer);
3519 assert(cb_access_context);
3520 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
3521 auto *context = cb_access_context->GetCurrentAccessContext();
3522 assert(context);
3523
3524 const auto *image_state = Get<IMAGE_STATE>(image);
3525
3526 for (uint32_t index = 0; index < rangeCount; index++) {
3527 const auto &range = pRanges[index];
3528 if (image_state) {
3529 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3530 tag);
3531 }
3532 }
3533}
3534
3535bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
3536 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
3537 VkDeviceSize dstOffset, VkDeviceSize stride,
3538 VkQueryResultFlags flags) const {
3539 bool skip = false;
3540 const auto *cb_access_context = GetAccessContext(commandBuffer);
3541 assert(cb_access_context);
3542 if (!cb_access_context) return skip;
3543
3544 const auto *context = cb_access_context->GetCurrentAccessContext();
3545 assert(context);
3546 if (!context) return skip;
3547
3548 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3549
3550 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003551 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003552 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3553 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003554 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3555 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Prior access %s.",
3556 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
3557 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003558 }
3559 }
locke-lunargff255f92020-05-13 18:53:52 -06003560
3561 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003562 return skip;
3563}
3564
3565void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
3566 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3567 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003568 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
3569 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06003570 auto *cb_access_context = GetAccessContext(commandBuffer);
3571 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06003572 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06003573 auto *context = cb_access_context->GetCurrentAccessContext();
3574 assert(context);
3575
3576 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3577
3578 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003579 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003580 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3581 }
locke-lunargff255f92020-05-13 18:53:52 -06003582
3583 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003584}
3585
3586bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3587 VkDeviceSize size, uint32_t data) const {
3588 bool skip = false;
3589 const auto *cb_access_context = GetAccessContext(commandBuffer);
3590 assert(cb_access_context);
3591 if (!cb_access_context) return skip;
3592
3593 const auto *context = cb_access_context->GetCurrentAccessContext();
3594 assert(context);
3595 if (!context) return skip;
3596
3597 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3598
3599 if (dst_buffer) {
3600 ResourceAccessRange range = MakeRange(dstOffset, size);
3601 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3602 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003603 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3604 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Prior access %s.", string_SyncHazard(hazard.hazard),
3605 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003606 }
3607 }
3608 return skip;
3609}
3610
3611void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3612 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003613 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06003614 auto *cb_access_context = GetAccessContext(commandBuffer);
3615 assert(cb_access_context);
3616 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
3617 auto *context = cb_access_context->GetCurrentAccessContext();
3618 assert(context);
3619
3620 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3621
3622 if (dst_buffer) {
3623 ResourceAccessRange range = MakeRange(dstOffset, size);
3624 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3625 }
3626}
3627
3628bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3629 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3630 const VkImageResolve *pRegions) const {
3631 bool skip = false;
3632 const auto *cb_access_context = GetAccessContext(commandBuffer);
3633 assert(cb_access_context);
3634 if (!cb_access_context) return skip;
3635
3636 const auto *context = cb_access_context->GetCurrentAccessContext();
3637 assert(context);
3638 if (!context) return skip;
3639
3640 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3641 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3642
3643 for (uint32_t region = 0; region < regionCount; region++) {
3644 const auto &resolve_region = pRegions[region];
3645 if (src_image) {
3646 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3647 resolve_region.srcOffset, resolve_region.extent);
3648 if (hazard.hazard) {
3649 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003650 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
3651 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
3652 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003653 }
3654 }
3655
3656 if (dst_image) {
3657 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3658 resolve_region.dstOffset, resolve_region.extent);
3659 if (hazard.hazard) {
3660 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003661 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
3662 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
3663 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003664 }
3665 if (skip) break;
3666 }
3667 }
3668
3669 return skip;
3670}
3671
3672void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3673 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3674 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003675 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3676 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06003677 auto *cb_access_context = GetAccessContext(commandBuffer);
3678 assert(cb_access_context);
3679 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
3680 auto *context = cb_access_context->GetCurrentAccessContext();
3681 assert(context);
3682
3683 auto *src_image = Get<IMAGE_STATE>(srcImage);
3684 auto *dst_image = Get<IMAGE_STATE>(dstImage);
3685
3686 for (uint32_t region = 0; region < regionCount; region++) {
3687 const auto &resolve_region = pRegions[region];
3688 if (src_image) {
3689 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3690 resolve_region.srcOffset, resolve_region.extent, tag);
3691 }
3692 if (dst_image) {
3693 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3694 resolve_region.dstOffset, resolve_region.extent, tag);
3695 }
3696 }
3697}
3698
3699bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3700 VkDeviceSize dataSize, const void *pData) const {
3701 bool skip = false;
3702 const auto *cb_access_context = GetAccessContext(commandBuffer);
3703 assert(cb_access_context);
3704 if (!cb_access_context) return skip;
3705
3706 const auto *context = cb_access_context->GetCurrentAccessContext();
3707 assert(context);
3708 if (!context) return skip;
3709
3710 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3711
3712 if (dst_buffer) {
3713 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3714 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3715 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003716 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3717 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Prior access %s.", string_SyncHazard(hazard.hazard),
3718 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003719 }
3720 }
3721 return skip;
3722}
3723
3724void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3725 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003726 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06003727 auto *cb_access_context = GetAccessContext(commandBuffer);
3728 assert(cb_access_context);
3729 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
3730 auto *context = cb_access_context->GetCurrentAccessContext();
3731 assert(context);
3732
3733 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3734
3735 if (dst_buffer) {
3736 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3737 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3738 }
3739}
locke-lunargff255f92020-05-13 18:53:52 -06003740
3741bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3742 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
3743 bool skip = false;
3744 const auto *cb_access_context = GetAccessContext(commandBuffer);
3745 assert(cb_access_context);
3746 if (!cb_access_context) return skip;
3747
3748 const auto *context = cb_access_context->GetCurrentAccessContext();
3749 assert(context);
3750 if (!context) return skip;
3751
3752 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3753
3754 if (dst_buffer) {
3755 ResourceAccessRange range = MakeRange(dstOffset, 4);
3756 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3757 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003758 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3759 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Prior access %s.",
3760 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
3761 string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003762 }
3763 }
3764 return skip;
3765}
3766
3767void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3768 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003769 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06003770 auto *cb_access_context = GetAccessContext(commandBuffer);
3771 assert(cb_access_context);
3772 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
3773 auto *context = cb_access_context->GetCurrentAccessContext();
3774 assert(context);
3775
3776 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3777
3778 if (dst_buffer) {
3779 ResourceAccessRange range = MakeRange(dstOffset, 4);
3780 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3781 }
3782}