blob: 82f42bb41b94d5f27950da9bf49fa65e1f060c6f [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;
86 out << "( command #" << (tag.index & 0xFFFFFFFF) << ", reset #" << (tag.index >> 32) << ")";
87 return out.str();
88}
89
John Zulaufb027cdb2020-05-21 14:25:22 -060090static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
91static constexpr SyncStageAccessFlags kColorAttachmentAccessScope =
92 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
93 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
94 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT;
95static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
96 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
97static constexpr SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
98 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
99 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
100 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
101 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
102
103static constexpr SyncOrderingBarrier kColorAttachmentRasterOrder = {kColorAttachmentExecScope, kColorAttachmentAccessScope};
104static constexpr SyncOrderingBarrier kDepthStencilAttachmentRasterOrder = {kDepthStencilAttachmentExecScope,
105 kDepthStencilAttachmentAccessScope};
106static constexpr SyncOrderingBarrier kAttachmentRasterOrder = {kDepthStencilAttachmentExecScope | kColorAttachmentExecScope,
107 kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope};
John Zulauf7635de32020-05-29 17:14:15 -0600108// Sometimes we have an internal access conflict, and we using the kCurrentCommandTag to set and detect in temporary/proxy contexts
109static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex);
John Zulaufb027cdb2020-05-21 14:25:22 -0600110
locke-lunarg3c038002020-04-30 23:08:08 -0600111inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
112 if (size == VK_WHOLE_SIZE) {
113 return (whole_size - offset);
114 }
115 return size;
116}
117
John Zulauf16adfc92020-04-08 10:28:33 -0600118template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600119static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600120 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
121}
122
John Zulauf355e49b2020-04-24 15:11:15 -0600123static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600124
John Zulauf0cb5be22020-01-23 12:18:22 -0700125// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
126VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
127 VkPipelineStageFlags expanded = stage_mask;
128 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
129 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
130 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
131 if (all_commands.first & queue_flags) {
132 expanded |= all_commands.second;
133 }
134 }
135 }
136 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
137 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
138 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
139 }
140 return expanded;
141}
142
John Zulauf36bcf6a2020-02-03 15:12:52 -0700143VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
144 std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
145 VkPipelineStageFlags unscanned = stage_mask;
146 VkPipelineStageFlags related = 0;
147 for (const auto entry : map) {
148 const auto stage = entry.first;
149 if (stage & unscanned) {
150 related = related | entry.second;
151 unscanned = unscanned & ~stage;
152 if (!unscanned) break;
153 }
154 }
155 return related;
156}
157
158VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
159 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
160}
161
162VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
163 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
164}
165
John Zulauf5c5e88d2019-12-26 11:22:02 -0700166static const ResourceAccessRange full_range(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700167
locke-lunargff255f92020-05-13 18:53:52 -0600168void GetBufferRange(VkDeviceSize &range_start, VkDeviceSize &range_size, VkDeviceSize offset, VkDeviceSize buf_whole_size,
169 uint32_t first_index, uint32_t count, VkDeviceSize stride) {
170 range_start = offset + first_index * stride;
171 range_size = 0;
172 if (count == UINT32_MAX) {
173 range_size = buf_whole_size - range_start;
174 } else {
175 range_size = count * stride;
176 }
177}
178
locke-lunarg654e3692020-06-04 17:19:15 -0600179SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
180 VkShaderStageFlagBits stage_flag) {
181 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
182 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
183 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
184 }
185 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
186 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
187 assert(0);
188 }
189 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
190 return stage_access->second.uniform_read;
191 }
192
193 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
194 // Because if write hazard happens, read hazard might or might not happen.
195 // But if write hazard doesn't happen, read hazard is impossible to happen.
196 if (descriptor_data.is_writable) {
197 return stage_access->second.shader_write;
198 }
199 return stage_access->second.shader_read;
200}
201
locke-lunarg37047832020-06-12 13:44:45 -0600202bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
203 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
204 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
205 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
206 ? true
207 : false;
208}
209
210bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
211 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
212 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
213 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
214 ? true
215 : false;
216}
217
John Zulauf355e49b2020-04-24 15:11:15 -0600218// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
219const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
220 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
221
John Zulauf7635de32020-05-29 17:14:15 -0600222// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
223// Used by both validation and record operations
224//
225// The signature for Action() reflect the needs of both uses.
226template <typename Action>
227void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
228 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
229 VkExtent3D extent = CastTo3D(render_area.extent);
230 VkOffset3D offset = CastTo3D(render_area.offset);
231 const auto &rp_ci = rp_state.createInfo;
232 const auto *attachment_ci = rp_ci.pAttachments;
233 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
234
235 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
236 const auto *color_attachments = subpass_ci.pColorAttachments;
237 const auto *color_resolve = subpass_ci.pResolveAttachments;
238 if (color_resolve && color_attachments) {
239 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
240 const auto &color_attach = color_attachments[i].attachment;
241 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
242 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
243 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
244 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
245 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
246 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
247 }
248 }
249 }
250
251 // Depth stencil resolve only if the extension is present
252 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
253 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
254 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
255 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
256 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
257 const auto src_ci = attachment_ci[src_at];
258 // The formats are required to match so we can pick either
259 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
260 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
261 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
262 VkImageAspectFlags aspect_mask = 0u;
263
264 // Figure out which aspects are actually touched during resolve operations
265 const char *aspect_string = nullptr;
266 if (resolve_depth && resolve_stencil) {
267 // Validate all aspects together
268 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
269 aspect_string = "depth/stencil";
270 } else if (resolve_depth) {
271 // Validate depth only
272 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
273 aspect_string = "depth";
274 } else if (resolve_stencil) {
275 // Validate all stencil only
276 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
277 aspect_string = "stencil";
278 }
279
280 if (aspect_mask) {
281 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
282 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kDepthStencilAttachmentRasterOrder, offset, extent,
283 aspect_mask);
284 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
285 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
286 }
287 }
288}
289
290// Action for validating resolve operations
291class ValidateResolveAction {
292 public:
293 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
294 const char *func_name)
295 : render_pass_(render_pass),
296 subpass_(subpass),
297 context_(context),
298 sync_state_(sync_state),
299 func_name_(func_name),
300 skip_(false) {}
301 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
302 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
303 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
304 HazardResult hazard;
305 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
306 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600307 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
308 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
309 " to resolve attachment %" PRIu32 ". Prior access %s.",
310 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
311 src_at, dst_at, string_UsageTag(hazard.tag).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600312 }
313 }
314 // Providing a mechanism for the constructing caller to get the result of the validation
315 bool GetSkip() const { return skip_; }
316
317 private:
318 VkRenderPass render_pass_;
319 const uint32_t subpass_;
320 const AccessContext &context_;
321 const SyncValidator &sync_state_;
322 const char *func_name_;
323 bool skip_;
324};
325
326// Update action for resolve operations
327class UpdateStateResolveAction {
328 public:
329 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
330 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
331 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
332 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
333 // Ignores validation only arguments...
334 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
335 }
336
337 private:
338 AccessContext &context_;
339 const ResourceUsageTag &tag_;
340};
341
John Zulauf540266b2020-04-06 18:54:53 -0600342AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
343 const std::vector<SubpassDependencyGraphNode> &dependencies,
344 const std::vector<AccessContext> &contexts, AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600345 Reset();
346 const auto &subpass_dep = dependencies[subpass];
347 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600348 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600349 for (const auto &prev_dep : subpass_dep.prev) {
350 assert(prev_dep.dependency);
351 const auto dep = *prev_dep.dependency;
John Zulauf540266b2020-04-06 18:54:53 -0600352 prev_.emplace_back(const_cast<AccessContext *>(&contexts[dep.srcSubpass]), queue_flags, dep);
John Zulauf355e49b2020-04-24 15:11:15 -0600353 prev_by_subpass_[dep.srcSubpass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700354 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600355
356 async_.reserve(subpass_dep.async.size());
357 for (const auto async_subpass : subpass_dep.async) {
John Zulauf540266b2020-04-06 18:54:53 -0600358 async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass]));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600359 }
John Zulaufe5da6e52020-03-18 15:32:18 -0600360 if (subpass_dep.barrier_from_external) {
361 src_external_ = TrackBack(external_context, queue_flags, *subpass_dep.barrier_from_external);
362 } else {
363 src_external_ = TrackBack();
364 }
365 if (subpass_dep.barrier_to_external) {
366 dst_external_ = TrackBack(this, queue_flags, *subpass_dep.barrier_to_external);
367 } else {
368 dst_external_ = TrackBack();
John Zulauf3d84f1b2020-03-09 13:33:25 -0600369 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700370}
371
John Zulauf5f13a792020-03-10 07:31:21 -0600372template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600373HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600374 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600375 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600376 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600377
378 HazardResult hazard;
379 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
380 hazard = detector.Detect(prev);
381 }
382 return hazard;
383}
384
John Zulauf3d84f1b2020-03-09 13:33:25 -0600385// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
386// the DAG of the contexts (for example subpasses)
387template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600388HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
389 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600390 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600391
John Zulauf355e49b2020-04-24 15:11:15 -0600392 if (static_cast<uint32_t>(options) | DetectOptions::kDetectAsync) {
393 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
394 // so we'll check these first
395 for (const auto &async_context : async_) {
396 hazard = async_context->DetectAsyncHazard(type, detector, range);
397 if (hazard.hazard) return hazard;
398 }
John Zulauf5f13a792020-03-10 07:31:21 -0600399 }
400
John Zulauf69133422020-05-20 14:55:53 -0600401 const bool detect_prev = (static_cast<uint32_t>(options) | DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600402
John Zulauf69133422020-05-20 14:55:53 -0600403 const auto &accesses = GetAccessStateMap(type);
404 const auto from = accesses.lower_bound(range);
405 const auto to = accesses.upper_bound(range);
406 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600407
John Zulauf69133422020-05-20 14:55:53 -0600408 for (auto pos = from; pos != to; ++pos) {
409 // Cover any leading gap, or gap between entries
410 if (detect_prev) {
411 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
412 // Cover any leading gap, or gap between entries
413 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600414 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600415 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600416 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600417 if (hazard.hazard) return hazard;
418 }
John Zulauf69133422020-05-20 14:55:53 -0600419 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
420 gap.begin = pos->first.end;
421 }
422
423 hazard = detector.Detect(pos);
424 if (hazard.hazard) return hazard;
425 }
426
427 if (detect_prev) {
428 // Detect in the trailing empty as needed
429 gap.end = range.end;
430 if (gap.non_empty()) {
431 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600432 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600433 }
434
435 return hazard;
436}
437
438// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
439template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600440HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600441 auto &accesses = GetAccessStateMap(type);
442 const auto from = accesses.lower_bound(range);
443 const auto to = accesses.upper_bound(range);
444
John Zulauf3d84f1b2020-03-09 13:33:25 -0600445 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600446 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
447 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600448 }
John Zulauf16adfc92020-04-08 10:28:33 -0600449
John Zulauf3d84f1b2020-03-09 13:33:25 -0600450 return hazard;
451}
452
John Zulauf355e49b2020-04-24 15:11:15 -0600453// Returns the last resolved entry
454static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
455 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
456 const SyncBarrier *barrier) {
457 auto at = entry;
458 for (auto pos = first; pos != last; ++pos) {
459 // Every member of the input iterator range must fit within the remaining portion of entry
460 assert(at->first.includes(pos->first));
461 assert(at != dest->end());
462 // Trim up at to the same size as the entry to resolve
463 at = sparse_container::split(at, *dest, pos->first);
464 auto access = pos->second;
465 if (barrier) {
466 access.ApplyBarrier(*barrier);
467 }
468 at->second.Resolve(access);
469 ++at; // Go to the remaining unused section of entry
470 }
471}
472
473void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const SyncBarrier *barrier,
474 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
475 bool recur_to_infill) const {
476 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
477 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf16adfc92020-04-08 10:28:33 -0600478 if (current->pos_B->valid) {
479 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600480 auto access = src_pos->second;
481 if (barrier) {
482 access.ApplyBarrier(*barrier);
483 }
John Zulauf16adfc92020-04-08 10:28:33 -0600484 if (current->pos_A->valid) {
485 current.trim_A();
John Zulauf355e49b2020-04-24 15:11:15 -0600486 current->pos_A->lower_bound->second.Resolve(access);
John Zulauf5f13a792020-03-10 07:31:21 -0600487 } else {
John Zulauf355e49b2020-04-24 15:11:15 -0600488 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, access));
489 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600490 }
John Zulauf16adfc92020-04-08 10:28:33 -0600491 } else {
492 // we have to descend to fill this gap
493 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600494 if (current->pos_A->valid) {
495 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
496 ResourceAccessRangeMap gap_map;
497 ResolvePreviousAccess(type, current->range, &gap_map, infill_state);
498 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier);
499 } else {
500 // There isn't anything in dest in current->range, so we can accumulate directly into it.
501 ResolvePreviousAccess(type, current->range, resolve_map, infill_state);
502 if (barrier) {
503 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
504 for (auto pos = resolve_map->lower_bound(current->range); pos != current->pos_A->lower_bound; ++pos) {
505 pos->second.ApplyBarrier(*barrier);
506 }
507 }
508 }
509 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
510 // iterator of the outer while.
511
512 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
513 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
514 // we stepped on the dest map
515 const auto seek_to = current->range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
516 current.invalidate_A(); // Changes current->range
517 current.seek(seek_to);
518 } else if (!current->pos_A->valid && infill_state) {
519 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
520 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
521 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600522 }
John Zulauf5f13a792020-03-10 07:31:21 -0600523 }
John Zulauf16adfc92020-04-08 10:28:33 -0600524 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600525 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600526}
527
John Zulauf355e49b2020-04-24 15:11:15 -0600528void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
529 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600530 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600531 if (range.non_empty() && infill_state) {
532 descent_map->insert(std::make_pair(range, *infill_state));
533 }
534 } else {
535 // Look for something to fill the gap further along.
536 for (const auto &prev_dep : prev_) {
John Zulauf355e49b2020-04-24 15:11:15 -0600537 prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600538 }
539
John Zulaufe5da6e52020-03-18 15:32:18 -0600540 if (src_external_.context) {
John Zulauf355e49b2020-04-24 15:11:15 -0600541 src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600542 }
543 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600544}
545
John Zulauf16adfc92020-04-08 10:28:33 -0600546AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600547 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600548}
549
550VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
551 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
552}
553
John Zulauf355e49b2020-04-24 15:11:15 -0600554static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600555
John Zulauf1507ee42020-05-18 11:33:09 -0600556static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
557 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
558 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
559 return stage_access;
560}
561static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
562 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
563 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
564 return stage_access;
565}
566
John Zulauf7635de32020-05-29 17:14:15 -0600567// Caller must manage returned pointer
568static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
569 uint32_t subpass, const VkRect2D &render_area,
570 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
571 auto *proxy = new AccessContext(context);
572 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600573 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600574 return proxy;
575}
576
John Zulauf540266b2020-04-06 18:54:53 -0600577void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600578 AddressType address_type, ResourceAccessRangeMap *descent_map,
579 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600580 if (!SimpleBinding(image_state)) return;
581
John Zulauf62f10592020-04-03 12:20:02 -0600582 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600583 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600584 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600585 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600586 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600587 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600588 }
589}
590
John Zulauf7635de32020-05-29 17:14:15 -0600591// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600592bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600593 const VkRect2D &render_area, uint32_t subpass,
594 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
595 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600596 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600597 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
598 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
599 // those affects have not been recorded yet.
600 //
601 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
602 // to apply and only copy then, if this proves a hot spot.
603 std::unique_ptr<AccessContext> proxy_for_prev;
604 TrackBack proxy_track_back;
605
John Zulauf355e49b2020-04-24 15:11:15 -0600606 const auto &transitions = rp_state.subpass_transitions[subpass];
607 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600608 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
609
610 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
611 if (prev_needs_proxy) {
612 if (!proxy_for_prev) {
613 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
614 render_area, attachment_views));
615 proxy_track_back = *track_back;
616 proxy_track_back.context = proxy_for_prev.get();
617 }
618 track_back = &proxy_track_back;
619 }
620 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600621 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600622 skip |= sync_state.LogError(
623 rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
624 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " image layout transition. Prior access %s.",
625 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment, string_UsageTag(hazard.tag).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600626 }
627 }
628 return skip;
629}
630
John Zulauf1507ee42020-05-18 11:33:09 -0600631bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600632 const VkRect2D &render_area, uint32_t subpass,
633 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
634 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600635 bool skip = false;
636 const auto *attachment_ci = rp_state.createInfo.pAttachments;
637 VkExtent3D extent = CastTo3D(render_area.extent);
638 VkOffset3D offset = CastTo3D(render_area.offset);
639 const auto external_access_scope = src_external_.barrier.dst_access_scope;
John Zulauf1507ee42020-05-18 11:33:09 -0600640
641 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
642 if (subpass == rp_state.attachment_first_subpass[i]) {
643 if (attachment_views[i] == nullptr) continue;
644 const IMAGE_VIEW_STATE &view = *attachment_views[i];
645 const IMAGE_STATE *image = view.image_state.get();
646 if (image == nullptr) continue;
647 const auto &ci = attachment_ci[i];
648 const bool is_transition = rp_state.attachment_first_is_transition[i];
649
650 // Need check in the following way
651 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
652 // vs. transition
653 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
654 // for each aspect loaded.
655
656 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600657 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600658 const bool is_color = !(has_depth || has_stencil);
659
660 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
661 const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U;
662 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
663 const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U;
664
John Zulaufaff20662020-06-01 14:07:58 -0600665 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600666 const char *aspect = nullptr;
667 if (is_transition) {
668 // For transition w
669 SyncHazard transition_hazard = SyncHazard::NONE;
670 bool checked_stencil = false;
671 if (load_mask) {
672 if ((load_mask & external_access_scope) != load_mask) {
673 transition_hazard =
674 SyncStageAccess::HasWrite(load_mask) ? SyncHazard::WRITE_AFTER_WRITE : SyncHazard::READ_AFTER_WRITE;
675 aspect = is_color ? "color" : "depth";
676 }
677 if (!transition_hazard && stencil_mask) {
678 if ((stencil_mask & external_access_scope) != stencil_mask) {
679 transition_hazard = SyncStageAccess::HasWrite(stencil_mask) ? SyncHazard::WRITE_AFTER_WRITE
680 : SyncHazard::READ_AFTER_WRITE;
681 aspect = "stencil";
682 checked_stencil = true;
683 }
684 }
685 }
686 if (transition_hazard) {
687 // Hazard vs. ILT
688 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
689 skip |=
690 sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
691 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
692 " aspect %s during load with loadOp %s.",
693 func_name, string_SyncHazard(transition_hazard), subpass, i, aspect, load_op_string);
694 }
695 } else {
696 auto hazard_range = view.normalized_subresource_range;
697 bool checked_stencil = false;
698 if (is_color) {
699 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
700 aspect = "color";
701 } else {
702 if (has_depth) {
703 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
704 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
705 aspect = "depth";
706 }
707 if (!hazard.hazard && has_stencil) {
708 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
709 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
710 aspect = "stencil";
711 checked_stencil = true;
712 }
713 }
714
715 if (hazard.hazard) {
716 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
717 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
718 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
719 " aspect %s during load with loadOp %s.",
720 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
721 }
722 }
723 }
724 }
725 return skip;
726}
727
John Zulaufaff20662020-06-01 14:07:58 -0600728// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
729// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
730// store is part of the same Next/End operation.
731// The latter is handled in layout transistion validation directly
732bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
733 const VkRect2D &render_area, uint32_t subpass,
734 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
735 const char *func_name) const {
736 bool skip = false;
737 const auto *attachment_ci = rp_state.createInfo.pAttachments;
738 VkExtent3D extent = CastTo3D(render_area.extent);
739 VkOffset3D offset = CastTo3D(render_area.offset);
740
741 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
742 if (subpass == rp_state.attachment_last_subpass[i]) {
743 if (attachment_views[i] == nullptr) continue;
744 const IMAGE_VIEW_STATE &view = *attachment_views[i];
745 const IMAGE_STATE *image = view.image_state.get();
746 if (image == nullptr) continue;
747 const auto &ci = attachment_ci[i];
748
749 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
750 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
751 // sake, we treat DONT_CARE as writing.
752 const bool has_depth = FormatHasDepth(ci.format);
753 const bool has_stencil = FormatHasStencil(ci.format);
754 const bool is_color = !(has_depth || has_stencil);
755 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
756 if (!has_stencil && !store_op_stores) continue;
757
758 HazardResult hazard;
759 const char *aspect = nullptr;
760 bool checked_stencil = false;
761 if (is_color) {
762 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
763 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
764 aspect = "color";
765 } else {
766 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
767 auto hazard_range = view.normalized_subresource_range;
768 if (has_depth && store_op_stores) {
769 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
770 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
771 kAttachmentRasterOrder, offset, extent);
772 aspect = "depth";
773 }
774 if (!hazard.hazard && has_stencil && stencil_op_stores) {
775 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
776 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
777 kAttachmentRasterOrder, offset, extent);
778 aspect = "stencil";
779 checked_stencil = true;
780 }
781 }
782
783 if (hazard.hazard) {
784 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
785 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -0600786 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
787 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
788 " %s aspect during store with %s %s. Prior access %s",
789 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
790 store_op_string, string_UsageTag(hazard.tag).c_str());
John Zulaufaff20662020-06-01 14:07:58 -0600791 }
792 }
793 }
794 return skip;
795}
796
John Zulaufb027cdb2020-05-21 14:25:22 -0600797bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
798 const VkRect2D &render_area,
799 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
800 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600801 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
802 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
803 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600804}
805
John Zulauf3d84f1b2020-03-09 13:33:25 -0600806class HazardDetector {
807 SyncStageAccessIndex usage_index_;
808
809 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600810 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600811 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
812 return pos->second.DetectAsyncHazard(usage_index_);
813 }
814 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
815};
816
John Zulauf69133422020-05-20 14:55:53 -0600817class HazardDetectorWithOrdering {
818 const SyncStageAccessIndex usage_index_;
819 const SyncOrderingBarrier &ordering_;
820
821 public:
822 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
823 return pos->second.DetectHazard(usage_index_, ordering_);
824 }
825 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
826 return pos->second.DetectAsyncHazard(usage_index_);
827 }
828 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
829 : usage_index_(usage), ordering_(ordering) {}
830};
831
John Zulauf16adfc92020-04-08 10:28:33 -0600832HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600833 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600834 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600835 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600836}
837
John Zulauf16adfc92020-04-08 10:28:33 -0600838HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600839 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600840 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600841 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600842}
843
John Zulauf69133422020-05-20 14:55:53 -0600844template <typename Detector>
845HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
846 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
847 const VkExtent3D &extent, DetectOptions options) const {
848 if (!SimpleBinding(image)) return HazardResult();
849 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
850 const auto address_type = ImageAddressType(image);
851 const auto base_address = ResourceBaseAddress(image);
852 for (; range_gen->non_empty(); ++range_gen) {
853 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
854 if (hazard.hazard) return hazard;
855 }
856 return HazardResult();
857}
858
John Zulauf540266b2020-04-06 18:54:53 -0600859HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
860 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
861 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700862 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
863 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -0600864 return DetectHazard(image, current_usage, subresource_range, offset, extent);
865}
866
867HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
868 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
869 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -0600870 HazardDetector detector(current_usage);
871 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
872}
873
874HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
875 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
876 const VkOffset3D &offset, const VkExtent3D &extent) const {
877 HazardDetectorWithOrdering detector(current_usage, ordering);
878 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -0600879}
880
John Zulaufb027cdb2020-05-21 14:25:22 -0600881// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
882// should have reported the issue regarding an invalid attachment entry
883HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
884 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
885 VkImageAspectFlags aspect_mask) const {
886 if (view != nullptr) {
887 const IMAGE_STATE *image = view->image_state.get();
888 if (image != nullptr) {
889 auto *detect_range = &view->normalized_subresource_range;
890 VkImageSubresourceRange masked_range;
891 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
892 masked_range = view->normalized_subresource_range;
893 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
894 detect_range = &masked_range;
895 }
896
897 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
898 if (detect_range->aspectMask) {
899 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
900 }
901 }
902 }
903 return HazardResult();
904}
John Zulauf3d84f1b2020-03-09 13:33:25 -0600905class BarrierHazardDetector {
906 public:
907 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
908 SyncStageAccessFlags src_access_scope)
909 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
910
John Zulauf5f13a792020-03-10 07:31:21 -0600911 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
912 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -0700913 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600914 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
915 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
916 return pos->second.DetectAsyncHazard(usage_index_);
917 }
918
919 private:
920 SyncStageAccessIndex usage_index_;
921 VkPipelineStageFlags src_exec_scope_;
922 SyncStageAccessFlags src_access_scope_;
923};
924
John Zulauf16adfc92020-04-08 10:28:33 -0600925HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -0600926 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600927 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600928 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -0600929 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700930}
931
John Zulauf16adfc92020-04-08 10:28:33 -0600932HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600933 SyncStageAccessFlags src_access_scope,
934 const VkImageSubresourceRange &subresource_range,
935 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -0600936 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
937 VkOffset3D zero_offset = {0, 0, 0};
938 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700939}
940
John Zulauf355e49b2020-04-24 15:11:15 -0600941HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
942 SyncStageAccessFlags src_stage_accesses,
943 const VkImageMemoryBarrier &barrier) const {
944 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
945 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
946 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
947}
948
John Zulauf9cb530d2019-09-30 14:14:10 -0600949template <typename Flags, typename Map>
950SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
951 SyncStageAccessFlags scope = 0;
952 for (const auto &bit_scope : map) {
953 if (flag_mask < bit_scope.first) break;
954
955 if (flag_mask & bit_scope.first) {
956 scope |= bit_scope.second;
957 }
958 }
959 return scope;
960}
961
962SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
963 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
964}
965
966SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
967 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
968}
969
970// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
971SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -0600972 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
973 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
974 // 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 -0600975 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
976}
977
978template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -0700979void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -0600980 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
981 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -0600982 auto pos = accesses->lower_bound(range);
983 if (pos == accesses->end() || !pos->first.intersects(range)) {
984 // The range is empty, fill it with a default value.
985 pos = action.Infill(accesses, pos, range);
986 } else if (range.begin < pos->first.begin) {
987 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -0700988 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -0600989 } else if (pos->first.begin < range.begin) {
990 // Trim the beginning if needed
991 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
992 ++pos;
993 }
994
995 const auto the_end = accesses->end();
996 while ((pos != the_end) && pos->first.intersects(range)) {
997 if (pos->first.end > range.end) {
998 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
999 }
1000
1001 pos = action(accesses, pos);
1002 if (pos == the_end) break;
1003
1004 auto next = pos;
1005 ++next;
1006 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1007 // Need to infill if next is disjoint
1008 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001009 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001010 next = action.Infill(accesses, next, new_range);
1011 }
1012 pos = next;
1013 }
1014}
1015
1016struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001017 using Iterator = ResourceAccessRangeMap::iterator;
1018 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001019 // this is only called on gaps, and never returns a gap.
1020 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001021 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001022 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001023 }
John Zulauf5f13a792020-03-10 07:31:21 -06001024
John Zulauf5c5e88d2019-12-26 11:22:02 -07001025 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001026 auto &access_state = pos->second;
1027 access_state.Update(usage, tag);
1028 return pos;
1029 }
1030
John Zulauf16adfc92020-04-08 10:28:33 -06001031 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001032 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001033 : type(type_), context(context_), usage(usage_), tag(tag_) {}
1034 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001035 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001036 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001037 const ResourceUsageTag &tag;
1038};
1039
1040struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001041 using Iterator = ResourceAccessRangeMap::iterator;
1042 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001043
John Zulauf5c5e88d2019-12-26 11:22:02 -07001044 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001045 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001046 access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001047 return pos;
1048 }
1049
John Zulauf36bcf6a2020-02-03 15:12:52 -07001050 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
1051 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
1052 : src_exec_scope(src_exec_scope_),
1053 src_access_scope(src_access_scope_),
1054 dst_exec_scope(dst_exec_scope_),
1055 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001056
John Zulauf36bcf6a2020-02-03 15:12:52 -07001057 VkPipelineStageFlags src_exec_scope;
1058 SyncStageAccessFlags src_access_scope;
1059 VkPipelineStageFlags dst_exec_scope;
1060 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001061};
1062
1063struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001064 using Iterator = ResourceAccessRangeMap::iterator;
1065 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001066
John Zulauf5c5e88d2019-12-26 11:22:02 -07001067 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001068 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001069 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001070
1071 for (const auto &functor : barrier_functor) {
1072 functor(accesses, pos);
1073 }
1074 return pos;
1075 }
1076
John Zulauf36bcf6a2020-02-03 15:12:52 -07001077 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
1078 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -06001079 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001080 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001081 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
1082 barrier_functor.reserve(memoryBarrierCount);
1083 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
1084 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -07001085 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
1086 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -06001087 }
1088 }
1089
John Zulauf36bcf6a2020-02-03 15:12:52 -07001090 const VkPipelineStageFlags src_exec_scope;
1091 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001092 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
1093};
1094
John Zulauf355e49b2020-04-24 15:11:15 -06001095void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1096 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001097 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1098 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001099}
1100
John Zulauf16adfc92020-04-08 10:28:33 -06001101void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001102 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001103 if (!SimpleBinding(buffer)) return;
1104 const auto base_address = ResourceBaseAddress(buffer);
1105 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1106}
John Zulauf355e49b2020-04-24 15:11:15 -06001107
John Zulauf540266b2020-04-06 18:54:53 -06001108void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001109 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001110 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001111 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001112 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001113 const auto address_type = ImageAddressType(image);
1114 const auto base_address = ResourceBaseAddress(image);
1115 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001116 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001117 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001118 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001119}
John Zulauf7635de32020-05-29 17:14:15 -06001120void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1121 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1122 if (view != nullptr) {
1123 const IMAGE_STATE *image = view->image_state.get();
1124 if (image != nullptr) {
1125 auto *update_range = &view->normalized_subresource_range;
1126 VkImageSubresourceRange masked_range;
1127 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1128 masked_range = view->normalized_subresource_range;
1129 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1130 update_range = &masked_range;
1131 }
1132 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1133 }
1134 }
1135}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001136
John Zulauf355e49b2020-04-24 15:11:15 -06001137void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1138 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1139 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001140 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1141 subresource.layerCount};
1142 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1143}
1144
John Zulauf540266b2020-04-06 18:54:53 -06001145template <typename Action>
1146void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001147 if (!SimpleBinding(buffer)) return;
1148 const auto base_address = ResourceBaseAddress(buffer);
1149 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001150}
1151
1152template <typename Action>
1153void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1154 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001155 if (!SimpleBinding(image)) return;
1156 const auto address_type = ImageAddressType(image);
1157 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001158
locke-lunargae26eac2020-04-16 15:29:05 -06001159 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001160 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001161
John Zulauf16adfc92020-04-08 10:28:33 -06001162 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001163 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001164 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001165 }
1166}
1167
John Zulauf7635de32020-05-29 17:14:15 -06001168void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1169 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1170 const ResourceUsageTag &tag) {
1171 UpdateStateResolveAction update(*this, tag);
1172 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1173}
1174
John Zulaufaff20662020-06-01 14:07:58 -06001175void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1176 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1177 const ResourceUsageTag &tag) {
1178 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1179 VkExtent3D extent = CastTo3D(render_area.extent);
1180 VkOffset3D offset = CastTo3D(render_area.offset);
1181
1182 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1183 if (rp_state.attachment_last_subpass[i] == subpass) {
1184 if (attachment_views[i] == nullptr) continue; // UNUSED
1185 const auto &view = *attachment_views[i];
1186 const IMAGE_STATE *image = view.image_state.get();
1187 if (image == nullptr) continue;
1188
1189 const auto &ci = attachment_ci[i];
1190 const bool has_depth = FormatHasDepth(ci.format);
1191 const bool has_stencil = FormatHasStencil(ci.format);
1192 const bool is_color = !(has_depth || has_stencil);
1193 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1194
1195 if (is_color && store_op_stores) {
1196 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1197 offset, extent, tag);
1198 } else {
1199 auto update_range = view.normalized_subresource_range;
1200 if (has_depth && store_op_stores) {
1201 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1202 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1203 tag);
1204 }
1205 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1206 if (has_stencil && stencil_op_stores) {
1207 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1208 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1209 tag);
1210 }
1211 }
1212 }
1213 }
1214}
1215
John Zulauf540266b2020-04-06 18:54:53 -06001216template <typename Action>
1217void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1218 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001219 for (const auto address_type : kAddressTypes) {
1220 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001221 }
1222}
1223
1224void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001225 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1226 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -06001227 for (const auto address_type : kAddressTypes) {
John Zulauf355e49b2020-04-24 15:11:15 -06001228 context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier,
1229 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001230 }
1231 }
1232}
1233
John Zulauf355e49b2020-04-24 15:11:15 -06001234void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1235 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1236 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
1237 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1238 UpdateMemoryAccess(image, subresource_range, barrier_action);
1239}
1240
John Zulauf7635de32020-05-29 17:14:15 -06001241// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001242void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1243 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1244 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
1245 bool layout_transition, const ResourceUsageTag &tag) {
1246 if (layout_transition) {
1247 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
1248 tag);
1249 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
1250 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -06001251 } else {
1252 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -06001253 }
John Zulauf355e49b2020-04-24 15:11:15 -06001254}
1255
John Zulauf7635de32020-05-29 17:14:15 -06001256// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001257void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
1258 const VkImageSubresourceRange &subresource_range, bool layout_transition,
1259 const ResourceUsageTag &tag) {
1260 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
1261 subresource_range, layout_transition, tag);
1262}
1263
1264// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001265HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001266 if (!attach_view) return HazardResult();
1267 const auto image_state = attach_view->image_state.get();
1268 if (!image_state) return HazardResult();
1269
John Zulauf355e49b2020-04-24 15:11:15 -06001270 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001271 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001272
1273 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulauf7635de32020-05-29 17:14:15 -06001274 auto hazard = track_back.context->DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope,
1275 track_back.barrier.src_access_scope,
1276 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001277 if (!hazard.hazard) {
1278 // The Async hazard check is against the current context's async set.
John Zulauf7635de32020-05-29 17:14:15 -06001279 hazard = DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope, track_back.barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001280 attach_view->normalized_subresource_range, kDetectAsync);
1281 }
1282 return hazard;
1283}
1284
1285// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1286bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1287
1288 const VkRenderPassBeginInfo *pRenderPassBegin,
1289 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1290 const char *func_name) const {
1291 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1292 bool skip = false;
1293 uint32_t subpass = 0;
1294 const auto &transitions = rp_state.subpass_transitions[subpass];
1295 if (transitions.size()) {
1296 const std::vector<AccessContext> empty_context_vector;
1297 // Create context we can use to validate against...
1298 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1299 const_cast<AccessContext *>(&cb_access_context_));
1300
1301 assert(pRenderPassBegin);
1302 if (nullptr == pRenderPassBegin) return skip;
1303
1304 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1305 assert(fb_state);
1306 if (nullptr == fb_state) return skip;
1307
1308 // Create a limited array of views (which we'll need to toss
1309 std::vector<const IMAGE_VIEW_STATE *> views;
1310 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
1311 const auto attachment_count = count_attachment.first;
1312 const auto *attachments = count_attachment.second;
1313 views.resize(attachment_count, nullptr);
1314 for (const auto &transition : transitions) {
1315 assert(transition.attachment < attachment_count);
1316 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
1317 }
1318
John Zulauf7635de32020-05-29 17:14:15 -06001319 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
1320 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001321 }
1322 return skip;
1323}
1324
locke-lunarg61870c22020-06-09 14:51:50 -06001325bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1326 const char *func_name) const {
1327 bool skip = false;
1328 const PIPELINE_STATE *pPipe = nullptr;
1329 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1330 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1331 if (!pPipe || !per_sets) {
1332 return skip;
1333 }
1334
1335 using DescriptorClass = cvdescriptorset::DescriptorClass;
1336 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1337 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1338 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1339 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1340
1341 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarg37047832020-06-12 13:44:45 -06001342 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001343 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1344 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001345 for (const auto &set_binding : stage_state.descriptor_uses) {
1346 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1347 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1348 set_binding.first.second);
1349 const auto descriptor_type = binding_it.GetType();
1350 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1351 auto array_idx = 0;
1352
1353 if (binding_it.IsVariableDescriptorCount()) {
1354 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1355 }
1356 SyncStageAccessIndex sync_index =
1357 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1358
1359 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1360 uint32_t index = i - index_range.start;
1361 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1362 switch (descriptor->GetClass()) {
1363 case DescriptorClass::ImageSampler:
1364 case DescriptorClass::Image: {
1365 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1366 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1367 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1368 } else {
1369 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1370 }
1371 if (!img_view_state) continue;
1372 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1373 VkExtent3D extent = {};
1374 VkOffset3D offset = {};
1375 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1376 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1377 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1378 } else {
1379 extent = img_state->createInfo.extent;
1380 }
1381 auto hazard = current_context_->DetectHazard(*img_state, sync_index,
1382 img_view_state->normalized_subresource_range, offset, extent);
1383 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06001384 skip |= sync_state_->LogError(
1385 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1386 "%s: Hazard %s for %s in %s, %s, and %s binding #%" PRIu32 " index %" PRIu32 ". Prior access %s.",
1387 func_name, string_SyncHazard(hazard.hazard),
1388 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1389 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1390 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1391 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(), set_binding.first.second,
1392 index, string_UsageTag(hazard.tag).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001393 }
1394 break;
1395 }
1396 case DescriptorClass::TexelBuffer: {
1397 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1398 if (!buf_view_state) continue;
1399 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1400 ResourceAccessRange range =
1401 MakeRange(buf_view_state->create_info.offset,
1402 GetRealWholeSize(buf_view_state->create_info.offset, buf_view_state->create_info.range,
1403 buf_state->createInfo.size));
1404 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
1405 if (hazard.hazard) {
1406 skip |=
1407 sync_state_->LogError(buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
1408 "%s: Hazard %s for %s in %s, %s, and %s binding #%d index %d", func_name,
1409 string_SyncHazard(hazard.hazard),
1410 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1411 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1412 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1413 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1414 set_binding.first.second, index);
1415 }
1416 break;
1417 }
1418 case DescriptorClass::GeneralBuffer: {
1419 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1420 auto buf_state = buffer_descriptor->GetBufferState();
1421 if (!buf_state) continue;
1422 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1423 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
1424 if (hazard.hazard) {
1425 skip |= sync_state_->LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
1426 "%s: Hazard %s for %s in %s, %s, and %s binding #%d index %d", func_name,
1427 string_SyncHazard(hazard.hazard),
1428 sync_state_->report_data->FormatHandle(buf_state->buffer).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(),
1432 set_binding.first.second, index);
1433 }
1434 break;
1435 }
1436 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1437 default:
1438 break;
1439 }
1440 }
1441 }
1442 }
1443 return skip;
1444}
1445
1446void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1447 const ResourceUsageTag &tag) {
1448 const PIPELINE_STATE *pPipe = nullptr;
1449 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1450 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1451 if (!pPipe || !per_sets) {
1452 return;
1453 }
1454
1455 using DescriptorClass = cvdescriptorset::DescriptorClass;
1456 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1457 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1458 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1459 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1460
1461 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001462 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
1463 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1464 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001465 for (const auto &set_binding : stage_state.descriptor_uses) {
1466 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1467 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1468 set_binding.first.second);
1469 const auto descriptor_type = binding_it.GetType();
1470 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1471 auto array_idx = 0;
1472
1473 if (binding_it.IsVariableDescriptorCount()) {
1474 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1475 }
1476 SyncStageAccessIndex sync_index =
1477 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1478
1479 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1480 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1481 switch (descriptor->GetClass()) {
1482 case DescriptorClass::ImageSampler:
1483 case DescriptorClass::Image: {
1484 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1485 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1486 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1487 } else {
1488 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1489 }
1490 if (!img_view_state) continue;
1491 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1492 VkExtent3D extent = {};
1493 VkOffset3D offset = {};
1494 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1495 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1496 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1497 } else {
1498 extent = img_state->createInfo.extent;
1499 }
1500 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1501 offset, extent, tag);
1502 break;
1503 }
1504 case DescriptorClass::TexelBuffer: {
1505 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1506 if (!buf_view_state) continue;
1507 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1508 ResourceAccessRange range =
1509 MakeRange(buf_view_state->create_info.offset, buf_view_state->create_info.range);
1510 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1511 break;
1512 }
1513 case DescriptorClass::GeneralBuffer: {
1514 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1515 auto buf_state = buffer_descriptor->GetBufferState();
1516 if (!buf_state) continue;
1517 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1518 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1519 break;
1520 }
1521 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1522 default:
1523 break;
1524 }
1525 }
1526 }
1527 }
1528}
1529
1530bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1531 bool skip = false;
1532 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1533 if (!pPipe) {
1534 return skip;
1535 }
1536
1537 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1538 const auto &binding_buffers_size = binding_buffers.size();
1539 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1540
1541 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1542 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1543 if (binding_description.binding < binding_buffers_size) {
1544 const auto &binding_buffer = binding_buffers[binding_description.binding];
1545 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1546
1547 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1548 VkDeviceSize range_start = 0;
1549 VkDeviceSize range_size = 0;
1550 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1551 binding_description.stride);
1552 ResourceAccessRange range = MakeRange(range_start, range_size);
1553 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1554 if (hazard.hazard) {
1555 skip |= sync_state_->LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
1556 "%s: Hazard %s for vertex %s in %s", func_name, string_SyncHazard(hazard.hazard),
1557 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
1558 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str());
1559 }
1560 }
1561 }
1562 return skip;
1563}
1564
1565void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1566 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1567 if (!pPipe) {
1568 return;
1569 }
1570 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1571 const auto &binding_buffers_size = binding_buffers.size();
1572 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1573
1574 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1575 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1576 if (binding_description.binding < binding_buffers_size) {
1577 const auto &binding_buffer = binding_buffers[binding_description.binding];
1578 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1579
1580 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1581 VkDeviceSize range_start = 0;
1582 VkDeviceSize range_size = 0;
1583 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1584 binding_description.stride);
1585 ResourceAccessRange range = MakeRange(range_start, range_size);
1586 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1587 }
1588 }
1589}
1590
1591bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1592 bool skip = false;
1593 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
1594
1595 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1596 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1597 VkDeviceSize range_start = 0;
1598 VkDeviceSize range_size = 0;
1599 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1600 indexCount, index_size);
1601 ResourceAccessRange range = MakeRange(range_start, range_size);
1602 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1603 if (hazard.hazard) {
1604 skip |= sync_state_->LogError(index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
1605 "%s: Hazard %s for index %s in %s", func_name, string_SyncHazard(hazard.hazard),
1606 sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
1607 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str());
1608 }
1609
1610 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1611 // We will detect more accurate range in the future.
1612 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1613 return skip;
1614}
1615
1616void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
1617 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return;
1618
1619 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1620 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1621 VkDeviceSize range_start = 0;
1622 VkDeviceSize range_size = 0;
1623 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1624 indexCount, index_size);
1625 ResourceAccessRange range = MakeRange(range_start, range_size);
1626 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
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 RecordDrawVertex(UINT32_MAX, 0, tag);
1631}
1632
1633bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
1634 return current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1635 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1636}
1637
1638void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001639 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1640 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001641}
1642
John Zulauf355e49b2020-04-24 15:11:15 -06001643bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001644 bool skip = false;
John Zulauf1507ee42020-05-18 11:33:09 -06001645 skip |=
1646 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001647
1648 return skip;
1649}
1650
1651bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1652 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001653 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001654 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -06001655 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1656 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001657
1658 return skip;
1659}
1660
1661void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1662 assert(sync_state_);
1663 if (!cb_state_) return;
1664
1665 // Create an access context the current renderpass.
1666 render_pass_contexts_.emplace_back(&cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06001667 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf355e49b2020-04-24 15:11:15 -06001668 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001669 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001670}
1671
John Zulauf355e49b2020-04-24 15:11:15 -06001672void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001673 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001674 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001675 current_context_ = &current_renderpass_context_->CurrentContext();
1676}
1677
John Zulauf355e49b2020-04-24 15:11:15 -06001678void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001679 assert(current_renderpass_context_);
1680 if (!current_renderpass_context_) return;
1681
John Zulauf7635de32020-05-29 17:14:15 -06001682 current_renderpass_context_->RecordEndRenderPass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001683 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001684 current_renderpass_context_ = nullptr;
1685}
1686
locke-lunarg61870c22020-06-09 14:51:50 -06001687bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1688 const VkRect2D &render_area, const char *func_name) const {
1689 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001690 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001691 if (!pPipe ||
1692 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001693 return skip;
1694 }
1695 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001696 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1697 VkExtent3D extent = CastTo3D(render_area.extent);
1698 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001699
locke-lunarg44f9bb12020-06-10 14:43:57 -06001700 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001701 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1702 for (const auto location : list) {
1703 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1704 continue;
1705 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
1706 HazardResult hazard = external_context_->DetectHazard(
1707 img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent);
1708 if (hazard.hazard) {
1709 skip |= sync_state.LogError(
1710 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1711 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d", func_name,
1712 string_SyncHazard(hazard.hazard), sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1713 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass, location);
locke-lunarg61870c22020-06-09 14:51:50 -06001714 }
1715 }
1716 }
locke-lunarg37047832020-06-12 13:44:45 -06001717
1718 // PHASE1 TODO: Add layout based read/vs. write selection.
1719 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1720 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1721 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001722 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001723 bool depth_write = false, stencil_write = false;
1724
1725 // PHASE1 TODO: These validation should be in core_checks.
1726 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1727 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1728 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1729 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1730 depth_write = true;
1731 }
1732 // PHASE1 TODO: It needs to check if stencil is writable.
1733 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1734 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1735 // PHASE1 TODO: These validation should be in core_checks.
1736 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1737 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1738 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1739 stencil_write = true;
1740 }
1741
1742 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1743 if (depth_write) {
1744 HazardResult hazard =
1745 external_context_->DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1746 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
1747 if (hazard.hazard) {
1748 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1749 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment",
1750 func_name, string_SyncHazard(hazard.hazard),
1751 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1752 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass);
1753 }
1754 }
1755 if (stencil_write) {
1756 HazardResult hazard =
1757 external_context_->DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1758 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
1759 if (hazard.hazard) {
1760 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1761 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment",
1762 func_name, string_SyncHazard(hazard.hazard),
1763 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1764 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass);
1765 }
locke-lunarg61870c22020-06-09 14:51:50 -06001766 }
1767 }
1768 return skip;
1769}
1770
locke-lunarg96dc9632020-06-10 17:22:18 -06001771void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1772 const ResourceUsageTag &tag) {
1773 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001774 if (!pPipe ||
1775 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001776 return;
1777 }
1778 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001779 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1780 VkExtent3D extent = CastTo3D(render_area.extent);
1781 VkOffset3D offset = CastTo3D(render_area.offset);
1782
locke-lunarg44f9bb12020-06-10 14:43:57 -06001783 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001784 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1785 for (const auto location : list) {
1786 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1787 continue;
1788 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
1789 external_context_->UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset,
1790 extent, 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001791 }
1792 }
locke-lunarg37047832020-06-12 13:44:45 -06001793
1794 // PHASE1 TODO: Add layout based read/vs. write selection.
1795 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1796 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1797 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001798 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001799 bool depth_write = false, stencil_write = false;
1800
1801 // PHASE1 TODO: These validation should be in core_checks.
1802 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1803 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1804 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1805 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1806 depth_write = true;
1807 }
1808 // PHASE1 TODO: It needs to check if stencil is writable.
1809 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1810 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1811 // PHASE1 TODO: These validation should be in core_checks.
1812 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1813 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1814 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1815 stencil_write = true;
1816 }
1817
1818 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1819 if (depth_write) {
1820 external_context_->UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1821 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
1822 }
1823 if (stencil_write) {
1824 external_context_->UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1825 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
1826 }
locke-lunarg61870c22020-06-09 14:51:50 -06001827 }
1828}
1829
John Zulauf1507ee42020-05-18 11:33:09 -06001830bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1831 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001832 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001833 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001834 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1835 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001836 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1837 func_name);
1838
John Zulauf355e49b2020-04-24 15:11:15 -06001839 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06001840 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06001841 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1842 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1843 return skip;
1844}
1845bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
1846 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001847 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06001848 bool skip = false;
1849 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1850 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001851 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1852 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06001853 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001854 return skip;
1855}
1856
John Zulauf7635de32020-05-29 17:14:15 -06001857AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
1858 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
1859}
1860
1861bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
1862 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001863 bool skip = false;
1864
John Zulauf7635de32020-05-29 17:14:15 -06001865 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
1866 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
1867 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1868 // to apply and only copy then, if this proves a hot spot.
1869 std::unique_ptr<AccessContext> proxy_for_current;
1870
John Zulauf355e49b2020-04-24 15:11:15 -06001871 // Validate the "finalLayout" transitions to external
1872 // Get them from where there we're hidding in the extra entry.
1873 const auto &final_transitions = rp_state_->subpass_transitions.back();
1874 for (const auto &transition : final_transitions) {
1875 const auto &attach_view = attachment_views_[transition.attachment];
1876 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
1877 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06001878 auto *context = trackback.context;
1879
1880 if (transition.prev_pass == current_subpass_) {
1881 if (!proxy_for_current) {
1882 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
1883 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
1884 }
1885 context = proxy_for_current.get();
1886 }
1887
1888 auto hazard = context->DetectImageBarrierHazard(
John Zulauf355e49b2020-04-24 15:11:15 -06001889 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
1890 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
1891 if (hazard.hazard) {
1892 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
1893 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf1dae9192020-06-16 15:46:44 -06001894 " final image layout transition. Prior access %s.",
1895 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
1896 string_UsageTag(hazard.tag).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001897 }
1898 }
1899 return skip;
1900}
1901
1902void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
1903 // Add layout transitions...
1904 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
1905 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06001906 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06001907 for (const auto &transition : transitions) {
1908 const auto attachment_view = attachment_views_[transition.attachment];
1909 if (!attachment_view) continue;
1910 const auto image = attachment_view->image_state.get();
1911 if (!image) continue;
1912
1913 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -06001914 auto insert_pair = view_seen.insert(attachment_view);
1915 if (insert_pair.second) {
1916 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
1917 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
1918
1919 } else {
1920 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
1921 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
1922 auto barrier_to_transition = barrier->barrier;
1923 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
1924 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
1925 }
John Zulauf355e49b2020-04-24 15:11:15 -06001926 }
1927}
1928
John Zulauf1507ee42020-05-18 11:33:09 -06001929void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
1930 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
1931 auto &subpass_context = subpass_contexts_[current_subpass_];
1932 VkExtent3D extent = CastTo3D(render_area.extent);
1933 VkOffset3D offset = CastTo3D(render_area.offset);
1934
1935 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
1936 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
1937 if (attachment_views_[i] == nullptr) continue; // UNUSED
1938 const auto &view = *attachment_views_[i];
1939 const IMAGE_STATE *image = view.image_state.get();
1940 if (image == nullptr) continue;
1941
1942 const auto &ci = attachment_ci[i];
1943 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001944 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001945 const bool is_color = !(has_depth || has_stencil);
1946
1947 if (is_color) {
1948 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
1949 extent, tag);
1950 } else {
1951 auto update_range = view.normalized_subresource_range;
1952 if (has_depth) {
1953 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1954 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
1955 }
1956 if (has_stencil) {
1957 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1958 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
1959 tag);
1960 }
1961 }
1962 }
1963 }
1964}
1965
John Zulauf355e49b2020-04-24 15:11:15 -06001966void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
1967 VkQueueFlags queue_flags, const ResourceUsageTag &tag) {
1968 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06001969 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06001970 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
1971 // Add this for all subpasses here so that they exsist during next subpass validation
1972 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
1973 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context_);
1974 }
1975 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
1976
1977 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06001978 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001979}
John Zulauf1507ee42020-05-18 11:33:09 -06001980
1981void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001982 // Resolves are against *prior* subpass context and thus *before* the subpass increment
1983 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001984 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06001985
John Zulauf355e49b2020-04-24 15:11:15 -06001986 current_subpass_++;
1987 assert(current_subpass_ < subpass_contexts_.size());
1988 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06001989 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001990}
1991
John Zulauf7635de32020-05-29 17:14:15 -06001992void RenderPassAccessContext::RecordEndRenderPass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001993 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06001994 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001995 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06001996
John Zulauf355e49b2020-04-24 15:11:15 -06001997 // Export the accesses from the renderpass...
1998 external_context_->ResolveChildContexts(subpass_contexts_);
1999
2000 // Add the "finalLayout" transitions to external
2001 // Get them from where there we're hidding in the extra entry.
2002 const auto &final_transitions = rp_state_->subpass_transitions.back();
2003 for (const auto &transition : final_transitions) {
2004 const auto &attachment = attachment_views_[transition.attachment];
2005 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2006 assert(external_context_ == last_trackback.context);
2007 external_context_->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
2008 attachment->normalized_subresource_range, true, tag);
2009 }
2010}
2011
John Zulauf3d84f1b2020-03-09 13:33:25 -06002012SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2013 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2014 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2015 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2016 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2017 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2018 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2019}
2020
2021void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
2022 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
2023 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
2024}
2025
John Zulauf9cb530d2019-09-30 14:14:10 -06002026HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2027 HazardResult hazard;
2028 auto usage = FlagBit(usage_index);
2029 if (IsRead(usage)) {
John Zulaufc9201222020-05-13 15:13:03 -06002030 if (last_write && IsWriteHazard(usage)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002031 hazard.Set(READ_AFTER_WRITE, write_tag);
2032 }
2033 } else {
2034 // Assume write
2035 // TODO determine what to do with READ-WRITE usage states if any
2036 // Write-After-Write check -- if we have a previous write to test against
2037 if (last_write && IsWriteHazard(usage)) {
2038 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2039 } else {
John Zulauf69133422020-05-20 14:55:53 -06002040 // Look for casus belli for WAR
John Zulauf9cb530d2019-09-30 14:14:10 -06002041 const auto usage_stage = PipelineStageBit(usage_index);
2042 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2043 if (IsReadHazard(usage_stage, last_reads[read_index])) {
2044 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
2045 break;
2046 }
2047 }
2048 }
2049 }
2050 return hazard;
2051}
2052
John Zulauf69133422020-05-20 14:55:53 -06002053HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2054 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2055 HazardResult hazard;
2056 const auto usage = FlagBit(usage_index);
2057 const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good.
2058 if (IsRead(usage)) {
2059 if (!write_is_ordered && IsWriteHazard(usage)) {
2060 hazard.Set(READ_AFTER_WRITE, write_tag);
2061 }
2062 } else {
2063 if (!write_is_ordered && IsWriteHazard(usage)) {
2064 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2065 } else {
2066 const auto usage_stage = PipelineStageBit(usage_index);
2067 const auto unordered_reads = last_read_stages & ~ordering.exec_scope;
2068 if (unordered_reads) {
2069 // Look for any WAR hazards outside the ordered set of stages
2070 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2071 if (last_reads[read_index].stage & unordered_reads) {
2072 if (IsReadHazard(usage_stage, last_reads[read_index])) {
2073 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
2074 break;
2075 }
2076 }
2077 }
2078 }
2079 }
2080 }
2081 return hazard;
2082}
2083
John Zulauf2f952d22020-02-10 11:34:51 -07002084// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002085HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002086 HazardResult hazard;
2087 auto usage = FlagBit(usage_index);
2088 if (IsRead(usage)) {
2089 if (last_write != 0) {
2090 hazard.Set(READ_RACING_WRITE, write_tag);
2091 }
2092 } else {
2093 if (last_write != 0) {
2094 hazard.Set(WRITE_RACING_WRITE, write_tag);
2095 } else if (last_read_count > 0) {
2096 hazard.Set(WRITE_RACING_READ, last_reads[0].tag);
2097 }
2098 }
2099 return hazard;
2100}
2101
John Zulauf36bcf6a2020-02-03 15:12:52 -07002102HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2103 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002104 // Only supporting image layout transitions for now
2105 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2106 HazardResult hazard;
2107 if (last_write) {
2108 // If the previous write is *not* in the 1st access scope
2109 // *AND* the current barrier is not in the dependency chain
2110 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2111 // then the barrier access is unsafe (R/W after W)
John Zulauf36bcf6a2020-02-03 15:12:52 -07002112 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
John Zulauf0cb5be22020-01-23 12:18:22 -07002113 // TODO: Do we need a difference hazard name for this?
2114 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2115 }
John Zulauf355e49b2020-04-24 15:11:15 -06002116 }
2117 if (!hazard.hazard) {
2118 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002119 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002120 const auto &read_access = last_reads[read_index];
2121 // If the read stage is not in the src sync sync
2122 // *AND* not execution chained with an existing sync barrier (that's the or)
2123 // then the barrier access is unsafe (R/W after R)
2124 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
2125 hazard.Set(WRITE_AFTER_READ, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002126 break;
2127 }
2128 }
2129 }
2130 return hazard;
2131}
2132
John Zulauf5f13a792020-03-10 07:31:21 -06002133// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2134// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2135// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2136void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2137 if (write_tag.IsBefore(other.write_tag)) {
2138 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
2139 *this = other;
2140 } else if (!other.write_tag.IsBefore(write_tag)) {
2141 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2142 // dependency chaining logic or any stage expansion)
2143 write_barriers |= other.write_barriers;
2144
2145 // Merge that read states
2146 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2147 auto &other_read = other.last_reads[other_read_index];
2148 if (last_read_stages & other_read.stage) {
2149 // Merge in the barriers for read stages that exist in *both* this and other
2150 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
2151 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
2152 auto &my_read = last_reads[my_read_index];
2153 if (other_read.stage == my_read.stage) {
2154 if (my_read.tag.IsBefore(other_read.tag)) {
2155 my_read.tag = other_read.tag;
2156 }
2157 my_read.barriers |= other_read.barriers;
2158 break;
2159 }
2160 }
2161 } else {
2162 // The other read stage doesn't exist in this, so add it.
2163 last_reads[last_read_count] = other_read;
2164 last_read_count++;
2165 last_read_stages |= other_read.stage;
2166 }
2167 }
2168 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
2169 // it.
2170}
2171
John Zulauf9cb530d2019-09-30 14:14:10 -06002172void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2173 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2174 const auto usage_bit = FlagBit(usage_index);
2175 if (IsRead(usage_index)) {
2176 // Mulitple outstanding reads may be of interest and do dependency chains independently
2177 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2178 const auto usage_stage = PipelineStageBit(usage_index);
2179 if (usage_stage & last_read_stages) {
2180 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2181 ReadState &access = last_reads[read_index];
2182 if (access.stage == usage_stage) {
2183 access.barriers = 0;
2184 access.tag = tag;
2185 break;
2186 }
2187 }
2188 } else {
2189 // We don't have this stage in the list yet...
2190 assert(last_read_count < last_reads.size());
2191 ReadState &access = last_reads[last_read_count++];
2192 access.stage = usage_stage;
2193 access.barriers = 0;
2194 access.tag = tag;
2195 last_read_stages |= usage_stage;
2196 }
2197 } else {
2198 // Assume write
2199 // TODO determine what to do with READ-WRITE operations if any
2200 // Clobber last read and both sets of barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2201 // if the last_reads/last_write were unsafe, we've reported them,
2202 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
2203 last_read_count = 0;
2204 last_read_stages = 0;
2205
2206 write_barriers = 0;
2207 write_dependency_chain = 0;
2208 write_tag = tag;
2209 last_write = usage_bit;
2210 }
2211}
John Zulauf5f13a792020-03-10 07:31:21 -06002212
John Zulauf9cb530d2019-09-30 14:14:10 -06002213void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
2214 // Execution Barriers only protect read operations
2215 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2216 ReadState &access = last_reads[read_index];
2217 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2218 if (srcStageMask & (access.stage | access.barriers)) {
2219 access.barriers |= dstStageMask;
2220 }
2221 }
2222 if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask;
2223}
2224
John Zulauf36bcf6a2020-02-03 15:12:52 -07002225void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
2226 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002227 // Assuming we've applied the execution side of this barrier, we update just the write
2228 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07002229 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
2230 write_barriers |= dst_access_scope;
2231 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06002232 }
2233}
2234
John Zulaufd1f85d42020-04-15 12:23:15 -06002235void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002236 auto *access_context = GetAccessContextNoInsert(command_buffer);
2237 if (access_context) {
2238 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002239 }
2240}
2241
John Zulaufd1f85d42020-04-15 12:23:15 -06002242void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2243 auto access_found = cb_access_state.find(command_buffer);
2244 if (access_found != cb_access_state.end()) {
2245 access_found->second->Reset();
2246 cb_access_state.erase(access_found);
2247 }
2248}
2249
John Zulauf540266b2020-04-06 18:54:53 -06002250void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002251 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
2252 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002253 const VkMemoryBarrier *pMemoryBarriers) {
2254 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07002255 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002256 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002257 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002258}
2259
John Zulauf540266b2020-04-06 18:54:53 -06002260void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002261 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2262 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002263 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002264 for (uint32_t index = 0; index < barrier_count; index++) {
locke-lunarg3c038002020-04-30 23:08:08 -06002265 auto barrier = barriers[index];
John Zulauf9cb530d2019-09-30 14:14:10 -06002266 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2267 if (!buffer) continue;
locke-lunarg3c038002020-04-30 23:08:08 -06002268 barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002269 ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002270 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2271 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2272 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2273 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002274 }
2275}
2276
John Zulauf540266b2020-04-06 18:54:53 -06002277void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2278 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2279 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002280 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002281 for (uint32_t index = 0; index < barrier_count; index++) {
2282 const auto &barrier = barriers[index];
2283 const auto *image = Get<IMAGE_STATE>(barrier.image);
2284 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002285 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002286 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2287 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2288 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2289 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
2290 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002291 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002292}
2293
2294bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2295 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2296 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002297 const auto *cb_context = GetAccessContext(commandBuffer);
2298 assert(cb_context);
2299 if (!cb_context) return skip;
2300 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002301
John Zulauf3d84f1b2020-03-09 13:33:25 -06002302 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002303 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002304 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002305
2306 for (uint32_t region = 0; region < regionCount; region++) {
2307 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002308 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002309 ResourceAccessRange src_range = MakeRange(
2310 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002311 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002312 if (hazard.hazard) {
2313 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002314 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002315 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Prior access %s.",
2316 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
2317 string_UsageTag(hazard.tag).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002318 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002319 }
John Zulauf16adfc92020-04-08 10:28:33 -06002320 if (dst_buffer && !skip) {
locke-lunargff255f92020-05-13 18:53:52 -06002321 ResourceAccessRange dst_range = MakeRange(
2322 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf355e49b2020-04-24 15:11:15 -06002323 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002324 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002325 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002326 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Prior access %s.",
2327 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
2328 string_UsageTag(hazard.tag).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002329 }
2330 }
2331 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002332 }
2333 return skip;
2334}
2335
2336void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2337 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002338 auto *cb_context = GetAccessContext(commandBuffer);
2339 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002340 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002341 auto *context = cb_context->GetCurrentAccessContext();
2342
John Zulauf9cb530d2019-09-30 14:14:10 -06002343 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002344 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002345
2346 for (uint32_t region = 0; region < regionCount; region++) {
2347 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002348 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002349 ResourceAccessRange src_range = MakeRange(
2350 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002351 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002352 }
John Zulauf16adfc92020-04-08 10:28:33 -06002353 if (dst_buffer) {
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 Zulauf16adfc92020-04-08 10:28:33 -06002356 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002357 }
2358 }
2359}
2360
2361bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2362 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2363 const VkImageCopy *pRegions) const {
2364 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002365 const auto *cb_access_context = GetAccessContext(commandBuffer);
2366 assert(cb_access_context);
2367 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002368
John Zulauf3d84f1b2020-03-09 13:33:25 -06002369 const auto *context = cb_access_context->GetCurrentAccessContext();
2370 assert(context);
2371 if (!context) return skip;
2372
2373 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2374 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002375 for (uint32_t region = 0; region < regionCount; region++) {
2376 const auto &copy_region = pRegions[region];
2377 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002378 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002379 copy_region.srcOffset, copy_region.extent);
2380 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002381 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002382 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
2383 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
2384 string_UsageTag(hazard.tag).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002385 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002386 }
2387
2388 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002389 VkExtent3D dst_copy_extent =
2390 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002391 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002392 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002393 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002394 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002395 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
2396 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
2397 string_UsageTag(hazard.tag).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002398 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002399 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002400 }
2401 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002402
John Zulauf5c5e88d2019-12-26 11:22:02 -07002403 return skip;
2404}
2405
2406void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2407 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2408 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002409 auto *cb_access_context = GetAccessContext(commandBuffer);
2410 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002411 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002412 auto *context = cb_access_context->GetCurrentAccessContext();
2413 assert(context);
2414
John Zulauf5c5e88d2019-12-26 11:22:02 -07002415 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002416 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002417
2418 for (uint32_t region = 0; region < regionCount; region++) {
2419 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002420 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002421 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2422 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002423 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002424 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002425 VkExtent3D dst_copy_extent =
2426 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002427 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2428 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002429 }
2430 }
2431}
2432
2433bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2434 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2435 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2436 uint32_t bufferMemoryBarrierCount,
2437 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2438 uint32_t imageMemoryBarrierCount,
2439 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2440 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002441 const auto *cb_access_context = GetAccessContext(commandBuffer);
2442 assert(cb_access_context);
2443 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002444
John Zulauf3d84f1b2020-03-09 13:33:25 -06002445 const auto *context = cb_access_context->GetCurrentAccessContext();
2446 assert(context);
2447 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002448
John Zulauf3d84f1b2020-03-09 13:33:25 -06002449 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002450 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2451 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002452 // Validate Image Layout transitions
2453 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2454 const auto &barrier = pImageMemoryBarriers[index];
2455 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2456 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2457 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002458 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002459 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002460 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002461 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002462 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Prior access %s.",
2463 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
2464 string_UsageTag(hazard.tag).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002465 }
2466 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002467
2468 return skip;
2469}
2470
2471void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2472 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2473 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2474 uint32_t bufferMemoryBarrierCount,
2475 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2476 uint32_t imageMemoryBarrierCount,
2477 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002478 auto *cb_access_context = GetAccessContext(commandBuffer);
2479 assert(cb_access_context);
2480 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002481 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002482 auto access_context = cb_access_context->GetCurrentAccessContext();
2483 assert(access_context);
2484 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002485
John Zulauf3d84f1b2020-03-09 13:33:25 -06002486 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002487 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002488 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002489 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2490 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2491 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002492 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
2493 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002494 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06002495 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002496
2497 // 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 -06002498 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07002499 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06002500}
2501
2502void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
2503 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
2504 // The state tracker sets up the device state
2505 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
2506
John Zulauf5f13a792020-03-10 07:31:21 -06002507 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
2508 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06002509 // TODO: Find a good way to do this hooklessly.
2510 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
2511 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
2512 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
2513
John Zulaufd1f85d42020-04-15 12:23:15 -06002514 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2515 sync_device_state->ResetCommandBufferCallback(command_buffer);
2516 });
2517 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2518 sync_device_state->FreeCommandBufferCallback(command_buffer);
2519 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002520}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002521
John Zulauf355e49b2020-04-24 15:11:15 -06002522bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2523 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2524 bool skip = false;
2525 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2526 auto cb_context = GetAccessContext(commandBuffer);
2527
2528 if (rp_state && cb_context) {
2529 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2530 }
2531
2532 return skip;
2533}
2534
2535bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2536 VkSubpassContents contents) const {
2537 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2538 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2539 subpass_begin_info.contents = contents;
2540 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
2541 return skip;
2542}
2543
2544bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2545 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2546 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2547 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
2548 return skip;
2549}
2550
2551bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2552 const VkRenderPassBeginInfo *pRenderPassBegin,
2553 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2554 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2555 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
2556 return skip;
2557}
2558
John Zulauf3d84f1b2020-03-09 13:33:25 -06002559void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
2560 VkResult result) {
2561 // The state tracker sets up the command buffer state
2562 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
2563
2564 // Create/initialize the structure that trackers accesses at the command buffer scope.
2565 auto cb_access_context = GetAccessContext(commandBuffer);
2566 assert(cb_access_context);
2567 cb_access_context->Reset();
2568}
2569
2570void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06002571 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002572 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002573 if (cb_context) {
2574 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002575 }
2576}
2577
2578void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2579 VkSubpassContents contents) {
2580 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2581 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2582 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002583 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002584}
2585
2586void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2587 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2588 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002589 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002590}
2591
2592void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2593 const VkRenderPassBeginInfo *pRenderPassBegin,
2594 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2595 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002596 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
2597}
2598
2599bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2600 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
2601 bool skip = false;
2602
2603 auto cb_context = GetAccessContext(commandBuffer);
2604 assert(cb_context);
2605 auto cb_state = cb_context->GetCommandBufferState();
2606 if (!cb_state) return skip;
2607
2608 auto rp_state = cb_state->activeRenderPass;
2609 if (!rp_state) return skip;
2610
2611 skip |= cb_context->ValidateNextSubpass(func_name);
2612
2613 return skip;
2614}
2615
2616bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
2617 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
2618 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2619 subpass_begin_info.contents = contents;
2620 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
2621 return skip;
2622}
2623
2624bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2625 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2626 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2627 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
2628 return skip;
2629}
2630
2631bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2632 const VkSubpassEndInfo *pSubpassEndInfo) const {
2633 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2634 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
2635 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002636}
2637
2638void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06002639 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002640 auto cb_context = GetAccessContext(commandBuffer);
2641 assert(cb_context);
2642 auto cb_state = cb_context->GetCommandBufferState();
2643 if (!cb_state) return;
2644
2645 auto rp_state = cb_state->activeRenderPass;
2646 if (!rp_state) return;
2647
John Zulauf355e49b2020-04-24 15:11:15 -06002648 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002649}
2650
2651void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
2652 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
2653 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2654 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002655 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002656}
2657
2658void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2659 const VkSubpassEndInfo *pSubpassEndInfo) {
2660 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002661 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002662}
2663
2664void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2665 const VkSubpassEndInfo *pSubpassEndInfo) {
2666 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002667 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002668}
2669
John Zulauf355e49b2020-04-24 15:11:15 -06002670bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
2671 const char *func_name) const {
2672 bool skip = false;
2673
2674 auto cb_context = GetAccessContext(commandBuffer);
2675 assert(cb_context);
2676 auto cb_state = cb_context->GetCommandBufferState();
2677 if (!cb_state) return skip;
2678
2679 auto rp_state = cb_state->activeRenderPass;
2680 if (!rp_state) return skip;
2681
2682 skip |= cb_context->ValidateEndRenderpass(func_name);
2683 return skip;
2684}
2685
2686bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2687 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
2688 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
2689 return skip;
2690}
2691
2692bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
2693 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2694 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2695 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
2696 return skip;
2697}
2698
2699bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
2700 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2701 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2702 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
2703 return skip;
2704}
2705
2706void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
2707 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06002708 // Resolve the all subpass contexts to the command buffer contexts
2709 auto cb_context = GetAccessContext(commandBuffer);
2710 assert(cb_context);
2711 auto cb_state = cb_context->GetCommandBufferState();
2712 if (!cb_state) return;
2713
locke-lunargaecf2152020-05-12 17:15:41 -06002714 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06002715 if (!rp_state) return;
2716
John Zulauf355e49b2020-04-24 15:11:15 -06002717 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06002718}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002719
2720void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
2721 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002722 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002723}
2724
2725void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
2726 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002727 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002728}
2729
2730void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
2731 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002732 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002733}
locke-lunarga19c71d2020-03-02 18:17:04 -07002734
2735bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2736 VkImageLayout dstImageLayout, uint32_t regionCount,
2737 const VkBufferImageCopy *pRegions) const {
2738 bool skip = false;
2739 const auto *cb_access_context = GetAccessContext(commandBuffer);
2740 assert(cb_access_context);
2741 if (!cb_access_context) return skip;
2742
2743 const auto *context = cb_access_context->GetCurrentAccessContext();
2744 assert(context);
2745 if (!context) return skip;
2746
2747 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07002748 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2749
2750 for (uint32_t region = 0; region < regionCount; region++) {
2751 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002752 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002753 ResourceAccessRange src_range =
2754 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002755 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002756 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002757 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002758 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002759 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32 ". Prior access %s.",
2760 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
2761 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002762 }
2763 }
2764 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002765 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002766 copy_region.imageOffset, copy_region.imageExtent);
2767 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002768 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002769 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
2770 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
2771 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002772 }
2773 if (skip) break;
2774 }
2775 if (skip) break;
2776 }
2777 return skip;
2778}
2779
2780void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2781 VkImageLayout dstImageLayout, uint32_t regionCount,
2782 const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06002783 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07002784 auto *cb_access_context = GetAccessContext(commandBuffer);
2785 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002786 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002787 auto *context = cb_access_context->GetCurrentAccessContext();
2788 assert(context);
2789
2790 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06002791 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002792
2793 for (uint32_t region = 0; region < regionCount; region++) {
2794 const auto &copy_region = pRegions[region];
2795 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002796 ResourceAccessRange src_range =
2797 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002798 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002799 }
2800 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002801 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002802 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002803 }
2804 }
2805}
2806
2807bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
2808 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
2809 const VkBufferImageCopy *pRegions) const {
2810 bool skip = false;
2811 const auto *cb_access_context = GetAccessContext(commandBuffer);
2812 assert(cb_access_context);
2813 if (!cb_access_context) return skip;
2814
2815 const auto *context = cb_access_context->GetCurrentAccessContext();
2816 assert(context);
2817 if (!context) return skip;
2818
2819 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2820 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2821 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
2822 for (uint32_t region = 0; region < regionCount; region++) {
2823 const auto &copy_region = pRegions[region];
2824 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002825 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002826 copy_region.imageOffset, copy_region.imageExtent);
2827 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002828 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002829 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
2830 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
2831 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002832 }
2833 }
2834 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06002835 ResourceAccessRange dst_range =
2836 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002837 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002838 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002839 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002840 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Prior access %s.",
2841 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
2842 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002843 }
2844 }
2845 if (skip) break;
2846 }
2847 return skip;
2848}
2849
2850void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2851 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06002852 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07002853 auto *cb_access_context = GetAccessContext(commandBuffer);
2854 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002855 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07002856 auto *context = cb_access_context->GetCurrentAccessContext();
2857 assert(context);
2858
2859 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002860 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2861 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 -06002862 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07002863
2864 for (uint32_t region = 0; region < regionCount; region++) {
2865 const auto &copy_region = pRegions[region];
2866 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002867 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002868 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002869 }
2870 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002871 ResourceAccessRange dst_range =
2872 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002873 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002874 }
2875 }
2876}
2877
2878bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2879 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2880 const VkImageBlit *pRegions, VkFilter filter) const {
2881 bool skip = false;
2882 const auto *cb_access_context = GetAccessContext(commandBuffer);
2883 assert(cb_access_context);
2884 if (!cb_access_context) return skip;
2885
2886 const auto *context = cb_access_context->GetCurrentAccessContext();
2887 assert(context);
2888 if (!context) return skip;
2889
2890 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2891 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2892
2893 for (uint32_t region = 0; region < regionCount; region++) {
2894 const auto &blit_region = pRegions[region];
2895 if (src_image) {
2896 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
2897 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
2898 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002899 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002900 blit_region.srcOffsets[0], extent);
2901 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002902 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002903 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
2904 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
2905 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002906 }
2907 }
2908
2909 if (dst_image) {
2910 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
2911 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
2912 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002913 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002914 blit_region.dstOffsets[0], extent);
2915 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002916 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002917 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
2918 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
2919 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002920 }
2921 if (skip) break;
2922 }
2923 }
2924
2925 return skip;
2926}
2927
2928void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2929 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2930 const VkImageBlit *pRegions, VkFilter filter) {
locke-lunarg8ec19162020-06-16 18:48:34 -06002931 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
2932 pRegions, filter);
locke-lunarga19c71d2020-03-02 18:17:04 -07002933 auto *cb_access_context = GetAccessContext(commandBuffer);
2934 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002935 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002936 auto *context = cb_access_context->GetCurrentAccessContext();
2937 assert(context);
2938
2939 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002940 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002941
2942 for (uint32_t region = 0; region < regionCount; region++) {
2943 const auto &blit_region = pRegions[region];
2944 if (src_image) {
2945 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
2946 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
2947 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002948 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002949 blit_region.srcOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002950 }
2951 if (dst_image) {
2952 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
2953 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
2954 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002955 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002956 blit_region.dstOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002957 }
2958 }
2959}
locke-lunarg36ba2592020-04-03 09:42:04 -06002960
locke-lunarg61870c22020-06-09 14:51:50 -06002961bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
2962 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
2963 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06002964 bool skip = false;
2965 if (drawCount == 0) return skip;
2966
2967 const auto *buf_state = Get<BUFFER_STATE>(buffer);
2968 VkDeviceSize size = struct_size;
2969 if (drawCount == 1 || stride == size) {
2970 if (drawCount > 1) size *= drawCount;
2971 ResourceAccessRange range = MakeRange(offset, size);
2972 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
2973 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06002974 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
2975 "%s: Hazard %s for indirect %s in %s. Prior access %s.", function, string_SyncHazard(hazard.hazard),
2976 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
2977 string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06002978 }
2979 } else {
2980 for (uint32_t i = 0; i < drawCount; ++i) {
2981 ResourceAccessRange range = MakeRange(offset + i * stride, size);
2982 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
2983 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06002984 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
2985 "%s: Hazard %s for indirect %s in %s. Prior access %s.", function,
2986 string_SyncHazard(hazard.hazard), report_data->FormatHandle(buffer).c_str(),
2987 report_data->FormatHandle(commandBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06002988 break;
2989 }
2990 }
2991 }
2992 return skip;
2993}
2994
locke-lunarg61870c22020-06-09 14:51:50 -06002995void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
2996 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
2997 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06002998 const auto *buf_state = Get<BUFFER_STATE>(buffer);
2999 VkDeviceSize size = struct_size;
3000 if (drawCount == 1 || stride == size) {
3001 if (drawCount > 1) size *= drawCount;
3002 ResourceAccessRange range = MakeRange(offset, size);
3003 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3004 } else {
3005 for (uint32_t i = 0; i < drawCount; ++i) {
3006 ResourceAccessRange range = MakeRange(offset + i * stride, size);
3007 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3008 }
3009 }
3010}
3011
locke-lunarg61870c22020-06-09 14:51:50 -06003012bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3013 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003014 bool skip = false;
3015
3016 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3017 ResourceAccessRange range = MakeRange(offset, 4);
3018 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3019 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003020 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
3021 "%s: Hazard %s for countBuffer %s in %s. Prior access %s.", function, string_SyncHazard(hazard.hazard),
3022 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3023 string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003024 }
3025 return skip;
3026}
3027
locke-lunarg61870c22020-06-09 14:51:50 -06003028void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003029 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3030 ResourceAccessRange range = MakeRange(offset, 4);
3031 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3032}
3033
locke-lunarg36ba2592020-04-03 09:42:04 -06003034bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003035 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003036 const auto *cb_access_context = GetAccessContext(commandBuffer);
3037 assert(cb_access_context);
3038 if (!cb_access_context) return skip;
3039
locke-lunarg61870c22020-06-09 14:51:50 -06003040 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003041 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003042}
3043
3044void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003045 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003046 auto *cb_access_context = GetAccessContext(commandBuffer);
3047 assert(cb_access_context);
3048 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003049
locke-lunarg61870c22020-06-09 14:51:50 -06003050 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003051}
locke-lunarge1a67022020-04-29 00:15:36 -06003052
3053bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003054 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003055 const auto *cb_access_context = GetAccessContext(commandBuffer);
3056 assert(cb_access_context);
3057 if (!cb_access_context) return skip;
3058
3059 const auto *context = cb_access_context->GetCurrentAccessContext();
3060 assert(context);
3061 if (!context) return skip;
3062
locke-lunarg61870c22020-06-09 14:51:50 -06003063 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3064 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3065 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003066 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003067}
3068
3069void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003070 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003071 auto *cb_access_context = GetAccessContext(commandBuffer);
3072 assert(cb_access_context);
3073 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3074 auto *context = cb_access_context->GetCurrentAccessContext();
3075 assert(context);
3076
locke-lunarg61870c22020-06-09 14:51:50 -06003077 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3078 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003079}
3080
3081bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3082 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003083 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003084 const auto *cb_access_context = GetAccessContext(commandBuffer);
3085 assert(cb_access_context);
3086 if (!cb_access_context) return skip;
3087
locke-lunarg61870c22020-06-09 14:51:50 -06003088 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3089 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3090 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003091 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003092}
3093
3094void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3095 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003096 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003097 auto *cb_access_context = GetAccessContext(commandBuffer);
3098 assert(cb_access_context);
3099 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003100
locke-lunarg61870c22020-06-09 14:51:50 -06003101 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3102 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3103 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003104}
3105
3106bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3107 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003108 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003109 const auto *cb_access_context = GetAccessContext(commandBuffer);
3110 assert(cb_access_context);
3111 if (!cb_access_context) return skip;
3112
locke-lunarg61870c22020-06-09 14:51:50 -06003113 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3114 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3115 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003116 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003117}
3118
3119void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3120 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003121 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003122 auto *cb_access_context = GetAccessContext(commandBuffer);
3123 assert(cb_access_context);
3124 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003125
locke-lunarg61870c22020-06-09 14:51:50 -06003126 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3127 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3128 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003129}
3130
3131bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3132 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003133 bool skip = false;
3134 if (drawCount == 0) return skip;
3135
locke-lunargff255f92020-05-13 18:53:52 -06003136 const auto *cb_access_context = GetAccessContext(commandBuffer);
3137 assert(cb_access_context);
3138 if (!cb_access_context) return skip;
3139
3140 const auto *context = cb_access_context->GetCurrentAccessContext();
3141 assert(context);
3142 if (!context) return skip;
3143
locke-lunarg61870c22020-06-09 14:51:50 -06003144 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3145 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3146 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3147 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003148
3149 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3150 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3151 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003152 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003153 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003154}
3155
3156void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3157 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003158 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003159 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003160 auto *cb_access_context = GetAccessContext(commandBuffer);
3161 assert(cb_access_context);
3162 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3163 auto *context = cb_access_context->GetCurrentAccessContext();
3164 assert(context);
3165
locke-lunarg61870c22020-06-09 14:51:50 -06003166 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3167 cb_access_context->RecordDrawSubpassAttachment(tag);
3168 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003169
3170 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3171 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3172 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003173 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003174}
3175
3176bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3177 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003178 bool skip = false;
3179 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003180 const auto *cb_access_context = GetAccessContext(commandBuffer);
3181 assert(cb_access_context);
3182 if (!cb_access_context) return skip;
3183
3184 const auto *context = cb_access_context->GetCurrentAccessContext();
3185 assert(context);
3186 if (!context) return skip;
3187
locke-lunarg61870c22020-06-09 14:51:50 -06003188 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3189 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3190 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3191 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003192
3193 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3194 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3195 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003196 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003197 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003198}
3199
3200void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3201 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003202 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
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_DRAWINDEXEDINDIRECT);
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(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003212
3213 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3214 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3215 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003216 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003217}
3218
3219bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3220 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3221 uint32_t stride, const char *function) const {
3222 bool skip = false;
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, function);
3232 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3233 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3234 function);
3235 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003236
3237 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3238 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3239 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003240 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003241 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003242}
3243
3244bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3245 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3246 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003247 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3248 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003249}
3250
3251void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3252 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3253 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003254 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3255 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003256 auto *cb_access_context = GetAccessContext(commandBuffer);
3257 assert(cb_access_context);
3258 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3259 auto *context = cb_access_context->GetCurrentAccessContext();
3260 assert(context);
3261
locke-lunarg61870c22020-06-09 14:51:50 -06003262 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3263 cb_access_context->RecordDrawSubpassAttachment(tag);
3264 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3265 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003266
3267 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3268 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3269 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003270 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003271}
3272
3273bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3274 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3275 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003276 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3277 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003278}
3279
3280void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3281 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3282 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003283 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3284 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003285 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003286}
3287
3288bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3289 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3290 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003291 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3292 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003293}
3294
3295void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3296 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3297 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003298 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3299 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003300 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3301}
3302
3303bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3304 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3305 uint32_t stride, const char *function) const {
3306 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003307 const auto *cb_access_context = GetAccessContext(commandBuffer);
3308 assert(cb_access_context);
3309 if (!cb_access_context) return skip;
3310
3311 const auto *context = cb_access_context->GetCurrentAccessContext();
3312 assert(context);
3313 if (!context) return skip;
3314
locke-lunarg61870c22020-06-09 14:51:50 -06003315 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3316 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3317 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3318 stride, function);
3319 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003320
3321 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3322 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3323 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003324 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003325 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003326}
3327
3328bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3329 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3330 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003331 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3332 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003333}
3334
3335void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3336 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3337 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003338 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3339 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003340 auto *cb_access_context = GetAccessContext(commandBuffer);
3341 assert(cb_access_context);
3342 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3343 auto *context = cb_access_context->GetCurrentAccessContext();
3344 assert(context);
3345
locke-lunarg61870c22020-06-09 14:51:50 -06003346 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3347 cb_access_context->RecordDrawSubpassAttachment(tag);
3348 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3349 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003350
3351 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3352 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003353 // We will update the index and vertex buffer in SubmitQueue in the future.
3354 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003355}
3356
3357bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3358 VkDeviceSize offset, VkBuffer countBuffer,
3359 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3360 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003361 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3362 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003363}
3364
3365void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3366 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3367 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003368 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3369 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003370 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3371}
3372
3373bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3374 VkDeviceSize offset, VkBuffer countBuffer,
3375 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3376 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003377 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3378 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003379}
3380
3381void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3382 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3383 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003384 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3385 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003386 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3387}
3388
3389bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3390 const VkClearColorValue *pColor, uint32_t rangeCount,
3391 const VkImageSubresourceRange *pRanges) const {
3392 bool skip = false;
3393 const auto *cb_access_context = GetAccessContext(commandBuffer);
3394 assert(cb_access_context);
3395 if (!cb_access_context) return skip;
3396
3397 const auto *context = cb_access_context->GetCurrentAccessContext();
3398 assert(context);
3399 if (!context) return skip;
3400
3401 const auto *image_state = Get<IMAGE_STATE>(image);
3402
3403 for (uint32_t index = 0; index < rangeCount; index++) {
3404 const auto &range = pRanges[index];
3405 if (image_state) {
3406 auto hazard =
3407 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3408 if (hazard.hazard) {
3409 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003410 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Prior access %s.",
3411 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
3412 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003413 }
3414 }
3415 }
3416 return skip;
3417}
3418
3419void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3420 const VkClearColorValue *pColor, uint32_t rangeCount,
3421 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003422 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003423 auto *cb_access_context = GetAccessContext(commandBuffer);
3424 assert(cb_access_context);
3425 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
3426 auto *context = cb_access_context->GetCurrentAccessContext();
3427 assert(context);
3428
3429 const auto *image_state = Get<IMAGE_STATE>(image);
3430
3431 for (uint32_t index = 0; index < rangeCount; index++) {
3432 const auto &range = pRanges[index];
3433 if (image_state) {
3434 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3435 tag);
3436 }
3437 }
3438}
3439
3440bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3441 VkImageLayout imageLayout,
3442 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3443 const VkImageSubresourceRange *pRanges) const {
3444 bool skip = false;
3445 const auto *cb_access_context = GetAccessContext(commandBuffer);
3446 assert(cb_access_context);
3447 if (!cb_access_context) return skip;
3448
3449 const auto *context = cb_access_context->GetCurrentAccessContext();
3450 assert(context);
3451 if (!context) return skip;
3452
3453 const auto *image_state = Get<IMAGE_STATE>(image);
3454
3455 for (uint32_t index = 0; index < rangeCount; index++) {
3456 const auto &range = pRanges[index];
3457 if (image_state) {
3458 auto hazard =
3459 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3460 if (hazard.hazard) {
3461 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003462 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Prior access %s.",
3463 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
3464 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003465 }
3466 }
3467 }
3468 return skip;
3469}
3470
3471void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3472 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3473 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003474 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003475 auto *cb_access_context = GetAccessContext(commandBuffer);
3476 assert(cb_access_context);
3477 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
3478 auto *context = cb_access_context->GetCurrentAccessContext();
3479 assert(context);
3480
3481 const auto *image_state = Get<IMAGE_STATE>(image);
3482
3483 for (uint32_t index = 0; index < rangeCount; index++) {
3484 const auto &range = pRanges[index];
3485 if (image_state) {
3486 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3487 tag);
3488 }
3489 }
3490}
3491
3492bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
3493 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
3494 VkDeviceSize dstOffset, VkDeviceSize stride,
3495 VkQueryResultFlags flags) const {
3496 bool skip = false;
3497 const auto *cb_access_context = GetAccessContext(commandBuffer);
3498 assert(cb_access_context);
3499 if (!cb_access_context) return skip;
3500
3501 const auto *context = cb_access_context->GetCurrentAccessContext();
3502 assert(context);
3503 if (!context) return skip;
3504
3505 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3506
3507 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003508 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003509 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3510 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003511 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3512 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Prior access %s.",
3513 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
3514 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003515 }
3516 }
locke-lunargff255f92020-05-13 18:53:52 -06003517
3518 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003519 return skip;
3520}
3521
3522void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
3523 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3524 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003525 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
3526 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06003527 auto *cb_access_context = GetAccessContext(commandBuffer);
3528 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06003529 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06003530 auto *context = cb_access_context->GetCurrentAccessContext();
3531 assert(context);
3532
3533 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3534
3535 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003536 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003537 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3538 }
locke-lunargff255f92020-05-13 18:53:52 -06003539
3540 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003541}
3542
3543bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3544 VkDeviceSize size, uint32_t data) const {
3545 bool skip = false;
3546 const auto *cb_access_context = GetAccessContext(commandBuffer);
3547 assert(cb_access_context);
3548 if (!cb_access_context) return skip;
3549
3550 const auto *context = cb_access_context->GetCurrentAccessContext();
3551 assert(context);
3552 if (!context) return skip;
3553
3554 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3555
3556 if (dst_buffer) {
3557 ResourceAccessRange range = MakeRange(dstOffset, size);
3558 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3559 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003560 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3561 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Prior access %s.", string_SyncHazard(hazard.hazard),
3562 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003563 }
3564 }
3565 return skip;
3566}
3567
3568void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3569 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003570 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06003571 auto *cb_access_context = GetAccessContext(commandBuffer);
3572 assert(cb_access_context);
3573 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
3574 auto *context = cb_access_context->GetCurrentAccessContext();
3575 assert(context);
3576
3577 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3578
3579 if (dst_buffer) {
3580 ResourceAccessRange range = MakeRange(dstOffset, size);
3581 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3582 }
3583}
3584
3585bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3586 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3587 const VkImageResolve *pRegions) 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 *src_image = Get<IMAGE_STATE>(srcImage);
3598 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3599
3600 for (uint32_t region = 0; region < regionCount; region++) {
3601 const auto &resolve_region = pRegions[region];
3602 if (src_image) {
3603 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3604 resolve_region.srcOffset, resolve_region.extent);
3605 if (hazard.hazard) {
3606 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003607 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
3608 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
3609 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003610 }
3611 }
3612
3613 if (dst_image) {
3614 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3615 resolve_region.dstOffset, resolve_region.extent);
3616 if (hazard.hazard) {
3617 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003618 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
3619 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
3620 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003621 }
3622 if (skip) break;
3623 }
3624 }
3625
3626 return skip;
3627}
3628
3629void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3630 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3631 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003632 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3633 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06003634 auto *cb_access_context = GetAccessContext(commandBuffer);
3635 assert(cb_access_context);
3636 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
3637 auto *context = cb_access_context->GetCurrentAccessContext();
3638 assert(context);
3639
3640 auto *src_image = Get<IMAGE_STATE>(srcImage);
3641 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 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3647 resolve_region.srcOffset, resolve_region.extent, tag);
3648 }
3649 if (dst_image) {
3650 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3651 resolve_region.dstOffset, resolve_region.extent, tag);
3652 }
3653 }
3654}
3655
3656bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3657 VkDeviceSize dataSize, const void *pData) const {
3658 bool skip = false;
3659 const auto *cb_access_context = GetAccessContext(commandBuffer);
3660 assert(cb_access_context);
3661 if (!cb_access_context) return skip;
3662
3663 const auto *context = cb_access_context->GetCurrentAccessContext();
3664 assert(context);
3665 if (!context) return skip;
3666
3667 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3668
3669 if (dst_buffer) {
3670 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3671 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3672 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003673 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3674 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Prior access %s.", string_SyncHazard(hazard.hazard),
3675 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003676 }
3677 }
3678 return skip;
3679}
3680
3681void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3682 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003683 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06003684 auto *cb_access_context = GetAccessContext(commandBuffer);
3685 assert(cb_access_context);
3686 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
3687 auto *context = cb_access_context->GetCurrentAccessContext();
3688 assert(context);
3689
3690 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3691
3692 if (dst_buffer) {
3693 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3694 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3695 }
3696}
locke-lunargff255f92020-05-13 18:53:52 -06003697
3698bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3699 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
3700 bool skip = false;
3701 const auto *cb_access_context = GetAccessContext(commandBuffer);
3702 assert(cb_access_context);
3703 if (!cb_access_context) return skip;
3704
3705 const auto *context = cb_access_context->GetCurrentAccessContext();
3706 assert(context);
3707 if (!context) return skip;
3708
3709 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3710
3711 if (dst_buffer) {
3712 ResourceAccessRange range = MakeRange(dstOffset, 4);
3713 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3714 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003715 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3716 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Prior access %s.",
3717 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
3718 string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003719 }
3720 }
3721 return skip;
3722}
3723
3724void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3725 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003726 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06003727 auto *cb_access_context = GetAccessContext(commandBuffer);
3728 assert(cb_access_context);
3729 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
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, 4);
3737 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3738 }
3739}