blob: 6b27007a09a6f2e02f079c015f21877abf52e5c0 [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
John Zulauf355e49b2020-04-24 15:11:15 -0600196// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
197const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
198 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
199
John Zulauf7635de32020-05-29 17:14:15 -0600200// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
201// Used by both validation and record operations
202//
203// The signature for Action() reflect the needs of both uses.
204template <typename Action>
205void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
206 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
207 VkExtent3D extent = CastTo3D(render_area.extent);
208 VkOffset3D offset = CastTo3D(render_area.offset);
209 const auto &rp_ci = rp_state.createInfo;
210 const auto *attachment_ci = rp_ci.pAttachments;
211 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
212
213 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
214 const auto *color_attachments = subpass_ci.pColorAttachments;
215 const auto *color_resolve = subpass_ci.pResolveAttachments;
216 if (color_resolve && color_attachments) {
217 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
218 const auto &color_attach = color_attachments[i].attachment;
219 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
220 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
221 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
222 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
223 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
224 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
225 }
226 }
227 }
228
229 // Depth stencil resolve only if the extension is present
230 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
231 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
232 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
233 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
234 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
235 const auto src_ci = attachment_ci[src_at];
236 // The formats are required to match so we can pick either
237 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
238 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
239 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
240 VkImageAspectFlags aspect_mask = 0u;
241
242 // Figure out which aspects are actually touched during resolve operations
243 const char *aspect_string = nullptr;
244 if (resolve_depth && resolve_stencil) {
245 // Validate all aspects together
246 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
247 aspect_string = "depth/stencil";
248 } else if (resolve_depth) {
249 // Validate depth only
250 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
251 aspect_string = "depth";
252 } else if (resolve_stencil) {
253 // Validate all stencil only
254 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
255 aspect_string = "stencil";
256 }
257
258 if (aspect_mask) {
259 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
260 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kDepthStencilAttachmentRasterOrder, offset, extent,
261 aspect_mask);
262 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
263 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
264 }
265 }
266}
267
268// Action for validating resolve operations
269class ValidateResolveAction {
270 public:
271 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
272 const char *func_name)
273 : render_pass_(render_pass),
274 subpass_(subpass),
275 context_(context),
276 sync_state_(sync_state),
277 func_name_(func_name),
278 skip_(false) {}
279 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
280 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
281 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
282 HazardResult hazard;
283 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
284 if (hazard.hazard) {
285 skip_ |= sync_state_.LogError(
286 render_pass_, string_SyncHazardVUID(hazard.hazard),
287 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32 " to resolve attachment %" PRIu32 ".",
288 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name, src_at, dst_at);
289 }
290 }
291 // Providing a mechanism for the constructing caller to get the result of the validation
292 bool GetSkip() const { return skip_; }
293
294 private:
295 VkRenderPass render_pass_;
296 const uint32_t subpass_;
297 const AccessContext &context_;
298 const SyncValidator &sync_state_;
299 const char *func_name_;
300 bool skip_;
301};
302
303// Update action for resolve operations
304class UpdateStateResolveAction {
305 public:
306 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
307 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
308 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
309 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
310 // Ignores validation only arguments...
311 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
312 }
313
314 private:
315 AccessContext &context_;
316 const ResourceUsageTag &tag_;
317};
318
John Zulauf540266b2020-04-06 18:54:53 -0600319AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
320 const std::vector<SubpassDependencyGraphNode> &dependencies,
321 const std::vector<AccessContext> &contexts, AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600322 Reset();
323 const auto &subpass_dep = dependencies[subpass];
324 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600325 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600326 for (const auto &prev_dep : subpass_dep.prev) {
327 assert(prev_dep.dependency);
328 const auto dep = *prev_dep.dependency;
John Zulauf540266b2020-04-06 18:54:53 -0600329 prev_.emplace_back(const_cast<AccessContext *>(&contexts[dep.srcSubpass]), queue_flags, dep);
John Zulauf355e49b2020-04-24 15:11:15 -0600330 prev_by_subpass_[dep.srcSubpass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700331 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600332
333 async_.reserve(subpass_dep.async.size());
334 for (const auto async_subpass : subpass_dep.async) {
John Zulauf540266b2020-04-06 18:54:53 -0600335 async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass]));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600336 }
John Zulaufe5da6e52020-03-18 15:32:18 -0600337 if (subpass_dep.barrier_from_external) {
338 src_external_ = TrackBack(external_context, queue_flags, *subpass_dep.barrier_from_external);
339 } else {
340 src_external_ = TrackBack();
341 }
342 if (subpass_dep.barrier_to_external) {
343 dst_external_ = TrackBack(this, queue_flags, *subpass_dep.barrier_to_external);
344 } else {
345 dst_external_ = TrackBack();
John Zulauf3d84f1b2020-03-09 13:33:25 -0600346 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700347}
348
John Zulauf5f13a792020-03-10 07:31:21 -0600349template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600350HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600351 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600352 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600353 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600354
355 HazardResult hazard;
356 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
357 hazard = detector.Detect(prev);
358 }
359 return hazard;
360}
361
John Zulauf3d84f1b2020-03-09 13:33:25 -0600362// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
363// the DAG of the contexts (for example subpasses)
364template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600365HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
366 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600367 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600368
John Zulauf355e49b2020-04-24 15:11:15 -0600369 if (static_cast<uint32_t>(options) | DetectOptions::kDetectAsync) {
370 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
371 // so we'll check these first
372 for (const auto &async_context : async_) {
373 hazard = async_context->DetectAsyncHazard(type, detector, range);
374 if (hazard.hazard) return hazard;
375 }
John Zulauf5f13a792020-03-10 07:31:21 -0600376 }
377
John Zulauf69133422020-05-20 14:55:53 -0600378 const bool detect_prev = (static_cast<uint32_t>(options) | DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600379
John Zulauf69133422020-05-20 14:55:53 -0600380 const auto &accesses = GetAccessStateMap(type);
381 const auto from = accesses.lower_bound(range);
382 const auto to = accesses.upper_bound(range);
383 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600384
John Zulauf69133422020-05-20 14:55:53 -0600385 for (auto pos = from; pos != to; ++pos) {
386 // Cover any leading gap, or gap between entries
387 if (detect_prev) {
388 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
389 // Cover any leading gap, or gap between entries
390 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600391 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600392 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600393 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600394 if (hazard.hazard) return hazard;
395 }
John Zulauf69133422020-05-20 14:55:53 -0600396 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
397 gap.begin = pos->first.end;
398 }
399
400 hazard = detector.Detect(pos);
401 if (hazard.hazard) return hazard;
402 }
403
404 if (detect_prev) {
405 // Detect in the trailing empty as needed
406 gap.end = range.end;
407 if (gap.non_empty()) {
408 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600409 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600410 }
411
412 return hazard;
413}
414
415// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
416template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600417HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600418 auto &accesses = GetAccessStateMap(type);
419 const auto from = accesses.lower_bound(range);
420 const auto to = accesses.upper_bound(range);
421
John Zulauf3d84f1b2020-03-09 13:33:25 -0600422 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600423 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
424 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600425 }
John Zulauf16adfc92020-04-08 10:28:33 -0600426
John Zulauf3d84f1b2020-03-09 13:33:25 -0600427 return hazard;
428}
429
John Zulauf355e49b2020-04-24 15:11:15 -0600430// Returns the last resolved entry
431static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
432 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
433 const SyncBarrier *barrier) {
434 auto at = entry;
435 for (auto pos = first; pos != last; ++pos) {
436 // Every member of the input iterator range must fit within the remaining portion of entry
437 assert(at->first.includes(pos->first));
438 assert(at != dest->end());
439 // Trim up at to the same size as the entry to resolve
440 at = sparse_container::split(at, *dest, pos->first);
441 auto access = pos->second;
442 if (barrier) {
443 access.ApplyBarrier(*barrier);
444 }
445 at->second.Resolve(access);
446 ++at; // Go to the remaining unused section of entry
447 }
448}
449
450void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const SyncBarrier *barrier,
451 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
452 bool recur_to_infill) const {
453 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
454 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf16adfc92020-04-08 10:28:33 -0600455 if (current->pos_B->valid) {
456 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600457 auto access = src_pos->second;
458 if (barrier) {
459 access.ApplyBarrier(*barrier);
460 }
John Zulauf16adfc92020-04-08 10:28:33 -0600461 if (current->pos_A->valid) {
462 current.trim_A();
John Zulauf355e49b2020-04-24 15:11:15 -0600463 current->pos_A->lower_bound->second.Resolve(access);
John Zulauf5f13a792020-03-10 07:31:21 -0600464 } else {
John Zulauf355e49b2020-04-24 15:11:15 -0600465 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, access));
466 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600467 }
John Zulauf16adfc92020-04-08 10:28:33 -0600468 } else {
469 // we have to descend to fill this gap
470 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600471 if (current->pos_A->valid) {
472 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
473 ResourceAccessRangeMap gap_map;
474 ResolvePreviousAccess(type, current->range, &gap_map, infill_state);
475 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier);
476 } else {
477 // There isn't anything in dest in current->range, so we can accumulate directly into it.
478 ResolvePreviousAccess(type, current->range, resolve_map, infill_state);
479 if (barrier) {
480 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
481 for (auto pos = resolve_map->lower_bound(current->range); pos != current->pos_A->lower_bound; ++pos) {
482 pos->second.ApplyBarrier(*barrier);
483 }
484 }
485 }
486 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
487 // iterator of the outer while.
488
489 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
490 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
491 // we stepped on the dest map
492 const auto seek_to = current->range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
493 current.invalidate_A(); // Changes current->range
494 current.seek(seek_to);
495 } else if (!current->pos_A->valid && infill_state) {
496 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
497 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
498 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600499 }
John Zulauf5f13a792020-03-10 07:31:21 -0600500 }
John Zulauf16adfc92020-04-08 10:28:33 -0600501 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600502 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600503}
504
John Zulauf355e49b2020-04-24 15:11:15 -0600505void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
506 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600507 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600508 if (range.non_empty() && infill_state) {
509 descent_map->insert(std::make_pair(range, *infill_state));
510 }
511 } else {
512 // Look for something to fill the gap further along.
513 for (const auto &prev_dep : prev_) {
John Zulauf355e49b2020-04-24 15:11:15 -0600514 prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600515 }
516
John Zulaufe5da6e52020-03-18 15:32:18 -0600517 if (src_external_.context) {
John Zulauf355e49b2020-04-24 15:11:15 -0600518 src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600519 }
520 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600521}
522
John Zulauf16adfc92020-04-08 10:28:33 -0600523AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600524 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600525}
526
527VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
528 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
529}
530
John Zulauf355e49b2020-04-24 15:11:15 -0600531static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600532
John Zulauf1507ee42020-05-18 11:33:09 -0600533static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
534 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
535 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
536 return stage_access;
537}
538static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
539 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
540 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
541 return stage_access;
542}
543
John Zulauf7635de32020-05-29 17:14:15 -0600544// Caller must manage returned pointer
545static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
546 uint32_t subpass, const VkRect2D &render_area,
547 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
548 auto *proxy = new AccessContext(context);
549 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600550 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600551 return proxy;
552}
553
John Zulauf540266b2020-04-06 18:54:53 -0600554void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600555 AddressType address_type, ResourceAccessRangeMap *descent_map,
556 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600557 if (!SimpleBinding(image_state)) return;
558
John Zulauf62f10592020-04-03 12:20:02 -0600559 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600560 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600561 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600562 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600563 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600564 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600565 }
566}
567
John Zulauf7635de32020-05-29 17:14:15 -0600568// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600569bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600570 const VkRect2D &render_area, uint32_t subpass,
571 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
572 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600573 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600574 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
575 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
576 // those affects have not been recorded yet.
577 //
578 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
579 // to apply and only copy then, if this proves a hot spot.
580 std::unique_ptr<AccessContext> proxy_for_prev;
581 TrackBack proxy_track_back;
582
John Zulauf355e49b2020-04-24 15:11:15 -0600583 const auto &transitions = rp_state.subpass_transitions[subpass];
584 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600585 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
586
587 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
588 if (prev_needs_proxy) {
589 if (!proxy_for_prev) {
590 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
591 render_area, attachment_views));
592 proxy_track_back = *track_back;
593 proxy_track_back.context = proxy_for_prev.get();
594 }
595 track_back = &proxy_track_back;
596 }
597 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600598 if (hazard.hazard) {
599 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
600 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " image layout transition.",
601 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment);
602 }
603 }
604 return skip;
605}
606
John Zulauf1507ee42020-05-18 11:33:09 -0600607bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600608 const VkRect2D &render_area, uint32_t subpass,
609 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
610 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600611 bool skip = false;
612 const auto *attachment_ci = rp_state.createInfo.pAttachments;
613 VkExtent3D extent = CastTo3D(render_area.extent);
614 VkOffset3D offset = CastTo3D(render_area.offset);
615 const auto external_access_scope = src_external_.barrier.dst_access_scope;
John Zulauf1507ee42020-05-18 11:33:09 -0600616
617 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
618 if (subpass == rp_state.attachment_first_subpass[i]) {
619 if (attachment_views[i] == nullptr) continue;
620 const IMAGE_VIEW_STATE &view = *attachment_views[i];
621 const IMAGE_STATE *image = view.image_state.get();
622 if (image == nullptr) continue;
623 const auto &ci = attachment_ci[i];
624 const bool is_transition = rp_state.attachment_first_is_transition[i];
625
626 // Need check in the following way
627 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
628 // vs. transition
629 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
630 // for each aspect loaded.
631
632 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600633 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600634 const bool is_color = !(has_depth || has_stencil);
635
636 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
637 const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U;
638 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
639 const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U;
640
John Zulaufaff20662020-06-01 14:07:58 -0600641 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600642 const char *aspect = nullptr;
643 if (is_transition) {
644 // For transition w
645 SyncHazard transition_hazard = SyncHazard::NONE;
646 bool checked_stencil = false;
647 if (load_mask) {
648 if ((load_mask & external_access_scope) != load_mask) {
649 transition_hazard =
650 SyncStageAccess::HasWrite(load_mask) ? SyncHazard::WRITE_AFTER_WRITE : SyncHazard::READ_AFTER_WRITE;
651 aspect = is_color ? "color" : "depth";
652 }
653 if (!transition_hazard && stencil_mask) {
654 if ((stencil_mask & external_access_scope) != stencil_mask) {
655 transition_hazard = SyncStageAccess::HasWrite(stencil_mask) ? SyncHazard::WRITE_AFTER_WRITE
656 : SyncHazard::READ_AFTER_WRITE;
657 aspect = "stencil";
658 checked_stencil = true;
659 }
660 }
661 }
662 if (transition_hazard) {
663 // Hazard vs. ILT
664 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
665 skip |=
666 sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
667 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
668 " aspect %s during load with loadOp %s.",
669 func_name, string_SyncHazard(transition_hazard), subpass, i, aspect, load_op_string);
670 }
671 } else {
672 auto hazard_range = view.normalized_subresource_range;
673 bool checked_stencil = false;
674 if (is_color) {
675 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
676 aspect = "color";
677 } else {
678 if (has_depth) {
679 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
680 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
681 aspect = "depth";
682 }
683 if (!hazard.hazard && has_stencil) {
684 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
685 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
686 aspect = "stencil";
687 checked_stencil = true;
688 }
689 }
690
691 if (hazard.hazard) {
692 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
693 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
694 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
695 " aspect %s during load with loadOp %s.",
696 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
697 }
698 }
699 }
700 }
701 return skip;
702}
703
John Zulaufaff20662020-06-01 14:07:58 -0600704// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
705// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
706// store is part of the same Next/End operation.
707// The latter is handled in layout transistion validation directly
708bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
709 const VkRect2D &render_area, uint32_t subpass,
710 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
711 const char *func_name) const {
712 bool skip = false;
713 const auto *attachment_ci = rp_state.createInfo.pAttachments;
714 VkExtent3D extent = CastTo3D(render_area.extent);
715 VkOffset3D offset = CastTo3D(render_area.offset);
716
717 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
718 if (subpass == rp_state.attachment_last_subpass[i]) {
719 if (attachment_views[i] == nullptr) continue;
720 const IMAGE_VIEW_STATE &view = *attachment_views[i];
721 const IMAGE_STATE *image = view.image_state.get();
722 if (image == nullptr) continue;
723 const auto &ci = attachment_ci[i];
724
725 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
726 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
727 // sake, we treat DONT_CARE as writing.
728 const bool has_depth = FormatHasDepth(ci.format);
729 const bool has_stencil = FormatHasStencil(ci.format);
730 const bool is_color = !(has_depth || has_stencil);
731 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
732 if (!has_stencil && !store_op_stores) continue;
733
734 HazardResult hazard;
735 const char *aspect = nullptr;
736 bool checked_stencil = false;
737 if (is_color) {
738 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
739 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
740 aspect = "color";
741 } else {
742 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
743 auto hazard_range = view.normalized_subresource_range;
744 if (has_depth && store_op_stores) {
745 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
746 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
747 kAttachmentRasterOrder, offset, extent);
748 aspect = "depth";
749 }
750 if (!hazard.hazard && has_stencil && stencil_op_stores) {
751 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
752 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
753 kAttachmentRasterOrder, offset, extent);
754 aspect = "stencil";
755 checked_stencil = true;
756 }
757 }
758
759 if (hazard.hazard) {
760 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
761 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
762 skip |= sync_state.LogError(
763 rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
764 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " %s aspect during store with %s %s.", func_name,
765 string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string, store_op_string);
766 }
767 }
768 }
769 return skip;
770}
771
John Zulaufb027cdb2020-05-21 14:25:22 -0600772bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
773 const VkRect2D &render_area,
774 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
775 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600776 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
777 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
778 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600779}
780
John Zulauf3d84f1b2020-03-09 13:33:25 -0600781class HazardDetector {
782 SyncStageAccessIndex usage_index_;
783
784 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600785 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600786 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
787 return pos->second.DetectAsyncHazard(usage_index_);
788 }
789 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
790};
791
John Zulauf69133422020-05-20 14:55:53 -0600792class HazardDetectorWithOrdering {
793 const SyncStageAccessIndex usage_index_;
794 const SyncOrderingBarrier &ordering_;
795
796 public:
797 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
798 return pos->second.DetectHazard(usage_index_, ordering_);
799 }
800 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
801 return pos->second.DetectAsyncHazard(usage_index_);
802 }
803 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
804 : usage_index_(usage), ordering_(ordering) {}
805};
806
John Zulauf16adfc92020-04-08 10:28:33 -0600807HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600808 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600809 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600810 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600811}
812
John Zulauf16adfc92020-04-08 10:28:33 -0600813HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600814 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600815 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600816 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600817}
818
John Zulauf69133422020-05-20 14:55:53 -0600819template <typename Detector>
820HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
821 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
822 const VkExtent3D &extent, DetectOptions options) const {
823 if (!SimpleBinding(image)) return HazardResult();
824 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
825 const auto address_type = ImageAddressType(image);
826 const auto base_address = ResourceBaseAddress(image);
827 for (; range_gen->non_empty(); ++range_gen) {
828 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
829 if (hazard.hazard) return hazard;
830 }
831 return HazardResult();
832}
833
John Zulauf540266b2020-04-06 18:54:53 -0600834HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
835 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
836 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700837 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
838 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -0600839 return DetectHazard(image, current_usage, subresource_range, offset, extent);
840}
841
842HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
843 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
844 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -0600845 HazardDetector detector(current_usage);
846 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
847}
848
849HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
850 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
851 const VkOffset3D &offset, const VkExtent3D &extent) const {
852 HazardDetectorWithOrdering detector(current_usage, ordering);
853 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -0600854}
855
John Zulaufb027cdb2020-05-21 14:25:22 -0600856// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
857// should have reported the issue regarding an invalid attachment entry
858HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
859 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
860 VkImageAspectFlags aspect_mask) const {
861 if (view != nullptr) {
862 const IMAGE_STATE *image = view->image_state.get();
863 if (image != nullptr) {
864 auto *detect_range = &view->normalized_subresource_range;
865 VkImageSubresourceRange masked_range;
866 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
867 masked_range = view->normalized_subresource_range;
868 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
869 detect_range = &masked_range;
870 }
871
872 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
873 if (detect_range->aspectMask) {
874 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
875 }
876 }
877 }
878 return HazardResult();
879}
John Zulauf3d84f1b2020-03-09 13:33:25 -0600880class BarrierHazardDetector {
881 public:
882 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
883 SyncStageAccessFlags src_access_scope)
884 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
885
John Zulauf5f13a792020-03-10 07:31:21 -0600886 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
887 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -0700888 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600889 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
890 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
891 return pos->second.DetectAsyncHazard(usage_index_);
892 }
893
894 private:
895 SyncStageAccessIndex usage_index_;
896 VkPipelineStageFlags src_exec_scope_;
897 SyncStageAccessFlags src_access_scope_;
898};
899
John Zulauf16adfc92020-04-08 10:28:33 -0600900HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -0600901 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600902 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600903 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -0600904 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700905}
906
John Zulauf16adfc92020-04-08 10:28:33 -0600907HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600908 SyncStageAccessFlags src_access_scope,
909 const VkImageSubresourceRange &subresource_range,
910 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -0600911 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
912 VkOffset3D zero_offset = {0, 0, 0};
913 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700914}
915
John Zulauf355e49b2020-04-24 15:11:15 -0600916HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
917 SyncStageAccessFlags src_stage_accesses,
918 const VkImageMemoryBarrier &barrier) const {
919 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
920 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
921 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
922}
923
John Zulauf9cb530d2019-09-30 14:14:10 -0600924template <typename Flags, typename Map>
925SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
926 SyncStageAccessFlags scope = 0;
927 for (const auto &bit_scope : map) {
928 if (flag_mask < bit_scope.first) break;
929
930 if (flag_mask & bit_scope.first) {
931 scope |= bit_scope.second;
932 }
933 }
934 return scope;
935}
936
937SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
938 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
939}
940
941SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
942 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
943}
944
945// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
946SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -0600947 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
948 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
949 // 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 -0600950 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
951}
952
953template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -0700954void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -0600955 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
956 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -0600957 auto pos = accesses->lower_bound(range);
958 if (pos == accesses->end() || !pos->first.intersects(range)) {
959 // The range is empty, fill it with a default value.
960 pos = action.Infill(accesses, pos, range);
961 } else if (range.begin < pos->first.begin) {
962 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -0700963 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -0600964 } else if (pos->first.begin < range.begin) {
965 // Trim the beginning if needed
966 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
967 ++pos;
968 }
969
970 const auto the_end = accesses->end();
971 while ((pos != the_end) && pos->first.intersects(range)) {
972 if (pos->first.end > range.end) {
973 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
974 }
975
976 pos = action(accesses, pos);
977 if (pos == the_end) break;
978
979 auto next = pos;
980 ++next;
981 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
982 // Need to infill if next is disjoint
983 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -0700984 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -0600985 next = action.Infill(accesses, next, new_range);
986 }
987 pos = next;
988 }
989}
990
991struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700992 using Iterator = ResourceAccessRangeMap::iterator;
993 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600994 // this is only called on gaps, and never returns a gap.
995 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -0600996 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600997 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -0600998 }
John Zulauf5f13a792020-03-10 07:31:21 -0600999
John Zulauf5c5e88d2019-12-26 11:22:02 -07001000 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001001 auto &access_state = pos->second;
1002 access_state.Update(usage, tag);
1003 return pos;
1004 }
1005
John Zulauf16adfc92020-04-08 10:28:33 -06001006 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001007 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001008 : type(type_), context(context_), usage(usage_), tag(tag_) {}
1009 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001010 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001011 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001012 const ResourceUsageTag &tag;
1013};
1014
1015struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001016 using Iterator = ResourceAccessRangeMap::iterator;
1017 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001018
John Zulauf5c5e88d2019-12-26 11:22:02 -07001019 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001020 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001021 access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001022 return pos;
1023 }
1024
John Zulauf36bcf6a2020-02-03 15:12:52 -07001025 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
1026 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
1027 : src_exec_scope(src_exec_scope_),
1028 src_access_scope(src_access_scope_),
1029 dst_exec_scope(dst_exec_scope_),
1030 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001031
John Zulauf36bcf6a2020-02-03 15:12:52 -07001032 VkPipelineStageFlags src_exec_scope;
1033 SyncStageAccessFlags src_access_scope;
1034 VkPipelineStageFlags dst_exec_scope;
1035 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001036};
1037
1038struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001039 using Iterator = ResourceAccessRangeMap::iterator;
1040 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001041
John Zulauf5c5e88d2019-12-26 11:22:02 -07001042 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001043 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001044 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001045
1046 for (const auto &functor : barrier_functor) {
1047 functor(accesses, pos);
1048 }
1049 return pos;
1050 }
1051
John Zulauf36bcf6a2020-02-03 15:12:52 -07001052 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
1053 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -06001054 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001055 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001056 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
1057 barrier_functor.reserve(memoryBarrierCount);
1058 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
1059 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -07001060 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
1061 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -06001062 }
1063 }
1064
John Zulauf36bcf6a2020-02-03 15:12:52 -07001065 const VkPipelineStageFlags src_exec_scope;
1066 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001067 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
1068};
1069
John Zulauf355e49b2020-04-24 15:11:15 -06001070void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1071 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001072 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1073 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001074}
1075
John Zulauf16adfc92020-04-08 10:28:33 -06001076void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001077 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001078 if (!SimpleBinding(buffer)) return;
1079 const auto base_address = ResourceBaseAddress(buffer);
1080 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1081}
John Zulauf355e49b2020-04-24 15:11:15 -06001082
John Zulauf540266b2020-04-06 18:54:53 -06001083void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001084 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001085 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001086 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001087 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001088 const auto address_type = ImageAddressType(image);
1089 const auto base_address = ResourceBaseAddress(image);
1090 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001091 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001092 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001093 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001094}
John Zulauf7635de32020-05-29 17:14:15 -06001095void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1096 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1097 if (view != nullptr) {
1098 const IMAGE_STATE *image = view->image_state.get();
1099 if (image != nullptr) {
1100 auto *update_range = &view->normalized_subresource_range;
1101 VkImageSubresourceRange masked_range;
1102 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1103 masked_range = view->normalized_subresource_range;
1104 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1105 update_range = &masked_range;
1106 }
1107 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1108 }
1109 }
1110}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001111
John Zulauf355e49b2020-04-24 15:11:15 -06001112void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1113 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1114 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001115 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1116 subresource.layerCount};
1117 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1118}
1119
John Zulauf540266b2020-04-06 18:54:53 -06001120template <typename Action>
1121void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001122 if (!SimpleBinding(buffer)) return;
1123 const auto base_address = ResourceBaseAddress(buffer);
1124 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001125}
1126
1127template <typename Action>
1128void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1129 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001130 if (!SimpleBinding(image)) return;
1131 const auto address_type = ImageAddressType(image);
1132 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001133
locke-lunargae26eac2020-04-16 15:29:05 -06001134 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001135 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001136
John Zulauf16adfc92020-04-08 10:28:33 -06001137 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001138 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001139 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001140 }
1141}
1142
John Zulauf7635de32020-05-29 17:14:15 -06001143void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1144 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1145 const ResourceUsageTag &tag) {
1146 UpdateStateResolveAction update(*this, tag);
1147 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1148}
1149
John Zulaufaff20662020-06-01 14:07:58 -06001150void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1151 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1152 const ResourceUsageTag &tag) {
1153 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1154 VkExtent3D extent = CastTo3D(render_area.extent);
1155 VkOffset3D offset = CastTo3D(render_area.offset);
1156
1157 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1158 if (rp_state.attachment_last_subpass[i] == subpass) {
1159 if (attachment_views[i] == nullptr) continue; // UNUSED
1160 const auto &view = *attachment_views[i];
1161 const IMAGE_STATE *image = view.image_state.get();
1162 if (image == nullptr) continue;
1163
1164 const auto &ci = attachment_ci[i];
1165 const bool has_depth = FormatHasDepth(ci.format);
1166 const bool has_stencil = FormatHasStencil(ci.format);
1167 const bool is_color = !(has_depth || has_stencil);
1168 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1169
1170 if (is_color && store_op_stores) {
1171 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1172 offset, extent, tag);
1173 } else {
1174 auto update_range = view.normalized_subresource_range;
1175 if (has_depth && store_op_stores) {
1176 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1177 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1178 tag);
1179 }
1180 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1181 if (has_stencil && stencil_op_stores) {
1182 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1183 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1184 tag);
1185 }
1186 }
1187 }
1188 }
1189}
1190
John Zulauf540266b2020-04-06 18:54:53 -06001191template <typename Action>
1192void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1193 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001194 for (const auto address_type : kAddressTypes) {
1195 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001196 }
1197}
1198
1199void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001200 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1201 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -06001202 for (const auto address_type : kAddressTypes) {
John Zulauf355e49b2020-04-24 15:11:15 -06001203 context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier,
1204 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001205 }
1206 }
1207}
1208
John Zulauf355e49b2020-04-24 15:11:15 -06001209void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1210 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1211 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
1212 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1213 UpdateMemoryAccess(image, subresource_range, barrier_action);
1214}
1215
John Zulauf7635de32020-05-29 17:14:15 -06001216// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001217void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1218 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1219 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
1220 bool layout_transition, const ResourceUsageTag &tag) {
1221 if (layout_transition) {
1222 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
1223 tag);
1224 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
1225 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -06001226 } else {
1227 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -06001228 }
John Zulauf355e49b2020-04-24 15:11:15 -06001229}
1230
John Zulauf7635de32020-05-29 17:14:15 -06001231// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001232void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
1233 const VkImageSubresourceRange &subresource_range, bool layout_transition,
1234 const ResourceUsageTag &tag) {
1235 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
1236 subresource_range, layout_transition, tag);
1237}
1238
1239// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001240HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001241 if (!attach_view) return HazardResult();
1242 const auto image_state = attach_view->image_state.get();
1243 if (!image_state) return HazardResult();
1244
John Zulauf355e49b2020-04-24 15:11:15 -06001245 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001246 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001247
1248 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulauf7635de32020-05-29 17:14:15 -06001249 auto hazard = track_back.context->DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope,
1250 track_back.barrier.src_access_scope,
1251 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001252 if (!hazard.hazard) {
1253 // The Async hazard check is against the current context's async set.
John Zulauf7635de32020-05-29 17:14:15 -06001254 hazard = DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope, track_back.barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001255 attach_view->normalized_subresource_range, kDetectAsync);
1256 }
1257 return hazard;
1258}
1259
1260// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1261bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1262
1263 const VkRenderPassBeginInfo *pRenderPassBegin,
1264 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1265 const char *func_name) const {
1266 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1267 bool skip = false;
1268 uint32_t subpass = 0;
1269 const auto &transitions = rp_state.subpass_transitions[subpass];
1270 if (transitions.size()) {
1271 const std::vector<AccessContext> empty_context_vector;
1272 // Create context we can use to validate against...
1273 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1274 const_cast<AccessContext *>(&cb_access_context_));
1275
1276 assert(pRenderPassBegin);
1277 if (nullptr == pRenderPassBegin) return skip;
1278
1279 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1280 assert(fb_state);
1281 if (nullptr == fb_state) return skip;
1282
1283 // Create a limited array of views (which we'll need to toss
1284 std::vector<const IMAGE_VIEW_STATE *> views;
1285 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
1286 const auto attachment_count = count_attachment.first;
1287 const auto *attachments = count_attachment.second;
1288 views.resize(attachment_count, nullptr);
1289 for (const auto &transition : transitions) {
1290 assert(transition.attachment < attachment_count);
1291 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
1292 }
1293
John Zulauf7635de32020-05-29 17:14:15 -06001294 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
1295 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001296 }
1297 return skip;
1298}
1299
locke-lunarg61870c22020-06-09 14:51:50 -06001300bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1301 const char *func_name) const {
1302 bool skip = false;
1303 const PIPELINE_STATE *pPipe = nullptr;
1304 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1305 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1306 if (!pPipe || !per_sets) {
1307 return skip;
1308 }
1309
1310 using DescriptorClass = cvdescriptorset::DescriptorClass;
1311 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1312 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1313 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1314 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1315
1316 for (const auto &stage_state : pPipe->stage_state) {
1317 for (const auto &set_binding : stage_state.descriptor_uses) {
1318 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1319 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1320 set_binding.first.second);
1321 const auto descriptor_type = binding_it.GetType();
1322 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1323 auto array_idx = 0;
1324
1325 if (binding_it.IsVariableDescriptorCount()) {
1326 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1327 }
1328 SyncStageAccessIndex sync_index =
1329 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1330
1331 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1332 uint32_t index = i - index_range.start;
1333 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1334 switch (descriptor->GetClass()) {
1335 case DescriptorClass::ImageSampler:
1336 case DescriptorClass::Image: {
1337 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1338 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1339 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1340 } else {
1341 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1342 }
1343 if (!img_view_state) continue;
1344 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1345 VkExtent3D extent = {};
1346 VkOffset3D offset = {};
1347 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1348 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1349 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1350 } else {
1351 extent = img_state->createInfo.extent;
1352 }
1353 auto hazard = current_context_->DetectHazard(*img_state, sync_index,
1354 img_view_state->normalized_subresource_range, offset, extent);
1355 if (hazard.hazard) {
1356 skip |=
1357 sync_state_->LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1358 "%s: Hazard %s for %s in %s, %s, and %s binding #%d index %d", func_name,
1359 string_SyncHazard(hazard.hazard),
1360 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1361 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1362 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1363 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1364 set_binding.first.second, index);
1365 }
1366 break;
1367 }
1368 case DescriptorClass::TexelBuffer: {
1369 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1370 if (!buf_view_state) continue;
1371 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1372 ResourceAccessRange range =
1373 MakeRange(buf_view_state->create_info.offset,
1374 GetRealWholeSize(buf_view_state->create_info.offset, buf_view_state->create_info.range,
1375 buf_state->createInfo.size));
1376 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
1377 if (hazard.hazard) {
1378 skip |=
1379 sync_state_->LogError(buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
1380 "%s: Hazard %s for %s in %s, %s, and %s binding #%d index %d", func_name,
1381 string_SyncHazard(hazard.hazard),
1382 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1383 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1384 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1385 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1386 set_binding.first.second, index);
1387 }
1388 break;
1389 }
1390 case DescriptorClass::GeneralBuffer: {
1391 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1392 auto buf_state = buffer_descriptor->GetBufferState();
1393 if (!buf_state) continue;
1394 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1395 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
1396 if (hazard.hazard) {
1397 skip |= sync_state_->LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
1398 "%s: Hazard %s for %s in %s, %s, and %s binding #%d index %d", func_name,
1399 string_SyncHazard(hazard.hazard),
1400 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
1401 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1402 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1403 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1404 set_binding.first.second, index);
1405 }
1406 break;
1407 }
1408 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1409 default:
1410 break;
1411 }
1412 }
1413 }
1414 }
1415 return skip;
1416}
1417
1418void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1419 const ResourceUsageTag &tag) {
1420 const PIPELINE_STATE *pPipe = nullptr;
1421 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1422 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1423 if (!pPipe || !per_sets) {
1424 return;
1425 }
1426
1427 using DescriptorClass = cvdescriptorset::DescriptorClass;
1428 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1429 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1430 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1431 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1432
1433 for (const auto &stage_state : pPipe->stage_state) {
1434 for (const auto &set_binding : stage_state.descriptor_uses) {
1435 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1436 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1437 set_binding.first.second);
1438 const auto descriptor_type = binding_it.GetType();
1439 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1440 auto array_idx = 0;
1441
1442 if (binding_it.IsVariableDescriptorCount()) {
1443 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1444 }
1445 SyncStageAccessIndex sync_index =
1446 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1447
1448 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1449 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1450 switch (descriptor->GetClass()) {
1451 case DescriptorClass::ImageSampler:
1452 case DescriptorClass::Image: {
1453 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1454 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1455 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1456 } else {
1457 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1458 }
1459 if (!img_view_state) continue;
1460 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1461 VkExtent3D extent = {};
1462 VkOffset3D offset = {};
1463 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1464 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1465 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1466 } else {
1467 extent = img_state->createInfo.extent;
1468 }
1469 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1470 offset, extent, tag);
1471 break;
1472 }
1473 case DescriptorClass::TexelBuffer: {
1474 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1475 if (!buf_view_state) continue;
1476 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1477 ResourceAccessRange range =
1478 MakeRange(buf_view_state->create_info.offset, buf_view_state->create_info.range);
1479 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1480 break;
1481 }
1482 case DescriptorClass::GeneralBuffer: {
1483 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1484 auto buf_state = buffer_descriptor->GetBufferState();
1485 if (!buf_state) continue;
1486 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1487 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1488 break;
1489 }
1490 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1491 default:
1492 break;
1493 }
1494 }
1495 }
1496 }
1497}
1498
1499bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1500 bool skip = false;
1501 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1502 if (!pPipe) {
1503 return skip;
1504 }
1505
1506 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1507 const auto &binding_buffers_size = binding_buffers.size();
1508 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1509
1510 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1511 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1512 if (binding_description.binding < binding_buffers_size) {
1513 const auto &binding_buffer = binding_buffers[binding_description.binding];
1514 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1515
1516 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1517 VkDeviceSize range_start = 0;
1518 VkDeviceSize range_size = 0;
1519 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1520 binding_description.stride);
1521 ResourceAccessRange range = MakeRange(range_start, range_size);
1522 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1523 if (hazard.hazard) {
1524 skip |= sync_state_->LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
1525 "%s: Hazard %s for vertex %s in %s", func_name, string_SyncHazard(hazard.hazard),
1526 sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
1527 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str());
1528 }
1529 }
1530 }
1531 return skip;
1532}
1533
1534void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1535 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1536 if (!pPipe) {
1537 return;
1538 }
1539 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1540 const auto &binding_buffers_size = binding_buffers.size();
1541 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1542
1543 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1544 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1545 if (binding_description.binding < binding_buffers_size) {
1546 const auto &binding_buffer = binding_buffers[binding_description.binding];
1547 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1548
1549 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1550 VkDeviceSize range_start = 0;
1551 VkDeviceSize range_size = 0;
1552 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1553 binding_description.stride);
1554 ResourceAccessRange range = MakeRange(range_start, range_size);
1555 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1556 }
1557 }
1558}
1559
1560bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1561 bool skip = false;
1562 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
1563
1564 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1565 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1566 VkDeviceSize range_start = 0;
1567 VkDeviceSize range_size = 0;
1568 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1569 indexCount, index_size);
1570 ResourceAccessRange range = MakeRange(range_start, range_size);
1571 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1572 if (hazard.hazard) {
1573 skip |= sync_state_->LogError(index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
1574 "%s: Hazard %s for index %s in %s", func_name, string_SyncHazard(hazard.hazard),
1575 sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
1576 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str());
1577 }
1578
1579 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1580 // We will detect more accurate range in the future.
1581 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1582 return skip;
1583}
1584
1585void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
1586 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return;
1587
1588 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1589 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1590 VkDeviceSize range_start = 0;
1591 VkDeviceSize range_size = 0;
1592 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1593 indexCount, index_size);
1594 ResourceAccessRange range = MakeRange(range_start, range_size);
1595 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1596
1597 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1598 // We will detect more accurate range in the future.
1599 RecordDrawVertex(UINT32_MAX, 0, tag);
1600}
1601
1602bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
1603 return current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1604 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1605}
1606
1607void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
1608 current_renderpass_context_->RecordDrawSubpassAttachment(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
1609}
1610
John Zulauf355e49b2020-04-24 15:11:15 -06001611bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001612 bool skip = false;
John Zulauf1507ee42020-05-18 11:33:09 -06001613 skip |=
1614 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001615
1616 return skip;
1617}
1618
1619bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1620 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001621 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001622 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -06001623 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1624 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001625
1626 return skip;
1627}
1628
1629void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1630 assert(sync_state_);
1631 if (!cb_state_) return;
1632
1633 // Create an access context the current renderpass.
1634 render_pass_contexts_.emplace_back(&cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06001635 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf355e49b2020-04-24 15:11:15 -06001636 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001637 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001638}
1639
John Zulauf355e49b2020-04-24 15:11:15 -06001640void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001641 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001642 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001643 current_context_ = &current_renderpass_context_->CurrentContext();
1644}
1645
John Zulauf355e49b2020-04-24 15:11:15 -06001646void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001647 assert(current_renderpass_context_);
1648 if (!current_renderpass_context_) return;
1649
John Zulauf7635de32020-05-29 17:14:15 -06001650 current_renderpass_context_->RecordEndRenderPass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001651 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001652 current_renderpass_context_ = nullptr;
1653}
1654
locke-lunarg61870c22020-06-09 14:51:50 -06001655bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1656 const VkRect2D &render_area, const char *func_name) const {
1657 bool skip = false;
1658 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1659 VkExtent3D extent = CastTo3D(render_area.extent);
1660 VkOffset3D offset = CastTo3D(render_area.offset);
1661
1662 if (subpass.inputAttachmentCount && subpass.pInputAttachments) {
1663 for (uint32_t i = 0; i < subpass.inputAttachmentCount; ++i) {
1664 if (subpass.pInputAttachments[i].attachment == VK_ATTACHMENT_UNUSED) continue;
1665 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pInputAttachments[i].attachment];
1666 if (!img_view_state) continue;
1667 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1668 if (!img_state) continue;
1669 HazardResult hazard = external_context_->DetectHazard(*img_state, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ,
1670 img_view_state->normalized_subresource_range, offset, extent);
1671 if (hazard.hazard) {
1672 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1673 "%s: Hazard %s for %s in %s, Subpass #%d, and pInputAttachments ##d", func_name,
1674 string_SyncHazard(hazard.hazard),
1675 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1676 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass, i);
1677 }
1678 }
1679 }
1680 if (subpass.colorAttachmentCount) {
1681 if (subpass.pColorAttachments) {
1682 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
1683 if (subpass.pColorAttachments[i].attachment == VK_ATTACHMENT_UNUSED) continue;
1684 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[i].attachment];
1685 HazardResult hazard =
1686 external_context_->DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1687 kColorAttachmentRasterOrder, offset, extent);
1688 if (hazard.hazard) {
1689 skip |= sync_state.LogError(
1690 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1691 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d", func_name,
1692 string_SyncHazard(hazard.hazard), sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1693 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass, i);
1694 }
1695 }
1696 }
1697 if (subpass.pResolveAttachments) {
1698 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
1699 if (subpass.pResolveAttachments[i].attachment == VK_ATTACHMENT_UNUSED) continue;
1700 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pResolveAttachments[i].attachment];
1701 HazardResult hazard =
1702 external_context_->DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1703 kColorAttachmentRasterOrder, offset, extent);
1704 if (hazard.hazard) {
1705 skip |= sync_state.LogError(
1706 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1707 "%s: Hazard %s for %s in %s, Subpass #%d, and pResolveAttachments #%d", func_name,
1708 string_SyncHazard(hazard.hazard), sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1709 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass, i);
1710 }
1711 }
1712 }
1713 }
1714 if (subpass.pDepthStencilAttachment && subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
1715 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
1716 HazardResult hazard = external_context_->DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1717 kDepthStencilAttachmentRasterOrder, offset, extent);
1718 if (hazard.hazard) {
1719 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1720 "%s: Hazard %s for %s in %s, Subpass #%d, and pDepthStencilAttachment", func_name,
1721 string_SyncHazard(hazard.hazard),
1722 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1723 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass);
1724 }
1725 }
1726 return skip;
1727}
1728
1729void RenderPassAccessContext::RecordDrawSubpassAttachment(const VkRect2D &render_area, const ResourceUsageTag &tag) {
1730 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1731 VkExtent3D extent = CastTo3D(render_area.extent);
1732 VkOffset3D offset = CastTo3D(render_area.offset);
1733
1734 if (subpass.inputAttachmentCount && subpass.pInputAttachments) {
1735 for (uint32_t i = 0; i < subpass.inputAttachmentCount; ++i) {
1736 if (subpass.pInputAttachments[i].attachment == VK_ATTACHMENT_UNUSED) continue;
1737 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pInputAttachments[i].attachment];
1738 external_context_->UpdateAccessState(img_view_state, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, offset, extent, 0,
1739 tag);
1740 }
1741 }
1742 if (subpass.colorAttachmentCount) {
1743 if (subpass.pColorAttachments) {
1744 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
1745 if (subpass.pColorAttachments[i].attachment == VK_ATTACHMENT_UNUSED) continue;
1746 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[i].attachment];
1747 external_context_->UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset,
1748 extent, 0, tag);
1749 }
1750 }
1751 if (subpass.pResolveAttachments) {
1752 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
1753 if (subpass.pResolveAttachments[i].attachment == VK_ATTACHMENT_UNUSED) continue;
1754 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pResolveAttachments[i].attachment];
1755 external_context_->UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset,
1756 extent, 0, tag);
1757 }
1758 }
1759 }
1760 if (subpass.pDepthStencilAttachment && subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
1761 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
1762 external_context_->UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent, 0,
1763 tag);
1764 }
1765}
1766
John Zulauf1507ee42020-05-18 11:33:09 -06001767bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1768 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001769 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001770 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001771 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1772 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001773 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1774 func_name);
1775
John Zulauf355e49b2020-04-24 15:11:15 -06001776 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06001777 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06001778 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1779 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1780 return skip;
1781}
1782bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
1783 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001784 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06001785 bool skip = false;
1786 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1787 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001788 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1789 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06001790 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001791 return skip;
1792}
1793
John Zulauf7635de32020-05-29 17:14:15 -06001794AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
1795 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
1796}
1797
1798bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
1799 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001800 bool skip = false;
1801
John Zulauf7635de32020-05-29 17:14:15 -06001802 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
1803 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
1804 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1805 // to apply and only copy then, if this proves a hot spot.
1806 std::unique_ptr<AccessContext> proxy_for_current;
1807
John Zulauf355e49b2020-04-24 15:11:15 -06001808 // Validate the "finalLayout" transitions to external
1809 // Get them from where there we're hidding in the extra entry.
1810 const auto &final_transitions = rp_state_->subpass_transitions.back();
1811 for (const auto &transition : final_transitions) {
1812 const auto &attach_view = attachment_views_[transition.attachment];
1813 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
1814 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06001815 auto *context = trackback.context;
1816
1817 if (transition.prev_pass == current_subpass_) {
1818 if (!proxy_for_current) {
1819 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
1820 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
1821 }
1822 context = proxy_for_current.get();
1823 }
1824
1825 auto hazard = context->DetectImageBarrierHazard(
John Zulauf355e49b2020-04-24 15:11:15 -06001826 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
1827 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
1828 if (hazard.hazard) {
1829 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
1830 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
1831 " final image layout transition.",
1832 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment);
1833 }
1834 }
1835 return skip;
1836}
1837
1838void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
1839 // Add layout transitions...
1840 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
1841 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06001842 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06001843 for (const auto &transition : transitions) {
1844 const auto attachment_view = attachment_views_[transition.attachment];
1845 if (!attachment_view) continue;
1846 const auto image = attachment_view->image_state.get();
1847 if (!image) continue;
1848
1849 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -06001850 auto insert_pair = view_seen.insert(attachment_view);
1851 if (insert_pair.second) {
1852 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
1853 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
1854
1855 } else {
1856 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
1857 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
1858 auto barrier_to_transition = barrier->barrier;
1859 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
1860 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
1861 }
John Zulauf355e49b2020-04-24 15:11:15 -06001862 }
1863}
1864
John Zulauf1507ee42020-05-18 11:33:09 -06001865void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
1866 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
1867 auto &subpass_context = subpass_contexts_[current_subpass_];
1868 VkExtent3D extent = CastTo3D(render_area.extent);
1869 VkOffset3D offset = CastTo3D(render_area.offset);
1870
1871 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
1872 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
1873 if (attachment_views_[i] == nullptr) continue; // UNUSED
1874 const auto &view = *attachment_views_[i];
1875 const IMAGE_STATE *image = view.image_state.get();
1876 if (image == nullptr) continue;
1877
1878 const auto &ci = attachment_ci[i];
1879 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001880 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001881 const bool is_color = !(has_depth || has_stencil);
1882
1883 if (is_color) {
1884 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
1885 extent, tag);
1886 } else {
1887 auto update_range = view.normalized_subresource_range;
1888 if (has_depth) {
1889 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1890 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
1891 }
1892 if (has_stencil) {
1893 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1894 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
1895 tag);
1896 }
1897 }
1898 }
1899 }
1900}
1901
John Zulauf355e49b2020-04-24 15:11:15 -06001902void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
1903 VkQueueFlags queue_flags, const ResourceUsageTag &tag) {
1904 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06001905 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06001906 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
1907 // Add this for all subpasses here so that they exsist during next subpass validation
1908 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
1909 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context_);
1910 }
1911 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
1912
1913 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06001914 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001915}
John Zulauf1507ee42020-05-18 11:33:09 -06001916
1917void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001918 // Resolves are against *prior* subpass context and thus *before* the subpass increment
1919 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001920 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06001921
John Zulauf355e49b2020-04-24 15:11:15 -06001922 current_subpass_++;
1923 assert(current_subpass_ < subpass_contexts_.size());
1924 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06001925 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001926}
1927
John Zulauf7635de32020-05-29 17:14:15 -06001928void RenderPassAccessContext::RecordEndRenderPass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001929 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06001930 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001931 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06001932
John Zulauf355e49b2020-04-24 15:11:15 -06001933 // Export the accesses from the renderpass...
1934 external_context_->ResolveChildContexts(subpass_contexts_);
1935
1936 // Add the "finalLayout" transitions to external
1937 // Get them from where there we're hidding in the extra entry.
1938 const auto &final_transitions = rp_state_->subpass_transitions.back();
1939 for (const auto &transition : final_transitions) {
1940 const auto &attachment = attachment_views_[transition.attachment];
1941 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
1942 assert(external_context_ == last_trackback.context);
1943 external_context_->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
1944 attachment->normalized_subresource_range, true, tag);
1945 }
1946}
1947
John Zulauf3d84f1b2020-03-09 13:33:25 -06001948SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
1949 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
1950 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
1951 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
1952 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
1953 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
1954 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
1955}
1956
1957void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
1958 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
1959 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
1960}
1961
John Zulauf9cb530d2019-09-30 14:14:10 -06001962HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
1963 HazardResult hazard;
1964 auto usage = FlagBit(usage_index);
1965 if (IsRead(usage)) {
John Zulaufc9201222020-05-13 15:13:03 -06001966 if (last_write && IsWriteHazard(usage)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001967 hazard.Set(READ_AFTER_WRITE, write_tag);
1968 }
1969 } else {
1970 // Assume write
1971 // TODO determine what to do with READ-WRITE usage states if any
1972 // Write-After-Write check -- if we have a previous write to test against
1973 if (last_write && IsWriteHazard(usage)) {
1974 hazard.Set(WRITE_AFTER_WRITE, write_tag);
1975 } else {
John Zulauf69133422020-05-20 14:55:53 -06001976 // Look for casus belli for WAR
John Zulauf9cb530d2019-09-30 14:14:10 -06001977 const auto usage_stage = PipelineStageBit(usage_index);
1978 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
1979 if (IsReadHazard(usage_stage, last_reads[read_index])) {
1980 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
1981 break;
1982 }
1983 }
1984 }
1985 }
1986 return hazard;
1987}
1988
John Zulauf69133422020-05-20 14:55:53 -06001989HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
1990 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
1991 HazardResult hazard;
1992 const auto usage = FlagBit(usage_index);
1993 const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good.
1994 if (IsRead(usage)) {
1995 if (!write_is_ordered && IsWriteHazard(usage)) {
1996 hazard.Set(READ_AFTER_WRITE, write_tag);
1997 }
1998 } else {
1999 if (!write_is_ordered && IsWriteHazard(usage)) {
2000 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2001 } else {
2002 const auto usage_stage = PipelineStageBit(usage_index);
2003 const auto unordered_reads = last_read_stages & ~ordering.exec_scope;
2004 if (unordered_reads) {
2005 // Look for any WAR hazards outside the ordered set of stages
2006 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2007 if (last_reads[read_index].stage & unordered_reads) {
2008 if (IsReadHazard(usage_stage, last_reads[read_index])) {
2009 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
2010 break;
2011 }
2012 }
2013 }
2014 }
2015 }
2016 }
2017 return hazard;
2018}
2019
John Zulauf2f952d22020-02-10 11:34:51 -07002020// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002021HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002022 HazardResult hazard;
2023 auto usage = FlagBit(usage_index);
2024 if (IsRead(usage)) {
2025 if (last_write != 0) {
2026 hazard.Set(READ_RACING_WRITE, write_tag);
2027 }
2028 } else {
2029 if (last_write != 0) {
2030 hazard.Set(WRITE_RACING_WRITE, write_tag);
2031 } else if (last_read_count > 0) {
2032 hazard.Set(WRITE_RACING_READ, last_reads[0].tag);
2033 }
2034 }
2035 return hazard;
2036}
2037
John Zulauf36bcf6a2020-02-03 15:12:52 -07002038HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2039 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002040 // Only supporting image layout transitions for now
2041 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2042 HazardResult hazard;
2043 if (last_write) {
2044 // If the previous write is *not* in the 1st access scope
2045 // *AND* the current barrier is not in the dependency chain
2046 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2047 // then the barrier access is unsafe (R/W after W)
John Zulauf36bcf6a2020-02-03 15:12:52 -07002048 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
John Zulauf0cb5be22020-01-23 12:18:22 -07002049 // TODO: Do we need a difference hazard name for this?
2050 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2051 }
John Zulauf355e49b2020-04-24 15:11:15 -06002052 }
2053 if (!hazard.hazard) {
2054 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002055 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002056 const auto &read_access = last_reads[read_index];
2057 // If the read stage is not in the src sync sync
2058 // *AND* not execution chained with an existing sync barrier (that's the or)
2059 // then the barrier access is unsafe (R/W after R)
2060 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
2061 hazard.Set(WRITE_AFTER_READ, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002062 break;
2063 }
2064 }
2065 }
2066 return hazard;
2067}
2068
John Zulauf5f13a792020-03-10 07:31:21 -06002069// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2070// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2071// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2072void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2073 if (write_tag.IsBefore(other.write_tag)) {
2074 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
2075 *this = other;
2076 } else if (!other.write_tag.IsBefore(write_tag)) {
2077 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2078 // dependency chaining logic or any stage expansion)
2079 write_barriers |= other.write_barriers;
2080
2081 // Merge that read states
2082 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2083 auto &other_read = other.last_reads[other_read_index];
2084 if (last_read_stages & other_read.stage) {
2085 // Merge in the barriers for read stages that exist in *both* this and other
2086 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
2087 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
2088 auto &my_read = last_reads[my_read_index];
2089 if (other_read.stage == my_read.stage) {
2090 if (my_read.tag.IsBefore(other_read.tag)) {
2091 my_read.tag = other_read.tag;
2092 }
2093 my_read.barriers |= other_read.barriers;
2094 break;
2095 }
2096 }
2097 } else {
2098 // The other read stage doesn't exist in this, so add it.
2099 last_reads[last_read_count] = other_read;
2100 last_read_count++;
2101 last_read_stages |= other_read.stage;
2102 }
2103 }
2104 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
2105 // it.
2106}
2107
John Zulauf9cb530d2019-09-30 14:14:10 -06002108void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2109 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2110 const auto usage_bit = FlagBit(usage_index);
2111 if (IsRead(usage_index)) {
2112 // Mulitple outstanding reads may be of interest and do dependency chains independently
2113 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2114 const auto usage_stage = PipelineStageBit(usage_index);
2115 if (usage_stage & last_read_stages) {
2116 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2117 ReadState &access = last_reads[read_index];
2118 if (access.stage == usage_stage) {
2119 access.barriers = 0;
2120 access.tag = tag;
2121 break;
2122 }
2123 }
2124 } else {
2125 // We don't have this stage in the list yet...
2126 assert(last_read_count < last_reads.size());
2127 ReadState &access = last_reads[last_read_count++];
2128 access.stage = usage_stage;
2129 access.barriers = 0;
2130 access.tag = tag;
2131 last_read_stages |= usage_stage;
2132 }
2133 } else {
2134 // Assume write
2135 // TODO determine what to do with READ-WRITE operations if any
2136 // Clobber last read and both sets of barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2137 // if the last_reads/last_write were unsafe, we've reported them,
2138 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
2139 last_read_count = 0;
2140 last_read_stages = 0;
2141
2142 write_barriers = 0;
2143 write_dependency_chain = 0;
2144 write_tag = tag;
2145 last_write = usage_bit;
2146 }
2147}
John Zulauf5f13a792020-03-10 07:31:21 -06002148
John Zulauf9cb530d2019-09-30 14:14:10 -06002149void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
2150 // Execution Barriers only protect read operations
2151 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2152 ReadState &access = last_reads[read_index];
2153 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2154 if (srcStageMask & (access.stage | access.barriers)) {
2155 access.barriers |= dstStageMask;
2156 }
2157 }
2158 if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask;
2159}
2160
John Zulauf36bcf6a2020-02-03 15:12:52 -07002161void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
2162 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002163 // Assuming we've applied the execution side of this barrier, we update just the write
2164 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07002165 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
2166 write_barriers |= dst_access_scope;
2167 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06002168 }
2169}
2170
John Zulaufd1f85d42020-04-15 12:23:15 -06002171void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002172 auto *access_context = GetAccessContextNoInsert(command_buffer);
2173 if (access_context) {
2174 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002175 }
2176}
2177
John Zulaufd1f85d42020-04-15 12:23:15 -06002178void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2179 auto access_found = cb_access_state.find(command_buffer);
2180 if (access_found != cb_access_state.end()) {
2181 access_found->second->Reset();
2182 cb_access_state.erase(access_found);
2183 }
2184}
2185
John Zulauf540266b2020-04-06 18:54:53 -06002186void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002187 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
2188 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002189 const VkMemoryBarrier *pMemoryBarriers) {
2190 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07002191 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002192 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002193 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002194}
2195
John Zulauf540266b2020-04-06 18:54:53 -06002196void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002197 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2198 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002199 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002200 for (uint32_t index = 0; index < barrier_count; index++) {
locke-lunarg3c038002020-04-30 23:08:08 -06002201 auto barrier = barriers[index];
John Zulauf9cb530d2019-09-30 14:14:10 -06002202 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2203 if (!buffer) continue;
locke-lunarg3c038002020-04-30 23:08:08 -06002204 barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002205 ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002206 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2207 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2208 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2209 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002210 }
2211}
2212
John Zulauf540266b2020-04-06 18:54:53 -06002213void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2214 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2215 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002216 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002217 for (uint32_t index = 0; index < barrier_count; index++) {
2218 const auto &barrier = barriers[index];
2219 const auto *image = Get<IMAGE_STATE>(barrier.image);
2220 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002221 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002222 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2223 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2224 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2225 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
2226 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002227 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002228}
2229
2230bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2231 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2232 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002233 const auto *cb_context = GetAccessContext(commandBuffer);
2234 assert(cb_context);
2235 if (!cb_context) return skip;
2236 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002237
John Zulauf3d84f1b2020-03-09 13:33:25 -06002238 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002239 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002240 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002241
2242 for (uint32_t region = 0; region < regionCount; region++) {
2243 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002244 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002245 ResourceAccessRange src_range = MakeRange(
2246 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002247 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002248 if (hazard.hazard) {
2249 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002250 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
2251 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
2252 report_data->FormatHandle(srcBuffer).c_str(), region);
John Zulauf9cb530d2019-09-30 14:14:10 -06002253 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002254 }
John Zulauf16adfc92020-04-08 10:28:33 -06002255 if (dst_buffer && !skip) {
locke-lunargff255f92020-05-13 18:53:52 -06002256 ResourceAccessRange dst_range = MakeRange(
2257 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf355e49b2020-04-24 15:11:15 -06002258 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002259 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002260 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
2261 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
2262 report_data->FormatHandle(dstBuffer).c_str(), region);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002263 }
2264 }
2265 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002266 }
2267 return skip;
2268}
2269
2270void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2271 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002272 auto *cb_context = GetAccessContext(commandBuffer);
2273 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002274 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002275 auto *context = cb_context->GetCurrentAccessContext();
2276
John Zulauf9cb530d2019-09-30 14:14:10 -06002277 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002278 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002279
2280 for (uint32_t region = 0; region < regionCount; region++) {
2281 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002282 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002283 ResourceAccessRange src_range = MakeRange(
2284 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002285 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002286 }
John Zulauf16adfc92020-04-08 10:28:33 -06002287 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002288 ResourceAccessRange dst_range = MakeRange(
2289 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002290 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002291 }
2292 }
2293}
2294
2295bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2296 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2297 const VkImageCopy *pRegions) const {
2298 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002299 const auto *cb_access_context = GetAccessContext(commandBuffer);
2300 assert(cb_access_context);
2301 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002302
John Zulauf3d84f1b2020-03-09 13:33:25 -06002303 const auto *context = cb_access_context->GetCurrentAccessContext();
2304 assert(context);
2305 if (!context) return skip;
2306
2307 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2308 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002309 for (uint32_t region = 0; region < regionCount; region++) {
2310 const auto &copy_region = pRegions[region];
2311 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002312 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002313 copy_region.srcOffset, copy_region.extent);
2314 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002315 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
2316 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
2317 report_data->FormatHandle(srcImage).c_str(), region);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002318 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002319 }
2320
2321 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002322 VkExtent3D dst_copy_extent =
2323 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002324 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002325 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002326 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002327 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
2328 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
2329 report_data->FormatHandle(dstImage).c_str(), region);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002330 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002331 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002332 }
2333 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002334
John Zulauf5c5e88d2019-12-26 11:22:02 -07002335 return skip;
2336}
2337
2338void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2339 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2340 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002341 auto *cb_access_context = GetAccessContext(commandBuffer);
2342 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002343 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002344 auto *context = cb_access_context->GetCurrentAccessContext();
2345 assert(context);
2346
John Zulauf5c5e88d2019-12-26 11:22:02 -07002347 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002348 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002349
2350 for (uint32_t region = 0; region < regionCount; region++) {
2351 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002352 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002353 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2354 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002355 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002356 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002357 VkExtent3D dst_copy_extent =
2358 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002359 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2360 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002361 }
2362 }
2363}
2364
2365bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2366 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2367 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2368 uint32_t bufferMemoryBarrierCount,
2369 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2370 uint32_t imageMemoryBarrierCount,
2371 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2372 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002373 const auto *cb_access_context = GetAccessContext(commandBuffer);
2374 assert(cb_access_context);
2375 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002376
John Zulauf3d84f1b2020-03-09 13:33:25 -06002377 const auto *context = cb_access_context->GetCurrentAccessContext();
2378 assert(context);
2379 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002380
John Zulauf3d84f1b2020-03-09 13:33:25 -06002381 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002382 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2383 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002384 // Validate Image Layout transitions
2385 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2386 const auto &barrier = pImageMemoryBarriers[index];
2387 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2388 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2389 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002390 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002391 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002392 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002393 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
2394 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s", string_SyncHazard(hazard.hazard),
2395 index, report_data->FormatHandle(barrier.image).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002396 }
2397 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002398
2399 return skip;
2400}
2401
2402void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2403 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2404 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2405 uint32_t bufferMemoryBarrierCount,
2406 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2407 uint32_t imageMemoryBarrierCount,
2408 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002409 auto *cb_access_context = GetAccessContext(commandBuffer);
2410 assert(cb_access_context);
2411 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002412 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002413 auto access_context = cb_access_context->GetCurrentAccessContext();
2414 assert(access_context);
2415 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002416
John Zulauf3d84f1b2020-03-09 13:33:25 -06002417 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002418 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002419 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002420 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2421 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2422 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002423 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
2424 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002425 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06002426 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002427
2428 // 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 -06002429 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07002430 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06002431}
2432
2433void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
2434 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
2435 // The state tracker sets up the device state
2436 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
2437
John Zulauf5f13a792020-03-10 07:31:21 -06002438 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
2439 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06002440 // TODO: Find a good way to do this hooklessly.
2441 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
2442 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
2443 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
2444
John Zulaufd1f85d42020-04-15 12:23:15 -06002445 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2446 sync_device_state->ResetCommandBufferCallback(command_buffer);
2447 });
2448 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2449 sync_device_state->FreeCommandBufferCallback(command_buffer);
2450 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002451}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002452
John Zulauf355e49b2020-04-24 15:11:15 -06002453bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2454 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2455 bool skip = false;
2456 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2457 auto cb_context = GetAccessContext(commandBuffer);
2458
2459 if (rp_state && cb_context) {
2460 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2461 }
2462
2463 return skip;
2464}
2465
2466bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2467 VkSubpassContents contents) const {
2468 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2469 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2470 subpass_begin_info.contents = contents;
2471 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
2472 return skip;
2473}
2474
2475bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2476 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2477 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2478 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
2479 return skip;
2480}
2481
2482bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2483 const VkRenderPassBeginInfo *pRenderPassBegin,
2484 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2485 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2486 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
2487 return skip;
2488}
2489
John Zulauf3d84f1b2020-03-09 13:33:25 -06002490void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
2491 VkResult result) {
2492 // The state tracker sets up the command buffer state
2493 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
2494
2495 // Create/initialize the structure that trackers accesses at the command buffer scope.
2496 auto cb_access_context = GetAccessContext(commandBuffer);
2497 assert(cb_access_context);
2498 cb_access_context->Reset();
2499}
2500
2501void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06002502 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002503 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002504 if (cb_context) {
2505 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002506 }
2507}
2508
2509void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2510 VkSubpassContents contents) {
2511 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2512 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2513 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002514 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002515}
2516
2517void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2518 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2519 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002520 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002521}
2522
2523void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2524 const VkRenderPassBeginInfo *pRenderPassBegin,
2525 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2526 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002527 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
2528}
2529
2530bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2531 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
2532 bool skip = false;
2533
2534 auto cb_context = GetAccessContext(commandBuffer);
2535 assert(cb_context);
2536 auto cb_state = cb_context->GetCommandBufferState();
2537 if (!cb_state) return skip;
2538
2539 auto rp_state = cb_state->activeRenderPass;
2540 if (!rp_state) return skip;
2541
2542 skip |= cb_context->ValidateNextSubpass(func_name);
2543
2544 return skip;
2545}
2546
2547bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
2548 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
2549 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2550 subpass_begin_info.contents = contents;
2551 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
2552 return skip;
2553}
2554
2555bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2556 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2557 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2558 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
2559 return skip;
2560}
2561
2562bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2563 const VkSubpassEndInfo *pSubpassEndInfo) const {
2564 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2565 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
2566 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002567}
2568
2569void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06002570 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002571 auto cb_context = GetAccessContext(commandBuffer);
2572 assert(cb_context);
2573 auto cb_state = cb_context->GetCommandBufferState();
2574 if (!cb_state) return;
2575
2576 auto rp_state = cb_state->activeRenderPass;
2577 if (!rp_state) return;
2578
John Zulauf355e49b2020-04-24 15:11:15 -06002579 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002580}
2581
2582void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
2583 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
2584 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2585 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002586 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002587}
2588
2589void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2590 const VkSubpassEndInfo *pSubpassEndInfo) {
2591 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002592 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002593}
2594
2595void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2596 const VkSubpassEndInfo *pSubpassEndInfo) {
2597 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002598 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002599}
2600
John Zulauf355e49b2020-04-24 15:11:15 -06002601bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
2602 const char *func_name) const {
2603 bool skip = false;
2604
2605 auto cb_context = GetAccessContext(commandBuffer);
2606 assert(cb_context);
2607 auto cb_state = cb_context->GetCommandBufferState();
2608 if (!cb_state) return skip;
2609
2610 auto rp_state = cb_state->activeRenderPass;
2611 if (!rp_state) return skip;
2612
2613 skip |= cb_context->ValidateEndRenderpass(func_name);
2614 return skip;
2615}
2616
2617bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2618 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
2619 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
2620 return skip;
2621}
2622
2623bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
2624 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2625 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2626 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
2627 return skip;
2628}
2629
2630bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
2631 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2632 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2633 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
2634 return skip;
2635}
2636
2637void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
2638 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06002639 // Resolve the all subpass contexts to the command buffer contexts
2640 auto cb_context = GetAccessContext(commandBuffer);
2641 assert(cb_context);
2642 auto cb_state = cb_context->GetCommandBufferState();
2643 if (!cb_state) return;
2644
locke-lunargaecf2152020-05-12 17:15:41 -06002645 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06002646 if (!rp_state) return;
2647
John Zulauf355e49b2020-04-24 15:11:15 -06002648 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06002649}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002650
2651void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
2652 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002653 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002654}
2655
2656void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
2657 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002658 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002659}
2660
2661void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
2662 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002663 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002664}
locke-lunarga19c71d2020-03-02 18:17:04 -07002665
2666bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2667 VkImageLayout dstImageLayout, uint32_t regionCount,
2668 const VkBufferImageCopy *pRegions) const {
2669 bool skip = false;
2670 const auto *cb_access_context = GetAccessContext(commandBuffer);
2671 assert(cb_access_context);
2672 if (!cb_access_context) return skip;
2673
2674 const auto *context = cb_access_context->GetCurrentAccessContext();
2675 assert(context);
2676 if (!context) return skip;
2677
2678 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07002679 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2680
2681 for (uint32_t region = 0; region < regionCount; region++) {
2682 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002683 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002684 ResourceAccessRange src_range =
2685 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002686 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002687 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002688 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002689 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
2690 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07002691 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region);
2692 }
2693 }
2694 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002695 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002696 copy_region.imageOffset, copy_region.imageExtent);
2697 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002698 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
2699 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07002700 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region);
2701 }
2702 if (skip) break;
2703 }
2704 if (skip) break;
2705 }
2706 return skip;
2707}
2708
2709void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2710 VkImageLayout dstImageLayout, uint32_t regionCount,
2711 const VkBufferImageCopy *pRegions) {
2712 auto *cb_access_context = GetAccessContext(commandBuffer);
2713 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002714 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002715 auto *context = cb_access_context->GetCurrentAccessContext();
2716 assert(context);
2717
2718 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06002719 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002720
2721 for (uint32_t region = 0; region < regionCount; region++) {
2722 const auto &copy_region = pRegions[region];
2723 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002724 ResourceAccessRange src_range =
2725 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002726 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002727 }
2728 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002729 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002730 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002731 }
2732 }
2733}
2734
2735bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
2736 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
2737 const VkBufferImageCopy *pRegions) const {
2738 bool skip = false;
2739 const auto *cb_access_context = GetAccessContext(commandBuffer);
2740 assert(cb_access_context);
2741 if (!cb_access_context) return skip;
2742
2743 const auto *context = cb_access_context->GetCurrentAccessContext();
2744 assert(context);
2745 if (!context) return skip;
2746
2747 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2748 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2749 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
2750 for (uint32_t region = 0; region < regionCount; region++) {
2751 const auto &copy_region = pRegions[region];
2752 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002753 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002754 copy_region.imageOffset, copy_region.imageExtent);
2755 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002756 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
2757 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07002758 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region);
2759 }
2760 }
2761 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06002762 ResourceAccessRange dst_range =
2763 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002764 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002765 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002766 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
2767 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32,
locke-lunarga19c71d2020-03-02 18:17:04 -07002768 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region);
2769 }
2770 }
2771 if (skip) break;
2772 }
2773 return skip;
2774}
2775
2776void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2777 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
2778 auto *cb_access_context = GetAccessContext(commandBuffer);
2779 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002780 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07002781 auto *context = cb_access_context->GetCurrentAccessContext();
2782 assert(context);
2783
2784 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002785 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2786 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 -06002787 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07002788
2789 for (uint32_t region = 0; region < regionCount; region++) {
2790 const auto &copy_region = pRegions[region];
2791 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002792 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002793 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002794 }
2795 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002796 ResourceAccessRange dst_range =
2797 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002798 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002799 }
2800 }
2801}
2802
2803bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2804 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2805 const VkImageBlit *pRegions, VkFilter filter) const {
2806 bool skip = false;
2807 const auto *cb_access_context = GetAccessContext(commandBuffer);
2808 assert(cb_access_context);
2809 if (!cb_access_context) return skip;
2810
2811 const auto *context = cb_access_context->GetCurrentAccessContext();
2812 assert(context);
2813 if (!context) return skip;
2814
2815 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2816 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2817
2818 for (uint32_t region = 0; region < regionCount; region++) {
2819 const auto &blit_region = pRegions[region];
2820 if (src_image) {
2821 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
2822 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
2823 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002824 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002825 blit_region.srcOffsets[0], extent);
2826 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002827 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
2828 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
2829 report_data->FormatHandle(srcImage).c_str(), region);
locke-lunarga19c71d2020-03-02 18:17:04 -07002830 }
2831 }
2832
2833 if (dst_image) {
2834 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
2835 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
2836 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002837 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002838 blit_region.dstOffsets[0], extent);
2839 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002840 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
2841 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
2842 report_data->FormatHandle(dstImage).c_str(), region);
locke-lunarga19c71d2020-03-02 18:17:04 -07002843 }
2844 if (skip) break;
2845 }
2846 }
2847
2848 return skip;
2849}
2850
2851void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2852 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2853 const VkImageBlit *pRegions, VkFilter filter) {
2854 auto *cb_access_context = GetAccessContext(commandBuffer);
2855 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002856 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002857 auto *context = cb_access_context->GetCurrentAccessContext();
2858 assert(context);
2859
2860 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002861 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002862
2863 for (uint32_t region = 0; region < regionCount; region++) {
2864 const auto &blit_region = pRegions[region];
2865 if (src_image) {
2866 VkExtent3D extent = {static_cast<uint32_t>(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x),
2867 static_cast<uint32_t>(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y),
2868 static_cast<uint32_t>(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002869 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002870 blit_region.srcOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002871 }
2872 if (dst_image) {
2873 VkExtent3D extent = {static_cast<uint32_t>(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x),
2874 static_cast<uint32_t>(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y),
2875 static_cast<uint32_t>(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z)};
John Zulauf540266b2020-04-06 18:54:53 -06002876 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002877 blit_region.dstOffsets[0], extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002878 }
2879 }
2880}
locke-lunarg36ba2592020-04-03 09:42:04 -06002881
locke-lunarg61870c22020-06-09 14:51:50 -06002882bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
2883 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
2884 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06002885 bool skip = false;
2886 if (drawCount == 0) return skip;
2887
2888 const auto *buf_state = Get<BUFFER_STATE>(buffer);
2889 VkDeviceSize size = struct_size;
2890 if (drawCount == 1 || stride == size) {
2891 if (drawCount > 1) size *= drawCount;
2892 ResourceAccessRange range = MakeRange(offset, size);
2893 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
2894 if (hazard.hazard) {
2895 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for indirect %s in %s",
2896 function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(buffer).c_str(),
2897 report_data->FormatHandle(commandBuffer).c_str());
2898 }
2899 } else {
2900 for (uint32_t i = 0; i < drawCount; ++i) {
2901 ResourceAccessRange range = MakeRange(offset + i * stride, size);
2902 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
2903 if (hazard.hazard) {
2904 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for indirect %s in %s",
2905 function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(buffer).c_str(),
2906 report_data->FormatHandle(commandBuffer).c_str());
2907 break;
2908 }
2909 }
2910 }
2911 return skip;
2912}
2913
locke-lunarg61870c22020-06-09 14:51:50 -06002914void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
2915 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
2916 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06002917 const auto *buf_state = Get<BUFFER_STATE>(buffer);
2918 VkDeviceSize size = struct_size;
2919 if (drawCount == 1 || stride == size) {
2920 if (drawCount > 1) size *= drawCount;
2921 ResourceAccessRange range = MakeRange(offset, size);
2922 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
2923 } else {
2924 for (uint32_t i = 0; i < drawCount; ++i) {
2925 ResourceAccessRange range = MakeRange(offset + i * stride, size);
2926 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
2927 }
2928 }
2929}
2930
locke-lunarg61870c22020-06-09 14:51:50 -06002931bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
2932 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06002933 bool skip = false;
2934
2935 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
2936 ResourceAccessRange range = MakeRange(offset, 4);
2937 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
2938 if (hazard.hazard) {
2939 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for countBuffer %s in %s",
2940 function, string_SyncHazard(hazard.hazard), report_data->FormatHandle(buffer).c_str(),
2941 report_data->FormatHandle(commandBuffer).c_str());
2942 }
2943 return skip;
2944}
2945
locke-lunarg61870c22020-06-09 14:51:50 -06002946void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06002947 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
2948 ResourceAccessRange range = MakeRange(offset, 4);
2949 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
2950}
2951
locke-lunarg36ba2592020-04-03 09:42:04 -06002952bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06002953 bool skip = false;
2954 ;
locke-lunargff255f92020-05-13 18:53:52 -06002955 const auto *cb_access_context = GetAccessContext(commandBuffer);
2956 assert(cb_access_context);
2957 if (!cb_access_context) return skip;
2958
locke-lunarg61870c22020-06-09 14:51:50 -06002959 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06002960 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06002961}
2962
2963void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunargff255f92020-05-13 18:53:52 -06002964 auto *cb_access_context = GetAccessContext(commandBuffer);
2965 assert(cb_access_context);
2966 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06002967
locke-lunarg61870c22020-06-09 14:51:50 -06002968 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06002969}
locke-lunarge1a67022020-04-29 00:15:36 -06002970
2971bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06002972 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06002973 const auto *cb_access_context = GetAccessContext(commandBuffer);
2974 assert(cb_access_context);
2975 if (!cb_access_context) return skip;
2976
2977 const auto *context = cb_access_context->GetCurrentAccessContext();
2978 assert(context);
2979 if (!context) return skip;
2980
locke-lunarg61870c22020-06-09 14:51:50 -06002981 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
2982 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
2983 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06002984 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06002985}
2986
2987void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06002988 auto *cb_access_context = GetAccessContext(commandBuffer);
2989 assert(cb_access_context);
2990 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
2991 auto *context = cb_access_context->GetCurrentAccessContext();
2992 assert(context);
2993
locke-lunarg61870c22020-06-09 14:51:50 -06002994 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
2995 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06002996}
2997
2998bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2999 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003000 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003001 const auto *cb_access_context = GetAccessContext(commandBuffer);
3002 assert(cb_access_context);
3003 if (!cb_access_context) return skip;
3004
locke-lunarg61870c22020-06-09 14:51:50 -06003005 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3006 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3007 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003008 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003009}
3010
3011void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3012 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunargff255f92020-05-13 18:53:52 -06003013 auto *cb_access_context = GetAccessContext(commandBuffer);
3014 assert(cb_access_context);
3015 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003016
locke-lunarg61870c22020-06-09 14:51:50 -06003017 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3018 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3019 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003020}
3021
3022bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3023 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003024 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003025 const auto *cb_access_context = GetAccessContext(commandBuffer);
3026 assert(cb_access_context);
3027 if (!cb_access_context) return skip;
3028
locke-lunarg61870c22020-06-09 14:51:50 -06003029 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3030 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3031 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003032 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003033}
3034
3035void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3036 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunargff255f92020-05-13 18:53:52 -06003037 auto *cb_access_context = GetAccessContext(commandBuffer);
3038 assert(cb_access_context);
3039 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003040
locke-lunarg61870c22020-06-09 14:51:50 -06003041 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3042 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3043 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003044}
3045
3046bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3047 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003048 bool skip = false;
3049 if (drawCount == 0) return skip;
3050
locke-lunargff255f92020-05-13 18:53:52 -06003051 const auto *cb_access_context = GetAccessContext(commandBuffer);
3052 assert(cb_access_context);
3053 if (!cb_access_context) return skip;
3054
3055 const auto *context = cb_access_context->GetCurrentAccessContext();
3056 assert(context);
3057 if (!context) return skip;
3058
locke-lunarg61870c22020-06-09 14:51:50 -06003059 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3060 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3061 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3062 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003063
3064 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3065 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3066 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003067 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003068 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003069}
3070
3071void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3072 uint32_t drawCount, uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003073 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003074 auto *cb_access_context = GetAccessContext(commandBuffer);
3075 assert(cb_access_context);
3076 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3077 auto *context = cb_access_context->GetCurrentAccessContext();
3078 assert(context);
3079
locke-lunarg61870c22020-06-09 14:51:50 -06003080 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3081 cb_access_context->RecordDrawSubpassAttachment(tag);
3082 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003083
3084 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3085 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3086 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003087 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003088}
3089
3090bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3091 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003092 bool skip = false;
3093 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003094 const auto *cb_access_context = GetAccessContext(commandBuffer);
3095 assert(cb_access_context);
3096 if (!cb_access_context) return skip;
3097
3098 const auto *context = cb_access_context->GetCurrentAccessContext();
3099 assert(context);
3100 if (!context) return skip;
3101
locke-lunarg61870c22020-06-09 14:51:50 -06003102 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3103 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3104 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3105 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003106
3107 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3108 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3109 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003110 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003111 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003112}
3113
3114void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3115 uint32_t drawCount, uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003116 auto *cb_access_context = GetAccessContext(commandBuffer);
3117 assert(cb_access_context);
3118 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3119 auto *context = cb_access_context->GetCurrentAccessContext();
3120 assert(context);
3121
locke-lunarg61870c22020-06-09 14:51:50 -06003122 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3123 cb_access_context->RecordDrawSubpassAttachment(tag);
3124 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003125
3126 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3127 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3128 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003129 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003130}
3131
3132bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3133 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3134 uint32_t stride, const char *function) const {
3135 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003136 const auto *cb_access_context = GetAccessContext(commandBuffer);
3137 assert(cb_access_context);
3138 if (!cb_access_context) return skip;
3139
3140 const auto *context = cb_access_context->GetCurrentAccessContext();
3141 assert(context);
3142 if (!context) return skip;
3143
locke-lunarg61870c22020-06-09 14:51:50 -06003144 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3145 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3146 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3147 function);
3148 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003149
3150 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3151 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3152 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003153 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003154 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003155}
3156
3157bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3158 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3159 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003160 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3161 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003162}
3163
3164void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3165 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3166 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003167 auto *cb_access_context = GetAccessContext(commandBuffer);
3168 assert(cb_access_context);
3169 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3170 auto *context = cb_access_context->GetCurrentAccessContext();
3171 assert(context);
3172
locke-lunarg61870c22020-06-09 14:51:50 -06003173 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3174 cb_access_context->RecordDrawSubpassAttachment(tag);
3175 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3176 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003177
3178 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3179 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3180 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003181 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003182}
3183
3184bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3185 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3186 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003187 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3188 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003189}
3190
3191void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3192 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3193 uint32_t maxDrawCount, uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003194 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003195}
3196
3197bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3198 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3199 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003200 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3201 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003202}
3203
3204void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3205 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3206 uint32_t maxDrawCount, uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003207 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3208}
3209
3210bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3211 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3212 uint32_t stride, const char *function) const {
3213 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003214 const auto *cb_access_context = GetAccessContext(commandBuffer);
3215 assert(cb_access_context);
3216 if (!cb_access_context) return skip;
3217
3218 const auto *context = cb_access_context->GetCurrentAccessContext();
3219 assert(context);
3220 if (!context) return skip;
3221
locke-lunarg61870c22020-06-09 14:51:50 -06003222 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3223 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3224 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3225 stride, function);
3226 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003227
3228 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3229 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3230 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003231 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003232 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003233}
3234
3235bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3236 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3237 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003238 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3239 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003240}
3241
3242void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3243 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3244 uint32_t maxDrawCount, uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003245 auto *cb_access_context = GetAccessContext(commandBuffer);
3246 assert(cb_access_context);
3247 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3248 auto *context = cb_access_context->GetCurrentAccessContext();
3249 assert(context);
3250
locke-lunarg61870c22020-06-09 14:51:50 -06003251 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3252 cb_access_context->RecordDrawSubpassAttachment(tag);
3253 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3254 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003255
3256 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3257 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003258 // We will update the index and vertex buffer in SubmitQueue in the future.
3259 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003260}
3261
3262bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3263 VkDeviceSize offset, VkBuffer countBuffer,
3264 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3265 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003266 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3267 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003268}
3269
3270void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3271 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3272 uint32_t maxDrawCount, uint32_t stride) {
3273 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3274}
3275
3276bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3277 VkDeviceSize offset, VkBuffer countBuffer,
3278 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3279 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003280 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3281 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003282}
3283
3284void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3285 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3286 uint32_t maxDrawCount, uint32_t stride) {
3287 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3288}
3289
3290bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3291 const VkClearColorValue *pColor, uint32_t rangeCount,
3292 const VkImageSubresourceRange *pRanges) const {
3293 bool skip = false;
3294 const auto *cb_access_context = GetAccessContext(commandBuffer);
3295 assert(cb_access_context);
3296 if (!cb_access_context) return skip;
3297
3298 const auto *context = cb_access_context->GetCurrentAccessContext();
3299 assert(context);
3300 if (!context) return skip;
3301
3302 const auto *image_state = Get<IMAGE_STATE>(image);
3303
3304 for (uint32_t index = 0; index < rangeCount; index++) {
3305 const auto &range = pRanges[index];
3306 if (image_state) {
3307 auto hazard =
3308 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3309 if (hazard.hazard) {
3310 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
3311 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32, string_SyncHazard(hazard.hazard),
3312 report_data->FormatHandle(image).c_str(), index);
3313 }
3314 }
3315 }
3316 return skip;
3317}
3318
3319void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3320 const VkClearColorValue *pColor, uint32_t rangeCount,
3321 const VkImageSubresourceRange *pRanges) {
3322 auto *cb_access_context = GetAccessContext(commandBuffer);
3323 assert(cb_access_context);
3324 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
3325 auto *context = cb_access_context->GetCurrentAccessContext();
3326 assert(context);
3327
3328 const auto *image_state = Get<IMAGE_STATE>(image);
3329
3330 for (uint32_t index = 0; index < rangeCount; index++) {
3331 const auto &range = pRanges[index];
3332 if (image_state) {
3333 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3334 tag);
3335 }
3336 }
3337}
3338
3339bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3340 VkImageLayout imageLayout,
3341 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3342 const VkImageSubresourceRange *pRanges) const {
3343 bool skip = false;
3344 const auto *cb_access_context = GetAccessContext(commandBuffer);
3345 assert(cb_access_context);
3346 if (!cb_access_context) return skip;
3347
3348 const auto *context = cb_access_context->GetCurrentAccessContext();
3349 assert(context);
3350 if (!context) return skip;
3351
3352 const auto *image_state = Get<IMAGE_STATE>(image);
3353
3354 for (uint32_t index = 0; index < rangeCount; index++) {
3355 const auto &range = pRanges[index];
3356 if (image_state) {
3357 auto hazard =
3358 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3359 if (hazard.hazard) {
3360 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
3361 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32,
3362 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index);
3363 }
3364 }
3365 }
3366 return skip;
3367}
3368
3369void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3370 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3371 const VkImageSubresourceRange *pRanges) {
3372 auto *cb_access_context = GetAccessContext(commandBuffer);
3373 assert(cb_access_context);
3374 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
3375 auto *context = cb_access_context->GetCurrentAccessContext();
3376 assert(context);
3377
3378 const auto *image_state = Get<IMAGE_STATE>(image);
3379
3380 for (uint32_t index = 0; index < rangeCount; index++) {
3381 const auto &range = pRanges[index];
3382 if (image_state) {
3383 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3384 tag);
3385 }
3386 }
3387}
3388
3389bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
3390 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
3391 VkDeviceSize dstOffset, VkDeviceSize stride,
3392 VkQueryResultFlags flags) const {
3393 bool skip = false;
3394 const auto *cb_access_context = GetAccessContext(commandBuffer);
3395 assert(cb_access_context);
3396 if (!cb_access_context) return skip;
3397
3398 const auto *context = cb_access_context->GetCurrentAccessContext();
3399 assert(context);
3400 if (!context) return skip;
3401
3402 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3403
3404 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003405 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003406 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3407 if (hazard.hazard) {
3408 skip |=
3409 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s",
3410 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3411 }
3412 }
locke-lunargff255f92020-05-13 18:53:52 -06003413
3414 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003415 return skip;
3416}
3417
3418void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
3419 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3420 VkDeviceSize stride, VkQueryResultFlags flags) {
3421 auto *cb_access_context = GetAccessContext(commandBuffer);
3422 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06003423 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06003424 auto *context = cb_access_context->GetCurrentAccessContext();
3425 assert(context);
3426
3427 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3428
3429 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003430 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003431 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3432 }
locke-lunargff255f92020-05-13 18:53:52 -06003433
3434 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003435}
3436
3437bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3438 VkDeviceSize size, uint32_t data) const {
3439 bool skip = false;
3440 const auto *cb_access_context = GetAccessContext(commandBuffer);
3441 assert(cb_access_context);
3442 if (!cb_access_context) return skip;
3443
3444 const auto *context = cb_access_context->GetCurrentAccessContext();
3445 assert(context);
3446 if (!context) return skip;
3447
3448 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3449
3450 if (dst_buffer) {
3451 ResourceAccessRange range = MakeRange(dstOffset, size);
3452 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3453 if (hazard.hazard) {
3454 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdFillBuffer: Hazard %s for dstBuffer %s",
3455 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3456 }
3457 }
3458 return skip;
3459}
3460
3461void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3462 VkDeviceSize size, uint32_t data) {
3463 auto *cb_access_context = GetAccessContext(commandBuffer);
3464 assert(cb_access_context);
3465 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
3466 auto *context = cb_access_context->GetCurrentAccessContext();
3467 assert(context);
3468
3469 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3470
3471 if (dst_buffer) {
3472 ResourceAccessRange range = MakeRange(dstOffset, size);
3473 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3474 }
3475}
3476
3477bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3478 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3479 const VkImageResolve *pRegions) const {
3480 bool skip = false;
3481 const auto *cb_access_context = GetAccessContext(commandBuffer);
3482 assert(cb_access_context);
3483 if (!cb_access_context) return skip;
3484
3485 const auto *context = cb_access_context->GetCurrentAccessContext();
3486 assert(context);
3487 if (!context) return skip;
3488
3489 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3490 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3491
3492 for (uint32_t region = 0; region < regionCount; region++) {
3493 const auto &resolve_region = pRegions[region];
3494 if (src_image) {
3495 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3496 resolve_region.srcOffset, resolve_region.extent);
3497 if (hazard.hazard) {
3498 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
3499 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
3500 report_data->FormatHandle(srcImage).c_str(), region);
3501 }
3502 }
3503
3504 if (dst_image) {
3505 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3506 resolve_region.dstOffset, resolve_region.extent);
3507 if (hazard.hazard) {
3508 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
3509 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32, string_SyncHazard(hazard.hazard),
3510 report_data->FormatHandle(dstImage).c_str(), region);
3511 }
3512 if (skip) break;
3513 }
3514 }
3515
3516 return skip;
3517}
3518
3519void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3520 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3521 const VkImageResolve *pRegions) {
3522 auto *cb_access_context = GetAccessContext(commandBuffer);
3523 assert(cb_access_context);
3524 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
3525 auto *context = cb_access_context->GetCurrentAccessContext();
3526 assert(context);
3527
3528 auto *src_image = Get<IMAGE_STATE>(srcImage);
3529 auto *dst_image = Get<IMAGE_STATE>(dstImage);
3530
3531 for (uint32_t region = 0; region < regionCount; region++) {
3532 const auto &resolve_region = pRegions[region];
3533 if (src_image) {
3534 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3535 resolve_region.srcOffset, resolve_region.extent, tag);
3536 }
3537 if (dst_image) {
3538 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3539 resolve_region.dstOffset, resolve_region.extent, tag);
3540 }
3541 }
3542}
3543
3544bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3545 VkDeviceSize dataSize, const void *pData) const {
3546 bool skip = false;
3547 const auto *cb_access_context = GetAccessContext(commandBuffer);
3548 assert(cb_access_context);
3549 if (!cb_access_context) return skip;
3550
3551 const auto *context = cb_access_context->GetCurrentAccessContext();
3552 assert(context);
3553 if (!context) return skip;
3554
3555 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3556
3557 if (dst_buffer) {
3558 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3559 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3560 if (hazard.hazard) {
3561 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s",
3562 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3563 }
3564 }
3565 return skip;
3566}
3567
3568void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3569 VkDeviceSize dataSize, const void *pData) {
3570 auto *cb_access_context = GetAccessContext(commandBuffer);
3571 assert(cb_access_context);
3572 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
3573 auto *context = cb_access_context->GetCurrentAccessContext();
3574 assert(context);
3575
3576 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3577
3578 if (dst_buffer) {
3579 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3580 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3581 }
3582}
locke-lunargff255f92020-05-13 18:53:52 -06003583
3584bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3585 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
3586 bool skip = false;
3587 const auto *cb_access_context = GetAccessContext(commandBuffer);
3588 assert(cb_access_context);
3589 if (!cb_access_context) return skip;
3590
3591 const auto *context = cb_access_context->GetCurrentAccessContext();
3592 assert(context);
3593 if (!context) return skip;
3594
3595 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3596
3597 if (dst_buffer) {
3598 ResourceAccessRange range = MakeRange(dstOffset, 4);
3599 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3600 if (hazard.hazard) {
3601 skip |=
3602 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard), "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s",
3603 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str());
3604 }
3605 }
3606 return skip;
3607}
3608
3609void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3610 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
3611 auto *cb_access_context = GetAccessContext(commandBuffer);
3612 assert(cb_access_context);
3613 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
3614 auto *context = cb_access_context->GetCurrentAccessContext();
3615 assert(context);
3616
3617 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3618
3619 if (dst_buffer) {
3620 ResourceAccessRange range = MakeRange(dstOffset, 4);
3621 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3622 }
3623}