blob: 7657be434749f2efdc63a4ac71d0264613e316f4 [file] [log] [blame]
locke-lunarg8ec19162020-06-16 18:48:34 -06001/* Copyright (c) 2019-2020 The Khronos Group Inc.
2 * Copyright (c) 2019-2020 Valve Corporation
3 * Copyright (c) 2019-2020 LunarG, Inc.
John Zulauf9cb530d2019-09-30 14:14:10 -06004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: John Zulauf <jzulauf@lunarg.com>
18 */
19
20#include <limits>
21#include <vector>
locke-lunarg296a3c92020-03-25 01:04:29 -060022#include <memory>
23#include <bitset>
John Zulauf9cb530d2019-09-30 14:14:10 -060024#include "synchronization_validation.h"
25
26static const char *string_SyncHazardVUID(SyncHazard hazard) {
27 switch (hazard) {
28 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070029 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060030 break;
31 case SyncHazard::READ_AFTER_WRITE:
32 return "SYNC-HAZARD-READ_AFTER_WRITE";
33 break;
34 case SyncHazard::WRITE_AFTER_READ:
35 return "SYNC-HAZARD-WRITE_AFTER_READ";
36 break;
37 case SyncHazard::WRITE_AFTER_WRITE:
38 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
39 break;
John Zulauf2f952d22020-02-10 11:34:51 -070040 case SyncHazard::READ_RACING_WRITE:
41 return "SYNC-HAZARD-READ-RACING-WRITE";
42 break;
43 case SyncHazard::WRITE_RACING_WRITE:
44 return "SYNC-HAZARD-WRITE-RACING-WRITE";
45 break;
46 case SyncHazard::WRITE_RACING_READ:
47 return "SYNC-HAZARD-WRITE-RACING-READ";
48 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060049 default:
50 assert(0);
51 }
52 return "SYNC-HAZARD-INVALID";
53}
54
55static const char *string_SyncHazard(SyncHazard hazard) {
56 switch (hazard) {
57 case SyncHazard::NONE:
58 return "NONR";
59 break;
60 case SyncHazard::READ_AFTER_WRITE:
61 return "READ_AFTER_WRITE";
62 break;
63 case SyncHazard::WRITE_AFTER_READ:
64 return "WRITE_AFTER_READ";
65 break;
66 case SyncHazard::WRITE_AFTER_WRITE:
67 return "WRITE_AFTER_WRITE";
68 break;
John Zulauf2f952d22020-02-10 11:34:51 -070069 case SyncHazard::READ_RACING_WRITE:
70 return "READ_RACING_WRITE";
71 break;
72 case SyncHazard::WRITE_RACING_WRITE:
73 return "WRITE_RACING_WRITE";
74 break;
75 case SyncHazard::WRITE_RACING_READ:
76 return "WRITE_RACING_READ";
77 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060078 default:
79 assert(0);
80 }
81 return "INVALID HAZARD";
82}
83
John Zulauf1dae9192020-06-16 15:46:44 -060084static std::string string_UsageTag(const ResourceUsageTag &tag) {
85 std::stringstream out;
John Zulaufcc6fecb2020-06-17 15:24:54 -060086 out << "(command " << CommandTypeString(tag.command) << ", seq #" << (tag.index & 0xFFFFFFFF) << ", reset #"
87 << (tag.index >> 32) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -060088 return out.str();
89}
90
John Zulaufd14743a2020-07-03 09:42:39 -060091// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
92// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
93// also reflects this special case for read hazard detection (using access instead of exec scope)
John Zulaufb027cdb2020-05-21 14:25:22 -060094static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
95static constexpr SyncStageAccessFlags kColorAttachmentAccessScope =
96 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
97 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
John Zulaufd14743a2020-07-03 09:42:39 -060098 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
99 SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600100static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
101 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
102static constexpr SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
103 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
104 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
105 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
John Zulaufd14743a2020-07-03 09:42:39 -0600106 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
107 SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600108
109static constexpr SyncOrderingBarrier kColorAttachmentRasterOrder = {kColorAttachmentExecScope, kColorAttachmentAccessScope};
110static constexpr SyncOrderingBarrier kDepthStencilAttachmentRasterOrder = {kDepthStencilAttachmentExecScope,
111 kDepthStencilAttachmentAccessScope};
112static constexpr SyncOrderingBarrier kAttachmentRasterOrder = {kDepthStencilAttachmentExecScope | kColorAttachmentExecScope,
113 kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope};
John Zulauf7635de32020-05-29 17:14:15 -0600114// Sometimes we have an internal access conflict, and we using the kCurrentCommandTag to set and detect in temporary/proxy contexts
John Zulaufcc6fecb2020-06-17 15:24:54 -0600115static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600116
locke-lunarg3c038002020-04-30 23:08:08 -0600117inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
118 if (size == VK_WHOLE_SIZE) {
119 return (whole_size - offset);
120 }
121 return size;
122}
123
John Zulauf16adfc92020-04-08 10:28:33 -0600124template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600125static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600126 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
127}
128
John Zulauf355e49b2020-04-24 15:11:15 -0600129static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600130
John Zulauf0cb5be22020-01-23 12:18:22 -0700131// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
132VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
133 VkPipelineStageFlags expanded = stage_mask;
134 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
135 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
136 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
137 if (all_commands.first & queue_flags) {
138 expanded |= all_commands.second;
139 }
140 }
141 }
142 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
143 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
144 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
145 }
146 return expanded;
147}
148
John Zulauf36bcf6a2020-02-03 15:12:52 -0700149VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
150 std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
151 VkPipelineStageFlags unscanned = stage_mask;
152 VkPipelineStageFlags related = 0;
153 for (const auto entry : map) {
154 const auto stage = entry.first;
155 if (stage & unscanned) {
156 related = related | entry.second;
157 unscanned = unscanned & ~stage;
158 if (!unscanned) break;
159 }
160 }
161 return related;
162}
163
164VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
165 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
166}
167
168VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
169 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
170}
171
John Zulauf5c5e88d2019-12-26 11:22:02 -0700172static const ResourceAccessRange full_range(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700173
locke-lunargff255f92020-05-13 18:53:52 -0600174void GetBufferRange(VkDeviceSize &range_start, VkDeviceSize &range_size, VkDeviceSize offset, VkDeviceSize buf_whole_size,
175 uint32_t first_index, uint32_t count, VkDeviceSize stride) {
176 range_start = offset + first_index * stride;
177 range_size = 0;
178 if (count == UINT32_MAX) {
179 range_size = buf_whole_size - range_start;
180 } else {
181 range_size = count * stride;
182 }
183}
184
locke-lunarg654e3692020-06-04 17:19:15 -0600185SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
186 VkShaderStageFlagBits stage_flag) {
187 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
188 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
189 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
190 }
191 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
192 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
193 assert(0);
194 }
195 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
196 return stage_access->second.uniform_read;
197 }
198
199 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
200 // Because if write hazard happens, read hazard might or might not happen.
201 // But if write hazard doesn't happen, read hazard is impossible to happen.
202 if (descriptor_data.is_writable) {
203 return stage_access->second.shader_write;
204 }
205 return stage_access->second.shader_read;
206}
207
locke-lunarg37047832020-06-12 13:44:45 -0600208bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
209 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
210 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
211 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
212 ? true
213 : false;
214}
215
216bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
217 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
218 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
219 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
220 ? true
221 : false;
222}
223
John Zulauf355e49b2020-04-24 15:11:15 -0600224// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
225const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
226 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
227
John Zulauf7635de32020-05-29 17:14:15 -0600228// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
229// Used by both validation and record operations
230//
231// The signature for Action() reflect the needs of both uses.
232template <typename Action>
233void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
234 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
235 VkExtent3D extent = CastTo3D(render_area.extent);
236 VkOffset3D offset = CastTo3D(render_area.offset);
237 const auto &rp_ci = rp_state.createInfo;
238 const auto *attachment_ci = rp_ci.pAttachments;
239 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
240
241 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
242 const auto *color_attachments = subpass_ci.pColorAttachments;
243 const auto *color_resolve = subpass_ci.pResolveAttachments;
244 if (color_resolve && color_attachments) {
245 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
246 const auto &color_attach = color_attachments[i].attachment;
247 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
248 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
249 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
250 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
251 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
252 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
253 }
254 }
255 }
256
257 // Depth stencil resolve only if the extension is present
258 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
259 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
260 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
261 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
262 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
263 const auto src_ci = attachment_ci[src_at];
264 // The formats are required to match so we can pick either
265 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
266 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
267 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
268 VkImageAspectFlags aspect_mask = 0u;
269
270 // Figure out which aspects are actually touched during resolve operations
271 const char *aspect_string = nullptr;
272 if (resolve_depth && resolve_stencil) {
273 // Validate all aspects together
274 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
275 aspect_string = "depth/stencil";
276 } else if (resolve_depth) {
277 // Validate depth only
278 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
279 aspect_string = "depth";
280 } else if (resolve_stencil) {
281 // Validate all stencil only
282 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
283 aspect_string = "stencil";
284 }
285
286 if (aspect_mask) {
287 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
288 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kDepthStencilAttachmentRasterOrder, offset, extent,
289 aspect_mask);
290 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
291 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
292 }
293 }
294}
295
296// Action for validating resolve operations
297class ValidateResolveAction {
298 public:
299 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
300 const char *func_name)
301 : render_pass_(render_pass),
302 subpass_(subpass),
303 context_(context),
304 sync_state_(sync_state),
305 func_name_(func_name),
306 skip_(false) {}
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 HazardResult hazard;
311 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
312 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600313 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
314 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
315 " to resolve attachment %" PRIu32 ". Prior access %s.",
316 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
317 src_at, dst_at, string_UsageTag(hazard.tag).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600318 }
319 }
320 // Providing a mechanism for the constructing caller to get the result of the validation
321 bool GetSkip() const { return skip_; }
322
323 private:
324 VkRenderPass render_pass_;
325 const uint32_t subpass_;
326 const AccessContext &context_;
327 const SyncValidator &sync_state_;
328 const char *func_name_;
329 bool skip_;
330};
331
332// Update action for resolve operations
333class UpdateStateResolveAction {
334 public:
335 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
336 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
337 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
338 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
339 // Ignores validation only arguments...
340 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
341 }
342
343 private:
344 AccessContext &context_;
345 const ResourceUsageTag &tag_;
346};
347
John Zulauf540266b2020-04-06 18:54:53 -0600348AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
349 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600350 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600351 Reset();
352 const auto &subpass_dep = dependencies[subpass];
353 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600354 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600355 for (const auto &prev_dep : subpass_dep.prev) {
356 assert(prev_dep.dependency);
357 const auto dep = *prev_dep.dependency;
John Zulauf540266b2020-04-06 18:54:53 -0600358 prev_.emplace_back(const_cast<AccessContext *>(&contexts[dep.srcSubpass]), queue_flags, dep);
John Zulauf355e49b2020-04-24 15:11:15 -0600359 prev_by_subpass_[dep.srcSubpass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700360 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600361
362 async_.reserve(subpass_dep.async.size());
363 for (const auto async_subpass : subpass_dep.async) {
John Zulauf540266b2020-04-06 18:54:53 -0600364 async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass]));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600365 }
John Zulaufe5da6e52020-03-18 15:32:18 -0600366 if (subpass_dep.barrier_from_external) {
367 src_external_ = TrackBack(external_context, queue_flags, *subpass_dep.barrier_from_external);
368 } else {
369 src_external_ = TrackBack();
370 }
371 if (subpass_dep.barrier_to_external) {
372 dst_external_ = TrackBack(this, queue_flags, *subpass_dep.barrier_to_external);
373 } else {
374 dst_external_ = TrackBack();
John Zulauf3d84f1b2020-03-09 13:33:25 -0600375 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700376}
377
John Zulauf5f13a792020-03-10 07:31:21 -0600378template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600379HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600380 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600381 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600382 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600383
384 HazardResult hazard;
385 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
386 hazard = detector.Detect(prev);
387 }
388 return hazard;
389}
390
John Zulauf3d84f1b2020-03-09 13:33:25 -0600391// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
392// the DAG of the contexts (for example subpasses)
393template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600394HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
395 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600396 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600397
John Zulauf1a224292020-06-30 14:52:13 -0600398 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600399 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
400 // so we'll check these first
401 for (const auto &async_context : async_) {
402 hazard = async_context->DetectAsyncHazard(type, detector, range);
403 if (hazard.hazard) return hazard;
404 }
John Zulauf5f13a792020-03-10 07:31:21 -0600405 }
406
John Zulauf1a224292020-06-30 14:52:13 -0600407 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600408
John Zulauf69133422020-05-20 14:55:53 -0600409 const auto &accesses = GetAccessStateMap(type);
410 const auto from = accesses.lower_bound(range);
411 const auto to = accesses.upper_bound(range);
412 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600413
John Zulauf69133422020-05-20 14:55:53 -0600414 for (auto pos = from; pos != to; ++pos) {
415 // Cover any leading gap, or gap between entries
416 if (detect_prev) {
417 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
418 // Cover any leading gap, or gap between entries
419 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600420 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600421 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600422 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600423 if (hazard.hazard) return hazard;
424 }
John Zulauf69133422020-05-20 14:55:53 -0600425 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
426 gap.begin = pos->first.end;
427 }
428
429 hazard = detector.Detect(pos);
430 if (hazard.hazard) return hazard;
431 }
432
433 if (detect_prev) {
434 // Detect in the trailing empty as needed
435 gap.end = range.end;
436 if (gap.non_empty()) {
437 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600438 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600439 }
440
441 return hazard;
442}
443
444// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
445template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600446HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600447 auto &accesses = GetAccessStateMap(type);
448 const auto from = accesses.lower_bound(range);
449 const auto to = accesses.upper_bound(range);
450
John Zulauf3d84f1b2020-03-09 13:33:25 -0600451 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600452 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
453 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600454 }
John Zulauf16adfc92020-04-08 10:28:33 -0600455
John Zulauf3d84f1b2020-03-09 13:33:25 -0600456 return hazard;
457}
458
John Zulauf355e49b2020-04-24 15:11:15 -0600459// Returns the last resolved entry
460static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
461 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
462 const SyncBarrier *barrier) {
463 auto at = entry;
464 for (auto pos = first; pos != last; ++pos) {
465 // Every member of the input iterator range must fit within the remaining portion of entry
466 assert(at->first.includes(pos->first));
467 assert(at != dest->end());
468 // Trim up at to the same size as the entry to resolve
469 at = sparse_container::split(at, *dest, pos->first);
470 auto access = pos->second;
471 if (barrier) {
472 access.ApplyBarrier(*barrier);
473 }
474 at->second.Resolve(access);
475 ++at; // Go to the remaining unused section of entry
476 }
477}
478
479void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const SyncBarrier *barrier,
480 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
481 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600482 if (!range.non_empty()) return;
483
John Zulauf355e49b2020-04-24 15:11:15 -0600484 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
485 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600486 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600487 if (current->pos_B->valid) {
488 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600489 auto access = src_pos->second;
490 if (barrier) {
491 access.ApplyBarrier(*barrier);
492 }
John Zulauf16adfc92020-04-08 10:28:33 -0600493 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600494 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
495 trimmed->second.Resolve(access);
496 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600497 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600498 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600499 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600500 }
John Zulauf16adfc92020-04-08 10:28:33 -0600501 } else {
502 // we have to descend to fill this gap
503 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600504 if (current->pos_A->valid) {
505 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
506 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600507 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulauf355e49b2020-04-24 15:11:15 -0600508 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier);
509 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600510 // There isn't anything in dest in current)range, so we can accumulate directly into it.
511 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulauf355e49b2020-04-24 15:11:15 -0600512 if (barrier) {
513 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
John Zulauf3bcab5e2020-06-19 14:42:32 -0600514 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
John Zulauf355e49b2020-04-24 15:11:15 -0600515 pos->second.ApplyBarrier(*barrier);
516 }
517 }
518 }
519 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
520 // iterator of the outer while.
521
522 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
523 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
524 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600525 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
526 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600527 current.seek(seek_to);
528 } else if (!current->pos_A->valid && infill_state) {
529 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
530 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
531 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600532 }
John Zulauf5f13a792020-03-10 07:31:21 -0600533 }
John Zulauf16adfc92020-04-08 10:28:33 -0600534 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600535 }
John Zulauf1a224292020-06-30 14:52:13 -0600536
537 // Infill if range goes passed both the current and resolve map prior contents
538 if (recur_to_infill && (current->range.end < range.end)) {
539 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
540 ResourceAccessRangeMap gap_map;
541 const auto the_end = resolve_map->end();
542 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
543 for (auto &access : gap_map) {
544 access.second.ApplyBarrier(*barrier);
545 resolve_map->insert(the_end, access);
546 }
547 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600548}
549
John Zulauf355e49b2020-04-24 15:11:15 -0600550void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
551 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600552 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600553 if (range.non_empty() && infill_state) {
554 descent_map->insert(std::make_pair(range, *infill_state));
555 }
556 } else {
557 // Look for something to fill the gap further along.
558 for (const auto &prev_dep : prev_) {
John Zulauf355e49b2020-04-24 15:11:15 -0600559 prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600560 }
561
John Zulaufe5da6e52020-03-18 15:32:18 -0600562 if (src_external_.context) {
John Zulauf355e49b2020-04-24 15:11:15 -0600563 src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600564 }
565 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600566}
567
John Zulauf16adfc92020-04-08 10:28:33 -0600568AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600569 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600570}
571
572VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
573 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
574}
575
John Zulauf355e49b2020-04-24 15:11:15 -0600576static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600577
John Zulauf1507ee42020-05-18 11:33:09 -0600578static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
579 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
580 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
581 return stage_access;
582}
583static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
584 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
585 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
586 return stage_access;
587}
588
John Zulauf7635de32020-05-29 17:14:15 -0600589// Caller must manage returned pointer
590static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
591 uint32_t subpass, const VkRect2D &render_area,
592 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
593 auto *proxy = new AccessContext(context);
594 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600595 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600596 return proxy;
597}
598
John Zulauf540266b2020-04-06 18:54:53 -0600599void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600600 AddressType address_type, ResourceAccessRangeMap *descent_map,
601 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600602 if (!SimpleBinding(image_state)) return;
603
John Zulauf62f10592020-04-03 12:20:02 -0600604 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600605 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600606 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600607 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600608 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600609 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600610 }
611}
612
John Zulauf7635de32020-05-29 17:14:15 -0600613// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600614bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600615 const VkRect2D &render_area, uint32_t subpass,
616 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
617 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600618 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600619 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
620 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
621 // those affects have not been recorded yet.
622 //
623 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
624 // to apply and only copy then, if this proves a hot spot.
625 std::unique_ptr<AccessContext> proxy_for_prev;
626 TrackBack proxy_track_back;
627
John Zulauf355e49b2020-04-24 15:11:15 -0600628 const auto &transitions = rp_state.subpass_transitions[subpass];
629 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600630 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
631
632 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
633 if (prev_needs_proxy) {
634 if (!proxy_for_prev) {
635 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
636 render_area, attachment_views));
637 proxy_track_back = *track_back;
638 proxy_track_back.context = proxy_for_prev.get();
639 }
640 track_back = &proxy_track_back;
641 }
642 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600643 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600644 skip |= sync_state.LogError(
645 rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
646 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " image layout transition. Prior access %s.",
647 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment, string_UsageTag(hazard.tag).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600648 }
649 }
650 return skip;
651}
652
John Zulauf1507ee42020-05-18 11:33:09 -0600653bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600654 const VkRect2D &render_area, uint32_t subpass,
655 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
656 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600657 bool skip = false;
658 const auto *attachment_ci = rp_state.createInfo.pAttachments;
659 VkExtent3D extent = CastTo3D(render_area.extent);
660 VkOffset3D offset = CastTo3D(render_area.offset);
661 const auto external_access_scope = src_external_.barrier.dst_access_scope;
John Zulauf1507ee42020-05-18 11:33:09 -0600662
663 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
664 if (subpass == rp_state.attachment_first_subpass[i]) {
665 if (attachment_views[i] == nullptr) continue;
666 const IMAGE_VIEW_STATE &view = *attachment_views[i];
667 const IMAGE_STATE *image = view.image_state.get();
668 if (image == nullptr) continue;
669 const auto &ci = attachment_ci[i];
670 const bool is_transition = rp_state.attachment_first_is_transition[i];
671
672 // Need check in the following way
673 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
674 // vs. transition
675 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
676 // for each aspect loaded.
677
678 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600679 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600680 const bool is_color = !(has_depth || has_stencil);
681
682 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
683 const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U;
684 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
685 const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U;
686
John Zulaufaff20662020-06-01 14:07:58 -0600687 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600688 const char *aspect = nullptr;
689 if (is_transition) {
690 // For transition w
691 SyncHazard transition_hazard = SyncHazard::NONE;
692 bool checked_stencil = false;
693 if (load_mask) {
694 if ((load_mask & external_access_scope) != load_mask) {
695 transition_hazard =
696 SyncStageAccess::HasWrite(load_mask) ? SyncHazard::WRITE_AFTER_WRITE : SyncHazard::READ_AFTER_WRITE;
697 aspect = is_color ? "color" : "depth";
698 }
699 if (!transition_hazard && stencil_mask) {
700 if ((stencil_mask & external_access_scope) != stencil_mask) {
701 transition_hazard = SyncStageAccess::HasWrite(stencil_mask) ? SyncHazard::WRITE_AFTER_WRITE
702 : SyncHazard::READ_AFTER_WRITE;
703 aspect = "stencil";
704 checked_stencil = true;
705 }
706 }
707 }
708 if (transition_hazard) {
709 // Hazard vs. ILT
710 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
711 skip |=
712 sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
713 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
714 " aspect %s during load with loadOp %s.",
715 func_name, string_SyncHazard(transition_hazard), subpass, i, aspect, load_op_string);
716 }
717 } else {
718 auto hazard_range = view.normalized_subresource_range;
719 bool checked_stencil = false;
720 if (is_color) {
721 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
722 aspect = "color";
723 } else {
724 if (has_depth) {
725 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
726 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
727 aspect = "depth";
728 }
729 if (!hazard.hazard && has_stencil) {
730 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
731 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
732 aspect = "stencil";
733 checked_stencil = true;
734 }
735 }
736
737 if (hazard.hazard) {
738 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
739 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
740 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
locke-lunarg88dbb542020-06-23 22:05:42 -0600741 " aspect %s during load with loadOp %s. Prior access %s.",
742 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
743 string_UsageTag(hazard.tag).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -0600744 }
745 }
746 }
747 }
748 return skip;
749}
750
John Zulaufaff20662020-06-01 14:07:58 -0600751// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
752// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
753// store is part of the same Next/End operation.
754// The latter is handled in layout transistion validation directly
755bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
756 const VkRect2D &render_area, uint32_t subpass,
757 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
758 const char *func_name) const {
759 bool skip = false;
760 const auto *attachment_ci = rp_state.createInfo.pAttachments;
761 VkExtent3D extent = CastTo3D(render_area.extent);
762 VkOffset3D offset = CastTo3D(render_area.offset);
763
764 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
765 if (subpass == rp_state.attachment_last_subpass[i]) {
766 if (attachment_views[i] == nullptr) continue;
767 const IMAGE_VIEW_STATE &view = *attachment_views[i];
768 const IMAGE_STATE *image = view.image_state.get();
769 if (image == nullptr) continue;
770 const auto &ci = attachment_ci[i];
771
772 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
773 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
774 // sake, we treat DONT_CARE as writing.
775 const bool has_depth = FormatHasDepth(ci.format);
776 const bool has_stencil = FormatHasStencil(ci.format);
777 const bool is_color = !(has_depth || has_stencil);
778 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
779 if (!has_stencil && !store_op_stores) continue;
780
781 HazardResult hazard;
782 const char *aspect = nullptr;
783 bool checked_stencil = false;
784 if (is_color) {
785 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
786 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
787 aspect = "color";
788 } else {
789 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
790 auto hazard_range = view.normalized_subresource_range;
791 if (has_depth && store_op_stores) {
792 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
793 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
794 kAttachmentRasterOrder, offset, extent);
795 aspect = "depth";
796 }
797 if (!hazard.hazard && has_stencil && stencil_op_stores) {
798 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
799 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
800 kAttachmentRasterOrder, offset, extent);
801 aspect = "stencil";
802 checked_stencil = true;
803 }
804 }
805
806 if (hazard.hazard) {
807 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
808 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -0600809 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
810 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
811 " %s aspect during store with %s %s. Prior access %s",
812 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
813 store_op_string, string_UsageTag(hazard.tag).c_str());
John Zulaufaff20662020-06-01 14:07:58 -0600814 }
815 }
816 }
817 return skip;
818}
819
John Zulaufb027cdb2020-05-21 14:25:22 -0600820bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
821 const VkRect2D &render_area,
822 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
823 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600824 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
825 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
826 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600827}
828
John Zulauf3d84f1b2020-03-09 13:33:25 -0600829class HazardDetector {
830 SyncStageAccessIndex usage_index_;
831
832 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600833 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600834 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
835 return pos->second.DetectAsyncHazard(usage_index_);
836 }
837 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
838};
839
John Zulauf69133422020-05-20 14:55:53 -0600840class HazardDetectorWithOrdering {
841 const SyncStageAccessIndex usage_index_;
842 const SyncOrderingBarrier &ordering_;
843
844 public:
845 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
846 return pos->second.DetectHazard(usage_index_, ordering_);
847 }
848 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
849 return pos->second.DetectAsyncHazard(usage_index_);
850 }
851 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
852 : usage_index_(usage), ordering_(ordering) {}
853};
854
John Zulauf16adfc92020-04-08 10:28:33 -0600855HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600856 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600857 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600858 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600859}
860
John Zulauf16adfc92020-04-08 10:28:33 -0600861HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600862 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600863 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600864 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600865}
866
John Zulauf69133422020-05-20 14:55:53 -0600867template <typename Detector>
868HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
869 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
870 const VkExtent3D &extent, DetectOptions options) const {
871 if (!SimpleBinding(image)) return HazardResult();
872 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
873 const auto address_type = ImageAddressType(image);
874 const auto base_address = ResourceBaseAddress(image);
875 for (; range_gen->non_empty(); ++range_gen) {
876 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
877 if (hazard.hazard) return hazard;
878 }
879 return HazardResult();
880}
881
John Zulauf540266b2020-04-06 18:54:53 -0600882HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
883 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
884 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700885 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
886 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -0600887 return DetectHazard(image, current_usage, subresource_range, offset, extent);
888}
889
890HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
891 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
892 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -0600893 HazardDetector detector(current_usage);
894 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
895}
896
897HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
898 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
899 const VkOffset3D &offset, const VkExtent3D &extent) const {
900 HazardDetectorWithOrdering detector(current_usage, ordering);
901 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -0600902}
903
John Zulaufb027cdb2020-05-21 14:25:22 -0600904// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
905// should have reported the issue regarding an invalid attachment entry
906HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
907 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
908 VkImageAspectFlags aspect_mask) const {
909 if (view != nullptr) {
910 const IMAGE_STATE *image = view->image_state.get();
911 if (image != nullptr) {
912 auto *detect_range = &view->normalized_subresource_range;
913 VkImageSubresourceRange masked_range;
914 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
915 masked_range = view->normalized_subresource_range;
916 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
917 detect_range = &masked_range;
918 }
919
920 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
921 if (detect_range->aspectMask) {
922 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
923 }
924 }
925 }
926 return HazardResult();
927}
John Zulauf3d84f1b2020-03-09 13:33:25 -0600928class BarrierHazardDetector {
929 public:
930 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
931 SyncStageAccessFlags src_access_scope)
932 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
933
John Zulauf5f13a792020-03-10 07:31:21 -0600934 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
935 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -0700936 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600937 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
938 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
939 return pos->second.DetectAsyncHazard(usage_index_);
940 }
941
942 private:
943 SyncStageAccessIndex usage_index_;
944 VkPipelineStageFlags src_exec_scope_;
945 SyncStageAccessFlags src_access_scope_;
946};
947
John Zulauf16adfc92020-04-08 10:28:33 -0600948HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -0600949 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600950 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600951 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -0600952 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700953}
954
John Zulauf16adfc92020-04-08 10:28:33 -0600955HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600956 SyncStageAccessFlags src_access_scope,
957 const VkImageSubresourceRange &subresource_range,
958 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -0600959 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
960 VkOffset3D zero_offset = {0, 0, 0};
961 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700962}
963
John Zulauf355e49b2020-04-24 15:11:15 -0600964HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
965 SyncStageAccessFlags src_stage_accesses,
966 const VkImageMemoryBarrier &barrier) const {
967 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
968 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
969 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
970}
971
John Zulauf9cb530d2019-09-30 14:14:10 -0600972template <typename Flags, typename Map>
973SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
974 SyncStageAccessFlags scope = 0;
975 for (const auto &bit_scope : map) {
976 if (flag_mask < bit_scope.first) break;
977
978 if (flag_mask & bit_scope.first) {
979 scope |= bit_scope.second;
980 }
981 }
982 return scope;
983}
984
985SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
986 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
987}
988
989SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
990 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
991}
992
993// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
994SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -0600995 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
996 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
997 // 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 -0600998 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
999}
1000
1001template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001002void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001003 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1004 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -06001005 auto pos = accesses->lower_bound(range);
1006 if (pos == accesses->end() || !pos->first.intersects(range)) {
1007 // The range is empty, fill it with a default value.
1008 pos = action.Infill(accesses, pos, range);
1009 } else if (range.begin < pos->first.begin) {
1010 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001011 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001012 } else if (pos->first.begin < range.begin) {
1013 // Trim the beginning if needed
1014 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1015 ++pos;
1016 }
1017
1018 const auto the_end = accesses->end();
1019 while ((pos != the_end) && pos->first.intersects(range)) {
1020 if (pos->first.end > range.end) {
1021 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1022 }
1023
1024 pos = action(accesses, pos);
1025 if (pos == the_end) break;
1026
1027 auto next = pos;
1028 ++next;
1029 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1030 // Need to infill if next is disjoint
1031 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001032 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001033 next = action.Infill(accesses, next, new_range);
1034 }
1035 pos = next;
1036 }
1037}
1038
1039struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001040 using Iterator = ResourceAccessRangeMap::iterator;
1041 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001042 // this is only called on gaps, and never returns a gap.
1043 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001044 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001045 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001046 }
John Zulauf5f13a792020-03-10 07:31:21 -06001047
John Zulauf5c5e88d2019-12-26 11:22:02 -07001048 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001049 auto &access_state = pos->second;
1050 access_state.Update(usage, tag);
1051 return pos;
1052 }
1053
John Zulauf16adfc92020-04-08 10:28:33 -06001054 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001055 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001056 : type(type_), context(context_), usage(usage_), tag(tag_) {}
1057 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001058 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001059 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001060 const ResourceUsageTag &tag;
1061};
1062
1063struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001064 using Iterator = ResourceAccessRangeMap::iterator;
1065 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001066
John Zulauf5c5e88d2019-12-26 11:22:02 -07001067 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001068 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001069 access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001070 return pos;
1071 }
1072
John Zulauf36bcf6a2020-02-03 15:12:52 -07001073 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
1074 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
1075 : src_exec_scope(src_exec_scope_),
1076 src_access_scope(src_access_scope_),
1077 dst_exec_scope(dst_exec_scope_),
1078 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001079
John Zulauf36bcf6a2020-02-03 15:12:52 -07001080 VkPipelineStageFlags src_exec_scope;
1081 SyncStageAccessFlags src_access_scope;
1082 VkPipelineStageFlags dst_exec_scope;
1083 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001084};
1085
1086struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001087 using Iterator = ResourceAccessRangeMap::iterator;
1088 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001089
John Zulauf5c5e88d2019-12-26 11:22:02 -07001090 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001091 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001092 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001093
1094 for (const auto &functor : barrier_functor) {
1095 functor(accesses, pos);
1096 }
1097 return pos;
1098 }
1099
John Zulauf36bcf6a2020-02-03 15:12:52 -07001100 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
1101 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -06001102 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001103 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001104 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
1105 barrier_functor.reserve(memoryBarrierCount);
1106 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
1107 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -07001108 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
1109 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -06001110 }
1111 }
1112
John Zulauf36bcf6a2020-02-03 15:12:52 -07001113 const VkPipelineStageFlags src_exec_scope;
1114 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001115 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
1116};
1117
John Zulauf355e49b2020-04-24 15:11:15 -06001118void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1119 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001120 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1121 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001122}
1123
John Zulauf16adfc92020-04-08 10:28:33 -06001124void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001125 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001126 if (!SimpleBinding(buffer)) return;
1127 const auto base_address = ResourceBaseAddress(buffer);
1128 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1129}
John Zulauf355e49b2020-04-24 15:11:15 -06001130
John Zulauf540266b2020-04-06 18:54:53 -06001131void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001132 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001133 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001134 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001135 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001136 const auto address_type = ImageAddressType(image);
1137 const auto base_address = ResourceBaseAddress(image);
1138 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001139 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001140 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001141 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001142}
John Zulauf7635de32020-05-29 17:14:15 -06001143void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1144 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1145 if (view != nullptr) {
1146 const IMAGE_STATE *image = view->image_state.get();
1147 if (image != nullptr) {
1148 auto *update_range = &view->normalized_subresource_range;
1149 VkImageSubresourceRange masked_range;
1150 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1151 masked_range = view->normalized_subresource_range;
1152 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1153 update_range = &masked_range;
1154 }
1155 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1156 }
1157 }
1158}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001159
John Zulauf355e49b2020-04-24 15:11:15 -06001160void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1161 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1162 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001163 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1164 subresource.layerCount};
1165 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1166}
1167
John Zulauf540266b2020-04-06 18:54:53 -06001168template <typename Action>
1169void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001170 if (!SimpleBinding(buffer)) return;
1171 const auto base_address = ResourceBaseAddress(buffer);
1172 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001173}
1174
1175template <typename Action>
1176void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1177 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001178 if (!SimpleBinding(image)) return;
1179 const auto address_type = ImageAddressType(image);
1180 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001181
locke-lunargae26eac2020-04-16 15:29:05 -06001182 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001183 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001184
John Zulauf16adfc92020-04-08 10:28:33 -06001185 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001186 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001187 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001188 }
1189}
1190
John Zulauf7635de32020-05-29 17:14:15 -06001191void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1192 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1193 const ResourceUsageTag &tag) {
1194 UpdateStateResolveAction update(*this, tag);
1195 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1196}
1197
John Zulaufaff20662020-06-01 14:07:58 -06001198void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1199 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1200 const ResourceUsageTag &tag) {
1201 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1202 VkExtent3D extent = CastTo3D(render_area.extent);
1203 VkOffset3D offset = CastTo3D(render_area.offset);
1204
1205 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1206 if (rp_state.attachment_last_subpass[i] == subpass) {
1207 if (attachment_views[i] == nullptr) continue; // UNUSED
1208 const auto &view = *attachment_views[i];
1209 const IMAGE_STATE *image = view.image_state.get();
1210 if (image == nullptr) continue;
1211
1212 const auto &ci = attachment_ci[i];
1213 const bool has_depth = FormatHasDepth(ci.format);
1214 const bool has_stencil = FormatHasStencil(ci.format);
1215 const bool is_color = !(has_depth || has_stencil);
1216 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1217
1218 if (is_color && store_op_stores) {
1219 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1220 offset, extent, tag);
1221 } else {
1222 auto update_range = view.normalized_subresource_range;
1223 if (has_depth && store_op_stores) {
1224 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1225 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1226 tag);
1227 }
1228 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1229 if (has_stencil && stencil_op_stores) {
1230 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1231 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1232 tag);
1233 }
1234 }
1235 }
1236 }
1237}
1238
John Zulauf540266b2020-04-06 18:54:53 -06001239template <typename Action>
1240void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1241 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001242 for (const auto address_type : kAddressTypes) {
1243 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001244 }
1245}
1246
1247void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001248 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1249 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -06001250 for (const auto address_type : kAddressTypes) {
John Zulauf355e49b2020-04-24 15:11:15 -06001251 context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier,
1252 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001253 }
1254 }
1255}
1256
John Zulauf355e49b2020-04-24 15:11:15 -06001257void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1258 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1259 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
1260 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1261 UpdateMemoryAccess(image, subresource_range, barrier_action);
1262}
1263
John Zulauf7635de32020-05-29 17:14:15 -06001264// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001265void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1266 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1267 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
1268 bool layout_transition, const ResourceUsageTag &tag) {
1269 if (layout_transition) {
1270 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
1271 tag);
1272 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
1273 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -06001274 } else {
1275 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -06001276 }
John Zulauf355e49b2020-04-24 15:11:15 -06001277}
1278
John Zulauf7635de32020-05-29 17:14:15 -06001279// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001280void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
1281 const VkImageSubresourceRange &subresource_range, bool layout_transition,
1282 const ResourceUsageTag &tag) {
1283 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
1284 subresource_range, layout_transition, tag);
1285}
1286
1287// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001288HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001289 if (!attach_view) return HazardResult();
1290 const auto image_state = attach_view->image_state.get();
1291 if (!image_state) return HazardResult();
1292
John Zulauf355e49b2020-04-24 15:11:15 -06001293 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001294 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001295
1296 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulauf7635de32020-05-29 17:14:15 -06001297 auto hazard = track_back.context->DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope,
1298 track_back.barrier.src_access_scope,
1299 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001300 if (!hazard.hazard) {
1301 // The Async hazard check is against the current context's async set.
John Zulauf7635de32020-05-29 17:14:15 -06001302 hazard = DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope, track_back.barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001303 attach_view->normalized_subresource_range, kDetectAsync);
1304 }
1305 return hazard;
1306}
1307
1308// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1309bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1310
1311 const VkRenderPassBeginInfo *pRenderPassBegin,
1312 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1313 const char *func_name) const {
1314 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1315 bool skip = false;
1316 uint32_t subpass = 0;
1317 const auto &transitions = rp_state.subpass_transitions[subpass];
1318 if (transitions.size()) {
1319 const std::vector<AccessContext> empty_context_vector;
1320 // Create context we can use to validate against...
1321 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1322 const_cast<AccessContext *>(&cb_access_context_));
1323
1324 assert(pRenderPassBegin);
1325 if (nullptr == pRenderPassBegin) return skip;
1326
1327 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1328 assert(fb_state);
1329 if (nullptr == fb_state) return skip;
1330
1331 // Create a limited array of views (which we'll need to toss
1332 std::vector<const IMAGE_VIEW_STATE *> views;
1333 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
1334 const auto attachment_count = count_attachment.first;
1335 const auto *attachments = count_attachment.second;
1336 views.resize(attachment_count, nullptr);
1337 for (const auto &transition : transitions) {
1338 assert(transition.attachment < attachment_count);
1339 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
1340 }
1341
John Zulauf7635de32020-05-29 17:14:15 -06001342 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
1343 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001344 }
1345 return skip;
1346}
1347
locke-lunarg61870c22020-06-09 14:51:50 -06001348bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1349 const char *func_name) const {
1350 bool skip = false;
1351 const PIPELINE_STATE *pPipe = nullptr;
1352 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1353 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1354 if (!pPipe || !per_sets) {
1355 return skip;
1356 }
1357
1358 using DescriptorClass = cvdescriptorset::DescriptorClass;
1359 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1360 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1361 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1362 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1363
1364 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarg37047832020-06-12 13:44:45 -06001365 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001366 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1367 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001368 for (const auto &set_binding : stage_state.descriptor_uses) {
1369 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1370 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1371 set_binding.first.second);
1372 const auto descriptor_type = binding_it.GetType();
1373 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1374 auto array_idx = 0;
1375
1376 if (binding_it.IsVariableDescriptorCount()) {
1377 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1378 }
1379 SyncStageAccessIndex sync_index =
1380 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1381
1382 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1383 uint32_t index = i - index_range.start;
1384 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1385 switch (descriptor->GetClass()) {
1386 case DescriptorClass::ImageSampler:
1387 case DescriptorClass::Image: {
1388 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1389 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1390 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1391 } else {
1392 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1393 }
1394 if (!img_view_state) continue;
1395 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1396 VkExtent3D extent = {};
1397 VkOffset3D offset = {};
1398 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1399 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1400 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1401 } else {
1402 extent = img_state->createInfo.extent;
1403 }
1404 auto hazard = current_context_->DetectHazard(*img_state, sync_index,
1405 img_view_state->normalized_subresource_range, offset, extent);
1406 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06001407 skip |= sync_state_->LogError(
1408 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1409 "%s: Hazard %s for %s in %s, %s, and %s binding #%" PRIu32 " index %" PRIu32 ". Prior access %s.",
1410 func_name, string_SyncHazard(hazard.hazard),
1411 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1412 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1413 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1414 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(), set_binding.first.second,
1415 index, string_UsageTag(hazard.tag).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001416 }
1417 break;
1418 }
1419 case DescriptorClass::TexelBuffer: {
1420 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1421 if (!buf_view_state) continue;
1422 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1423 ResourceAccessRange range =
1424 MakeRange(buf_view_state->create_info.offset,
1425 GetRealWholeSize(buf_view_state->create_info.offset, buf_view_state->create_info.range,
1426 buf_state->createInfo.size));
1427 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
1428 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001429 skip |= sync_state_->LogError(
1430 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
1431 "%s: Hazard %s for %s in %s, %s, and %s binding #%d index %d. Prior access %s.", func_name,
1432 string_SyncHazard(hazard.hazard),
1433 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1434 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1435 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1436 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(), set_binding.first.second,
1437 index, string_UsageTag(hazard.tag).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001438 }
1439 break;
1440 }
1441 case DescriptorClass::GeneralBuffer: {
1442 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1443 auto buf_state = buffer_descriptor->GetBufferState();
1444 if (!buf_state) continue;
1445 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1446 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
1447 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001448 skip |= sync_state_->LogError(
1449 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
1450 "%s: Hazard %s for %s in %s, %s, and %s binding #%d index %d. Prior access %s.", func_name,
1451 string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
1452 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1453 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1454 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(), set_binding.first.second,
1455 index, string_UsageTag(hazard.tag).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001456 }
1457 break;
1458 }
1459 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1460 default:
1461 break;
1462 }
1463 }
1464 }
1465 }
1466 return skip;
1467}
1468
1469void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1470 const ResourceUsageTag &tag) {
1471 const PIPELINE_STATE *pPipe = nullptr;
1472 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1473 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1474 if (!pPipe || !per_sets) {
1475 return;
1476 }
1477
1478 using DescriptorClass = cvdescriptorset::DescriptorClass;
1479 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1480 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1481 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1482 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1483
1484 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001485 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
1486 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1487 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001488 for (const auto &set_binding : stage_state.descriptor_uses) {
1489 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1490 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1491 set_binding.first.second);
1492 const auto descriptor_type = binding_it.GetType();
1493 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1494 auto array_idx = 0;
1495
1496 if (binding_it.IsVariableDescriptorCount()) {
1497 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1498 }
1499 SyncStageAccessIndex sync_index =
1500 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1501
1502 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1503 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1504 switch (descriptor->GetClass()) {
1505 case DescriptorClass::ImageSampler:
1506 case DescriptorClass::Image: {
1507 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1508 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1509 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1510 } else {
1511 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1512 }
1513 if (!img_view_state) continue;
1514 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1515 VkExtent3D extent = {};
1516 VkOffset3D offset = {};
1517 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1518 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1519 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1520 } else {
1521 extent = img_state->createInfo.extent;
1522 }
1523 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1524 offset, extent, tag);
1525 break;
1526 }
1527 case DescriptorClass::TexelBuffer: {
1528 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1529 if (!buf_view_state) continue;
1530 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1531 ResourceAccessRange range =
1532 MakeRange(buf_view_state->create_info.offset, buf_view_state->create_info.range);
1533 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1534 break;
1535 }
1536 case DescriptorClass::GeneralBuffer: {
1537 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1538 auto buf_state = buffer_descriptor->GetBufferState();
1539 if (!buf_state) continue;
1540 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1541 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1542 break;
1543 }
1544 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1545 default:
1546 break;
1547 }
1548 }
1549 }
1550 }
1551}
1552
1553bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1554 bool skip = false;
1555 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1556 if (!pPipe) {
1557 return skip;
1558 }
1559
1560 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1561 const auto &binding_buffers_size = binding_buffers.size();
1562 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1563
1564 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1565 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1566 if (binding_description.binding < binding_buffers_size) {
1567 const auto &binding_buffer = binding_buffers[binding_description.binding];
1568 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1569
1570 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1571 VkDeviceSize range_start = 0;
1572 VkDeviceSize range_size = 0;
1573 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1574 binding_description.stride);
1575 ResourceAccessRange range = MakeRange(range_start, range_size);
1576 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1577 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001578 skip |= sync_state_->LogError(
1579 buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Prior access %s.",
1580 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
1581 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001582 }
1583 }
1584 }
1585 return skip;
1586}
1587
1588void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1589 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1590 if (!pPipe) {
1591 return;
1592 }
1593 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1594 const auto &binding_buffers_size = binding_buffers.size();
1595 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1596
1597 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1598 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1599 if (binding_description.binding < binding_buffers_size) {
1600 const auto &binding_buffer = binding_buffers[binding_description.binding];
1601 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1602
1603 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1604 VkDeviceSize range_start = 0;
1605 VkDeviceSize range_size = 0;
1606 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1607 binding_description.stride);
1608 ResourceAccessRange range = MakeRange(range_start, range_size);
1609 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1610 }
1611 }
1612}
1613
1614bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1615 bool skip = false;
1616 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
1617
1618 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1619 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1620 VkDeviceSize range_start = 0;
1621 VkDeviceSize range_size = 0;
1622 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1623 indexCount, index_size);
1624 ResourceAccessRange range = MakeRange(range_start, range_size);
1625 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1626 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001627 skip |= sync_state_->LogError(
1628 index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Prior access %s.",
1629 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
1630 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001631 }
1632
1633 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1634 // We will detect more accurate range in the future.
1635 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1636 return skip;
1637}
1638
1639void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
1640 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return;
1641
1642 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1643 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1644 VkDeviceSize range_start = 0;
1645 VkDeviceSize range_size = 0;
1646 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1647 indexCount, index_size);
1648 ResourceAccessRange range = MakeRange(range_start, range_size);
1649 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1650
1651 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1652 // We will detect more accurate range in the future.
1653 RecordDrawVertex(UINT32_MAX, 0, tag);
1654}
1655
1656bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001657 bool skip = false;
1658 if (!current_renderpass_context_) return skip;
1659 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1660 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1661 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001662}
1663
1664void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg7077d502020-06-18 21:37:26 -06001665 if (current_renderpass_context_)
1666 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1667 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001668}
1669
John Zulauf355e49b2020-04-24 15:11:15 -06001670bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001671 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001672 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001673 skip |=
1674 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001675
1676 return skip;
1677}
1678
1679bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1680 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001681 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001682 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001683 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001684 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1685 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001686
1687 return skip;
1688}
1689
1690void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1691 assert(sync_state_);
1692 if (!cb_state_) return;
1693
1694 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001695 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001696 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001697 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001698 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001699}
1700
John Zulauf355e49b2020-04-24 15:11:15 -06001701void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001702 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001703 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001704 current_context_ = &current_renderpass_context_->CurrentContext();
1705}
1706
John Zulauf355e49b2020-04-24 15:11:15 -06001707void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001708 assert(current_renderpass_context_);
1709 if (!current_renderpass_context_) return;
1710
John Zulauf1a224292020-06-30 14:52:13 -06001711 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001712 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001713 current_renderpass_context_ = nullptr;
1714}
1715
locke-lunarg61870c22020-06-09 14:51:50 -06001716bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1717 const VkRect2D &render_area, const char *func_name) const {
1718 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001719 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001720 if (!pPipe ||
1721 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001722 return skip;
1723 }
1724 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001725 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1726 VkExtent3D extent = CastTo3D(render_area.extent);
1727 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001728
John Zulauf1a224292020-06-30 14:52:13 -06001729 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001730 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001731 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1732 for (const auto location : list) {
1733 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1734 continue;
1735 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001736 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1737 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001738 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001739 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1740 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Prior access %s.",
1741 func_name, string_SyncHazard(hazard.hazard),
1742 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1743 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
1744 location, string_UsageTag(hazard.tag).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001745 }
1746 }
1747 }
locke-lunarg37047832020-06-12 13:44:45 -06001748
1749 // PHASE1 TODO: Add layout based read/vs. write selection.
1750 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1751 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1752 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001753 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001754 bool depth_write = false, stencil_write = false;
1755
1756 // PHASE1 TODO: These validation should be in core_checks.
1757 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1758 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1759 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1760 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1761 depth_write = true;
1762 }
1763 // PHASE1 TODO: It needs to check if stencil is writable.
1764 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1765 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1766 // PHASE1 TODO: These validation should be in core_checks.
1767 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1768 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1769 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1770 stencil_write = true;
1771 }
1772
1773 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1774 if (depth_write) {
1775 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001776 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1777 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001778 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001779 skip |= sync_state.LogError(
1780 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1781 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Prior access %s.",
1782 func_name, string_SyncHazard(hazard.hazard),
1783 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1784 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
1785 string_UsageTag(hazard.tag).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001786 }
1787 }
1788 if (stencil_write) {
1789 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001790 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1791 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001792 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001793 skip |= sync_state.LogError(
1794 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1795 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Prior access %s.",
1796 func_name, string_SyncHazard(hazard.hazard),
1797 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1798 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
1799 string_UsageTag(hazard.tag).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001800 }
locke-lunarg61870c22020-06-09 14:51:50 -06001801 }
1802 }
1803 return skip;
1804}
1805
locke-lunarg96dc9632020-06-10 17:22:18 -06001806void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1807 const ResourceUsageTag &tag) {
1808 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001809 if (!pPipe ||
1810 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001811 return;
1812 }
1813 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001814 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1815 VkExtent3D extent = CastTo3D(render_area.extent);
1816 VkOffset3D offset = CastTo3D(render_area.offset);
1817
John Zulauf1a224292020-06-30 14:52:13 -06001818 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001819 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001820 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1821 for (const auto location : list) {
1822 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1823 continue;
1824 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001825 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
1826 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001827 }
1828 }
locke-lunarg37047832020-06-12 13:44:45 -06001829
1830 // PHASE1 TODO: Add layout based read/vs. write selection.
1831 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1832 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1833 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001834 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001835 bool depth_write = false, stencil_write = false;
1836
1837 // PHASE1 TODO: These validation should be in core_checks.
1838 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1839 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1840 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1841 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1842 depth_write = true;
1843 }
1844 // PHASE1 TODO: It needs to check if stencil is writable.
1845 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1846 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1847 // PHASE1 TODO: These validation should be in core_checks.
1848 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1849 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1850 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1851 stencil_write = true;
1852 }
1853
1854 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1855 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001856 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1857 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001858 }
1859 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001860 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1861 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001862 }
locke-lunarg61870c22020-06-09 14:51:50 -06001863 }
1864}
1865
John Zulauf1507ee42020-05-18 11:33:09 -06001866bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1867 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001868 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001869 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001870 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1871 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001872 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1873 func_name);
1874
John Zulauf355e49b2020-04-24 15:11:15 -06001875 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06001876 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06001877 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1878 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1879 return skip;
1880}
1881bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
1882 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001883 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06001884 bool skip = false;
1885 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1886 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001887 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1888 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06001889 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001890 return skip;
1891}
1892
John Zulauf7635de32020-05-29 17:14:15 -06001893AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
1894 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
1895}
1896
1897bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
1898 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001899 bool skip = false;
1900
John Zulauf7635de32020-05-29 17:14:15 -06001901 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
1902 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
1903 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1904 // to apply and only copy then, if this proves a hot spot.
1905 std::unique_ptr<AccessContext> proxy_for_current;
1906
John Zulauf355e49b2020-04-24 15:11:15 -06001907 // Validate the "finalLayout" transitions to external
1908 // Get them from where there we're hidding in the extra entry.
1909 const auto &final_transitions = rp_state_->subpass_transitions.back();
1910 for (const auto &transition : final_transitions) {
1911 const auto &attach_view = attachment_views_[transition.attachment];
1912 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
1913 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06001914 auto *context = trackback.context;
1915
1916 if (transition.prev_pass == current_subpass_) {
1917 if (!proxy_for_current) {
1918 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
1919 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
1920 }
1921 context = proxy_for_current.get();
1922 }
1923
1924 auto hazard = context->DetectImageBarrierHazard(
John Zulauf355e49b2020-04-24 15:11:15 -06001925 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
1926 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
1927 if (hazard.hazard) {
1928 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
1929 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf1dae9192020-06-16 15:46:44 -06001930 " final image layout transition. Prior access %s.",
1931 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
1932 string_UsageTag(hazard.tag).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001933 }
1934 }
1935 return skip;
1936}
1937
1938void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
1939 // Add layout transitions...
1940 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
1941 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06001942 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06001943 for (const auto &transition : transitions) {
1944 const auto attachment_view = attachment_views_[transition.attachment];
1945 if (!attachment_view) continue;
1946 const auto image = attachment_view->image_state.get();
1947 if (!image) continue;
1948
1949 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -06001950 auto insert_pair = view_seen.insert(attachment_view);
1951 if (insert_pair.second) {
1952 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
1953 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
1954
1955 } else {
1956 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
1957 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
1958 auto barrier_to_transition = barrier->barrier;
1959 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
1960 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
1961 }
John Zulauf355e49b2020-04-24 15:11:15 -06001962 }
1963}
1964
John Zulauf1507ee42020-05-18 11:33:09 -06001965void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
1966 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
1967 auto &subpass_context = subpass_contexts_[current_subpass_];
1968 VkExtent3D extent = CastTo3D(render_area.extent);
1969 VkOffset3D offset = CastTo3D(render_area.offset);
1970
1971 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
1972 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
1973 if (attachment_views_[i] == nullptr) continue; // UNUSED
1974 const auto &view = *attachment_views_[i];
1975 const IMAGE_STATE *image = view.image_state.get();
1976 if (image == nullptr) continue;
1977
1978 const auto &ci = attachment_ci[i];
1979 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001980 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001981 const bool is_color = !(has_depth || has_stencil);
1982
1983 if (is_color) {
1984 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
1985 extent, tag);
1986 } else {
1987 auto update_range = view.normalized_subresource_range;
1988 if (has_depth) {
1989 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1990 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
1991 }
1992 if (has_stencil) {
1993 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1994 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
1995 tag);
1996 }
1997 }
1998 }
1999 }
2000}
2001
John Zulauf355e49b2020-04-24 15:11:15 -06002002void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002003 const AccessContext *external_context, VkQueueFlags queue_flags,
2004 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002005 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002006 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002007 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2008 // Add this for all subpasses here so that they exsist during next subpass validation
2009 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002010 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002011 }
2012 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2013
2014 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002015 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002016}
John Zulauf1507ee42020-05-18 11:33:09 -06002017
2018void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002019 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2020 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002021 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002022
John Zulauf355e49b2020-04-24 15:11:15 -06002023 current_subpass_++;
2024 assert(current_subpass_ < subpass_contexts_.size());
2025 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002026 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002027}
2028
John Zulauf1a224292020-06-30 14:52:13 -06002029void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2030 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002031 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002032 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002033 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002034
John Zulauf355e49b2020-04-24 15:11:15 -06002035 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002036 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002037
2038 // Add the "finalLayout" transitions to external
2039 // Get them from where there we're hidding in the extra entry.
2040 const auto &final_transitions = rp_state_->subpass_transitions.back();
2041 for (const auto &transition : final_transitions) {
2042 const auto &attachment = attachment_views_[transition.attachment];
2043 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulauf1a224292020-06-30 14:52:13 -06002044 assert(external_context == last_trackback.context);
2045 external_context->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
2046 attachment->normalized_subresource_range, true, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002047 }
2048}
2049
John Zulauf3d84f1b2020-03-09 13:33:25 -06002050SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2051 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2052 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2053 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2054 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2055 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2056 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2057}
2058
2059void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
2060 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
2061 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
2062}
2063
John Zulauf9cb530d2019-09-30 14:14:10 -06002064HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2065 HazardResult hazard;
2066 auto usage = FlagBit(usage_index);
2067 if (IsRead(usage)) {
John Zulaufc9201222020-05-13 15:13:03 -06002068 if (last_write && IsWriteHazard(usage)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002069 hazard.Set(READ_AFTER_WRITE, write_tag);
2070 }
2071 } else {
2072 // Assume write
2073 // TODO determine what to do with READ-WRITE usage states if any
2074 // Write-After-Write check -- if we have a previous write to test against
2075 if (last_write && IsWriteHazard(usage)) {
2076 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2077 } else {
John Zulauf69133422020-05-20 14:55:53 -06002078 // Look for casus belli for WAR
John Zulauf9cb530d2019-09-30 14:14:10 -06002079 const auto usage_stage = PipelineStageBit(usage_index);
John Zulaufd14743a2020-07-03 09:42:39 -06002080 // Note: kNoAttachmentRead is ~0, and thus the no attachment read hazard check doesn't need a separate path.
2081 if (IsReadHazard(usage_stage, input_attachment_barriers)) {
2082 hazard.Set(WRITE_AFTER_READ, input_attachment_tag);
2083 }
2084 if (!hazard.hazard) {
2085 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2086 if (IsReadHazard(usage_stage, last_reads[read_index])) {
2087 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
2088 break;
2089 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002090 }
2091 }
2092 }
2093 }
2094 return hazard;
2095}
2096
John Zulauf69133422020-05-20 14:55:53 -06002097HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2098 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2099 HazardResult hazard;
2100 const auto usage = FlagBit(usage_index);
2101 const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good.
2102 if (IsRead(usage)) {
2103 if (!write_is_ordered && IsWriteHazard(usage)) {
2104 hazard.Set(READ_AFTER_WRITE, write_tag);
2105 }
2106 } else {
2107 if (!write_is_ordered && IsWriteHazard(usage)) {
2108 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2109 } else {
2110 const auto usage_stage = PipelineStageBit(usage_index);
2111 const auto unordered_reads = last_read_stages & ~ordering.exec_scope;
2112 if (unordered_reads) {
2113 // Look for any WAR hazards outside the ordered set of stages
2114 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2115 if (last_reads[read_index].stage & unordered_reads) {
2116 if (IsReadHazard(usage_stage, last_reads[read_index])) {
2117 hazard.Set(WRITE_AFTER_READ, last_reads[read_index].tag);
2118 break;
2119 }
2120 }
2121 }
2122 }
John Zulaufd14743a2020-07-03 09:42:39 -06002123
2124 // This is special case code for the fragment shader input attachment, which unlike all other fragment shader operations
2125 // is framebuffer local, and thus subject to raster ordering guarantees
2126 if (!hazard.hazard && (input_attachment_barriers != kNoAttachmentRead)) {
2127 if (0 == (ordering.access_scope & SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT)) {
2128 // NOTE: Currently all ordering barriers include this bit, so this code may never be reached, but it's
2129 // here s.t. if we need to change the ordering barrier/rules we needn't change the code.
2130 hazard.Set(WRITE_AFTER_READ, input_attachment_tag);
2131 }
2132 }
John Zulauf69133422020-05-20 14:55:53 -06002133 }
2134 }
2135 return hazard;
2136}
2137
John Zulauf2f952d22020-02-10 11:34:51 -07002138// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002139HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002140 HazardResult hazard;
2141 auto usage = FlagBit(usage_index);
2142 if (IsRead(usage)) {
2143 if (last_write != 0) {
2144 hazard.Set(READ_RACING_WRITE, write_tag);
2145 }
2146 } else {
2147 if (last_write != 0) {
2148 hazard.Set(WRITE_RACING_WRITE, write_tag);
2149 } else if (last_read_count > 0) {
2150 hazard.Set(WRITE_RACING_READ, last_reads[0].tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002151 } else if (input_attachment_barriers != kNoAttachmentRead) {
2152 hazard.Set(WRITE_RACING_READ, input_attachment_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002153 }
2154 }
2155 return hazard;
2156}
2157
John Zulauf36bcf6a2020-02-03 15:12:52 -07002158HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2159 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002160 // Only supporting image layout transitions for now
2161 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2162 HazardResult hazard;
2163 if (last_write) {
2164 // If the previous write is *not* in the 1st access scope
2165 // *AND* the current barrier is not in the dependency chain
2166 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2167 // then the barrier access is unsafe (R/W after W)
John Zulauf36bcf6a2020-02-03 15:12:52 -07002168 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
John Zulauf0cb5be22020-01-23 12:18:22 -07002169 // TODO: Do we need a difference hazard name for this?
2170 hazard.Set(WRITE_AFTER_WRITE, write_tag);
2171 }
John Zulauf355e49b2020-04-24 15:11:15 -06002172 }
2173 if (!hazard.hazard) {
2174 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002175 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002176 const auto &read_access = last_reads[read_index];
2177 // If the read stage is not in the src sync sync
2178 // *AND* not execution chained with an existing sync barrier (that's the or)
2179 // then the barrier access is unsafe (R/W after R)
2180 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
2181 hazard.Set(WRITE_AFTER_READ, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002182 break;
2183 }
2184 }
2185 }
John Zulaufd14743a2020-07-03 09:42:39 -06002186 if (!hazard.hazard) {
2187 // Same logic as read acces above for the special case of input attachment read
2188 // Note: kNoReadAttachment is ~0 and thus cannot cause a hazard return.
2189 if ((src_exec_scope & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers)) == 0) {
2190 hazard.Set(WRITE_AFTER_READ, input_attachment_tag);
2191 }
2192 }
John Zulauf0cb5be22020-01-23 12:18:22 -07002193 return hazard;
2194}
2195
John Zulauf5f13a792020-03-10 07:31:21 -06002196// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2197// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2198// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2199void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2200 if (write_tag.IsBefore(other.write_tag)) {
2201 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
2202 *this = other;
2203 } else if (!other.write_tag.IsBefore(write_tag)) {
2204 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2205 // dependency chaining logic or any stage expansion)
2206 write_barriers |= other.write_barriers;
2207
John Zulaufd14743a2020-07-03 09:42:39 -06002208 // Merge the read states
2209 if (input_attachment_barriers == kNoAttachmentRead) {
2210 // this doesn't have an input attachment read, so we'll take other, unconditionally (even if it's kNoAttachmentRead)
2211 input_attachment_barriers = other.input_attachment_barriers;
2212 input_attachment_tag = other.input_attachment_tag;
2213 } else if (other.input_attachment_barriers != kNoAttachmentRead) {
2214 // Both states have an input attachment read, pick the newest tag and merge barriers.
2215 if (input_attachment_tag.IsBefore(other.input_attachment_tag)) {
2216 input_attachment_tag = other.input_attachment_tag;
2217 }
2218 input_attachment_barriers |= other.input_attachment_barriers;
2219 }
2220 // The else clause is that only this has an attachment read and no merge is needed
2221
John Zulauf5f13a792020-03-10 07:31:21 -06002222 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2223 auto &other_read = other.last_reads[other_read_index];
2224 if (last_read_stages & other_read.stage) {
2225 // Merge in the barriers for read stages that exist in *both* this and other
2226 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
2227 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
2228 auto &my_read = last_reads[my_read_index];
2229 if (other_read.stage == my_read.stage) {
2230 if (my_read.tag.IsBefore(other_read.tag)) {
2231 my_read.tag = other_read.tag;
2232 }
2233 my_read.barriers |= other_read.barriers;
2234 break;
2235 }
2236 }
2237 } else {
2238 // The other read stage doesn't exist in this, so add it.
2239 last_reads[last_read_count] = other_read;
2240 last_read_count++;
2241 last_read_stages |= other_read.stage;
2242 }
2243 }
2244 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
2245 // it.
2246}
2247
John Zulauf9cb530d2019-09-30 14:14:10 -06002248void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2249 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2250 const auto usage_bit = FlagBit(usage_index);
John Zulaufd14743a2020-07-03 09:42:39 -06002251 if (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) {
2252 // Input attachment requires special treatment for raster/load/store ordering guarantees
2253 input_attachment_barriers = 0;
2254 input_attachment_tag = tag;
2255 } else if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002256 // Mulitple outstanding reads may be of interest and do dependency chains independently
2257 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2258 const auto usage_stage = PipelineStageBit(usage_index);
2259 if (usage_stage & last_read_stages) {
2260 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2261 ReadState &access = last_reads[read_index];
2262 if (access.stage == usage_stage) {
2263 access.barriers = 0;
2264 access.tag = tag;
2265 break;
2266 }
2267 }
2268 } else {
2269 // We don't have this stage in the list yet...
2270 assert(last_read_count < last_reads.size());
2271 ReadState &access = last_reads[last_read_count++];
2272 access.stage = usage_stage;
2273 access.barriers = 0;
2274 access.tag = tag;
2275 last_read_stages |= usage_stage;
2276 }
2277 } else {
2278 // Assume write
2279 // TODO determine what to do with READ-WRITE operations if any
John Zulaufd14743a2020-07-03 09:42:39 -06002280 // Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
John Zulauf9cb530d2019-09-30 14:14:10 -06002281 // if the last_reads/last_write were unsafe, we've reported them,
2282 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
2283 last_read_count = 0;
2284 last_read_stages = 0;
2285
John Zulaufd14743a2020-07-03 09:42:39 -06002286 input_attachment_barriers = kNoAttachmentRead; // Denotes no outstanding input attachment read after the last write.
2287 // NOTE: we don't reset the tag, as the equality check ignores it when kNoAttachmentRead is set.
2288
John Zulauf9cb530d2019-09-30 14:14:10 -06002289 write_barriers = 0;
2290 write_dependency_chain = 0;
2291 write_tag = tag;
2292 last_write = usage_bit;
2293 }
2294}
John Zulauf5f13a792020-03-10 07:31:21 -06002295
John Zulauf9cb530d2019-09-30 14:14:10 -06002296void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
2297 // Execution Barriers only protect read operations
2298 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2299 ReadState &access = last_reads[read_index];
2300 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2301 if (srcStageMask & (access.stage | access.barriers)) {
2302 access.barriers |= dstStageMask;
2303 }
2304 }
John Zulaufd14743a2020-07-03 09:42:39 -06002305 if (srcStageMask & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers)) {
2306 input_attachment_barriers |= dstStageMask;
2307 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002308 if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask;
2309}
2310
John Zulauf36bcf6a2020-02-03 15:12:52 -07002311void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
2312 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002313 // Assuming we've applied the execution side of this barrier, we update just the write
2314 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07002315 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
2316 write_barriers |= dst_access_scope;
2317 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06002318 }
2319}
2320
John Zulaufd1f85d42020-04-15 12:23:15 -06002321void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002322 auto *access_context = GetAccessContextNoInsert(command_buffer);
2323 if (access_context) {
2324 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002325 }
2326}
2327
John Zulaufd1f85d42020-04-15 12:23:15 -06002328void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2329 auto access_found = cb_access_state.find(command_buffer);
2330 if (access_found != cb_access_state.end()) {
2331 access_found->second->Reset();
2332 cb_access_state.erase(access_found);
2333 }
2334}
2335
John Zulauf540266b2020-04-06 18:54:53 -06002336void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002337 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
2338 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002339 const VkMemoryBarrier *pMemoryBarriers) {
2340 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07002341 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002342 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002343 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002344}
2345
John Zulauf540266b2020-04-06 18:54:53 -06002346void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002347 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2348 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002349 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002350 for (uint32_t index = 0; index < barrier_count; index++) {
locke-lunarg3c038002020-04-30 23:08:08 -06002351 auto barrier = barriers[index];
John Zulauf9cb530d2019-09-30 14:14:10 -06002352 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2353 if (!buffer) continue;
locke-lunarg3c038002020-04-30 23:08:08 -06002354 barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002355 ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002356 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2357 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2358 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2359 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002360 }
2361}
2362
John Zulauf540266b2020-04-06 18:54:53 -06002363void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2364 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2365 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002366 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002367 for (uint32_t index = 0; index < barrier_count; index++) {
2368 const auto &barrier = barriers[index];
2369 const auto *image = Get<IMAGE_STATE>(barrier.image);
2370 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002371 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002372 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2373 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2374 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2375 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
2376 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002377 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002378}
2379
2380bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2381 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2382 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002383 const auto *cb_context = GetAccessContext(commandBuffer);
2384 assert(cb_context);
2385 if (!cb_context) return skip;
2386 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002387
John Zulauf3d84f1b2020-03-09 13:33:25 -06002388 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002389 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002390 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002391
2392 for (uint32_t region = 0; region < regionCount; region++) {
2393 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002394 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002395 ResourceAccessRange src_range = MakeRange(
2396 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002397 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002398 if (hazard.hazard) {
2399 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002400 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002401 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Prior access %s.",
2402 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
2403 string_UsageTag(hazard.tag).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002404 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002405 }
John Zulauf16adfc92020-04-08 10:28:33 -06002406 if (dst_buffer && !skip) {
locke-lunargff255f92020-05-13 18:53:52 -06002407 ResourceAccessRange dst_range = MakeRange(
2408 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf355e49b2020-04-24 15:11:15 -06002409 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002410 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002411 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002412 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Prior access %s.",
2413 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
2414 string_UsageTag(hazard.tag).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002415 }
2416 }
2417 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002418 }
2419 return skip;
2420}
2421
2422void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2423 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002424 auto *cb_context = GetAccessContext(commandBuffer);
2425 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002426 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002427 auto *context = cb_context->GetCurrentAccessContext();
2428
John Zulauf9cb530d2019-09-30 14:14:10 -06002429 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002430 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002431
2432 for (uint32_t region = 0; region < regionCount; region++) {
2433 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002434 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002435 ResourceAccessRange src_range = MakeRange(
2436 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002437 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002438 }
John Zulauf16adfc92020-04-08 10:28:33 -06002439 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002440 ResourceAccessRange dst_range = MakeRange(
2441 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002442 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002443 }
2444 }
2445}
2446
2447bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2448 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2449 const VkImageCopy *pRegions) const {
2450 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002451 const auto *cb_access_context = GetAccessContext(commandBuffer);
2452 assert(cb_access_context);
2453 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002454
John Zulauf3d84f1b2020-03-09 13:33:25 -06002455 const auto *context = cb_access_context->GetCurrentAccessContext();
2456 assert(context);
2457 if (!context) return skip;
2458
2459 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2460 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002461 for (uint32_t region = 0; region < regionCount; region++) {
2462 const auto &copy_region = pRegions[region];
2463 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002464 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002465 copy_region.srcOffset, copy_region.extent);
2466 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002467 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002468 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
2469 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
2470 string_UsageTag(hazard.tag).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002471 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002472 }
2473
2474 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002475 VkExtent3D dst_copy_extent =
2476 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002477 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002478 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002479 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002480 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002481 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
2482 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
2483 string_UsageTag(hazard.tag).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002484 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002485 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002486 }
2487 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002488
John Zulauf5c5e88d2019-12-26 11:22:02 -07002489 return skip;
2490}
2491
2492void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2493 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2494 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002495 auto *cb_access_context = GetAccessContext(commandBuffer);
2496 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002497 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002498 auto *context = cb_access_context->GetCurrentAccessContext();
2499 assert(context);
2500
John Zulauf5c5e88d2019-12-26 11:22:02 -07002501 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002502 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002503
2504 for (uint32_t region = 0; region < regionCount; region++) {
2505 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002506 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002507 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2508 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002509 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002510 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002511 VkExtent3D dst_copy_extent =
2512 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002513 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2514 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002515 }
2516 }
2517}
2518
2519bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2520 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2521 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2522 uint32_t bufferMemoryBarrierCount,
2523 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2524 uint32_t imageMemoryBarrierCount,
2525 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2526 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002527 const auto *cb_access_context = GetAccessContext(commandBuffer);
2528 assert(cb_access_context);
2529 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002530
John Zulauf3d84f1b2020-03-09 13:33:25 -06002531 const auto *context = cb_access_context->GetCurrentAccessContext();
2532 assert(context);
2533 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002534
John Zulauf3d84f1b2020-03-09 13:33:25 -06002535 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002536 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2537 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002538 // Validate Image Layout transitions
2539 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2540 const auto &barrier = pImageMemoryBarriers[index];
2541 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2542 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2543 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002544 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002545 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002546 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002547 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002548 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Prior access %s.",
2549 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
2550 string_UsageTag(hazard.tag).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002551 }
2552 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002553
2554 return skip;
2555}
2556
2557void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2558 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2559 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2560 uint32_t bufferMemoryBarrierCount,
2561 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2562 uint32_t imageMemoryBarrierCount,
2563 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002564 auto *cb_access_context = GetAccessContext(commandBuffer);
2565 assert(cb_access_context);
2566 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002567 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002568 auto access_context = cb_access_context->GetCurrentAccessContext();
2569 assert(access_context);
2570 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002571
John Zulauf3d84f1b2020-03-09 13:33:25 -06002572 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002573 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002574 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002575 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2576 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2577 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002578 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
2579 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002580 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06002581 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002582
2583 // 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 -06002584 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07002585 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06002586}
2587
2588void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
2589 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
2590 // The state tracker sets up the device state
2591 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
2592
John Zulauf5f13a792020-03-10 07:31:21 -06002593 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
2594 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06002595 // TODO: Find a good way to do this hooklessly.
2596 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
2597 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
2598 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
2599
John Zulaufd1f85d42020-04-15 12:23:15 -06002600 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2601 sync_device_state->ResetCommandBufferCallback(command_buffer);
2602 });
2603 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2604 sync_device_state->FreeCommandBufferCallback(command_buffer);
2605 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002606}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002607
John Zulauf355e49b2020-04-24 15:11:15 -06002608bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2609 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2610 bool skip = false;
2611 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2612 auto cb_context = GetAccessContext(commandBuffer);
2613
2614 if (rp_state && cb_context) {
2615 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2616 }
2617
2618 return skip;
2619}
2620
2621bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2622 VkSubpassContents contents) const {
2623 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2624 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2625 subpass_begin_info.contents = contents;
2626 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
2627 return skip;
2628}
2629
2630bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2631 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2632 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2633 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
2634 return skip;
2635}
2636
2637bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2638 const VkRenderPassBeginInfo *pRenderPassBegin,
2639 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2640 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2641 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
2642 return skip;
2643}
2644
John Zulauf3d84f1b2020-03-09 13:33:25 -06002645void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
2646 VkResult result) {
2647 // The state tracker sets up the command buffer state
2648 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
2649
2650 // Create/initialize the structure that trackers accesses at the command buffer scope.
2651 auto cb_access_context = GetAccessContext(commandBuffer);
2652 assert(cb_access_context);
2653 cb_access_context->Reset();
2654}
2655
2656void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06002657 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002658 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002659 if (cb_context) {
2660 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002661 }
2662}
2663
2664void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2665 VkSubpassContents contents) {
2666 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2667 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2668 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002669 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002670}
2671
2672void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2673 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2674 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002675 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002676}
2677
2678void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2679 const VkRenderPassBeginInfo *pRenderPassBegin,
2680 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2681 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002682 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
2683}
2684
2685bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2686 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
2687 bool skip = false;
2688
2689 auto cb_context = GetAccessContext(commandBuffer);
2690 assert(cb_context);
2691 auto cb_state = cb_context->GetCommandBufferState();
2692 if (!cb_state) return skip;
2693
2694 auto rp_state = cb_state->activeRenderPass;
2695 if (!rp_state) return skip;
2696
2697 skip |= cb_context->ValidateNextSubpass(func_name);
2698
2699 return skip;
2700}
2701
2702bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
2703 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
2704 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2705 subpass_begin_info.contents = contents;
2706 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
2707 return skip;
2708}
2709
2710bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2711 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2712 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2713 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
2714 return skip;
2715}
2716
2717bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2718 const VkSubpassEndInfo *pSubpassEndInfo) const {
2719 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2720 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
2721 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002722}
2723
2724void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06002725 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002726 auto cb_context = GetAccessContext(commandBuffer);
2727 assert(cb_context);
2728 auto cb_state = cb_context->GetCommandBufferState();
2729 if (!cb_state) return;
2730
2731 auto rp_state = cb_state->activeRenderPass;
2732 if (!rp_state) return;
2733
John Zulauf355e49b2020-04-24 15:11:15 -06002734 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002735}
2736
2737void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
2738 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
2739 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2740 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002741 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002742}
2743
2744void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2745 const VkSubpassEndInfo *pSubpassEndInfo) {
2746 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002747 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002748}
2749
2750void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2751 const VkSubpassEndInfo *pSubpassEndInfo) {
2752 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002753 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002754}
2755
John Zulauf355e49b2020-04-24 15:11:15 -06002756bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
2757 const char *func_name) const {
2758 bool skip = false;
2759
2760 auto cb_context = GetAccessContext(commandBuffer);
2761 assert(cb_context);
2762 auto cb_state = cb_context->GetCommandBufferState();
2763 if (!cb_state) return skip;
2764
2765 auto rp_state = cb_state->activeRenderPass;
2766 if (!rp_state) return skip;
2767
2768 skip |= cb_context->ValidateEndRenderpass(func_name);
2769 return skip;
2770}
2771
2772bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2773 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
2774 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
2775 return skip;
2776}
2777
2778bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
2779 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2780 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2781 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
2782 return skip;
2783}
2784
2785bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
2786 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2787 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2788 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
2789 return skip;
2790}
2791
2792void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
2793 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06002794 // Resolve the all subpass contexts to the command buffer contexts
2795 auto cb_context = GetAccessContext(commandBuffer);
2796 assert(cb_context);
2797 auto cb_state = cb_context->GetCommandBufferState();
2798 if (!cb_state) return;
2799
locke-lunargaecf2152020-05-12 17:15:41 -06002800 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06002801 if (!rp_state) return;
2802
John Zulauf355e49b2020-04-24 15:11:15 -06002803 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06002804}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002805
2806void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002807 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06002808 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002809}
2810
2811void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06002812 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06002813 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002814}
2815
2816void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06002817 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06002818 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002819}
locke-lunarga19c71d2020-03-02 18:17:04 -07002820
2821bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2822 VkImageLayout dstImageLayout, uint32_t regionCount,
2823 const VkBufferImageCopy *pRegions) const {
2824 bool skip = false;
2825 const auto *cb_access_context = GetAccessContext(commandBuffer);
2826 assert(cb_access_context);
2827 if (!cb_access_context) return skip;
2828
2829 const auto *context = cb_access_context->GetCurrentAccessContext();
2830 assert(context);
2831 if (!context) return skip;
2832
2833 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07002834 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2835
2836 for (uint32_t region = 0; region < regionCount; region++) {
2837 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002838 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002839 ResourceAccessRange src_range =
2840 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002841 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002842 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002843 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002844 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002845 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32 ". Prior access %s.",
2846 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
2847 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002848 }
2849 }
2850 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002851 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002852 copy_region.imageOffset, copy_region.imageExtent);
2853 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002854 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002855 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
2856 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
2857 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002858 }
2859 if (skip) break;
2860 }
2861 if (skip) break;
2862 }
2863 return skip;
2864}
2865
2866void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2867 VkImageLayout dstImageLayout, uint32_t regionCount,
2868 const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06002869 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07002870 auto *cb_access_context = GetAccessContext(commandBuffer);
2871 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002872 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002873 auto *context = cb_access_context->GetCurrentAccessContext();
2874 assert(context);
2875
2876 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06002877 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002878
2879 for (uint32_t region = 0; region < regionCount; region++) {
2880 const auto &copy_region = pRegions[region];
2881 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002882 ResourceAccessRange src_range =
2883 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002884 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002885 }
2886 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002887 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002888 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002889 }
2890 }
2891}
2892
2893bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
2894 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
2895 const VkBufferImageCopy *pRegions) const {
2896 bool skip = false;
2897 const auto *cb_access_context = GetAccessContext(commandBuffer);
2898 assert(cb_access_context);
2899 if (!cb_access_context) return skip;
2900
2901 const auto *context = cb_access_context->GetCurrentAccessContext();
2902 assert(context);
2903 if (!context) return skip;
2904
2905 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2906 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2907 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
2908 for (uint32_t region = 0; region < regionCount; region++) {
2909 const auto &copy_region = pRegions[region];
2910 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002911 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002912 copy_region.imageOffset, copy_region.imageExtent);
2913 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002914 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002915 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
2916 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
2917 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002918 }
2919 }
2920 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06002921 ResourceAccessRange dst_range =
2922 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002923 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002924 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002925 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002926 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Prior access %s.",
2927 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
2928 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002929 }
2930 }
2931 if (skip) break;
2932 }
2933 return skip;
2934}
2935
2936void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2937 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06002938 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07002939 auto *cb_access_context = GetAccessContext(commandBuffer);
2940 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002941 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07002942 auto *context = cb_access_context->GetCurrentAccessContext();
2943 assert(context);
2944
2945 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002946 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2947 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 -06002948 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07002949
2950 for (uint32_t region = 0; region < regionCount; region++) {
2951 const auto &copy_region = pRegions[region];
2952 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002953 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002954 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002955 }
2956 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002957 ResourceAccessRange dst_range =
2958 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002959 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002960 }
2961 }
2962}
2963
2964bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2965 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2966 const VkImageBlit *pRegions, VkFilter filter) const {
2967 bool skip = false;
2968 const auto *cb_access_context = GetAccessContext(commandBuffer);
2969 assert(cb_access_context);
2970 if (!cb_access_context) return skip;
2971
2972 const auto *context = cb_access_context->GetCurrentAccessContext();
2973 assert(context);
2974 if (!context) return skip;
2975
2976 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2977 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2978
2979 for (uint32_t region = 0; region < regionCount; region++) {
2980 const auto &blit_region = pRegions[region];
2981 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06002982 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
2983 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
2984 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
2985 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
2986 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
2987 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
2988 auto hazard =
2989 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07002990 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002991 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002992 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
2993 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
2994 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002995 }
2996 }
2997
2998 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06002999 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3000 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3001 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3002 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3003 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3004 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3005 auto hazard =
3006 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003007 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003008 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003009 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
3010 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
3011 string_UsageTag(hazard.tag).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003012 }
3013 if (skip) break;
3014 }
3015 }
3016
3017 return skip;
3018}
3019
3020void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3021 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3022 const VkImageBlit *pRegions, VkFilter filter) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003023 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3024 pRegions, filter);
locke-lunarga19c71d2020-03-02 18:17:04 -07003025 auto *cb_access_context = GetAccessContext(commandBuffer);
3026 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003027 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07003028 auto *context = cb_access_context->GetCurrentAccessContext();
3029 assert(context);
3030
3031 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003032 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003033
3034 for (uint32_t region = 0; region < regionCount; region++) {
3035 const auto &blit_region = pRegions[region];
3036 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003037 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3038 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3039 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3040 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3041 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3042 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3043 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003044 }
3045 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003046 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3047 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3048 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3049 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3050 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3051 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3052 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003053 }
3054 }
3055}
locke-lunarg36ba2592020-04-03 09:42:04 -06003056
locke-lunarg61870c22020-06-09 14:51:50 -06003057bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3058 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3059 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003060 bool skip = false;
3061 if (drawCount == 0) return skip;
3062
3063 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3064 VkDeviceSize size = struct_size;
3065 if (drawCount == 1 || stride == size) {
3066 if (drawCount > 1) size *= drawCount;
3067 ResourceAccessRange range = MakeRange(offset, size);
3068 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3069 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003070 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
3071 "%s: Hazard %s for indirect %s in %s. Prior access %s.", function, string_SyncHazard(hazard.hazard),
3072 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3073 string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003074 }
3075 } else {
3076 for (uint32_t i = 0; i < drawCount; ++i) {
3077 ResourceAccessRange range = MakeRange(offset + i * stride, size);
3078 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3079 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003080 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
3081 "%s: Hazard %s for indirect %s in %s. Prior access %s.", function,
3082 string_SyncHazard(hazard.hazard), report_data->FormatHandle(buffer).c_str(),
3083 report_data->FormatHandle(commandBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003084 break;
3085 }
3086 }
3087 }
3088 return skip;
3089}
3090
locke-lunarg61870c22020-06-09 14:51:50 -06003091void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3092 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3093 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003094 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3095 VkDeviceSize size = struct_size;
3096 if (drawCount == 1 || stride == size) {
3097 if (drawCount > 1) size *= drawCount;
3098 ResourceAccessRange range = MakeRange(offset, size);
3099 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3100 } else {
3101 for (uint32_t i = 0; i < drawCount; ++i) {
3102 ResourceAccessRange range = MakeRange(offset + i * stride, size);
3103 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3104 }
3105 }
3106}
3107
locke-lunarg61870c22020-06-09 14:51:50 -06003108bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3109 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003110 bool skip = false;
3111
3112 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3113 ResourceAccessRange range = MakeRange(offset, 4);
3114 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3115 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003116 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
3117 "%s: Hazard %s for countBuffer %s in %s. Prior access %s.", function, string_SyncHazard(hazard.hazard),
3118 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
3119 string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003120 }
3121 return skip;
3122}
3123
locke-lunarg61870c22020-06-09 14:51:50 -06003124void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003125 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3126 ResourceAccessRange range = MakeRange(offset, 4);
3127 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3128}
3129
locke-lunarg36ba2592020-04-03 09:42:04 -06003130bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003131 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003132 const auto *cb_access_context = GetAccessContext(commandBuffer);
3133 assert(cb_access_context);
3134 if (!cb_access_context) return skip;
3135
locke-lunarg61870c22020-06-09 14:51:50 -06003136 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003137 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003138}
3139
3140void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003141 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003142 auto *cb_access_context = GetAccessContext(commandBuffer);
3143 assert(cb_access_context);
3144 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003145
locke-lunarg61870c22020-06-09 14:51:50 -06003146 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003147}
locke-lunarge1a67022020-04-29 00:15:36 -06003148
3149bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003150 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003151 const auto *cb_access_context = GetAccessContext(commandBuffer);
3152 assert(cb_access_context);
3153 if (!cb_access_context) return skip;
3154
3155 const auto *context = cb_access_context->GetCurrentAccessContext();
3156 assert(context);
3157 if (!context) return skip;
3158
locke-lunarg61870c22020-06-09 14:51:50 -06003159 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3160 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3161 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003162 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003163}
3164
3165void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003166 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
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_DISPATCHINDIRECT);
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_COMPUTE, tag);
3174 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003175}
3176
3177bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3178 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003179 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003180 const auto *cb_access_context = GetAccessContext(commandBuffer);
3181 assert(cb_access_context);
3182 if (!cb_access_context) return skip;
3183
locke-lunarg61870c22020-06-09 14:51:50 -06003184 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3185 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3186 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003187 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003188}
3189
3190void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3191 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003192 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003193 auto *cb_access_context = GetAccessContext(commandBuffer);
3194 assert(cb_access_context);
3195 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003196
locke-lunarg61870c22020-06-09 14:51:50 -06003197 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3198 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3199 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003200}
3201
3202bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3203 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003204 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003205 const auto *cb_access_context = GetAccessContext(commandBuffer);
3206 assert(cb_access_context);
3207 if (!cb_access_context) return skip;
3208
locke-lunarg61870c22020-06-09 14:51:50 -06003209 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3210 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3211 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003212 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003213}
3214
3215void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3216 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003217 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003218 auto *cb_access_context = GetAccessContext(commandBuffer);
3219 assert(cb_access_context);
3220 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003221
locke-lunarg61870c22020-06-09 14:51:50 -06003222 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3223 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3224 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003225}
3226
3227bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3228 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003229 bool skip = false;
3230 if (drawCount == 0) return skip;
3231
locke-lunargff255f92020-05-13 18:53:52 -06003232 const auto *cb_access_context = GetAccessContext(commandBuffer);
3233 assert(cb_access_context);
3234 if (!cb_access_context) return skip;
3235
3236 const auto *context = cb_access_context->GetCurrentAccessContext();
3237 assert(context);
3238 if (!context) return skip;
3239
locke-lunarg61870c22020-06-09 14:51:50 -06003240 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3241 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3242 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3243 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003244
3245 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3246 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3247 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003248 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003249 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003250}
3251
3252void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3253 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003254 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003255 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003256 auto *cb_access_context = GetAccessContext(commandBuffer);
3257 assert(cb_access_context);
3258 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3259 auto *context = cb_access_context->GetCurrentAccessContext();
3260 assert(context);
3261
locke-lunarg61870c22020-06-09 14:51:50 -06003262 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3263 cb_access_context->RecordDrawSubpassAttachment(tag);
3264 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003265
3266 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3267 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3268 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003269 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003270}
3271
3272bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3273 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003274 bool skip = false;
3275 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003276 const auto *cb_access_context = GetAccessContext(commandBuffer);
3277 assert(cb_access_context);
3278 if (!cb_access_context) return skip;
3279
3280 const auto *context = cb_access_context->GetCurrentAccessContext();
3281 assert(context);
3282 if (!context) return skip;
3283
locke-lunarg61870c22020-06-09 14:51:50 -06003284 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3285 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3286 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3287 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003288
3289 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3290 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3291 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003292 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003293 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003294}
3295
3296void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3297 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003298 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003299 auto *cb_access_context = GetAccessContext(commandBuffer);
3300 assert(cb_access_context);
3301 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3302 auto *context = cb_access_context->GetCurrentAccessContext();
3303 assert(context);
3304
locke-lunarg61870c22020-06-09 14:51:50 -06003305 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3306 cb_access_context->RecordDrawSubpassAttachment(tag);
3307 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003308
3309 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3310 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3311 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003312 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003313}
3314
3315bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3316 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3317 uint32_t stride, const char *function) const {
3318 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003319 const auto *cb_access_context = GetAccessContext(commandBuffer);
3320 assert(cb_access_context);
3321 if (!cb_access_context) return skip;
3322
3323 const auto *context = cb_access_context->GetCurrentAccessContext();
3324 assert(context);
3325 if (!context) return skip;
3326
locke-lunarg61870c22020-06-09 14:51:50 -06003327 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3328 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3329 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3330 function);
3331 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003332
3333 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3334 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3335 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003336 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003337 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003338}
3339
3340bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3341 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3342 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003343 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3344 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003345}
3346
3347void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3348 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3349 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003350 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3351 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003352 auto *cb_access_context = GetAccessContext(commandBuffer);
3353 assert(cb_access_context);
3354 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3355 auto *context = cb_access_context->GetCurrentAccessContext();
3356 assert(context);
3357
locke-lunarg61870c22020-06-09 14:51:50 -06003358 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3359 cb_access_context->RecordDrawSubpassAttachment(tag);
3360 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3361 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003362
3363 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3364 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3365 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003366 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003367}
3368
3369bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3370 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3371 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003372 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3373 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003374}
3375
3376void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3377 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3378 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003379 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3380 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003381 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003382}
3383
3384bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3385 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3386 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003387 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3388 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003389}
3390
3391void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3392 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3393 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003394 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3395 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003396 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3397}
3398
3399bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3400 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3401 uint32_t stride, const char *function) const {
3402 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003403 const auto *cb_access_context = GetAccessContext(commandBuffer);
3404 assert(cb_access_context);
3405 if (!cb_access_context) return skip;
3406
3407 const auto *context = cb_access_context->GetCurrentAccessContext();
3408 assert(context);
3409 if (!context) return skip;
3410
locke-lunarg61870c22020-06-09 14:51:50 -06003411 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3412 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3413 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3414 stride, function);
3415 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003416
3417 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3418 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3419 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003420 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003421 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003422}
3423
3424bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3425 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3426 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003427 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3428 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003429}
3430
3431void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3432 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3433 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003434 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3435 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003436 auto *cb_access_context = GetAccessContext(commandBuffer);
3437 assert(cb_access_context);
3438 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3439 auto *context = cb_access_context->GetCurrentAccessContext();
3440 assert(context);
3441
locke-lunarg61870c22020-06-09 14:51:50 -06003442 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3443 cb_access_context->RecordDrawSubpassAttachment(tag);
3444 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3445 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003446
3447 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3448 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003449 // We will update the index and vertex buffer in SubmitQueue in the future.
3450 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003451}
3452
3453bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3454 VkDeviceSize offset, VkBuffer countBuffer,
3455 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3456 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003457 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3458 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003459}
3460
3461void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3462 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3463 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003464 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3465 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003466 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3467}
3468
3469bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3470 VkDeviceSize offset, VkBuffer countBuffer,
3471 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3472 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003473 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3474 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003475}
3476
3477void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3478 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3479 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003480 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3481 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003482 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3483}
3484
3485bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3486 const VkClearColorValue *pColor, uint32_t rangeCount,
3487 const VkImageSubresourceRange *pRanges) const {
3488 bool skip = false;
3489 const auto *cb_access_context = GetAccessContext(commandBuffer);
3490 assert(cb_access_context);
3491 if (!cb_access_context) return skip;
3492
3493 const auto *context = cb_access_context->GetCurrentAccessContext();
3494 assert(context);
3495 if (!context) return skip;
3496
3497 const auto *image_state = Get<IMAGE_STATE>(image);
3498
3499 for (uint32_t index = 0; index < rangeCount; index++) {
3500 const auto &range = pRanges[index];
3501 if (image_state) {
3502 auto hazard =
3503 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3504 if (hazard.hazard) {
3505 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003506 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Prior access %s.",
3507 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
3508 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003509 }
3510 }
3511 }
3512 return skip;
3513}
3514
3515void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3516 const VkClearColorValue *pColor, uint32_t rangeCount,
3517 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003518 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003519 auto *cb_access_context = GetAccessContext(commandBuffer);
3520 assert(cb_access_context);
3521 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
3522 auto *context = cb_access_context->GetCurrentAccessContext();
3523 assert(context);
3524
3525 const auto *image_state = Get<IMAGE_STATE>(image);
3526
3527 for (uint32_t index = 0; index < rangeCount; index++) {
3528 const auto &range = pRanges[index];
3529 if (image_state) {
3530 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3531 tag);
3532 }
3533 }
3534}
3535
3536bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3537 VkImageLayout imageLayout,
3538 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3539 const VkImageSubresourceRange *pRanges) const {
3540 bool skip = false;
3541 const auto *cb_access_context = GetAccessContext(commandBuffer);
3542 assert(cb_access_context);
3543 if (!cb_access_context) return skip;
3544
3545 const auto *context = cb_access_context->GetCurrentAccessContext();
3546 assert(context);
3547 if (!context) return skip;
3548
3549 const auto *image_state = Get<IMAGE_STATE>(image);
3550
3551 for (uint32_t index = 0; index < rangeCount; index++) {
3552 const auto &range = pRanges[index];
3553 if (image_state) {
3554 auto hazard =
3555 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3556 if (hazard.hazard) {
3557 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003558 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Prior access %s.",
3559 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
3560 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003561 }
3562 }
3563 }
3564 return skip;
3565}
3566
3567void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3568 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3569 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003570 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003571 auto *cb_access_context = GetAccessContext(commandBuffer);
3572 assert(cb_access_context);
3573 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
3574 auto *context = cb_access_context->GetCurrentAccessContext();
3575 assert(context);
3576
3577 const auto *image_state = Get<IMAGE_STATE>(image);
3578
3579 for (uint32_t index = 0; index < rangeCount; index++) {
3580 const auto &range = pRanges[index];
3581 if (image_state) {
3582 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3583 tag);
3584 }
3585 }
3586}
3587
3588bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
3589 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
3590 VkDeviceSize dstOffset, VkDeviceSize stride,
3591 VkQueryResultFlags flags) const {
3592 bool skip = false;
3593 const auto *cb_access_context = GetAccessContext(commandBuffer);
3594 assert(cb_access_context);
3595 if (!cb_access_context) return skip;
3596
3597 const auto *context = cb_access_context->GetCurrentAccessContext();
3598 assert(context);
3599 if (!context) return skip;
3600
3601 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3602
3603 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003604 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003605 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3606 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003607 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3608 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Prior access %s.",
3609 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
3610 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003611 }
3612 }
locke-lunargff255f92020-05-13 18:53:52 -06003613
3614 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003615 return skip;
3616}
3617
3618void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
3619 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3620 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003621 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
3622 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06003623 auto *cb_access_context = GetAccessContext(commandBuffer);
3624 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06003625 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06003626 auto *context = cb_access_context->GetCurrentAccessContext();
3627 assert(context);
3628
3629 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3630
3631 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003632 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003633 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3634 }
locke-lunargff255f92020-05-13 18:53:52 -06003635
3636 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003637}
3638
3639bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3640 VkDeviceSize size, uint32_t data) const {
3641 bool skip = false;
3642 const auto *cb_access_context = GetAccessContext(commandBuffer);
3643 assert(cb_access_context);
3644 if (!cb_access_context) return skip;
3645
3646 const auto *context = cb_access_context->GetCurrentAccessContext();
3647 assert(context);
3648 if (!context) return skip;
3649
3650 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3651
3652 if (dst_buffer) {
3653 ResourceAccessRange range = MakeRange(dstOffset, size);
3654 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3655 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003656 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3657 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Prior access %s.", string_SyncHazard(hazard.hazard),
3658 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003659 }
3660 }
3661 return skip;
3662}
3663
3664void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3665 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003666 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06003667 auto *cb_access_context = GetAccessContext(commandBuffer);
3668 assert(cb_access_context);
3669 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
3670 auto *context = cb_access_context->GetCurrentAccessContext();
3671 assert(context);
3672
3673 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3674
3675 if (dst_buffer) {
3676 ResourceAccessRange range = MakeRange(dstOffset, size);
3677 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3678 }
3679}
3680
3681bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3682 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3683 const VkImageResolve *pRegions) const {
3684 bool skip = false;
3685 const auto *cb_access_context = GetAccessContext(commandBuffer);
3686 assert(cb_access_context);
3687 if (!cb_access_context) return skip;
3688
3689 const auto *context = cb_access_context->GetCurrentAccessContext();
3690 assert(context);
3691 if (!context) return skip;
3692
3693 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3694 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3695
3696 for (uint32_t region = 0; region < regionCount; region++) {
3697 const auto &resolve_region = pRegions[region];
3698 if (src_image) {
3699 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3700 resolve_region.srcOffset, resolve_region.extent);
3701 if (hazard.hazard) {
3702 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003703 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
3704 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
3705 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003706 }
3707 }
3708
3709 if (dst_image) {
3710 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3711 resolve_region.dstOffset, resolve_region.extent);
3712 if (hazard.hazard) {
3713 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003714 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
3715 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
3716 string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003717 }
3718 if (skip) break;
3719 }
3720 }
3721
3722 return skip;
3723}
3724
3725void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3726 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3727 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003728 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3729 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06003730 auto *cb_access_context = GetAccessContext(commandBuffer);
3731 assert(cb_access_context);
3732 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
3733 auto *context = cb_access_context->GetCurrentAccessContext();
3734 assert(context);
3735
3736 auto *src_image = Get<IMAGE_STATE>(srcImage);
3737 auto *dst_image = Get<IMAGE_STATE>(dstImage);
3738
3739 for (uint32_t region = 0; region < regionCount; region++) {
3740 const auto &resolve_region = pRegions[region];
3741 if (src_image) {
3742 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3743 resolve_region.srcOffset, resolve_region.extent, tag);
3744 }
3745 if (dst_image) {
3746 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3747 resolve_region.dstOffset, resolve_region.extent, tag);
3748 }
3749 }
3750}
3751
3752bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3753 VkDeviceSize dataSize, const void *pData) const {
3754 bool skip = false;
3755 const auto *cb_access_context = GetAccessContext(commandBuffer);
3756 assert(cb_access_context);
3757 if (!cb_access_context) return skip;
3758
3759 const auto *context = cb_access_context->GetCurrentAccessContext();
3760 assert(context);
3761 if (!context) return skip;
3762
3763 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3764
3765 if (dst_buffer) {
3766 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3767 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3768 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003769 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3770 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Prior access %s.", string_SyncHazard(hazard.hazard),
3771 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard.tag).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003772 }
3773 }
3774 return skip;
3775}
3776
3777void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3778 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003779 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06003780 auto *cb_access_context = GetAccessContext(commandBuffer);
3781 assert(cb_access_context);
3782 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
3783 auto *context = cb_access_context->GetCurrentAccessContext();
3784 assert(context);
3785
3786 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3787
3788 if (dst_buffer) {
3789 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3790 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3791 }
3792}
locke-lunargff255f92020-05-13 18:53:52 -06003793
3794bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3795 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
3796 bool skip = false;
3797 const auto *cb_access_context = GetAccessContext(commandBuffer);
3798 assert(cb_access_context);
3799 if (!cb_access_context) return skip;
3800
3801 const auto *context = cb_access_context->GetCurrentAccessContext();
3802 assert(context);
3803 if (!context) return skip;
3804
3805 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3806
3807 if (dst_buffer) {
3808 ResourceAccessRange range = MakeRange(dstOffset, 4);
3809 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3810 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003811 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3812 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Prior access %s.",
3813 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
3814 string_UsageTag(hazard.tag).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003815 }
3816 }
3817 return skip;
3818}
3819
3820void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3821 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003822 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06003823 auto *cb_access_context = GetAccessContext(commandBuffer);
3824 assert(cb_access_context);
3825 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
3826 auto *context = cb_access_context->GetCurrentAccessContext();
3827 assert(context);
3828
3829 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3830
3831 if (dst_buffer) {
3832 ResourceAccessRange range = MakeRange(dstOffset, 4);
3833 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3834 }
3835}