blob: 0d4eed0a5b5dad717688fb79dc4dc6fb87cd1a42 [file] [log] [blame]
John Zulauf9cb530d2019-09-30 14:14:10 -06001/* Copyright (c) 2019 The Khronos Group Inc.
2 * Copyright (c) 2019 Valve Corporation
3 * Copyright (c) 2019 LunarG, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: John Zulauf <jzulauf@lunarg.com>
18 */
19
20#include <limits>
21#include <vector>
locke-lunarg296a3c92020-03-25 01:04:29 -060022#include <memory>
23#include <bitset>
John Zulauf9cb530d2019-09-30 14:14:10 -060024#include "synchronization_validation.h"
25
26static const char *string_SyncHazardVUID(SyncHazard hazard) {
27 switch (hazard) {
28 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070029 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060030 break;
31 case SyncHazard::READ_AFTER_WRITE:
32 return "SYNC-HAZARD-READ_AFTER_WRITE";
33 break;
34 case SyncHazard::WRITE_AFTER_READ:
35 return "SYNC-HAZARD-WRITE_AFTER_READ";
36 break;
37 case SyncHazard::WRITE_AFTER_WRITE:
38 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
39 break;
John Zulauf2f952d22020-02-10 11:34:51 -070040 case SyncHazard::READ_RACING_WRITE:
41 return "SYNC-HAZARD-READ-RACING-WRITE";
42 break;
43 case SyncHazard::WRITE_RACING_WRITE:
44 return "SYNC-HAZARD-WRITE-RACING-WRITE";
45 break;
46 case SyncHazard::WRITE_RACING_READ:
47 return "SYNC-HAZARD-WRITE-RACING-READ";
48 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060049 default:
50 assert(0);
51 }
52 return "SYNC-HAZARD-INVALID";
53}
54
55static const char *string_SyncHazard(SyncHazard hazard) {
56 switch (hazard) {
57 case SyncHazard::NONE:
58 return "NONR";
59 break;
60 case SyncHazard::READ_AFTER_WRITE:
61 return "READ_AFTER_WRITE";
62 break;
63 case SyncHazard::WRITE_AFTER_READ:
64 return "WRITE_AFTER_READ";
65 break;
66 case SyncHazard::WRITE_AFTER_WRITE:
67 return "WRITE_AFTER_WRITE";
68 break;
John Zulauf2f952d22020-02-10 11:34:51 -070069 case SyncHazard::READ_RACING_WRITE:
70 return "READ_RACING_WRITE";
71 break;
72 case SyncHazard::WRITE_RACING_WRITE:
73 return "WRITE_RACING_WRITE";
74 break;
75 case SyncHazard::WRITE_RACING_READ:
76 return "WRITE_RACING_READ";
77 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060078 default:
79 assert(0);
80 }
81 return "INVALID HAZARD";
82}
83
John Zulaufb027cdb2020-05-21 14:25:22 -060084static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
85static constexpr SyncStageAccessFlags kColorAttachmentAccessScope =
86 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
87 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
88 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT;
89static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
90 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
91static constexpr SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
92 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
93 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
94 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
95 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
96
97static constexpr SyncOrderingBarrier kColorAttachmentRasterOrder = {kColorAttachmentExecScope, kColorAttachmentAccessScope};
98static constexpr SyncOrderingBarrier kDepthStencilAttachmentRasterOrder = {kDepthStencilAttachmentExecScope,
99 kDepthStencilAttachmentAccessScope};
100static constexpr SyncOrderingBarrier kAttachmentRasterOrder = {kDepthStencilAttachmentExecScope | kColorAttachmentExecScope,
101 kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope};
John Zulauf7635de32020-05-29 17:14:15 -0600102// Sometimes we have an internal access conflict, and we using the kCurrentCommandTag to set and detect in temporary/proxy contexts
103static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex);
John Zulaufb027cdb2020-05-21 14:25:22 -0600104
locke-lunarg3c038002020-04-30 23:08:08 -0600105inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
106 if (size == VK_WHOLE_SIZE) {
107 return (whole_size - offset);
108 }
109 return size;
110}
111
John Zulauf16adfc92020-04-08 10:28:33 -0600112template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600113static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600114 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
115}
116
John Zulauf355e49b2020-04-24 15:11:15 -0600117static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600118
John Zulauf0cb5be22020-01-23 12:18:22 -0700119// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
120VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
121 VkPipelineStageFlags expanded = stage_mask;
122 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
123 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
124 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
125 if (all_commands.first & queue_flags) {
126 expanded |= all_commands.second;
127 }
128 }
129 }
130 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
131 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
132 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
133 }
134 return expanded;
135}
136
John Zulauf36bcf6a2020-02-03 15:12:52 -0700137VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
138 std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
139 VkPipelineStageFlags unscanned = stage_mask;
140 VkPipelineStageFlags related = 0;
141 for (const auto entry : map) {
142 const auto stage = entry.first;
143 if (stage & unscanned) {
144 related = related | entry.second;
145 unscanned = unscanned & ~stage;
146 if (!unscanned) break;
147 }
148 }
149 return related;
150}
151
152VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
153 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
154}
155
156VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
157 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
158}
159
John Zulauf5c5e88d2019-12-26 11:22:02 -0700160static const ResourceAccessRange full_range(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700161
locke-lunargff255f92020-05-13 18:53:52 -0600162void GetBufferRange(VkDeviceSize &range_start, VkDeviceSize &range_size, VkDeviceSize offset, VkDeviceSize buf_whole_size,
163 uint32_t first_index, uint32_t count, VkDeviceSize stride) {
164 range_start = offset + first_index * stride;
165 range_size = 0;
166 if (count == UINT32_MAX) {
167 range_size = buf_whole_size - range_start;
168 } else {
169 range_size = count * stride;
170 }
171}
172
locke-lunarg654e3692020-06-04 17:19:15 -0600173SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
174 VkShaderStageFlagBits stage_flag) {
175 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
176 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
177 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
178 }
179 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
180 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
181 assert(0);
182 }
183 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
184 return stage_access->second.uniform_read;
185 }
186
187 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
188 // Because if write hazard happens, read hazard might or might not happen.
189 // But if write hazard doesn't happen, read hazard is impossible to happen.
190 if (descriptor_data.is_writable) {
191 return stage_access->second.shader_write;
192 }
193 return stage_access->second.shader_read;
194}
195
locke-lunarg37047832020-06-12 13:44:45 -0600196bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
197 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
198 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
199 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
200 ? true
201 : false;
202}
203
204bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
205 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
206 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
207 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
208 ? true
209 : false;
210}
211
John Zulauf355e49b2020-04-24 15:11:15 -0600212// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
213const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
214 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
215
John Zulauf7635de32020-05-29 17:14:15 -0600216// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
217// Used by both validation and record operations
218//
219// The signature for Action() reflect the needs of both uses.
220template <typename Action>
221void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
222 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
223 VkExtent3D extent = CastTo3D(render_area.extent);
224 VkOffset3D offset = CastTo3D(render_area.offset);
225 const auto &rp_ci = rp_state.createInfo;
226 const auto *attachment_ci = rp_ci.pAttachments;
227 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
228
229 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
230 const auto *color_attachments = subpass_ci.pColorAttachments;
231 const auto *color_resolve = subpass_ci.pResolveAttachments;
232 if (color_resolve && color_attachments) {
233 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
234 const auto &color_attach = color_attachments[i].attachment;
235 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
236 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
237 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
238 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
239 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
240 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
241 }
242 }
243 }
244
245 // Depth stencil resolve only if the extension is present
246 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
247 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
248 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
249 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
250 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
251 const auto src_ci = attachment_ci[src_at];
252 // The formats are required to match so we can pick either
253 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
254 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
255 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
256 VkImageAspectFlags aspect_mask = 0u;
257
258 // Figure out which aspects are actually touched during resolve operations
259 const char *aspect_string = nullptr;
260 if (resolve_depth && resolve_stencil) {
261 // Validate all aspects together
262 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
263 aspect_string = "depth/stencil";
264 } else if (resolve_depth) {
265 // Validate depth only
266 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
267 aspect_string = "depth";
268 } else if (resolve_stencil) {
269 // Validate all stencil only
270 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
271 aspect_string = "stencil";
272 }
273
274 if (aspect_mask) {
275 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
276 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kDepthStencilAttachmentRasterOrder, offset, extent,
277 aspect_mask);
278 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
279 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
280 }
281 }
282}
283
284// Action for validating resolve operations
285class ValidateResolveAction {
286 public:
287 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
288 const char *func_name)
289 : render_pass_(render_pass),
290 subpass_(subpass),
291 context_(context),
292 sync_state_(sync_state),
293 func_name_(func_name),
294 skip_(false) {}
295 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
296 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
297 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
298 HazardResult hazard;
299 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
300 if (hazard.hazard) {
301 skip_ |= sync_state_.LogError(
302 render_pass_, string_SyncHazardVUID(hazard.hazard),
303 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32 " to resolve attachment %" PRIu32 ".",
304 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name, src_at, dst_at);
305 }
306 }
307 // Providing a mechanism for the constructing caller to get the result of the validation
308 bool GetSkip() const { return skip_; }
309
310 private:
311 VkRenderPass render_pass_;
312 const uint32_t subpass_;
313 const AccessContext &context_;
314 const SyncValidator &sync_state_;
315 const char *func_name_;
316 bool skip_;
317};
318
319// Update action for resolve operations
320class UpdateStateResolveAction {
321 public:
322 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
323 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
324 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
325 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
326 // Ignores validation only arguments...
327 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
328 }
329
330 private:
331 AccessContext &context_;
332 const ResourceUsageTag &tag_;
333};
334
John Zulauf540266b2020-04-06 18:54:53 -0600335AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
336 const std::vector<SubpassDependencyGraphNode> &dependencies,
337 const std::vector<AccessContext> &contexts, AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600338 Reset();
339 const auto &subpass_dep = dependencies[subpass];
340 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600341 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600342 for (const auto &prev_dep : subpass_dep.prev) {
343 assert(prev_dep.dependency);
344 const auto dep = *prev_dep.dependency;
John Zulauf540266b2020-04-06 18:54:53 -0600345 prev_.emplace_back(const_cast<AccessContext *>(&contexts[dep.srcSubpass]), queue_flags, dep);
John Zulauf355e49b2020-04-24 15:11:15 -0600346 prev_by_subpass_[dep.srcSubpass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700347 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600348
349 async_.reserve(subpass_dep.async.size());
350 for (const auto async_subpass : subpass_dep.async) {
John Zulauf540266b2020-04-06 18:54:53 -0600351 async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass]));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600352 }
John Zulaufe5da6e52020-03-18 15:32:18 -0600353 if (subpass_dep.barrier_from_external) {
354 src_external_ = TrackBack(external_context, queue_flags, *subpass_dep.barrier_from_external);
355 } else {
356 src_external_ = TrackBack();
357 }
358 if (subpass_dep.barrier_to_external) {
359 dst_external_ = TrackBack(this, queue_flags, *subpass_dep.barrier_to_external);
360 } else {
361 dst_external_ = TrackBack();
John Zulauf3d84f1b2020-03-09 13:33:25 -0600362 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700363}
364
John Zulauf5f13a792020-03-10 07:31:21 -0600365template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600366HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600367 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600368 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600369 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600370
371 HazardResult hazard;
372 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
373 hazard = detector.Detect(prev);
374 }
375 return hazard;
376}
377
John Zulauf3d84f1b2020-03-09 13:33:25 -0600378// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
379// the DAG of the contexts (for example subpasses)
380template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600381HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
382 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600383 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600384
John Zulauf355e49b2020-04-24 15:11:15 -0600385 if (static_cast<uint32_t>(options) | DetectOptions::kDetectAsync) {
386 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
387 // so we'll check these first
388 for (const auto &async_context : async_) {
389 hazard = async_context->DetectAsyncHazard(type, detector, range);
390 if (hazard.hazard) return hazard;
391 }
John Zulauf5f13a792020-03-10 07:31:21 -0600392 }
393
John Zulauf69133422020-05-20 14:55:53 -0600394 const bool detect_prev = (static_cast<uint32_t>(options) | DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600395
John Zulauf69133422020-05-20 14:55:53 -0600396 const auto &accesses = GetAccessStateMap(type);
397 const auto from = accesses.lower_bound(range);
398 const auto to = accesses.upper_bound(range);
399 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600400
John Zulauf69133422020-05-20 14:55:53 -0600401 for (auto pos = from; pos != to; ++pos) {
402 // Cover any leading gap, or gap between entries
403 if (detect_prev) {
404 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
405 // Cover any leading gap, or gap between entries
406 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600407 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600408 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600409 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600410 if (hazard.hazard) return hazard;
411 }
John Zulauf69133422020-05-20 14:55:53 -0600412 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
413 gap.begin = pos->first.end;
414 }
415
416 hazard = detector.Detect(pos);
417 if (hazard.hazard) return hazard;
418 }
419
420 if (detect_prev) {
421 // Detect in the trailing empty as needed
422 gap.end = range.end;
423 if (gap.non_empty()) {
424 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600425 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600426 }
427
428 return hazard;
429}
430
431// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
432template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600433HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600434 auto &accesses = GetAccessStateMap(type);
435 const auto from = accesses.lower_bound(range);
436 const auto to = accesses.upper_bound(range);
437
John Zulauf3d84f1b2020-03-09 13:33:25 -0600438 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600439 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
440 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600441 }
John Zulauf16adfc92020-04-08 10:28:33 -0600442
John Zulauf3d84f1b2020-03-09 13:33:25 -0600443 return hazard;
444}
445
John Zulauf355e49b2020-04-24 15:11:15 -0600446// Returns the last resolved entry
447static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
448 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
449 const SyncBarrier *barrier) {
450 auto at = entry;
451 for (auto pos = first; pos != last; ++pos) {
452 // Every member of the input iterator range must fit within the remaining portion of entry
453 assert(at->first.includes(pos->first));
454 assert(at != dest->end());
455 // Trim up at to the same size as the entry to resolve
456 at = sparse_container::split(at, *dest, pos->first);
457 auto access = pos->second;
458 if (barrier) {
459 access.ApplyBarrier(*barrier);
460 }
461 at->second.Resolve(access);
462 ++at; // Go to the remaining unused section of entry
463 }
464}
465
466void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const SyncBarrier *barrier,
467 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
468 bool recur_to_infill) const {
469 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
470 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf16adfc92020-04-08 10:28:33 -0600471 if (current->pos_B->valid) {
472 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600473 auto access = src_pos->second;
474 if (barrier) {
475 access.ApplyBarrier(*barrier);
476 }
John Zulauf16adfc92020-04-08 10:28:33 -0600477 if (current->pos_A->valid) {
478 current.trim_A();
John Zulauf355e49b2020-04-24 15:11:15 -0600479 current->pos_A->lower_bound->second.Resolve(access);
John Zulauf5f13a792020-03-10 07:31:21 -0600480 } else {
John Zulauf355e49b2020-04-24 15:11:15 -0600481 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, access));
482 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600483 }
John Zulauf16adfc92020-04-08 10:28:33 -0600484 } else {
485 // we have to descend to fill this gap
486 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600487 if (current->pos_A->valid) {
488 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
489 ResourceAccessRangeMap gap_map;
490 ResolvePreviousAccess(type, current->range, &gap_map, infill_state);
491 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier);
492 } else {
493 // There isn't anything in dest in current->range, so we can accumulate directly into it.
494 ResolvePreviousAccess(type, current->range, resolve_map, infill_state);
495 if (barrier) {
496 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
497 for (auto pos = resolve_map->lower_bound(current->range); pos != current->pos_A->lower_bound; ++pos) {
498 pos->second.ApplyBarrier(*barrier);
499 }
500 }
501 }
502 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
503 // iterator of the outer while.
504
505 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
506 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
507 // we stepped on the dest map
508 const auto seek_to = current->range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
509 current.invalidate_A(); // Changes current->range
510 current.seek(seek_to);
511 } else if (!current->pos_A->valid && infill_state) {
512 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
513 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
514 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600515 }
John Zulauf5f13a792020-03-10 07:31:21 -0600516 }
John Zulauf16adfc92020-04-08 10:28:33 -0600517 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600518 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600519}
520
John Zulauf355e49b2020-04-24 15:11:15 -0600521void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
522 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600523 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600524 if (range.non_empty() && infill_state) {
525 descent_map->insert(std::make_pair(range, *infill_state));
526 }
527 } else {
528 // Look for something to fill the gap further along.
529 for (const auto &prev_dep : prev_) {
John Zulauf355e49b2020-04-24 15:11:15 -0600530 prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600531 }
532
John Zulaufe5da6e52020-03-18 15:32:18 -0600533 if (src_external_.context) {
John Zulauf355e49b2020-04-24 15:11:15 -0600534 src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600535 }
536 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600537}
538
John Zulauf16adfc92020-04-08 10:28:33 -0600539AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600540 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600541}
542
543VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
544 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
545}
546
John Zulauf355e49b2020-04-24 15:11:15 -0600547static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600548
John Zulauf1507ee42020-05-18 11:33:09 -0600549static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
550 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
551 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
552 return stage_access;
553}
554static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
555 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
556 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
557 return stage_access;
558}
559
John Zulauf7635de32020-05-29 17:14:15 -0600560// Caller must manage returned pointer
561static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
562 uint32_t subpass, const VkRect2D &render_area,
563 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
564 auto *proxy = new AccessContext(context);
565 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600566 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600567 return proxy;
568}
569
John Zulauf540266b2020-04-06 18:54:53 -0600570void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600571 AddressType address_type, ResourceAccessRangeMap *descent_map,
572 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600573 if (!SimpleBinding(image_state)) return;
574
John Zulauf62f10592020-04-03 12:20:02 -0600575 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600576 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600577 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600578 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600579 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600580 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600581 }
582}
583
John Zulauf7635de32020-05-29 17:14:15 -0600584// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600585bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600586 const VkRect2D &render_area, uint32_t subpass,
587 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
588 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600589 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600590 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
591 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
592 // those affects have not been recorded yet.
593 //
594 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
595 // to apply and only copy then, if this proves a hot spot.
596 std::unique_ptr<AccessContext> proxy_for_prev;
597 TrackBack proxy_track_back;
598
John Zulauf355e49b2020-04-24 15:11:15 -0600599 const auto &transitions = rp_state.subpass_transitions[subpass];
600 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600601 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
602
603 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
604 if (prev_needs_proxy) {
605 if (!proxy_for_prev) {
606 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
607 render_area, attachment_views));
608 proxy_track_back = *track_back;
609 proxy_track_back.context = proxy_for_prev.get();
610 }
611 track_back = &proxy_track_back;
612 }
613 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600614 if (hazard.hazard) {
615 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
616 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " image layout transition.",
617 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment);
618 }
619 }
620 return skip;
621}
622
John Zulauf1507ee42020-05-18 11:33:09 -0600623bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600624 const VkRect2D &render_area, uint32_t subpass,
625 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
626 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600627 bool skip = false;
628 const auto *attachment_ci = rp_state.createInfo.pAttachments;
629 VkExtent3D extent = CastTo3D(render_area.extent);
630 VkOffset3D offset = CastTo3D(render_area.offset);
631 const auto external_access_scope = src_external_.barrier.dst_access_scope;
John Zulauf1507ee42020-05-18 11:33:09 -0600632
633 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
634 if (subpass == rp_state.attachment_first_subpass[i]) {
635 if (attachment_views[i] == nullptr) continue;
636 const IMAGE_VIEW_STATE &view = *attachment_views[i];
637 const IMAGE_STATE *image = view.image_state.get();
638 if (image == nullptr) continue;
639 const auto &ci = attachment_ci[i];
640 const bool is_transition = rp_state.attachment_first_is_transition[i];
641
642 // Need check in the following way
643 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
644 // vs. transition
645 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
646 // for each aspect loaded.
647
648 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600649 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600650 const bool is_color = !(has_depth || has_stencil);
651
652 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
653 const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U;
654 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
655 const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U;
656
John Zulaufaff20662020-06-01 14:07:58 -0600657 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600658 const char *aspect = nullptr;
659 if (is_transition) {
660 // For transition w
661 SyncHazard transition_hazard = SyncHazard::NONE;
662 bool checked_stencil = false;
663 if (load_mask) {
664 if ((load_mask & external_access_scope) != load_mask) {
665 transition_hazard =
666 SyncStageAccess::HasWrite(load_mask) ? SyncHazard::WRITE_AFTER_WRITE : SyncHazard::READ_AFTER_WRITE;
667 aspect = is_color ? "color" : "depth";
668 }
669 if (!transition_hazard && stencil_mask) {
670 if ((stencil_mask & external_access_scope) != stencil_mask) {
671 transition_hazard = SyncStageAccess::HasWrite(stencil_mask) ? SyncHazard::WRITE_AFTER_WRITE
672 : SyncHazard::READ_AFTER_WRITE;
673 aspect = "stencil";
674 checked_stencil = true;
675 }
676 }
677 }
678 if (transition_hazard) {
679 // Hazard vs. ILT
680 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
681 skip |=
682 sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
683 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
684 " aspect %s during load with loadOp %s.",
685 func_name, string_SyncHazard(transition_hazard), subpass, i, aspect, load_op_string);
686 }
687 } else {
688 auto hazard_range = view.normalized_subresource_range;
689 bool checked_stencil = false;
690 if (is_color) {
691 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
692 aspect = "color";
693 } else {
694 if (has_depth) {
695 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
696 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
697 aspect = "depth";
698 }
699 if (!hazard.hazard && has_stencil) {
700 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
701 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
702 aspect = "stencil";
703 checked_stencil = true;
704 }
705 }
706
707 if (hazard.hazard) {
708 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
709 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
710 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
711 " aspect %s during load with loadOp %s.",
712 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
713 }
714 }
715 }
716 }
717 return skip;
718}
719
John Zulaufaff20662020-06-01 14:07:58 -0600720// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
721// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
722// store is part of the same Next/End operation.
723// The latter is handled in layout transistion validation directly
724bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
725 const VkRect2D &render_area, uint32_t subpass,
726 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
727 const char *func_name) const {
728 bool skip = false;
729 const auto *attachment_ci = rp_state.createInfo.pAttachments;
730 VkExtent3D extent = CastTo3D(render_area.extent);
731 VkOffset3D offset = CastTo3D(render_area.offset);
732
733 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
734 if (subpass == rp_state.attachment_last_subpass[i]) {
735 if (attachment_views[i] == nullptr) continue;
736 const IMAGE_VIEW_STATE &view = *attachment_views[i];
737 const IMAGE_STATE *image = view.image_state.get();
738 if (image == nullptr) continue;
739 const auto &ci = attachment_ci[i];
740
741 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
742 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
743 // sake, we treat DONT_CARE as writing.
744 const bool has_depth = FormatHasDepth(ci.format);
745 const bool has_stencil = FormatHasStencil(ci.format);
746 const bool is_color = !(has_depth || has_stencil);
747 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
748 if (!has_stencil && !store_op_stores) continue;
749
750 HazardResult hazard;
751 const char *aspect = nullptr;
752 bool checked_stencil = false;
753 if (is_color) {
754 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
755 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
756 aspect = "color";
757 } else {
758 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
759 auto hazard_range = view.normalized_subresource_range;
760 if (has_depth && store_op_stores) {
761 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
762 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
763 kAttachmentRasterOrder, offset, extent);
764 aspect = "depth";
765 }
766 if (!hazard.hazard && has_stencil && stencil_op_stores) {
767 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
768 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
769 kAttachmentRasterOrder, offset, extent);
770 aspect = "stencil";
771 checked_stencil = true;
772 }
773 }
774
775 if (hazard.hazard) {
776 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
777 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
778 skip |= sync_state.LogError(
779 rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
780 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " %s aspect during store with %s %s.", func_name,
781 string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string, store_op_string);
782 }
783 }
784 }
785 return skip;
786}
787
John Zulaufb027cdb2020-05-21 14:25:22 -0600788bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
789 const VkRect2D &render_area,
790 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
791 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600792 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
793 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
794 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600795}
796
John Zulauf3d84f1b2020-03-09 13:33:25 -0600797class HazardDetector {
798 SyncStageAccessIndex usage_index_;
799
800 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600801 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600802 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
803 return pos->second.DetectAsyncHazard(usage_index_);
804 }
805 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
806};
807
John Zulauf69133422020-05-20 14:55:53 -0600808class HazardDetectorWithOrdering {
809 const SyncStageAccessIndex usage_index_;
810 const SyncOrderingBarrier &ordering_;
811
812 public:
813 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
814 return pos->second.DetectHazard(usage_index_, ordering_);
815 }
816 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
817 return pos->second.DetectAsyncHazard(usage_index_);
818 }
819 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
820 : usage_index_(usage), ordering_(ordering) {}
821};
822
John Zulauf16adfc92020-04-08 10:28:33 -0600823HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600824 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600825 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600826 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600827}
828
John Zulauf16adfc92020-04-08 10:28:33 -0600829HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600830 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600831 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600832 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600833}
834
John Zulauf69133422020-05-20 14:55:53 -0600835template <typename Detector>
836HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
837 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
838 const VkExtent3D &extent, DetectOptions options) const {
839 if (!SimpleBinding(image)) return HazardResult();
840 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
841 const auto address_type = ImageAddressType(image);
842 const auto base_address = ResourceBaseAddress(image);
843 for (; range_gen->non_empty(); ++range_gen) {
844 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
845 if (hazard.hazard) return hazard;
846 }
847 return HazardResult();
848}
849
John Zulauf540266b2020-04-06 18:54:53 -0600850HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
851 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
852 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700853 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
854 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -0600855 return DetectHazard(image, current_usage, subresource_range, offset, extent);
856}
857
858HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
859 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
860 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -0600861 HazardDetector detector(current_usage);
862 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
863}
864
865HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
866 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
867 const VkOffset3D &offset, const VkExtent3D &extent) const {
868 HazardDetectorWithOrdering detector(current_usage, ordering);
869 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -0600870}
871
John Zulaufb027cdb2020-05-21 14:25:22 -0600872// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
873// should have reported the issue regarding an invalid attachment entry
874HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
875 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
876 VkImageAspectFlags aspect_mask) const {
877 if (view != nullptr) {
878 const IMAGE_STATE *image = view->image_state.get();
879 if (image != nullptr) {
880 auto *detect_range = &view->normalized_subresource_range;
881 VkImageSubresourceRange masked_range;
882 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
883 masked_range = view->normalized_subresource_range;
884 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
885 detect_range = &masked_range;
886 }
887
888 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
889 if (detect_range->aspectMask) {
890 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
891 }
892 }
893 }
894 return HazardResult();
895}
John Zulauf3d84f1b2020-03-09 13:33:25 -0600896class BarrierHazardDetector {
897 public:
898 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
899 SyncStageAccessFlags src_access_scope)
900 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
901
John Zulauf5f13a792020-03-10 07:31:21 -0600902 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
903 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -0700904 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600905 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
906 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
907 return pos->second.DetectAsyncHazard(usage_index_);
908 }
909
910 private:
911 SyncStageAccessIndex usage_index_;
912 VkPipelineStageFlags src_exec_scope_;
913 SyncStageAccessFlags src_access_scope_;
914};
915
John Zulauf16adfc92020-04-08 10:28:33 -0600916HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -0600917 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600918 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600919 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -0600920 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700921}
922
John Zulauf16adfc92020-04-08 10:28:33 -0600923HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600924 SyncStageAccessFlags src_access_scope,
925 const VkImageSubresourceRange &subresource_range,
926 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -0600927 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
928 VkOffset3D zero_offset = {0, 0, 0};
929 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700930}
931
John Zulauf355e49b2020-04-24 15:11:15 -0600932HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
933 SyncStageAccessFlags src_stage_accesses,
934 const VkImageMemoryBarrier &barrier) const {
935 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
936 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
937 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
938}
939
John Zulauf9cb530d2019-09-30 14:14:10 -0600940template <typename Flags, typename Map>
941SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
942 SyncStageAccessFlags scope = 0;
943 for (const auto &bit_scope : map) {
944 if (flag_mask < bit_scope.first) break;
945
946 if (flag_mask & bit_scope.first) {
947 scope |= bit_scope.second;
948 }
949 }
950 return scope;
951}
952
953SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
954 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
955}
956
957SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
958 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
959}
960
961// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
962SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -0600963 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
964 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
965 // 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 -0600966 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
967}
968
969template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -0700970void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -0600971 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
972 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -0600973 auto pos = accesses->lower_bound(range);
974 if (pos == accesses->end() || !pos->first.intersects(range)) {
975 // The range is empty, fill it with a default value.
976 pos = action.Infill(accesses, pos, range);
977 } else if (range.begin < pos->first.begin) {
978 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -0700979 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -0600980 } else if (pos->first.begin < range.begin) {
981 // Trim the beginning if needed
982 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
983 ++pos;
984 }
985
986 const auto the_end = accesses->end();
987 while ((pos != the_end) && pos->first.intersects(range)) {
988 if (pos->first.end > range.end) {
989 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
990 }
991
992 pos = action(accesses, pos);
993 if (pos == the_end) break;
994
995 auto next = pos;
996 ++next;
997 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
998 // Need to infill if next is disjoint
999 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001000 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001001 next = action.Infill(accesses, next, new_range);
1002 }
1003 pos = next;
1004 }
1005}
1006
1007struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001008 using Iterator = ResourceAccessRangeMap::iterator;
1009 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001010 // this is only called on gaps, and never returns a gap.
1011 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001012 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001013 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001014 }
John Zulauf5f13a792020-03-10 07:31:21 -06001015
John Zulauf5c5e88d2019-12-26 11:22:02 -07001016 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001017 auto &access_state = pos->second;
1018 access_state.Update(usage, tag);
1019 return pos;
1020 }
1021
John Zulauf16adfc92020-04-08 10:28:33 -06001022 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001023 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001024 : type(type_), context(context_), usage(usage_), tag(tag_) {}
1025 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001026 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001027 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001028 const ResourceUsageTag &tag;
1029};
1030
1031struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001032 using Iterator = ResourceAccessRangeMap::iterator;
1033 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001034
John Zulauf5c5e88d2019-12-26 11:22:02 -07001035 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001036 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001037 access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001038 return pos;
1039 }
1040
John Zulauf36bcf6a2020-02-03 15:12:52 -07001041 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
1042 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
1043 : src_exec_scope(src_exec_scope_),
1044 src_access_scope(src_access_scope_),
1045 dst_exec_scope(dst_exec_scope_),
1046 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001047
John Zulauf36bcf6a2020-02-03 15:12:52 -07001048 VkPipelineStageFlags src_exec_scope;
1049 SyncStageAccessFlags src_access_scope;
1050 VkPipelineStageFlags dst_exec_scope;
1051 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001052};
1053
1054struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001055 using Iterator = ResourceAccessRangeMap::iterator;
1056 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001057
John Zulauf5c5e88d2019-12-26 11:22:02 -07001058 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001059 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001060 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001061
1062 for (const auto &functor : barrier_functor) {
1063 functor(accesses, pos);
1064 }
1065 return pos;
1066 }
1067
John Zulauf36bcf6a2020-02-03 15:12:52 -07001068 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
1069 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -06001070 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001071 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001072 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
1073 barrier_functor.reserve(memoryBarrierCount);
1074 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
1075 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -07001076 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
1077 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -06001078 }
1079 }
1080
John Zulauf36bcf6a2020-02-03 15:12:52 -07001081 const VkPipelineStageFlags src_exec_scope;
1082 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001083 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
1084};
1085
John Zulauf355e49b2020-04-24 15:11:15 -06001086void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1087 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001088 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1089 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001090}
1091
John Zulauf16adfc92020-04-08 10:28:33 -06001092void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001093 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001094 if (!SimpleBinding(buffer)) return;
1095 const auto base_address = ResourceBaseAddress(buffer);
1096 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1097}
John Zulauf355e49b2020-04-24 15:11:15 -06001098
John Zulauf540266b2020-04-06 18:54:53 -06001099void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001100 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001101 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001102 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001103 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001104 const auto address_type = ImageAddressType(image);
1105 const auto base_address = ResourceBaseAddress(image);
1106 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001107 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001108 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001109 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001110}
John Zulauf7635de32020-05-29 17:14:15 -06001111void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1112 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1113 if (view != nullptr) {
1114 const IMAGE_STATE *image = view->image_state.get();
1115 if (image != nullptr) {
1116 auto *update_range = &view->normalized_subresource_range;
1117 VkImageSubresourceRange masked_range;
1118 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1119 masked_range = view->normalized_subresource_range;
1120 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1121 update_range = &masked_range;
1122 }
1123 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1124 }
1125 }
1126}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001127
John Zulauf355e49b2020-04-24 15:11:15 -06001128void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1129 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1130 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001131 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1132 subresource.layerCount};
1133 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1134}
1135
John Zulauf540266b2020-04-06 18:54:53 -06001136template <typename Action>
1137void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001138 if (!SimpleBinding(buffer)) return;
1139 const auto base_address = ResourceBaseAddress(buffer);
1140 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001141}
1142
1143template <typename Action>
1144void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1145 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001146 if (!SimpleBinding(image)) return;
1147 const auto address_type = ImageAddressType(image);
1148 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001149
locke-lunargae26eac2020-04-16 15:29:05 -06001150 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001151 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001152
John Zulauf16adfc92020-04-08 10:28:33 -06001153 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001154 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001155 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001156 }
1157}
1158
John Zulauf7635de32020-05-29 17:14:15 -06001159void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1160 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1161 const ResourceUsageTag &tag) {
1162 UpdateStateResolveAction update(*this, tag);
1163 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1164}
1165
John Zulaufaff20662020-06-01 14:07:58 -06001166void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1167 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1168 const ResourceUsageTag &tag) {
1169 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1170 VkExtent3D extent = CastTo3D(render_area.extent);
1171 VkOffset3D offset = CastTo3D(render_area.offset);
1172
1173 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1174 if (rp_state.attachment_last_subpass[i] == subpass) {
1175 if (attachment_views[i] == nullptr) continue; // UNUSED
1176 const auto &view = *attachment_views[i];
1177 const IMAGE_STATE *image = view.image_state.get();
1178 if (image == nullptr) continue;
1179
1180 const auto &ci = attachment_ci[i];
1181 const bool has_depth = FormatHasDepth(ci.format);
1182 const bool has_stencil = FormatHasStencil(ci.format);
1183 const bool is_color = !(has_depth || has_stencil);
1184 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1185
1186 if (is_color && store_op_stores) {
1187 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1188 offset, extent, tag);
1189 } else {
1190 auto update_range = view.normalized_subresource_range;
1191 if (has_depth && store_op_stores) {
1192 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1193 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1194 tag);
1195 }
1196 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1197 if (has_stencil && stencil_op_stores) {
1198 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1199 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1200 tag);
1201 }
1202 }
1203 }
1204 }
1205}
1206
John Zulauf540266b2020-04-06 18:54:53 -06001207template <typename Action>
1208void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1209 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001210 for (const auto address_type : kAddressTypes) {
1211 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001212 }
1213}
1214
1215void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001216 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1217 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -06001218 for (const auto address_type : kAddressTypes) {
John Zulauf355e49b2020-04-24 15:11:15 -06001219 context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier,
1220 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001221 }
1222 }
1223}
1224
John Zulauf355e49b2020-04-24 15:11:15 -06001225void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1226 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1227 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
1228 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1229 UpdateMemoryAccess(image, subresource_range, barrier_action);
1230}
1231
John Zulauf7635de32020-05-29 17:14:15 -06001232// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001233void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1234 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1235 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
1236 bool layout_transition, const ResourceUsageTag &tag) {
1237 if (layout_transition) {
1238 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
1239 tag);
1240 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
1241 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -06001242 } else {
1243 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -06001244 }
John Zulauf355e49b2020-04-24 15:11:15 -06001245}
1246
John Zulauf7635de32020-05-29 17:14:15 -06001247// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001248void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
1249 const VkImageSubresourceRange &subresource_range, bool layout_transition,
1250 const ResourceUsageTag &tag) {
1251 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
1252 subresource_range, layout_transition, tag);
1253}
1254
1255// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001256HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001257 if (!attach_view) return HazardResult();
1258 const auto image_state = attach_view->image_state.get();
1259 if (!image_state) return HazardResult();
1260
John Zulauf355e49b2020-04-24 15:11:15 -06001261 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001262 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001263
1264 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulauf7635de32020-05-29 17:14:15 -06001265 auto hazard = track_back.context->DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope,
1266 track_back.barrier.src_access_scope,
1267 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001268 if (!hazard.hazard) {
1269 // The Async hazard check is against the current context's async set.
John Zulauf7635de32020-05-29 17:14:15 -06001270 hazard = DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope, track_back.barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001271 attach_view->normalized_subresource_range, kDetectAsync);
1272 }
1273 return hazard;
1274}
1275
1276// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1277bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1278
1279 const VkRenderPassBeginInfo *pRenderPassBegin,
1280 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1281 const char *func_name) const {
1282 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1283 bool skip = false;
1284 uint32_t subpass = 0;
1285 const auto &transitions = rp_state.subpass_transitions[subpass];
1286 if (transitions.size()) {
1287 const std::vector<AccessContext> empty_context_vector;
1288 // Create context we can use to validate against...
1289 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1290 const_cast<AccessContext *>(&cb_access_context_));
1291
1292 assert(pRenderPassBegin);
1293 if (nullptr == pRenderPassBegin) return skip;
1294
1295 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1296 assert(fb_state);
1297 if (nullptr == fb_state) return skip;
1298
1299 // Create a limited array of views (which we'll need to toss
1300 std::vector<const IMAGE_VIEW_STATE *> views;
1301 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
1302 const auto attachment_count = count_attachment.first;
1303 const auto *attachments = count_attachment.second;
1304 views.resize(attachment_count, nullptr);
1305 for (const auto &transition : transitions) {
1306 assert(transition.attachment < attachment_count);
1307 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
1308 }
1309
John Zulauf7635de32020-05-29 17:14:15 -06001310 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
1311 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001312 }
1313 return skip;
1314}
1315
locke-lunarg61870c22020-06-09 14:51:50 -06001316bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1317 const char *func_name) const {
1318 bool skip = false;
1319 const PIPELINE_STATE *pPipe = nullptr;
1320 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1321 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1322 if (!pPipe || !per_sets) {
1323 return skip;
1324 }
1325
1326 using DescriptorClass = cvdescriptorset::DescriptorClass;
1327 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1328 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1329 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1330 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1331
1332 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarg37047832020-06-12 13:44:45 -06001333 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001334 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1335 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001336 for (const auto &set_binding : stage_state.descriptor_uses) {
1337 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1338 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1339 set_binding.first.second);
1340 const auto descriptor_type = binding_it.GetType();
1341 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1342 auto array_idx = 0;
1343
1344 if (binding_it.IsVariableDescriptorCount()) {
1345 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1346 }
1347 SyncStageAccessIndex sync_index =
1348 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1349
1350 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1351 uint32_t index = i - index_range.start;
1352 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1353 switch (descriptor->GetClass()) {
1354 case DescriptorClass::ImageSampler:
1355 case DescriptorClass::Image: {
1356 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1357 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1358 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1359 } else {
1360 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1361 }
1362 if (!img_view_state) continue;
1363 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1364 VkExtent3D extent = {};
1365 VkOffset3D offset = {};
1366 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1367 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1368 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1369 } else {
1370 extent = img_state->createInfo.extent;
1371 }
1372 auto hazard = current_context_->DetectHazard(*img_state, sync_index,
1373 img_view_state->normalized_subresource_range, offset, extent);
1374 if (hazard.hazard) {
1375 skip |=
1376 sync_state_->LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1377 "%s: Hazard %s for %s in %s, %s, and %s binding #%d index %d", func_name,
1378 string_SyncHazard(hazard.hazard),
1379 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1380 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1381 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1382 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1383 set_binding.first.second, index);
1384 }
1385 break;
1386 }
1387 case DescriptorClass::TexelBuffer: {
1388 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1389 if (!buf_view_state) continue;
1390 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1391 ResourceAccessRange range =
1392 MakeRange(buf_view_state->create_info.offset,
1393 GetRealWholeSize(buf_view_state->create_info.offset, buf_view_state->create_info.range,
1394 buf_state->createInfo.size));
1395 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
1396 if (hazard.hazard) {
1397 skip |=
1398 sync_state_->LogError(buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
1399 "%s: Hazard %s for %s in %s, %s, and %s binding #%d index %d", func_name,
1400 string_SyncHazard(hazard.hazard),
1401 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1402 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1403 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1404 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1405 set_binding.first.second, index);
1406 }
1407 break;
1408 }
1409 case DescriptorClass::GeneralBuffer: {
1410 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1411 auto buf_state = buffer_descriptor->GetBufferState();
1412 if (!buf_state) continue;
1413 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1414 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
1415 if (hazard.hazard) {
1416 skip |= sync_state_->LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
1417 "%s: Hazard %s for %s in %s, %s, and %s binding #%d index %d", func_name,
1418 string_SyncHazard(hazard.hazard),
1419 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
1420 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1421 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1422 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1423 set_binding.first.second, index);
1424 }
1425 break;
1426 }
1427 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1428 default:
1429 break;
1430 }
1431 }
1432 }
1433 }
1434 return skip;
1435}
1436
1437void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1438 const ResourceUsageTag &tag) {
1439 const PIPELINE_STATE *pPipe = nullptr;
1440 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1441 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1442 if (!pPipe || !per_sets) {
1443 return;
1444 }
1445
1446 using DescriptorClass = cvdescriptorset::DescriptorClass;
1447 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1448 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1449 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1450 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1451
1452 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001453 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
1454 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1455 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001456 for (const auto &set_binding : stage_state.descriptor_uses) {
1457 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1458 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1459 set_binding.first.second);
1460 const auto descriptor_type = binding_it.GetType();
1461 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1462 auto array_idx = 0;
1463
1464 if (binding_it.IsVariableDescriptorCount()) {
1465 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1466 }
1467 SyncStageAccessIndex sync_index =
1468 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1469
1470 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1471 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1472 switch (descriptor->GetClass()) {
1473 case DescriptorClass::ImageSampler:
1474 case DescriptorClass::Image: {
1475 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1476 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1477 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1478 } else {
1479 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1480 }
1481 if (!img_view_state) continue;
1482 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1483 VkExtent3D extent = {};
1484 VkOffset3D offset = {};
1485 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1486 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1487 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1488 } else {
1489 extent = img_state->createInfo.extent;
1490 }
1491 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1492 offset, extent, tag);
1493 break;
1494 }
1495 case DescriptorClass::TexelBuffer: {
1496 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1497 if (!buf_view_state) continue;
1498 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1499 ResourceAccessRange range =
1500 MakeRange(buf_view_state->create_info.offset, buf_view_state->create_info.range);
1501 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1502 break;
1503 }
1504 case DescriptorClass::GeneralBuffer: {
1505 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1506 auto buf_state = buffer_descriptor->GetBufferState();
1507 if (!buf_state) continue;
1508 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1509 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1510 break;
1511 }
1512 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1513 default:
1514 break;
1515 }
1516 }
1517 }
1518 }
1519}
1520
1521bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1522 bool skip = false;
1523 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1524 if (!pPipe) {
1525 return skip;
1526 }
1527
1528 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1529 const auto &binding_buffers_size = binding_buffers.size();
1530 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1531
1532 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1533 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1534 if (binding_description.binding < binding_buffers_size) {
1535 const auto &binding_buffer = binding_buffers[binding_description.binding];
1536 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1537
1538 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1539 VkDeviceSize range_start = 0;
1540 VkDeviceSize range_size = 0;
1541 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1542 binding_description.stride);
1543 ResourceAccessRange range = MakeRange(range_start, range_size);
1544 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1545 if (hazard.hazard) {
1546 skip |= sync_state_->LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
1547 "%s: Hazard %s for vertex %s in %s", func_name, string_SyncHazard(hazard.hazard),
1548 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
1549 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str());
1550 }
1551 }
1552 }
1553 return skip;
1554}
1555
1556void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1557 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1558 if (!pPipe) {
1559 return;
1560 }
1561 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1562 const auto &binding_buffers_size = binding_buffers.size();
1563 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1564
1565 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1566 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1567 if (binding_description.binding < binding_buffers_size) {
1568 const auto &binding_buffer = binding_buffers[binding_description.binding];
1569 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1570
1571 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1572 VkDeviceSize range_start = 0;
1573 VkDeviceSize range_size = 0;
1574 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1575 binding_description.stride);
1576 ResourceAccessRange range = MakeRange(range_start, range_size);
1577 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1578 }
1579 }
1580}
1581
1582bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1583 bool skip = false;
1584 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
1585
1586 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1587 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1588 VkDeviceSize range_start = 0;
1589 VkDeviceSize range_size = 0;
1590 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1591 indexCount, index_size);
1592 ResourceAccessRange range = MakeRange(range_start, range_size);
1593 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1594 if (hazard.hazard) {
1595 skip |= sync_state_->LogError(index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
1596 "%s: Hazard %s for index %s in %s", func_name, string_SyncHazard(hazard.hazard),
1597 sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
1598 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str());
1599 }
1600
1601 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1602 // We will detect more accurate range in the future.
1603 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1604 return skip;
1605}
1606
1607void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
1608 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return;
1609
1610 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1611 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1612 VkDeviceSize range_start = 0;
1613 VkDeviceSize range_size = 0;
1614 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1615 indexCount, index_size);
1616 ResourceAccessRange range = MakeRange(range_start, range_size);
1617 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1618
1619 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1620 // We will detect more accurate range in the future.
1621 RecordDrawVertex(UINT32_MAX, 0, tag);
1622}
1623
1624bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
1625 return current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1626 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1627}
1628
1629void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001630 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1631 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001632}
1633
John Zulauf355e49b2020-04-24 15:11:15 -06001634bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001635 bool skip = false;
John Zulauf1507ee42020-05-18 11:33:09 -06001636 skip |=
1637 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001638
1639 return skip;
1640}
1641
1642bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1643 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001644 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001645 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -06001646 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1647 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001648
1649 return skip;
1650}
1651
1652void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1653 assert(sync_state_);
1654 if (!cb_state_) return;
1655
1656 // Create an access context the current renderpass.
1657 render_pass_contexts_.emplace_back(&cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06001658 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf355e49b2020-04-24 15:11:15 -06001659 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001660 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001661}
1662
John Zulauf355e49b2020-04-24 15:11:15 -06001663void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001664 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001665 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001666 current_context_ = &current_renderpass_context_->CurrentContext();
1667}
1668
John Zulauf355e49b2020-04-24 15:11:15 -06001669void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001670 assert(current_renderpass_context_);
1671 if (!current_renderpass_context_) return;
1672
John Zulauf7635de32020-05-29 17:14:15 -06001673 current_renderpass_context_->RecordEndRenderPass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001674 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001675 current_renderpass_context_ = nullptr;
1676}
1677
locke-lunarg61870c22020-06-09 14:51:50 -06001678bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1679 const VkRect2D &render_area, const char *func_name) const {
1680 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001681 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001682 if (!pPipe ||
1683 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001684 return skip;
1685 }
1686 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001687 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1688 VkExtent3D extent = CastTo3D(render_area.extent);
1689 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001690
locke-lunarg44f9bb12020-06-10 14:43:57 -06001691 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001692 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1693 for (const auto location : list) {
1694 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1695 continue;
1696 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
1697 HazardResult hazard = external_context_->DetectHazard(
1698 img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent);
1699 if (hazard.hazard) {
1700 skip |= sync_state.LogError(
1701 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1702 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d", func_name,
1703 string_SyncHazard(hazard.hazard), sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1704 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass, location);
locke-lunarg61870c22020-06-09 14:51:50 -06001705 }
1706 }
1707 }
locke-lunarg37047832020-06-12 13:44:45 -06001708
1709 // PHASE1 TODO: Add layout based read/vs. write selection.
1710 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1711 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1712 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001713 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001714 bool depth_write = false, stencil_write = false;
1715
1716 // PHASE1 TODO: These validation should be in core_checks.
1717 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1718 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1719 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1720 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1721 depth_write = true;
1722 }
1723 // PHASE1 TODO: It needs to check if stencil is writable.
1724 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1725 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1726 // PHASE1 TODO: These validation should be in core_checks.
1727 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1728 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1729 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1730 stencil_write = true;
1731 }
1732
1733 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1734 if (depth_write) {
1735 HazardResult hazard =
1736 external_context_->DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1737 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
1738 if (hazard.hazard) {
1739 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1740 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment",
1741 func_name, string_SyncHazard(hazard.hazard),
1742 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1743 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass);
1744 }
1745 }
1746 if (stencil_write) {
1747 HazardResult hazard =
1748 external_context_->DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1749 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
1750 if (hazard.hazard) {
1751 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1752 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment",
1753 func_name, string_SyncHazard(hazard.hazard),
1754 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1755 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass);
1756 }
locke-lunarg61870c22020-06-09 14:51:50 -06001757 }
1758 }
1759 return skip;
1760}
1761
locke-lunarg96dc9632020-06-10 17:22:18 -06001762void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1763 const ResourceUsageTag &tag) {
1764 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001765 if (!pPipe ||
1766 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001767 return;
1768 }
1769 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001770 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1771 VkExtent3D extent = CastTo3D(render_area.extent);
1772 VkOffset3D offset = CastTo3D(render_area.offset);
1773
locke-lunarg44f9bb12020-06-10 14:43:57 -06001774 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001775 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1776 for (const auto location : list) {
1777 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1778 continue;
1779 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
1780 external_context_->UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset,
1781 extent, 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001782 }
1783 }
locke-lunarg37047832020-06-12 13:44:45 -06001784
1785 // PHASE1 TODO: Add layout based read/vs. write selection.
1786 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1787 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1788 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001789 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001790 bool depth_write = false, stencil_write = false;
1791
1792 // PHASE1 TODO: These validation should be in core_checks.
1793 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1794 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1795 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1796 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1797 depth_write = true;
1798 }
1799 // PHASE1 TODO: It needs to check if stencil is writable.
1800 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1801 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1802 // PHASE1 TODO: These validation should be in core_checks.
1803 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1804 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1805 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1806 stencil_write = true;
1807 }
1808
1809 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1810 if (depth_write) {
1811 external_context_->UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1812 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
1813 }
1814 if (stencil_write) {
1815 external_context_->UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1816 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
1817 }
locke-lunarg61870c22020-06-09 14:51:50 -06001818 }
1819}
1820
John Zulauf1507ee42020-05-18 11:33:09 -06001821bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1822 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001823 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001824 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001825 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1826 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001827 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1828 func_name);
1829
John Zulauf355e49b2020-04-24 15:11:15 -06001830 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06001831 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06001832 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1833 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1834 return skip;
1835}
1836bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
1837 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001838 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06001839 bool skip = false;
1840 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1841 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001842 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1843 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06001844 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001845 return skip;
1846}
1847
John Zulauf7635de32020-05-29 17:14:15 -06001848AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
1849 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
1850}
1851
1852bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
1853 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001854 bool skip = false;
1855
John Zulauf7635de32020-05-29 17:14:15 -06001856 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
1857 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
1858 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1859 // to apply and only copy then, if this proves a hot spot.
1860 std::unique_ptr<AccessContext> proxy_for_current;
1861
John Zulauf355e49b2020-04-24 15:11:15 -06001862 // Validate the "finalLayout" transitions to external
1863 // Get them from where there we're hidding in the extra entry.
1864 const auto &final_transitions = rp_state_->subpass_transitions.back();
1865 for (const auto &transition : final_transitions) {
1866 const auto &attach_view = attachment_views_[transition.attachment];
1867 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
1868 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06001869 auto *context = trackback.context;
1870
1871 if (transition.prev_pass == current_subpass_) {
1872 if (!proxy_for_current) {
1873 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
1874 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
1875 }
1876 context = proxy_for_current.get();
1877 }
1878
1879 auto hazard = context->DetectImageBarrierHazard(
John Zulauf355e49b2020-04-24 15:11:15 -06001880 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
1881 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
1882 if (hazard.hazard) {
1883 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
1884 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
1885 " final image layout transition.",
1886 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment);
1887 }
1888 }
1889 return skip;
1890}
1891
1892void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
1893 // Add layout transitions...
1894 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
1895 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06001896 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06001897 for (const auto &transition : transitions) {
1898 const auto attachment_view = attachment_views_[transition.attachment];
1899 if (!attachment_view) continue;
1900 const auto image = attachment_view->image_state.get();
1901 if (!image) continue;
1902
1903 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -06001904 auto insert_pair = view_seen.insert(attachment_view);
1905 if (insert_pair.second) {
1906 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
1907 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
1908
1909 } else {
1910 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
1911 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
1912 auto barrier_to_transition = barrier->barrier;
1913 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
1914 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
1915 }
John Zulauf355e49b2020-04-24 15:11:15 -06001916 }
1917}
1918
John Zulauf1507ee42020-05-18 11:33:09 -06001919void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
1920 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
1921 auto &subpass_context = subpass_contexts_[current_subpass_];
1922 VkExtent3D extent = CastTo3D(render_area.extent);
1923 VkOffset3D offset = CastTo3D(render_area.offset);
1924
1925 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
1926 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
1927 if (attachment_views_[i] == nullptr) continue; // UNUSED
1928 const auto &view = *attachment_views_[i];
1929 const IMAGE_STATE *image = view.image_state.get();
1930 if (image == nullptr) continue;
1931
1932 const auto &ci = attachment_ci[i];
1933 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001934 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001935 const bool is_color = !(has_depth || has_stencil);
1936
1937 if (is_color) {
1938 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
1939 extent, tag);
1940 } else {
1941 auto update_range = view.normalized_subresource_range;
1942 if (has_depth) {
1943 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1944 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
1945 }
1946 if (has_stencil) {
1947 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1948 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
1949 tag);
1950 }
1951 }
1952 }
1953 }
1954}
1955
John Zulauf355e49b2020-04-24 15:11:15 -06001956void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
1957 VkQueueFlags queue_flags, const ResourceUsageTag &tag) {
1958 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06001959 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06001960 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
1961 // Add this for all subpasses here so that they exsist during next subpass validation
1962 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
1963 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context_);
1964 }
1965 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
1966
1967 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06001968 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001969}
John Zulauf1507ee42020-05-18 11:33:09 -06001970
1971void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001972 // Resolves are against *prior* subpass context and thus *before* the subpass increment
1973 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001974 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06001975
John Zulauf355e49b2020-04-24 15:11:15 -06001976 current_subpass_++;
1977 assert(current_subpass_ < subpass_contexts_.size());
1978 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06001979 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001980}
1981
John Zulauf7635de32020-05-29 17:14:15 -06001982void RenderPassAccessContext::RecordEndRenderPass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001983 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06001984 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001985 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06001986
John Zulauf355e49b2020-04-24 15:11:15 -06001987 // Export the accesses from the renderpass...
1988 external_context_->ResolveChildContexts(subpass_contexts_);
1989
1990 // Add the "finalLayout" transitions to external
1991 // Get them from where there we're hidding in the extra entry.
1992 const auto &final_transitions = rp_state_->subpass_transitions.back();
1993 for (const auto &transition : final_transitions) {
1994 const auto &attachment = attachment_views_[transition.attachment];
1995 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
1996 assert(external_context_ == last_trackback.context);
1997 external_context_->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
1998 attachment->normalized_subresource_range, true, tag);
1999 }
2000}
2001
John Zulauf3d84f1b2020-03-09 13:33:25 -06002002SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2003 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2004 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2005 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2006 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2007 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2008 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2009}
2010
2011void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
2012 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
2013 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
2014}
2015
John Zulauf9cb530d2019-09-30 14:14:10 -06002016HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2017 HazardResult hazard;
2018 auto usage = FlagBit(usage_index);
2019 if (IsRead(usage)) {
John Zulaufc9201222020-05-13 15:13:03 -06002020 if (last_write && IsWriteHazard(usage)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002021 hazard.Set(READ_AFTER_WRITE, write_tag);
2022 }
2023 } else {
2024 // Assume write
2025 // TODO determine what to do with READ-WRITE usage states if any
2026 // Write-After-Write check -- if we have a previous write to test against
2027 if (last_write && IsWriteHazard(usage)) {
2028 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2029 } else {
John Zulauf69133422020-05-20 14:55:53 -06002030 // Look for casus belli for WAR
John Zulauf9cb530d2019-09-30 14:14:10 -06002031 const auto usage_stage = PipelineStageBit(usage_index);
2032 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2033 if (IsReadHazard(usage_stage, last_reads[read_index])) {
2034 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
2035 break;
2036 }
2037 }
2038 }
2039 }
2040 return hazard;
2041}
2042
John Zulauf69133422020-05-20 14:55:53 -06002043HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2044 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2045 HazardResult hazard;
2046 const auto usage = FlagBit(usage_index);
2047 const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good.
2048 if (IsRead(usage)) {
2049 if (!write_is_ordered && IsWriteHazard(usage)) {
2050 hazard.Set(READ_AFTER_WRITE, write_tag);
2051 }
2052 } else {
2053 if (!write_is_ordered && IsWriteHazard(usage)) {
2054 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2055 } else {
2056 const auto usage_stage = PipelineStageBit(usage_index);
2057 const auto unordered_reads = last_read_stages & ~ordering.exec_scope;
2058 if (unordered_reads) {
2059 // Look for any WAR hazards outside the ordered set of stages
2060 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2061 if (last_reads[read_index].stage & unordered_reads) {
2062 if (IsReadHazard(usage_stage, last_reads[read_index])) {
2063 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
2064 break;
2065 }
2066 }
2067 }
2068 }
2069 }
2070 }
2071 return hazard;
2072}
2073
John Zulauf2f952d22020-02-10 11:34:51 -07002074// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002075HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002076 HazardResult hazard;
2077 auto usage = FlagBit(usage_index);
2078 if (IsRead(usage)) {
2079 if (last_write != 0) {
2080 hazard.Set(READ_RACING_WRITE, write_tag);
2081 }
2082 } else {
2083 if (last_write != 0) {
2084 hazard.Set(WRITE_RACING_WRITE, write_tag);
2085 } else if (last_read_count > 0) {
2086 hazard.Set(WRITE_RACING_READ, last_reads[0].tag);
2087 }
2088 }
2089 return hazard;
2090}
2091
John Zulauf36bcf6a2020-02-03 15:12:52 -07002092HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2093 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002094 // Only supporting image layout transitions for now
2095 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2096 HazardResult hazard;
2097 if (last_write) {
2098 // If the previous write is *not* in the 1st access scope
2099 // *AND* the current barrier is not in the dependency chain
2100 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2101 // then the barrier access is unsafe (R/W after W)
John Zulauf36bcf6a2020-02-03 15:12:52 -07002102 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
John Zulauf0cb5be22020-01-23 12:18:22 -07002103 // TODO: Do we need a difference hazard name for this?
2104 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2105 }
John Zulauf355e49b2020-04-24 15:11:15 -06002106 }
2107 if (!hazard.hazard) {
2108 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002109 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002110 const auto &read_access = last_reads[read_index];
2111 // If the read stage is not in the src sync sync
2112 // *AND* not execution chained with an existing sync barrier (that's the or)
2113 // then the barrier access is unsafe (R/W after R)
2114 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
2115 hazard.Set(WRITE_AFTER_READ, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002116 break;
2117 }
2118 }
2119 }
2120 return hazard;
2121}
2122
John Zulauf5f13a792020-03-10 07:31:21 -06002123// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2124// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2125// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2126void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2127 if (write_tag.IsBefore(other.write_tag)) {
2128 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
2129 *this = other;
2130 } else if (!other.write_tag.IsBefore(write_tag)) {
2131 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2132 // dependency chaining logic or any stage expansion)
2133 write_barriers |= other.write_barriers;
2134
2135 // Merge that read states
2136 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2137 auto &other_read = other.last_reads[other_read_index];
2138 if (last_read_stages & other_read.stage) {
2139 // Merge in the barriers for read stages that exist in *both* this and other
2140 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
2141 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
2142 auto &my_read = last_reads[my_read_index];
2143 if (other_read.stage == my_read.stage) {
2144 if (my_read.tag.IsBefore(other_read.tag)) {
2145 my_read.tag = other_read.tag;
2146 }
2147 my_read.barriers |= other_read.barriers;
2148 break;
2149 }
2150 }
2151 } else {
2152 // The other read stage doesn't exist in this, so add it.
2153 last_reads[last_read_count] = other_read;
2154 last_read_count++;
2155 last_read_stages |= other_read.stage;
2156 }
2157 }
2158 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
2159 // it.
2160}
2161
John Zulauf9cb530d2019-09-30 14:14:10 -06002162void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2163 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2164 const auto usage_bit = FlagBit(usage_index);
2165 if (IsRead(usage_index)) {
2166 // Mulitple outstanding reads may be of interest and do dependency chains independently
2167 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2168 const auto usage_stage = PipelineStageBit(usage_index);
2169 if (usage_stage & last_read_stages) {
2170 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2171 ReadState &access = last_reads[read_index];
2172 if (access.stage == usage_stage) {
2173 access.barriers = 0;
2174 access.tag = tag;
2175 break;
2176 }
2177 }
2178 } else {
2179 // We don't have this stage in the list yet...
2180 assert(last_read_count < last_reads.size());
2181 ReadState &access = last_reads[last_read_count++];
2182 access.stage = usage_stage;
2183 access.barriers = 0;
2184 access.tag = tag;
2185 last_read_stages |= usage_stage;
2186 }
2187 } else {
2188 // Assume write
2189 // TODO determine what to do with READ-WRITE operations if any
2190 // Clobber last read and both sets of barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2191 // if the last_reads/last_write were unsafe, we've reported them,
2192 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
2193 last_read_count = 0;
2194 last_read_stages = 0;
2195
2196 write_barriers = 0;
2197 write_dependency_chain = 0;
2198 write_tag = tag;
2199 last_write = usage_bit;
2200 }
2201}
John Zulauf5f13a792020-03-10 07:31:21 -06002202
John Zulauf9cb530d2019-09-30 14:14:10 -06002203void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
2204 // Execution Barriers only protect read operations
2205 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2206 ReadState &access = last_reads[read_index];
2207 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2208 if (srcStageMask & (access.stage | access.barriers)) {
2209 access.barriers |= dstStageMask;
2210 }
2211 }
2212 if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask;
2213}
2214
John Zulauf36bcf6a2020-02-03 15:12:52 -07002215void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
2216 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002217 // Assuming we've applied the execution side of this barrier, we update just the write
2218 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07002219 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
2220 write_barriers |= dst_access_scope;
2221 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06002222 }
2223}
2224
John Zulaufd1f85d42020-04-15 12:23:15 -06002225void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002226 auto *access_context = GetAccessContextNoInsert(command_buffer);
2227 if (access_context) {
2228 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002229 }
2230}
2231
John Zulaufd1f85d42020-04-15 12:23:15 -06002232void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2233 auto access_found = cb_access_state.find(command_buffer);
2234 if (access_found != cb_access_state.end()) {
2235 access_found->second->Reset();
2236 cb_access_state.erase(access_found);
2237 }
2238}
2239
John Zulauf540266b2020-04-06 18:54:53 -06002240void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002241 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
2242 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002243 const VkMemoryBarrier *pMemoryBarriers) {
2244 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07002245 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002246 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002247 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002248}
2249
John Zulauf540266b2020-04-06 18:54:53 -06002250void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002251 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2252 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002253 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002254 for (uint32_t index = 0; index < barrier_count; index++) {
locke-lunarg3c038002020-04-30 23:08:08 -06002255 auto barrier = barriers[index];
John Zulauf9cb530d2019-09-30 14:14:10 -06002256 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2257 if (!buffer) continue;
locke-lunarg3c038002020-04-30 23:08:08 -06002258 barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002259 ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002260 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2261 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2262 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2263 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002264 }
2265}
2266
John Zulauf540266b2020-04-06 18:54:53 -06002267void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2268 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2269 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002270 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002271 for (uint32_t index = 0; index < barrier_count; index++) {
2272 const auto &barrier = barriers[index];
2273 const auto *image = Get<IMAGE_STATE>(barrier.image);
2274 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002275 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002276 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2277 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2278 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2279 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
2280 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002281 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002282}
2283
2284bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2285 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2286 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002287 const auto *cb_context = GetAccessContext(commandBuffer);
2288 assert(cb_context);
2289 if (!cb_context) return skip;
2290 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002291
John Zulauf3d84f1b2020-03-09 13:33:25 -06002292 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002293 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002294 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002295
2296 for (uint32_t region = 0; region < regionCount; region++) {
2297 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002298 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002299 ResourceAccessRange src_range = MakeRange(
2300 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002301 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002302 if (hazard.hazard) {
2303 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002304 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
2305 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
2306 report_data->FormatHandle(srcBuffer).c_str(), region);
John Zulauf9cb530d2019-09-30 14:14:10 -06002307 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002308 }
John Zulauf16adfc92020-04-08 10:28:33 -06002309 if (dst_buffer && !skip) {
locke-lunargff255f92020-05-13 18:53:52 -06002310 ResourceAccessRange dst_range = MakeRange(
2311 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf355e49b2020-04-24 15:11:15 -06002312 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002313 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002314 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
2315 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
2316 report_data->FormatHandle(dstBuffer).c_str(), region);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002317 }
2318 }
2319 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002320 }
2321 return skip;
2322}
2323
2324void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2325 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002326 auto *cb_context = GetAccessContext(commandBuffer);
2327 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002328 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002329 auto *context = cb_context->GetCurrentAccessContext();
2330
John Zulauf9cb530d2019-09-30 14:14:10 -06002331 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002332 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002333
2334 for (uint32_t region = 0; region < regionCount; region++) {
2335 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002336 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002337 ResourceAccessRange src_range = MakeRange(
2338 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002339 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002340 }
John Zulauf16adfc92020-04-08 10:28:33 -06002341 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002342 ResourceAccessRange dst_range = MakeRange(
2343 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002344 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002345 }
2346 }
2347}
2348
2349bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2350 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2351 const VkImageCopy *pRegions) const {
2352 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002353 const auto *cb_access_context = GetAccessContext(commandBuffer);
2354 assert(cb_access_context);
2355 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002356
John Zulauf3d84f1b2020-03-09 13:33:25 -06002357 const auto *context = cb_access_context->GetCurrentAccessContext();
2358 assert(context);
2359 if (!context) return skip;
2360
2361 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2362 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002363 for (uint32_t region = 0; region < regionCount; region++) {
2364 const auto &copy_region = pRegions[region];
2365 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002366 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002367 copy_region.srcOffset, copy_region.extent);
2368 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002369 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
2370 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
2371 report_data->FormatHandle(srcImage).c_str(), region);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002372 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002373 }
2374
2375 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002376 VkExtent3D dst_copy_extent =
2377 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002378 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002379 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002380 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002381 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
2382 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
2383 report_data->FormatHandle(dstImage).c_str(), region);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002384 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002385 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002386 }
2387 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002388
John Zulauf5c5e88d2019-12-26 11:22:02 -07002389 return skip;
2390}
2391
2392void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2393 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2394 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002395 auto *cb_access_context = GetAccessContext(commandBuffer);
2396 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002397 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002398 auto *context = cb_access_context->GetCurrentAccessContext();
2399 assert(context);
2400
John Zulauf5c5e88d2019-12-26 11:22:02 -07002401 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002402 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002403
2404 for (uint32_t region = 0; region < regionCount; region++) {
2405 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002406 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002407 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2408 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002409 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002410 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002411 VkExtent3D dst_copy_extent =
2412 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002413 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2414 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002415 }
2416 }
2417}
2418
2419bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2420 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2421 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2422 uint32_t bufferMemoryBarrierCount,
2423 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2424 uint32_t imageMemoryBarrierCount,
2425 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2426 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002427 const auto *cb_access_context = GetAccessContext(commandBuffer);
2428 assert(cb_access_context);
2429 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002430
John Zulauf3d84f1b2020-03-09 13:33:25 -06002431 const auto *context = cb_access_context->GetCurrentAccessContext();
2432 assert(context);
2433 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002434
John Zulauf3d84f1b2020-03-09 13:33:25 -06002435 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002436 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2437 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002438 // Validate Image Layout transitions
2439 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2440 const auto &barrier = pImageMemoryBarriers[index];
2441 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2442 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2443 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002444 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002445 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002446 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002447 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
2448 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s", string_SyncHazard(hazard.hazard),
2449 index, report_data->FormatHandle(barrier.image).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002450 }
2451 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002452
2453 return skip;
2454}
2455
2456void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2457 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2458 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2459 uint32_t bufferMemoryBarrierCount,
2460 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2461 uint32_t imageMemoryBarrierCount,
2462 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002463 auto *cb_access_context = GetAccessContext(commandBuffer);
2464 assert(cb_access_context);
2465 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002466 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002467 auto access_context = cb_access_context->GetCurrentAccessContext();
2468 assert(access_context);
2469 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002470
John Zulauf3d84f1b2020-03-09 13:33:25 -06002471 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002472 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002473 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002474 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2475 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2476 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002477 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
2478 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002479 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06002480 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002481
2482 // 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 -06002483 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07002484 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06002485}
2486
2487void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
2488 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
2489 // The state tracker sets up the device state
2490 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
2491
John Zulauf5f13a792020-03-10 07:31:21 -06002492 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
2493 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06002494 // TODO: Find a good way to do this hooklessly.
2495 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
2496 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
2497 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
2498
John Zulaufd1f85d42020-04-15 12:23:15 -06002499 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2500 sync_device_state->ResetCommandBufferCallback(command_buffer);
2501 });
2502 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2503 sync_device_state->FreeCommandBufferCallback(command_buffer);
2504 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002505}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002506
John Zulauf355e49b2020-04-24 15:11:15 -06002507bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2508 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2509 bool skip = false;
2510 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2511 auto cb_context = GetAccessContext(commandBuffer);
2512
2513 if (rp_state && cb_context) {
2514 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2515 }
2516
2517 return skip;
2518}
2519
2520bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2521 VkSubpassContents contents) const {
2522 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2523 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2524 subpass_begin_info.contents = contents;
2525 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
2526 return skip;
2527}
2528
2529bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2530 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2531 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2532 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
2533 return skip;
2534}
2535
2536bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2537 const VkRenderPassBeginInfo *pRenderPassBegin,
2538 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2539 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2540 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
2541 return skip;
2542}
2543
John Zulauf3d84f1b2020-03-09 13:33:25 -06002544void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
2545 VkResult result) {
2546 // The state tracker sets up the command buffer state
2547 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
2548
2549 // Create/initialize the structure that trackers accesses at the command buffer scope.
2550 auto cb_access_context = GetAccessContext(commandBuffer);
2551 assert(cb_access_context);
2552 cb_access_context->Reset();
2553}
2554
2555void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06002556 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002557 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002558 if (cb_context) {
2559 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002560 }
2561}
2562
2563void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2564 VkSubpassContents contents) {
2565 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2566 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2567 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002568 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002569}
2570
2571void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2572 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2573 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002574 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002575}
2576
2577void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2578 const VkRenderPassBeginInfo *pRenderPassBegin,
2579 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2580 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002581 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
2582}
2583
2584bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2585 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
2586 bool skip = false;
2587
2588 auto cb_context = GetAccessContext(commandBuffer);
2589 assert(cb_context);
2590 auto cb_state = cb_context->GetCommandBufferState();
2591 if (!cb_state) return skip;
2592
2593 auto rp_state = cb_state->activeRenderPass;
2594 if (!rp_state) return skip;
2595
2596 skip |= cb_context->ValidateNextSubpass(func_name);
2597
2598 return skip;
2599}
2600
2601bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
2602 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
2603 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2604 subpass_begin_info.contents = contents;
2605 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
2606 return skip;
2607}
2608
2609bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2610 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2611 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2612 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
2613 return skip;
2614}
2615
2616bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2617 const VkSubpassEndInfo *pSubpassEndInfo) const {
2618 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2619 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
2620 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002621}
2622
2623void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06002624 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002625 auto cb_context = GetAccessContext(commandBuffer);
2626 assert(cb_context);
2627 auto cb_state = cb_context->GetCommandBufferState();
2628 if (!cb_state) return;
2629
2630 auto rp_state = cb_state->activeRenderPass;
2631 if (!rp_state) return;
2632
John Zulauf355e49b2020-04-24 15:11:15 -06002633 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002634}
2635
2636void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
2637 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
2638 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2639 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002640 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002641}
2642
2643void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2644 const VkSubpassEndInfo *pSubpassEndInfo) {
2645 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002646 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002647}
2648
2649void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2650 const VkSubpassEndInfo *pSubpassEndInfo) {
2651 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002652 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002653}
2654
John Zulauf355e49b2020-04-24 15:11:15 -06002655bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
2656 const char *func_name) const {
2657 bool skip = false;
2658
2659 auto cb_context = GetAccessContext(commandBuffer);
2660 assert(cb_context);
2661 auto cb_state = cb_context->GetCommandBufferState();
2662 if (!cb_state) return skip;
2663
2664 auto rp_state = cb_state->activeRenderPass;
2665 if (!rp_state) return skip;
2666
2667 skip |= cb_context->ValidateEndRenderpass(func_name);
2668 return skip;
2669}
2670
2671bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2672 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
2673 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
2674 return skip;
2675}
2676
2677bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
2678 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2679 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2680 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
2681 return skip;
2682}
2683
2684bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
2685 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2686 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2687 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
2688 return skip;
2689}
2690
2691void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
2692 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06002693 // Resolve the all subpass contexts to the command buffer contexts
2694 auto cb_context = GetAccessContext(commandBuffer);
2695 assert(cb_context);
2696 auto cb_state = cb_context->GetCommandBufferState();
2697 if (!cb_state) return;
2698
locke-lunargaecf2152020-05-12 17:15:41 -06002699 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06002700 if (!rp_state) return;
2701
John Zulauf355e49b2020-04-24 15:11:15 -06002702 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06002703}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002704
2705void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
2706 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002707 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002708}
2709
2710void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
2711 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002712 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002713}
2714
2715void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
2716 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002717 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002718}
locke-lunarga19c71d2020-03-02 18:17:04 -07002719
2720bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2721 VkImageLayout dstImageLayout, uint32_t regionCount,
2722 const VkBufferImageCopy *pRegions) const {
2723 bool skip = false;
2724 const auto *cb_access_context = GetAccessContext(commandBuffer);
2725 assert(cb_access_context);
2726 if (!cb_access_context) return skip;
2727
2728 const auto *context = cb_access_context->GetCurrentAccessContext();
2729 assert(context);
2730 if (!context) return skip;
2731
2732 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07002733 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2734
2735 for (uint32_t region = 0; region < regionCount; region++) {
2736 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002737 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002738 ResourceAccessRange src_range =
2739 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002740 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002741 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002742 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002743 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
2744 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07002745 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region);
2746 }
2747 }
2748 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002749 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002750 copy_region.imageOffset, copy_region.imageExtent);
2751 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002752 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
2753 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07002754 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region);
2755 }
2756 if (skip) break;
2757 }
2758 if (skip) break;
2759 }
2760 return skip;
2761}
2762
2763void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2764 VkImageLayout dstImageLayout, uint32_t regionCount,
2765 const VkBufferImageCopy *pRegions) {
2766 auto *cb_access_context = GetAccessContext(commandBuffer);
2767 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002768 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002769 auto *context = cb_access_context->GetCurrentAccessContext();
2770 assert(context);
2771
2772 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06002773 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002774
2775 for (uint32_t region = 0; region < regionCount; region++) {
2776 const auto &copy_region = pRegions[region];
2777 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002778 ResourceAccessRange src_range =
2779 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002780 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002781 }
2782 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002783 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002784 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002785 }
2786 }
2787}
2788
2789bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
2790 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
2791 const VkBufferImageCopy *pRegions) const {
2792 bool skip = false;
2793 const auto *cb_access_context = GetAccessContext(commandBuffer);
2794 assert(cb_access_context);
2795 if (!cb_access_context) return skip;
2796
2797 const auto *context = cb_access_context->GetCurrentAccessContext();
2798 assert(context);
2799 if (!context) return skip;
2800
2801 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2802 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2803 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
2804 for (uint32_t region = 0; region < regionCount; region++) {
2805 const auto &copy_region = pRegions[region];
2806 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002807 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002808 copy_region.imageOffset, copy_region.imageExtent);
2809 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002810 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
2811 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07002812 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region);
2813 }
2814 }
2815 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06002816 ResourceAccessRange dst_range =
2817 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002818 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002819 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002820 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
2821 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07002822 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region);
2823 }
2824 }
2825 if (skip) break;
2826 }
2827 return skip;
2828}
2829
2830void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2831 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
2832 auto *cb_access_context = GetAccessContext(commandBuffer);
2833 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002834 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07002835 auto *context = cb_access_context->GetCurrentAccessContext();
2836 assert(context);
2837
2838 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002839 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2840 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 -06002841 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07002842
2843 for (uint32_t region = 0; region < regionCount; region++) {
2844 const auto &copy_region = pRegions[region];
2845 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002846 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002847 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002848 }
2849 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002850 ResourceAccessRange dst_range =
2851 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002852 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002853 }
2854 }
2855}
2856
2857bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2858 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2859 const VkImageBlit *pRegions, VkFilter filter) const {
2860 bool skip = false;
2861 const auto *cb_access_context = GetAccessContext(commandBuffer);
2862 assert(cb_access_context);
2863 if (!cb_access_context) return skip;
2864
2865 const auto *context = cb_access_context->GetCurrentAccessContext();
2866 assert(context);
2867 if (!context) return skip;
2868
2869 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2870 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2871
2872 for (uint32_t region = 0; region < regionCount; region++) {
2873 const auto &blit_region = pRegions[region];
2874 if (src_image) {
2875 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
2876 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
2877 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002878 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002879 blit_region.srcOffsets[0], extent);
2880 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002881 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
2882 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
2883 report_data->FormatHandle(srcImage).c_str(), region);
locke-lunarga19c71d2020-03-02 18:17:04 -07002884 }
2885 }
2886
2887 if (dst_image) {
2888 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
2889 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
2890 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002891 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002892 blit_region.dstOffsets[0], extent);
2893 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002894 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
2895 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
2896 report_data->FormatHandle(dstImage).c_str(), region);
locke-lunarga19c71d2020-03-02 18:17:04 -07002897 }
2898 if (skip) break;
2899 }
2900 }
2901
2902 return skip;
2903}
2904
2905void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2906 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2907 const VkImageBlit *pRegions, VkFilter filter) {
2908 auto *cb_access_context = GetAccessContext(commandBuffer);
2909 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002910 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002911 auto *context = cb_access_context->GetCurrentAccessContext();
2912 assert(context);
2913
2914 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002915 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002916
2917 for (uint32_t region = 0; region < regionCount; region++) {
2918 const auto &blit_region = pRegions[region];
2919 if (src_image) {
2920 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
2921 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
2922 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002923 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002924 blit_region.srcOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002925 }
2926 if (dst_image) {
2927 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
2928 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
2929 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002930 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002931 blit_region.dstOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002932 }
2933 }
2934}
locke-lunarg36ba2592020-04-03 09:42:04 -06002935
locke-lunarg61870c22020-06-09 14:51:50 -06002936bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
2937 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
2938 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06002939 bool skip = false;
2940 if (drawCount == 0) return skip;
2941
2942 const auto *buf_state = Get<BUFFER_STATE>(buffer);
2943 VkDeviceSize size = struct_size;
2944 if (drawCount == 1 || stride == size) {
2945 if (drawCount > 1) size *= drawCount;
2946 ResourceAccessRange range = MakeRange(offset, size);
2947 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
2948 if (hazard.hazard) {
2949 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for indirect %s in %s",
2950 function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(buffer).c_str(),
2951 report_data->FormatHandle(commandBuffer).c_str());
2952 }
2953 } else {
2954 for (uint32_t i = 0; i < drawCount; ++i) {
2955 ResourceAccessRange range = MakeRange(offset + i * stride, size);
2956 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
2957 if (hazard.hazard) {
2958 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for indirect %s in %s",
2959 function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(buffer).c_str(),
2960 report_data->FormatHandle(commandBuffer).c_str());
2961 break;
2962 }
2963 }
2964 }
2965 return skip;
2966}
2967
locke-lunarg61870c22020-06-09 14:51:50 -06002968void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
2969 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
2970 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06002971 const auto *buf_state = Get<BUFFER_STATE>(buffer);
2972 VkDeviceSize size = struct_size;
2973 if (drawCount == 1 || stride == size) {
2974 if (drawCount > 1) size *= drawCount;
2975 ResourceAccessRange range = MakeRange(offset, size);
2976 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
2977 } else {
2978 for (uint32_t i = 0; i < drawCount; ++i) {
2979 ResourceAccessRange range = MakeRange(offset + i * stride, size);
2980 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
2981 }
2982 }
2983}
2984
locke-lunarg61870c22020-06-09 14:51:50 -06002985bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
2986 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06002987 bool skip = false;
2988
2989 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
2990 ResourceAccessRange range = MakeRange(offset, 4);
2991 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
2992 if (hazard.hazard) {
2993 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for countBuffer %s in %s",
2994 function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(buffer).c_str(),
2995 report_data->FormatHandle(commandBuffer).c_str());
2996 }
2997 return skip;
2998}
2999
locke-lunarg61870c22020-06-09 14:51:50 -06003000void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003001 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3002 ResourceAccessRange range = MakeRange(offset, 4);
3003 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3004}
3005
locke-lunarg36ba2592020-04-03 09:42:04 -06003006bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003007 bool skip = false;
3008 ;
locke-lunargff255f92020-05-13 18:53:52 -06003009 const auto *cb_access_context = GetAccessContext(commandBuffer);
3010 assert(cb_access_context);
3011 if (!cb_access_context) return skip;
3012
locke-lunarg61870c22020-06-09 14:51:50 -06003013 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003014 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003015}
3016
3017void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunargff255f92020-05-13 18:53:52 -06003018 auto *cb_access_context = GetAccessContext(commandBuffer);
3019 assert(cb_access_context);
3020 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003021
locke-lunarg61870c22020-06-09 14:51:50 -06003022 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003023}
locke-lunarge1a67022020-04-29 00:15:36 -06003024
3025bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003026 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003027 const auto *cb_access_context = GetAccessContext(commandBuffer);
3028 assert(cb_access_context);
3029 if (!cb_access_context) return skip;
3030
3031 const auto *context = cb_access_context->GetCurrentAccessContext();
3032 assert(context);
3033 if (!context) return skip;
3034
locke-lunarg61870c22020-06-09 14:51:50 -06003035 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3036 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3037 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003038 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003039}
3040
3041void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003042 auto *cb_access_context = GetAccessContext(commandBuffer);
3043 assert(cb_access_context);
3044 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3045 auto *context = cb_access_context->GetCurrentAccessContext();
3046 assert(context);
3047
locke-lunarg61870c22020-06-09 14:51:50 -06003048 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3049 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003050}
3051
3052bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3053 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -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
locke-lunarg61870c22020-06-09 14:51:50 -06003059 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3060 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3061 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003062 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003063}
3064
3065void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3066 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunargff255f92020-05-13 18:53:52 -06003067 auto *cb_access_context = GetAccessContext(commandBuffer);
3068 assert(cb_access_context);
3069 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003070
locke-lunarg61870c22020-06-09 14:51:50 -06003071 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3072 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3073 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003074}
3075
3076bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3077 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003078 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003079 const auto *cb_access_context = GetAccessContext(commandBuffer);
3080 assert(cb_access_context);
3081 if (!cb_access_context) return skip;
3082
locke-lunarg61870c22020-06-09 14:51:50 -06003083 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3084 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3085 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003086 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003087}
3088
3089void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3090 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunargff255f92020-05-13 18:53:52 -06003091 auto *cb_access_context = GetAccessContext(commandBuffer);
3092 assert(cb_access_context);
3093 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003094
locke-lunarg61870c22020-06-09 14:51:50 -06003095 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3096 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3097 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003098}
3099
3100bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3101 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003102 bool skip = false;
3103 if (drawCount == 0) return skip;
3104
locke-lunargff255f92020-05-13 18:53:52 -06003105 const auto *cb_access_context = GetAccessContext(commandBuffer);
3106 assert(cb_access_context);
3107 if (!cb_access_context) return skip;
3108
3109 const auto *context = cb_access_context->GetCurrentAccessContext();
3110 assert(context);
3111 if (!context) return skip;
3112
locke-lunarg61870c22020-06-09 14:51:50 -06003113 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3114 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3115 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3116 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003117
3118 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3119 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3120 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003121 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003122 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003123}
3124
3125void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3126 uint32_t drawCount, uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003127 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003128 auto *cb_access_context = GetAccessContext(commandBuffer);
3129 assert(cb_access_context);
3130 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3131 auto *context = cb_access_context->GetCurrentAccessContext();
3132 assert(context);
3133
locke-lunarg61870c22020-06-09 14:51:50 -06003134 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3135 cb_access_context->RecordDrawSubpassAttachment(tag);
3136 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003137
3138 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3139 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3140 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003141 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003142}
3143
3144bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3145 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003146 bool skip = false;
3147 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003148 const auto *cb_access_context = GetAccessContext(commandBuffer);
3149 assert(cb_access_context);
3150 if (!cb_access_context) return skip;
3151
3152 const auto *context = cb_access_context->GetCurrentAccessContext();
3153 assert(context);
3154 if (!context) return skip;
3155
locke-lunarg61870c22020-06-09 14:51:50 -06003156 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3157 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3158 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3159 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003160
3161 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3162 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3163 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003164 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003165 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003166}
3167
3168void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3169 uint32_t drawCount, uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003170 auto *cb_access_context = GetAccessContext(commandBuffer);
3171 assert(cb_access_context);
3172 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3173 auto *context = cb_access_context->GetCurrentAccessContext();
3174 assert(context);
3175
locke-lunarg61870c22020-06-09 14:51:50 -06003176 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3177 cb_access_context->RecordDrawSubpassAttachment(tag);
3178 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003179
3180 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3181 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3182 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003183 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003184}
3185
3186bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3187 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3188 uint32_t stride, const char *function) const {
3189 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003190 const auto *cb_access_context = GetAccessContext(commandBuffer);
3191 assert(cb_access_context);
3192 if (!cb_access_context) return skip;
3193
3194 const auto *context = cb_access_context->GetCurrentAccessContext();
3195 assert(context);
3196 if (!context) return skip;
3197
locke-lunarg61870c22020-06-09 14:51:50 -06003198 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3199 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3200 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3201 function);
3202 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003203
3204 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3205 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3206 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003207 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003208 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003209}
3210
3211bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3212 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3213 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003214 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3215 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003216}
3217
3218void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3219 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3220 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003221 auto *cb_access_context = GetAccessContext(commandBuffer);
3222 assert(cb_access_context);
3223 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3224 auto *context = cb_access_context->GetCurrentAccessContext();
3225 assert(context);
3226
locke-lunarg61870c22020-06-09 14:51:50 -06003227 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3228 cb_access_context->RecordDrawSubpassAttachment(tag);
3229 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3230 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003231
3232 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3233 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3234 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003235 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003236}
3237
3238bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3239 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3240 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003241 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3242 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003243}
3244
3245void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3246 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3247 uint32_t maxDrawCount, uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003248 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003249}
3250
3251bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3252 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3253 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003254 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3255 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003256}
3257
3258void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3259 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3260 uint32_t maxDrawCount, uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003261 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3262}
3263
3264bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3265 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3266 uint32_t stride, const char *function) const {
3267 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003268 const auto *cb_access_context = GetAccessContext(commandBuffer);
3269 assert(cb_access_context);
3270 if (!cb_access_context) return skip;
3271
3272 const auto *context = cb_access_context->GetCurrentAccessContext();
3273 assert(context);
3274 if (!context) return skip;
3275
locke-lunarg61870c22020-06-09 14:51:50 -06003276 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3277 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3278 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3279 stride, function);
3280 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003281
3282 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3283 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3284 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003285 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003286 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003287}
3288
3289bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3290 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3291 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003292 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3293 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003294}
3295
3296void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3297 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3298 uint32_t maxDrawCount, uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003299 auto *cb_access_context = GetAccessContext(commandBuffer);
3300 assert(cb_access_context);
3301 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3302 auto *context = cb_access_context->GetCurrentAccessContext();
3303 assert(context);
3304
locke-lunarg61870c22020-06-09 14:51:50 -06003305 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3306 cb_access_context->RecordDrawSubpassAttachment(tag);
3307 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3308 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003309
3310 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3311 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003312 // We will update the index and vertex buffer in SubmitQueue in the future.
3313 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003314}
3315
3316bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3317 VkDeviceSize offset, VkBuffer countBuffer,
3318 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3319 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003320 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3321 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003322}
3323
3324void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3325 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3326 uint32_t maxDrawCount, uint32_t stride) {
3327 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3328}
3329
3330bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3331 VkDeviceSize offset, VkBuffer countBuffer,
3332 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3333 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003334 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3335 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003336}
3337
3338void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3339 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3340 uint32_t maxDrawCount, uint32_t stride) {
3341 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3342}
3343
3344bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3345 const VkClearColorValue *pColor, uint32_t rangeCount,
3346 const VkImageSubresourceRange *pRanges) const {
3347 bool skip = false;
3348 const auto *cb_access_context = GetAccessContext(commandBuffer);
3349 assert(cb_access_context);
3350 if (!cb_access_context) return skip;
3351
3352 const auto *context = cb_access_context->GetCurrentAccessContext();
3353 assert(context);
3354 if (!context) return skip;
3355
3356 const auto *image_state = Get<IMAGE_STATE>(image);
3357
3358 for (uint32_t index = 0; index < rangeCount; index++) {
3359 const auto &range = pRanges[index];
3360 if (image_state) {
3361 auto hazard =
3362 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3363 if (hazard.hazard) {
3364 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
3365 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32, string_SyncHazard(hazard.hazard),
3366 report_data->FormatHandle(image).c_str(), index);
3367 }
3368 }
3369 }
3370 return skip;
3371}
3372
3373void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3374 const VkClearColorValue *pColor, uint32_t rangeCount,
3375 const VkImageSubresourceRange *pRanges) {
3376 auto *cb_access_context = GetAccessContext(commandBuffer);
3377 assert(cb_access_context);
3378 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
3379 auto *context = cb_access_context->GetCurrentAccessContext();
3380 assert(context);
3381
3382 const auto *image_state = Get<IMAGE_STATE>(image);
3383
3384 for (uint32_t index = 0; index < rangeCount; index++) {
3385 const auto &range = pRanges[index];
3386 if (image_state) {
3387 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3388 tag);
3389 }
3390 }
3391}
3392
3393bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3394 VkImageLayout imageLayout,
3395 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3396 const VkImageSubresourceRange *pRanges) const {
3397 bool skip = false;
3398 const auto *cb_access_context = GetAccessContext(commandBuffer);
3399 assert(cb_access_context);
3400 if (!cb_access_context) return skip;
3401
3402 const auto *context = cb_access_context->GetCurrentAccessContext();
3403 assert(context);
3404 if (!context) return skip;
3405
3406 const auto *image_state = Get<IMAGE_STATE>(image);
3407
3408 for (uint32_t index = 0; index < rangeCount; index++) {
3409 const auto &range = pRanges[index];
3410 if (image_state) {
3411 auto hazard =
3412 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3413 if (hazard.hazard) {
3414 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
3415 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32,
3416 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index);
3417 }
3418 }
3419 }
3420 return skip;
3421}
3422
3423void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3424 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3425 const VkImageSubresourceRange *pRanges) {
3426 auto *cb_access_context = GetAccessContext(commandBuffer);
3427 assert(cb_access_context);
3428 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
3429 auto *context = cb_access_context->GetCurrentAccessContext();
3430 assert(context);
3431
3432 const auto *image_state = Get<IMAGE_STATE>(image);
3433
3434 for (uint32_t index = 0; index < rangeCount; index++) {
3435 const auto &range = pRanges[index];
3436 if (image_state) {
3437 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3438 tag);
3439 }
3440 }
3441}
3442
3443bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
3444 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
3445 VkDeviceSize dstOffset, VkDeviceSize stride,
3446 VkQueryResultFlags flags) const {
3447 bool skip = false;
3448 const auto *cb_access_context = GetAccessContext(commandBuffer);
3449 assert(cb_access_context);
3450 if (!cb_access_context) return skip;
3451
3452 const auto *context = cb_access_context->GetCurrentAccessContext();
3453 assert(context);
3454 if (!context) return skip;
3455
3456 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3457
3458 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003459 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003460 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3461 if (hazard.hazard) {
3462 skip |=
3463 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s",
3464 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3465 }
3466 }
locke-lunargff255f92020-05-13 18:53:52 -06003467
3468 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003469 return skip;
3470}
3471
3472void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
3473 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3474 VkDeviceSize stride, VkQueryResultFlags flags) {
3475 auto *cb_access_context = GetAccessContext(commandBuffer);
3476 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06003477 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06003478 auto *context = cb_access_context->GetCurrentAccessContext();
3479 assert(context);
3480
3481 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3482
3483 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003484 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003485 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3486 }
locke-lunargff255f92020-05-13 18:53:52 -06003487
3488 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003489}
3490
3491bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3492 VkDeviceSize size, uint32_t data) const {
3493 bool skip = false;
3494 const auto *cb_access_context = GetAccessContext(commandBuffer);
3495 assert(cb_access_context);
3496 if (!cb_access_context) return skip;
3497
3498 const auto *context = cb_access_context->GetCurrentAccessContext();
3499 assert(context);
3500 if (!context) return skip;
3501
3502 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3503
3504 if (dst_buffer) {
3505 ResourceAccessRange range = MakeRange(dstOffset, size);
3506 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3507 if (hazard.hazard) {
3508 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdFillBuffer: Hazard %s for dstBuffer %s",
3509 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3510 }
3511 }
3512 return skip;
3513}
3514
3515void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3516 VkDeviceSize size, uint32_t data) {
3517 auto *cb_access_context = GetAccessContext(commandBuffer);
3518 assert(cb_access_context);
3519 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
3520 auto *context = cb_access_context->GetCurrentAccessContext();
3521 assert(context);
3522
3523 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3524
3525 if (dst_buffer) {
3526 ResourceAccessRange range = MakeRange(dstOffset, size);
3527 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3528 }
3529}
3530
3531bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3532 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3533 const VkImageResolve *pRegions) const {
3534 bool skip = false;
3535 const auto *cb_access_context = GetAccessContext(commandBuffer);
3536 assert(cb_access_context);
3537 if (!cb_access_context) return skip;
3538
3539 const auto *context = cb_access_context->GetCurrentAccessContext();
3540 assert(context);
3541 if (!context) return skip;
3542
3543 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3544 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3545
3546 for (uint32_t region = 0; region < regionCount; region++) {
3547 const auto &resolve_region = pRegions[region];
3548 if (src_image) {
3549 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3550 resolve_region.srcOffset, resolve_region.extent);
3551 if (hazard.hazard) {
3552 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
3553 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
3554 report_data->FormatHandle(srcImage).c_str(), region);
3555 }
3556 }
3557
3558 if (dst_image) {
3559 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3560 resolve_region.dstOffset, resolve_region.extent);
3561 if (hazard.hazard) {
3562 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
3563 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
3564 report_data->FormatHandle(dstImage).c_str(), region);
3565 }
3566 if (skip) break;
3567 }
3568 }
3569
3570 return skip;
3571}
3572
3573void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3574 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3575 const VkImageResolve *pRegions) {
3576 auto *cb_access_context = GetAccessContext(commandBuffer);
3577 assert(cb_access_context);
3578 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
3579 auto *context = cb_access_context->GetCurrentAccessContext();
3580 assert(context);
3581
3582 auto *src_image = Get<IMAGE_STATE>(srcImage);
3583 auto *dst_image = Get<IMAGE_STATE>(dstImage);
3584
3585 for (uint32_t region = 0; region < regionCount; region++) {
3586 const auto &resolve_region = pRegions[region];
3587 if (src_image) {
3588 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3589 resolve_region.srcOffset, resolve_region.extent, tag);
3590 }
3591 if (dst_image) {
3592 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3593 resolve_region.dstOffset, resolve_region.extent, tag);
3594 }
3595 }
3596}
3597
3598bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3599 VkDeviceSize dataSize, const void *pData) const {
3600 bool skip = false;
3601 const auto *cb_access_context = GetAccessContext(commandBuffer);
3602 assert(cb_access_context);
3603 if (!cb_access_context) return skip;
3604
3605 const auto *context = cb_access_context->GetCurrentAccessContext();
3606 assert(context);
3607 if (!context) return skip;
3608
3609 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3610
3611 if (dst_buffer) {
3612 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3613 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3614 if (hazard.hazard) {
3615 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s",
3616 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3617 }
3618 }
3619 return skip;
3620}
3621
3622void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3623 VkDeviceSize dataSize, const void *pData) {
3624 auto *cb_access_context = GetAccessContext(commandBuffer);
3625 assert(cb_access_context);
3626 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
3627 auto *context = cb_access_context->GetCurrentAccessContext();
3628 assert(context);
3629
3630 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3631
3632 if (dst_buffer) {
3633 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3634 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3635 }
3636}
locke-lunargff255f92020-05-13 18:53:52 -06003637
3638bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3639 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
3640 bool skip = false;
3641 const auto *cb_access_context = GetAccessContext(commandBuffer);
3642 assert(cb_access_context);
3643 if (!cb_access_context) return skip;
3644
3645 const auto *context = cb_access_context->GetCurrentAccessContext();
3646 assert(context);
3647 if (!context) return skip;
3648
3649 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3650
3651 if (dst_buffer) {
3652 ResourceAccessRange range = MakeRange(dstOffset, 4);
3653 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3654 if (hazard.hazard) {
3655 skip |=
3656 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s",
3657 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3658 }
3659 }
3660 return skip;
3661}
3662
3663void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3664 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
3665 auto *cb_access_context = GetAccessContext(commandBuffer);
3666 assert(cb_access_context);
3667 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
3668 auto *context = cb_access_context->GetCurrentAccessContext();
3669 assert(context);
3670
3671 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3672
3673 if (dst_buffer) {
3674 ResourceAccessRange range = MakeRange(dstOffset, 4);
3675 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3676 }
3677}