blob: 457ca6f6c422132b378d2ba3c02fc3c67654c6f1 [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 Zulauf37ceaed2020-07-03 16:18:15 -060084static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
85 // Return the info for the first bit found
86 const SyncStageAccessInfoType *info = nullptr;
87 uint32_t index = 0;
88 while (flags) {
89 if (flags & 0x1) {
90 flags = 0;
91 info = &syncStageAccessInfoByStageAccessIndex[index];
92 } else {
93 flags = flags >> 1;
94 index++;
95 }
96 }
97 return info;
98}
99
100static std::string string_UsageTag(const HazardResult &hazard) {
101 const auto &tag = hazard.tag;
John Zulauf1dae9192020-06-16 15:46:44 -0600102 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600103 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
104 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
105 out << "(stage/access " << stage_access_name;
106 out << ", command " << CommandTypeString(tag.command);
107 out << ", seq #" << (tag.index & 0xFFFFFFFF) << ", reset #" << (tag.index >> 32) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600108 return out.str();
109}
110
John Zulaufd14743a2020-07-03 09:42:39 -0600111// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
112// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
113// also reflects this special case for read hazard detection (using access instead of exec scope)
John Zulaufb027cdb2020-05-21 14:25:22 -0600114static constexpr VkPipelineStageFlags kColorAttachmentExecScope = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
115static constexpr SyncStageAccessFlags kColorAttachmentAccessScope =
116 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
117 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
John Zulaufd14743a2020-07-03 09:42:39 -0600118 SyncStageAccessFlagBits::SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
119 SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600120static constexpr VkPipelineStageFlags kDepthStencilAttachmentExecScope =
121 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
122static constexpr SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
123 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
124 SyncStageAccessFlagBits::SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
125 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
John Zulaufd14743a2020-07-03 09:42:39 -0600126 SyncStageAccessFlagBits::SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
127 SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
John Zulaufb027cdb2020-05-21 14:25:22 -0600128
129static constexpr SyncOrderingBarrier kColorAttachmentRasterOrder = {kColorAttachmentExecScope, kColorAttachmentAccessScope};
130static constexpr SyncOrderingBarrier kDepthStencilAttachmentRasterOrder = {kDepthStencilAttachmentExecScope,
131 kDepthStencilAttachmentAccessScope};
132static constexpr SyncOrderingBarrier kAttachmentRasterOrder = {kDepthStencilAttachmentExecScope | kColorAttachmentExecScope,
133 kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope};
John Zulauf7635de32020-05-29 17:14:15 -0600134// 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 -0600135static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, CMD_NONE);
John Zulaufb027cdb2020-05-21 14:25:22 -0600136
locke-lunarg3c038002020-04-30 23:08:08 -0600137inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
138 if (size == VK_WHOLE_SIZE) {
139 return (whole_size - offset);
140 }
141 return size;
142}
143
John Zulauf16adfc92020-04-08 10:28:33 -0600144template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600145static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600146 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
147}
148
John Zulauf355e49b2020-04-24 15:11:15 -0600149static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600150
John Zulauf0cb5be22020-01-23 12:18:22 -0700151// Expand the pipeline stage without regard to whether the are valid w.r.t. queue or extension
152VkPipelineStageFlags ExpandPipelineStages(VkQueueFlags queue_flags, VkPipelineStageFlags stage_mask) {
153 VkPipelineStageFlags expanded = stage_mask;
154 if (VK_PIPELINE_STAGE_ALL_COMMANDS_BIT & stage_mask) {
155 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
156 for (const auto &all_commands : syncAllCommandStagesByQueueFlags) {
157 if (all_commands.first & queue_flags) {
158 expanded |= all_commands.second;
159 }
160 }
161 }
162 if (VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT & stage_mask) {
163 expanded = expanded & ~VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
164 expanded |= syncAllCommandStagesByQueueFlags.at(VK_QUEUE_GRAPHICS_BIT) & ~VK_PIPELINE_STAGE_HOST_BIT;
165 }
166 return expanded;
167}
168
John Zulauf36bcf6a2020-02-03 15:12:52 -0700169VkPipelineStageFlags RelatedPipelineStages(VkPipelineStageFlags stage_mask,
170 std::map<VkPipelineStageFlagBits, VkPipelineStageFlags> &map) {
171 VkPipelineStageFlags unscanned = stage_mask;
172 VkPipelineStageFlags related = 0;
173 for (const auto entry : map) {
174 const auto stage = entry.first;
175 if (stage & unscanned) {
176 related = related | entry.second;
177 unscanned = unscanned & ~stage;
178 if (!unscanned) break;
179 }
180 }
181 return related;
182}
183
184VkPipelineStageFlags WithEarlierPipelineStages(VkPipelineStageFlags stage_mask) {
185 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages);
186}
187
188VkPipelineStageFlags WithLaterPipelineStages(VkPipelineStageFlags stage_mask) {
189 return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages);
190}
191
John Zulauf5c5e88d2019-12-26 11:22:02 -0700192static const ResourceAccessRange full_range(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700193
locke-lunargff255f92020-05-13 18:53:52 -0600194void GetBufferRange(VkDeviceSize &range_start, VkDeviceSize &range_size, VkDeviceSize offset, VkDeviceSize buf_whole_size,
195 uint32_t first_index, uint32_t count, VkDeviceSize stride) {
196 range_start = offset + first_index * stride;
197 range_size = 0;
198 if (count == UINT32_MAX) {
199 range_size = buf_whole_size - range_start;
200 } else {
201 range_size = count * stride;
202 }
203}
204
locke-lunarg654e3692020-06-04 17:19:15 -0600205SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
206 VkShaderStageFlagBits stage_flag) {
207 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
208 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
209 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
210 }
211 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
212 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
213 assert(0);
214 }
215 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
216 return stage_access->second.uniform_read;
217 }
218
219 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
220 // Because if write hazard happens, read hazard might or might not happen.
221 // But if write hazard doesn't happen, read hazard is impossible to happen.
222 if (descriptor_data.is_writable) {
223 return stage_access->second.shader_write;
224 }
225 return stage_access->second.shader_read;
226}
227
locke-lunarg37047832020-06-12 13:44:45 -0600228bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
229 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
230 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
231 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
232 ? true
233 : false;
234}
235
236bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
237 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
238 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
239 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
240 ? true
241 : false;
242}
243
John Zulauf355e49b2020-04-24 15:11:15 -0600244// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
245const std::array<AccessContext::AddressType, AccessContext::kAddressTypeCount> AccessContext::kAddressTypes = {
246 AccessContext::AddressType::kLinearAddress, AccessContext::AddressType::kIdealizedAddress};
247
John Zulauf7635de32020-05-29 17:14:15 -0600248// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
249// Used by both validation and record operations
250//
251// The signature for Action() reflect the needs of both uses.
252template <typename Action>
253void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
254 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass) {
255 VkExtent3D extent = CastTo3D(render_area.extent);
256 VkOffset3D offset = CastTo3D(render_area.offset);
257 const auto &rp_ci = rp_state.createInfo;
258 const auto *attachment_ci = rp_ci.pAttachments;
259 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
260
261 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
262 const auto *color_attachments = subpass_ci.pColorAttachments;
263 const auto *color_resolve = subpass_ci.pResolveAttachments;
264 if (color_resolve && color_attachments) {
265 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
266 const auto &color_attach = color_attachments[i].attachment;
267 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
268 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
269 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
270 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kColorAttachmentRasterOrder, offset, extent, 0);
271 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
272 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kColorAttachmentRasterOrder, offset, extent, 0);
273 }
274 }
275 }
276
277 // Depth stencil resolve only if the extension is present
278 const auto ds_resolve = lvl_find_in_chain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
279 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
280 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
281 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
282 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
283 const auto src_ci = attachment_ci[src_at];
284 // The formats are required to match so we can pick either
285 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
286 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
287 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
288 VkImageAspectFlags aspect_mask = 0u;
289
290 // Figure out which aspects are actually touched during resolve operations
291 const char *aspect_string = nullptr;
292 if (resolve_depth && resolve_stencil) {
293 // Validate all aspects together
294 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
295 aspect_string = "depth/stencil";
296 } else if (resolve_depth) {
297 // Validate depth only
298 aspect_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
299 aspect_string = "depth";
300 } else if (resolve_stencil) {
301 // Validate all stencil only
302 aspect_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
303 aspect_string = "stencil";
304 }
305
306 if (aspect_mask) {
307 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at],
308 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, kDepthStencilAttachmentRasterOrder, offset, extent,
309 aspect_mask);
310 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at],
311 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, kAttachmentRasterOrder, offset, extent, aspect_mask);
312 }
313 }
314}
315
316// Action for validating resolve operations
317class ValidateResolveAction {
318 public:
319 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context, const SyncValidator &sync_state,
320 const char *func_name)
321 : render_pass_(render_pass),
322 subpass_(subpass),
323 context_(context),
324 sync_state_(sync_state),
325 func_name_(func_name),
326 skip_(false) {}
327 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
328 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
329 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
330 HazardResult hazard;
331 hazard = context_.DetectHazard(view, current_usage, ordering, offset, extent, aspect_mask);
332 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600333 skip_ |= sync_state_.LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
334 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
335 " to resolve attachment %" PRIu32 ". Prior access %s.",
336 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name, attachment_name,
John Zulauf37ceaed2020-07-03 16:18:15 -0600337 src_at, dst_at, string_UsageTag(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600338 }
339 }
340 // Providing a mechanism for the constructing caller to get the result of the validation
341 bool GetSkip() const { return skip_; }
342
343 private:
344 VkRenderPass render_pass_;
345 const uint32_t subpass_;
346 const AccessContext &context_;
347 const SyncValidator &sync_state_;
348 const char *func_name_;
349 bool skip_;
350};
351
352// Update action for resolve operations
353class UpdateStateResolveAction {
354 public:
355 UpdateStateResolveAction(AccessContext &context, const ResourceUsageTag &tag) : context_(context), tag_(tag) {}
356 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
357 const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const SyncOrderingBarrier &ordering,
358 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask) {
359 // Ignores validation only arguments...
360 context_.UpdateAccessState(view, current_usage, offset, extent, aspect_mask, tag_);
361 }
362
363 private:
364 AccessContext &context_;
365 const ResourceUsageTag &tag_;
366};
367
John Zulauf540266b2020-04-06 18:54:53 -0600368AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
369 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600370 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600371 Reset();
372 const auto &subpass_dep = dependencies[subpass];
373 prev_.reserve(subpass_dep.prev.size());
John Zulauf355e49b2020-04-24 15:11:15 -0600374 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600375 for (const auto &prev_dep : subpass_dep.prev) {
376 assert(prev_dep.dependency);
377 const auto dep = *prev_dep.dependency;
John Zulauf540266b2020-04-06 18:54:53 -0600378 prev_.emplace_back(const_cast<AccessContext *>(&contexts[dep.srcSubpass]), queue_flags, dep);
John Zulauf355e49b2020-04-24 15:11:15 -0600379 prev_by_subpass_[dep.srcSubpass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700380 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600381
382 async_.reserve(subpass_dep.async.size());
383 for (const auto async_subpass : subpass_dep.async) {
John Zulauf540266b2020-04-06 18:54:53 -0600384 async_.emplace_back(const_cast<AccessContext *>(&contexts[async_subpass]));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600385 }
John Zulaufe5da6e52020-03-18 15:32:18 -0600386 if (subpass_dep.barrier_from_external) {
387 src_external_ = TrackBack(external_context, queue_flags, *subpass_dep.barrier_from_external);
388 } else {
389 src_external_ = TrackBack();
390 }
391 if (subpass_dep.barrier_to_external) {
392 dst_external_ = TrackBack(this, queue_flags, *subpass_dep.barrier_to_external);
393 } else {
394 dst_external_ = TrackBack();
John Zulauf3d84f1b2020-03-09 13:33:25 -0600395 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700396}
397
John Zulauf5f13a792020-03-10 07:31:21 -0600398template <typename Detector>
John Zulauf16adfc92020-04-08 10:28:33 -0600399HazardResult AccessContext::DetectPreviousHazard(AddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600400 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600401 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600402 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600403
404 HazardResult hazard;
405 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
406 hazard = detector.Detect(prev);
407 }
408 return hazard;
409}
410
John Zulauf3d84f1b2020-03-09 13:33:25 -0600411// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
412// the DAG of the contexts (for example subpasses)
413template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600414HazardResult AccessContext::DetectHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range,
415 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600416 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600417
John Zulauf1a224292020-06-30 14:52:13 -0600418 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600419 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
420 // so we'll check these first
421 for (const auto &async_context : async_) {
422 hazard = async_context->DetectAsyncHazard(type, detector, range);
423 if (hazard.hazard) return hazard;
424 }
John Zulauf5f13a792020-03-10 07:31:21 -0600425 }
426
John Zulauf1a224292020-06-30 14:52:13 -0600427 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600428
John Zulauf69133422020-05-20 14:55:53 -0600429 const auto &accesses = GetAccessStateMap(type);
430 const auto from = accesses.lower_bound(range);
431 const auto to = accesses.upper_bound(range);
432 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600433
John Zulauf69133422020-05-20 14:55:53 -0600434 for (auto pos = from; pos != to; ++pos) {
435 // Cover any leading gap, or gap between entries
436 if (detect_prev) {
437 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
438 // Cover any leading gap, or gap between entries
439 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600440 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600441 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600442 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600443 if (hazard.hazard) return hazard;
444 }
John Zulauf69133422020-05-20 14:55:53 -0600445 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
446 gap.begin = pos->first.end;
447 }
448
449 hazard = detector.Detect(pos);
450 if (hazard.hazard) return hazard;
451 }
452
453 if (detect_prev) {
454 // Detect in the trailing empty as needed
455 gap.end = range.end;
456 if (gap.non_empty()) {
457 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600458 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600459 }
460
461 return hazard;
462}
463
464// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
465template <typename Detector>
John Zulauf355e49b2020-04-24 15:11:15 -0600466HazardResult AccessContext::DetectAsyncHazard(AddressType type, const Detector &detector, const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600467 auto &accesses = GetAccessStateMap(type);
468 const auto from = accesses.lower_bound(range);
469 const auto to = accesses.upper_bound(range);
470
John Zulauf3d84f1b2020-03-09 13:33:25 -0600471 HazardResult hazard;
John Zulauf16adfc92020-04-08 10:28:33 -0600472 for (auto pos = from; pos != to && !hazard.hazard; ++pos) {
473 hazard = detector.DetectAsync(pos);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600474 }
John Zulauf16adfc92020-04-08 10:28:33 -0600475
John Zulauf3d84f1b2020-03-09 13:33:25 -0600476 return hazard;
477}
478
John Zulauf355e49b2020-04-24 15:11:15 -0600479// Returns the last resolved entry
480static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
481 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
482 const SyncBarrier *barrier) {
483 auto at = entry;
484 for (auto pos = first; pos != last; ++pos) {
485 // Every member of the input iterator range must fit within the remaining portion of entry
486 assert(at->first.includes(pos->first));
487 assert(at != dest->end());
488 // Trim up at to the same size as the entry to resolve
489 at = sparse_container::split(at, *dest, pos->first);
490 auto access = pos->second;
491 if (barrier) {
492 access.ApplyBarrier(*barrier);
493 }
494 at->second.Resolve(access);
495 ++at; // Go to the remaining unused section of entry
496 }
497}
498
499void AccessContext::ResolveAccessRange(AddressType type, const ResourceAccessRange &range, const SyncBarrier *barrier,
500 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
501 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600502 if (!range.non_empty()) return;
503
John Zulauf355e49b2020-04-24 15:11:15 -0600504 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
505 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600506 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600507 if (current->pos_B->valid) {
508 const auto &src_pos = current->pos_B->lower_bound;
John Zulauf355e49b2020-04-24 15:11:15 -0600509 auto access = src_pos->second;
510 if (barrier) {
511 access.ApplyBarrier(*barrier);
512 }
John Zulauf16adfc92020-04-08 10:28:33 -0600513 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600514 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
515 trimmed->second.Resolve(access);
516 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600517 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600518 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600519 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600520 }
John Zulauf16adfc92020-04-08 10:28:33 -0600521 } else {
522 // we have to descend to fill this gap
523 if (recur_to_infill) {
John Zulauf355e49b2020-04-24 15:11:15 -0600524 if (current->pos_A->valid) {
525 // Dest is valid, so we need to accumulate along the DAG and then resolve... in an N-to-1 resolve operation
526 ResourceAccessRangeMap gap_map;
John Zulauf3bcab5e2020-06-19 14:42:32 -0600527 ResolvePreviousAccess(type, current_range, &gap_map, infill_state);
John Zulauf355e49b2020-04-24 15:11:15 -0600528 ResolveMapToEntry(resolve_map, current->pos_A->lower_bound, gap_map.begin(), gap_map.end(), barrier);
529 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600530 // There isn't anything in dest in current)range, so we can accumulate directly into it.
531 ResolvePreviousAccess(type, current_range, resolve_map, infill_state);
John Zulauf355e49b2020-04-24 15:11:15 -0600532 if (barrier) {
533 // Need to apply the barrier to the accesses we accumulated, noting that we haven't updated current
John Zulauf3bcab5e2020-06-19 14:42:32 -0600534 for (auto pos = resolve_map->lower_bound(current_range); pos != current->pos_A->lower_bound; ++pos) {
John Zulauf355e49b2020-04-24 15:11:15 -0600535 pos->second.ApplyBarrier(*barrier);
536 }
537 }
538 }
539 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
540 // iterator of the outer while.
541
542 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
543 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
544 // we stepped on the dest map
locke-lunarg88dbb542020-06-23 22:05:42 -0600545 const auto seek_to = current_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
546 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600547 current.seek(seek_to);
548 } else if (!current->pos_A->valid && infill_state) {
549 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
550 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
551 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600552 }
John Zulauf5f13a792020-03-10 07:31:21 -0600553 }
John Zulauf16adfc92020-04-08 10:28:33 -0600554 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600555 }
John Zulauf1a224292020-06-30 14:52:13 -0600556
557 // Infill if range goes passed both the current and resolve map prior contents
558 if (recur_to_infill && (current->range.end < range.end)) {
559 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
560 ResourceAccessRangeMap gap_map;
561 const auto the_end = resolve_map->end();
562 ResolvePreviousAccess(type, trailing_fill_range, &gap_map, infill_state);
563 for (auto &access : gap_map) {
564 access.second.ApplyBarrier(*barrier);
565 resolve_map->insert(the_end, access);
566 }
567 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600568}
569
John Zulauf355e49b2020-04-24 15:11:15 -0600570void AccessContext::ResolvePreviousAccess(AddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
571 const ResourceAccessState *infill_state) const {
John Zulaufe5da6e52020-03-18 15:32:18 -0600572 if ((prev_.size() == 0) && (src_external_.context == nullptr)) {
John Zulauf5f13a792020-03-10 07:31:21 -0600573 if (range.non_empty() && infill_state) {
574 descent_map->insert(std::make_pair(range, *infill_state));
575 }
576 } else {
577 // Look for something to fill the gap further along.
578 for (const auto &prev_dep : prev_) {
John Zulauf355e49b2020-04-24 15:11:15 -0600579 prev_dep.context->ResolveAccessRange(type, range, &prev_dep.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600580 }
581
John Zulaufe5da6e52020-03-18 15:32:18 -0600582 if (src_external_.context) {
John Zulauf355e49b2020-04-24 15:11:15 -0600583 src_external_.context->ResolveAccessRange(type, range, &src_external_.barrier, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600584 }
585 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600586}
587
John Zulauf16adfc92020-04-08 10:28:33 -0600588AccessContext::AddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
locke-lunarg3f6978b2020-04-16 16:51:35 -0600589 return (image.fragment_encoder->IsLinearImage()) ? AddressType::kLinearAddress : AddressType::kIdealizedAddress;
John Zulauf16adfc92020-04-08 10:28:33 -0600590}
591
592VkDeviceSize AccessContext::ResourceBaseAddress(const BINDABLE &bindable) {
593 return bindable.binding.offset + bindable.binding.mem_state->fake_base_address;
594}
595
John Zulauf355e49b2020-04-24 15:11:15 -0600596static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.binding.mem_state; }
John Zulauf16adfc92020-04-08 10:28:33 -0600597
John Zulauf1507ee42020-05-18 11:33:09 -0600598static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
599 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
600 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE;
601 return stage_access;
602}
603static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
604 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
605 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE;
606 return stage_access;
607}
608
John Zulauf7635de32020-05-29 17:14:15 -0600609// Caller must manage returned pointer
610static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
611 uint32_t subpass, const VkRect2D &render_area,
612 std::vector<const IMAGE_VIEW_STATE *> attachment_views) {
613 auto *proxy = new AccessContext(context);
614 proxy->UpdateAttachmentResolveAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulaufaff20662020-06-01 14:07:58 -0600615 proxy->UpdateAttachmentStoreAccess(rp_state, render_area, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600616 return proxy;
617}
618
John Zulauf540266b2020-04-06 18:54:53 -0600619void AccessContext::ResolvePreviousAccess(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
John Zulauf355e49b2020-04-24 15:11:15 -0600620 AddressType address_type, ResourceAccessRangeMap *descent_map,
621 const ResourceAccessState *infill_state) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600622 if (!SimpleBinding(image_state)) return;
623
John Zulauf62f10592020-04-03 12:20:02 -0600624 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
locke-lunargae26eac2020-04-16 15:29:05 -0600625 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -0600626 image_state.createInfo.extent);
John Zulauf16adfc92020-04-08 10:28:33 -0600627 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf62f10592020-04-03 12:20:02 -0600628 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -0600629 ResolvePreviousAccess(address_type, (*range_gen + base_address), descent_map, infill_state);
John Zulauf62f10592020-04-03 12:20:02 -0600630 }
631}
632
John Zulauf7635de32020-05-29 17:14:15 -0600633// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf1507ee42020-05-18 11:33:09 -0600634bool AccessContext::ValidateLayoutTransitions(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600635 const VkRect2D &render_area, uint32_t subpass,
636 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
637 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600638 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600639 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
640 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
641 // those affects have not been recorded yet.
642 //
643 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
644 // to apply and only copy then, if this proves a hot spot.
645 std::unique_ptr<AccessContext> proxy_for_prev;
646 TrackBack proxy_track_back;
647
John Zulauf355e49b2020-04-24 15:11:15 -0600648 const auto &transitions = rp_state.subpass_transitions[subpass];
649 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600650 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
651
652 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
653 if (prev_needs_proxy) {
654 if (!proxy_for_prev) {
655 proxy_for_prev.reset(CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass,
656 render_area, attachment_views));
657 proxy_track_back = *track_back;
658 proxy_track_back.context = proxy_for_prev.get();
659 }
660 track_back = &proxy_track_back;
661 }
662 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600663 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -0600664 skip |= sync_state.LogError(
665 rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
666 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32 " image layout transition. Prior access %s.",
John Zulauf37ceaed2020-07-03 16:18:15 -0600667 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment, string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600668 }
669 }
670 return skip;
671}
672
John Zulauf1507ee42020-05-18 11:33:09 -0600673bool AccessContext::ValidateLoadOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600674 const VkRect2D &render_area, uint32_t subpass,
675 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
676 const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -0600677 bool skip = false;
678 const auto *attachment_ci = rp_state.createInfo.pAttachments;
679 VkExtent3D extent = CastTo3D(render_area.extent);
680 VkOffset3D offset = CastTo3D(render_area.offset);
681 const auto external_access_scope = src_external_.barrier.dst_access_scope;
John Zulauf1507ee42020-05-18 11:33:09 -0600682
683 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
684 if (subpass == rp_state.attachment_first_subpass[i]) {
685 if (attachment_views[i] == nullptr) continue;
686 const IMAGE_VIEW_STATE &view = *attachment_views[i];
687 const IMAGE_STATE *image = view.image_state.get();
688 if (image == nullptr) continue;
689 const auto &ci = attachment_ci[i];
690 const bool is_transition = rp_state.attachment_first_is_transition[i];
691
692 // Need check in the following way
693 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
694 // vs. transition
695 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
696 // for each aspect loaded.
697
698 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -0600699 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -0600700 const bool is_color = !(has_depth || has_stencil);
701
702 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
703 const SyncStageAccessFlags load_mask = (has_depth || is_color) ? SyncStageAccess::Flags(load_index) : 0U;
704 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
705 const SyncStageAccessFlags stencil_mask = has_stencil ? SyncStageAccess::Flags(stencil_load_index) : 0U;
706
John Zulaufaff20662020-06-01 14:07:58 -0600707 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -0600708 const char *aspect = nullptr;
709 if (is_transition) {
710 // For transition w
711 SyncHazard transition_hazard = SyncHazard::NONE;
712 bool checked_stencil = false;
713 if (load_mask) {
714 if ((load_mask & external_access_scope) != load_mask) {
715 transition_hazard =
716 SyncStageAccess::HasWrite(load_mask) ? SyncHazard::WRITE_AFTER_WRITE : SyncHazard::READ_AFTER_WRITE;
717 aspect = is_color ? "color" : "depth";
718 }
719 if (!transition_hazard && stencil_mask) {
720 if ((stencil_mask & external_access_scope) != stencil_mask) {
721 transition_hazard = SyncStageAccess::HasWrite(stencil_mask) ? SyncHazard::WRITE_AFTER_WRITE
722 : SyncHazard::READ_AFTER_WRITE;
723 aspect = "stencil";
724 checked_stencil = true;
725 }
726 }
727 }
728 if (transition_hazard) {
729 // Hazard vs. ILT
730 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
731 skip |=
732 sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
733 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
734 " aspect %s during load with loadOp %s.",
735 func_name, string_SyncHazard(transition_hazard), subpass, i, aspect, load_op_string);
736 }
737 } else {
738 auto hazard_range = view.normalized_subresource_range;
739 bool checked_stencil = false;
740 if (is_color) {
741 hazard = DetectHazard(*image, load_index, view.normalized_subresource_range, offset, extent);
742 aspect = "color";
743 } else {
744 if (has_depth) {
745 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
746 hazard = DetectHazard(*image, load_index, hazard_range, offset, extent);
747 aspect = "depth";
748 }
749 if (!hazard.hazard && has_stencil) {
750 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
751 hazard = DetectHazard(*image, stencil_load_index, hazard_range, offset, extent);
752 aspect = "stencil";
753 checked_stencil = true;
754 }
755 }
756
757 if (hazard.hazard) {
758 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
759 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
760 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
locke-lunarg88dbb542020-06-23 22:05:42 -0600761 " aspect %s during load with loadOp %s. Prior access %s.",
762 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600763 string_UsageTag(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -0600764 }
765 }
766 }
767 }
768 return skip;
769}
770
John Zulaufaff20662020-06-01 14:07:58 -0600771// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
772// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
773// store is part of the same Next/End operation.
774// The latter is handled in layout transistion validation directly
775bool AccessContext::ValidateStoreOperation(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
776 const VkRect2D &render_area, uint32_t subpass,
777 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
778 const char *func_name) const {
779 bool skip = false;
780 const auto *attachment_ci = rp_state.createInfo.pAttachments;
781 VkExtent3D extent = CastTo3D(render_area.extent);
782 VkOffset3D offset = CastTo3D(render_area.offset);
783
784 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
785 if (subpass == rp_state.attachment_last_subpass[i]) {
786 if (attachment_views[i] == nullptr) continue;
787 const IMAGE_VIEW_STATE &view = *attachment_views[i];
788 const IMAGE_STATE *image = view.image_state.get();
789 if (image == nullptr) continue;
790 const auto &ci = attachment_ci[i];
791
792 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
793 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
794 // sake, we treat DONT_CARE as writing.
795 const bool has_depth = FormatHasDepth(ci.format);
796 const bool has_stencil = FormatHasStencil(ci.format);
797 const bool is_color = !(has_depth || has_stencil);
798 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
799 if (!has_stencil && !store_op_stores) continue;
800
801 HazardResult hazard;
802 const char *aspect = nullptr;
803 bool checked_stencil = false;
804 if (is_color) {
805 hazard = DetectHazard(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
806 view.normalized_subresource_range, kAttachmentRasterOrder, offset, extent);
807 aspect = "color";
808 } else {
809 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
810 auto hazard_range = view.normalized_subresource_range;
811 if (has_depth && store_op_stores) {
812 hazard_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
813 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
814 kAttachmentRasterOrder, offset, extent);
815 aspect = "depth";
816 }
817 if (!hazard.hazard && has_stencil && stencil_op_stores) {
818 hazard_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
819 hazard = DetectHazard(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, hazard_range,
820 kAttachmentRasterOrder, offset, extent);
821 aspect = "stencil";
822 checked_stencil = true;
823 }
824 }
825
826 if (hazard.hazard) {
827 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
828 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf1dae9192020-06-16 15:46:44 -0600829 skip |= sync_state.LogError(rp_state.renderPass, string_SyncHazardVUID(hazard.hazard),
830 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
831 " %s aspect during store with %s %s. Prior access %s",
832 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, op_type_string,
John Zulauf37ceaed2020-07-03 16:18:15 -0600833 store_op_string, string_UsageTag(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -0600834 }
835 }
836 }
837 return skip;
838}
839
John Zulaufb027cdb2020-05-21 14:25:22 -0600840bool AccessContext::ValidateResolveOperations(const SyncValidator &sync_state, const RENDER_PASS_STATE &rp_state,
841 const VkRect2D &render_area,
842 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name,
843 uint32_t subpass) const {
John Zulauf7635de32020-05-29 17:14:15 -0600844 ValidateResolveAction validate_action(rp_state.renderPass, subpass, *this, sync_state, func_name);
845 ResolveOperation(validate_action, rp_state, render_area, attachment_views, subpass);
846 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -0600847}
848
John Zulauf3d84f1b2020-03-09 13:33:25 -0600849class HazardDetector {
850 SyncStageAccessIndex usage_index_;
851
852 public:
John Zulauf5f13a792020-03-10 07:31:21 -0600853 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600854 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
855 return pos->second.DetectAsyncHazard(usage_index_);
856 }
857 HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
858};
859
John Zulauf69133422020-05-20 14:55:53 -0600860class HazardDetectorWithOrdering {
861 const SyncStageAccessIndex usage_index_;
862 const SyncOrderingBarrier &ordering_;
863
864 public:
865 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
866 return pos->second.DetectHazard(usage_index_, ordering_);
867 }
868 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
869 return pos->second.DetectAsyncHazard(usage_index_);
870 }
871 HazardDetectorWithOrdering(SyncStageAccessIndex usage, const SyncOrderingBarrier &ordering)
872 : usage_index_(usage), ordering_(ordering) {}
873};
874
John Zulauf16adfc92020-04-08 10:28:33 -0600875HazardResult AccessContext::DetectHazard(AddressType type, SyncStageAccessIndex usage_index,
John Zulauf540266b2020-04-06 18:54:53 -0600876 const ResourceAccessRange &range) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600877 HazardDetector detector(usage_index);
John Zulauf355e49b2020-04-24 15:11:15 -0600878 return DetectHazard(type, detector, range, DetectOptions::kDetectAll);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600879}
880
John Zulauf16adfc92020-04-08 10:28:33 -0600881HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -0600882 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600883 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -0600884 return DetectHazard(AddressType::kLinearAddress, usage_index, range + ResourceBaseAddress(buffer));
John Zulaufe5da6e52020-03-18 15:32:18 -0600885}
886
John Zulauf69133422020-05-20 14:55:53 -0600887template <typename Detector>
888HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
889 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
890 const VkExtent3D &extent, DetectOptions options) const {
891 if (!SimpleBinding(image)) return HazardResult();
892 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
893 const auto address_type = ImageAddressType(image);
894 const auto base_address = ResourceBaseAddress(image);
895 for (; range_gen->non_empty(); ++range_gen) {
896 HazardResult hazard = DetectHazard(address_type, detector, (*range_gen + base_address), options);
897 if (hazard.hazard) return hazard;
898 }
899 return HazardResult();
900}
901
John Zulauf540266b2020-04-06 18:54:53 -0600902HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
903 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
904 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700905 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
906 subresource.layerCount};
John Zulauf1507ee42020-05-18 11:33:09 -0600907 return DetectHazard(image, current_usage, subresource_range, offset, extent);
908}
909
910HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
911 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
912 const VkExtent3D &extent) const {
John Zulauf69133422020-05-20 14:55:53 -0600913 HazardDetector detector(current_usage);
914 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
915}
916
917HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
918 const VkImageSubresourceRange &subresource_range, const SyncOrderingBarrier &ordering,
919 const VkOffset3D &offset, const VkExtent3D &extent) const {
920 HazardDetectorWithOrdering detector(current_usage, ordering);
921 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -0600922}
923
John Zulaufb027cdb2020-05-21 14:25:22 -0600924// Some common code for looking at attachments, if there's anything wrong, we return no hazard, core validation
925// should have reported the issue regarding an invalid attachment entry
926HazardResult AccessContext::DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage,
927 const SyncOrderingBarrier &ordering, const VkOffset3D &offset, const VkExtent3D &extent,
928 VkImageAspectFlags aspect_mask) const {
929 if (view != nullptr) {
930 const IMAGE_STATE *image = view->image_state.get();
931 if (image != nullptr) {
932 auto *detect_range = &view->normalized_subresource_range;
933 VkImageSubresourceRange masked_range;
934 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
935 masked_range = view->normalized_subresource_range;
936 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
937 detect_range = &masked_range;
938 }
939
940 // NOTE: The range encoding code is not robust to invalid ranges, so we protect it from our change
941 if (detect_range->aspectMask) {
942 return DetectHazard(*image, current_usage, *detect_range, ordering, offset, extent);
943 }
944 }
945 }
946 return HazardResult();
947}
John Zulauf3d84f1b2020-03-09 13:33:25 -0600948class BarrierHazardDetector {
949 public:
950 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
951 SyncStageAccessFlags src_access_scope)
952 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
953
John Zulauf5f13a792020-03-10 07:31:21 -0600954 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
955 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -0700956 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600957 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos) const {
958 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
959 return pos->second.DetectAsyncHazard(usage_index_);
960 }
961
962 private:
963 SyncStageAccessIndex usage_index_;
964 VkPipelineStageFlags src_exec_scope_;
965 SyncStageAccessFlags src_access_scope_;
966};
967
John Zulauf16adfc92020-04-08 10:28:33 -0600968HazardResult AccessContext::DetectBarrierHazard(AddressType type, SyncStageAccessIndex current_usage,
John Zulauf540266b2020-04-06 18:54:53 -0600969 VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600970 const ResourceAccessRange &range, DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600971 BarrierHazardDetector detector(current_usage, src_exec_scope, src_access_scope);
John Zulauf69133422020-05-20 14:55:53 -0600972 return DetectHazard(type, detector, range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700973}
974
John Zulauf16adfc92020-04-08 10:28:33 -0600975HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
John Zulauf355e49b2020-04-24 15:11:15 -0600976 SyncStageAccessFlags src_access_scope,
977 const VkImageSubresourceRange &subresource_range,
978 DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -0600979 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
980 VkOffset3D zero_offset = {0, 0, 0};
981 return DetectHazard(detector, image, subresource_range, zero_offset, image.createInfo.extent, options);
John Zulauf0cb5be22020-01-23 12:18:22 -0700982}
983
John Zulauf355e49b2020-04-24 15:11:15 -0600984HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
985 SyncStageAccessFlags src_stage_accesses,
986 const VkImageMemoryBarrier &barrier) const {
987 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
988 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
989 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
990}
991
John Zulauf9cb530d2019-09-30 14:14:10 -0600992template <typename Flags, typename Map>
993SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
994 SyncStageAccessFlags scope = 0;
995 for (const auto &bit_scope : map) {
996 if (flag_mask < bit_scope.first) break;
997
998 if (flag_mask & bit_scope.first) {
999 scope |= bit_scope.second;
1000 }
1001 }
1002 return scope;
1003}
1004
1005SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags stages) {
1006 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1007}
1008
1009SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags accesses) {
1010 return AccessScopeImpl(accesses, syncStageAccessMaskByAccessBit);
1011}
1012
1013// Getting from stage mask and access mask to stage/acess masks is something we need to be good at...
1014SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags stages, VkAccessFlags accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001015 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1016 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1017 // 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 -06001018 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1019}
1020
1021template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001022void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001023 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1024 // that do incrementalupdates
John Zulauf9cb530d2019-09-30 14:14:10 -06001025 auto pos = accesses->lower_bound(range);
1026 if (pos == accesses->end() || !pos->first.intersects(range)) {
1027 // The range is empty, fill it with a default value.
1028 pos = action.Infill(accesses, pos, range);
1029 } else if (range.begin < pos->first.begin) {
1030 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001031 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001032 } else if (pos->first.begin < range.begin) {
1033 // Trim the beginning if needed
1034 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1035 ++pos;
1036 }
1037
1038 const auto the_end = accesses->end();
1039 while ((pos != the_end) && pos->first.intersects(range)) {
1040 if (pos->first.end > range.end) {
1041 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1042 }
1043
1044 pos = action(accesses, pos);
1045 if (pos == the_end) break;
1046
1047 auto next = pos;
1048 ++next;
1049 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1050 // Need to infill if next is disjoint
1051 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001052 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001053 next = action.Infill(accesses, next, new_range);
1054 }
1055 pos = next;
1056 }
1057}
1058
1059struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001060 using Iterator = ResourceAccessRangeMap::iterator;
1061 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001062 // this is only called on gaps, and never returns a gap.
1063 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001064 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001065 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001066 }
John Zulauf5f13a792020-03-10 07:31:21 -06001067
John Zulauf5c5e88d2019-12-26 11:22:02 -07001068 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001069 auto &access_state = pos->second;
1070 access_state.Update(usage, tag);
1071 return pos;
1072 }
1073
John Zulauf16adfc92020-04-08 10:28:33 -06001074 UpdateMemoryAccessStateFunctor(AccessContext::AddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf540266b2020-04-06 18:54:53 -06001075 const ResourceUsageTag &tag_)
John Zulauf16adfc92020-04-08 10:28:33 -06001076 : type(type_), context(context_), usage(usage_), tag(tag_) {}
1077 const AccessContext::AddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001078 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001079 const SyncStageAccessIndex usage;
John Zulauf9cb530d2019-09-30 14:14:10 -06001080 const ResourceUsageTag &tag;
1081};
1082
1083struct ApplyMemoryAccessBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001084 using Iterator = ResourceAccessRangeMap::iterator;
1085 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001086
John Zulauf5c5e88d2019-12-26 11:22:02 -07001087 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001088 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001089 access_state.ApplyMemoryAccessBarrier(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001090 return pos;
1091 }
1092
John Zulauf36bcf6a2020-02-03 15:12:52 -07001093 ApplyMemoryAccessBarrierFunctor(VkPipelineStageFlags src_exec_scope_, SyncStageAccessFlags src_access_scope_,
1094 VkPipelineStageFlags dst_exec_scope_, SyncStageAccessFlags dst_access_scope_)
1095 : src_exec_scope(src_exec_scope_),
1096 src_access_scope(src_access_scope_),
1097 dst_exec_scope(dst_exec_scope_),
1098 dst_access_scope(dst_access_scope_) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001099
John Zulauf36bcf6a2020-02-03 15:12:52 -07001100 VkPipelineStageFlags src_exec_scope;
1101 SyncStageAccessFlags src_access_scope;
1102 VkPipelineStageFlags dst_exec_scope;
1103 SyncStageAccessFlags dst_access_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001104};
1105
1106struct ApplyGlobalBarrierFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001107 using Iterator = ResourceAccessRangeMap::iterator;
1108 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001109
John Zulauf5c5e88d2019-12-26 11:22:02 -07001110 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001111 auto &access_state = pos->second;
John Zulauf36bcf6a2020-02-03 15:12:52 -07001112 access_state.ApplyExecutionBarrier(src_exec_scope, dst_exec_scope);
John Zulauf9cb530d2019-09-30 14:14:10 -06001113
1114 for (const auto &functor : barrier_functor) {
1115 functor(accesses, pos);
1116 }
1117 return pos;
1118 }
1119
John Zulauf36bcf6a2020-02-03 15:12:52 -07001120 ApplyGlobalBarrierFunctor(VkPipelineStageFlags src_exec_scope, VkPipelineStageFlags dst_exec_scope,
1121 SyncStageAccessFlags src_stage_accesses, SyncStageAccessFlags dst_stage_accesses,
John Zulauf9cb530d2019-09-30 14:14:10 -06001122 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers)
John Zulauf36bcf6a2020-02-03 15:12:52 -07001123 : src_exec_scope(src_exec_scope), dst_exec_scope(dst_exec_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001124 // Don't want to create this per tracked item, but don't want to loop through all tracked items per barrier...
1125 barrier_functor.reserve(memoryBarrierCount);
1126 for (uint32_t barrier_index = 0; barrier_index < memoryBarrierCount; barrier_index++) {
1127 const auto &barrier = pMemoryBarriers[barrier_index];
John Zulauf36bcf6a2020-02-03 15:12:52 -07001128 barrier_functor.emplace_back(src_exec_scope, SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask),
1129 dst_exec_scope, SyncStageAccess::AccessScope(dst_stage_accesses, barrier.dstAccessMask));
John Zulauf9cb530d2019-09-30 14:14:10 -06001130 }
1131 }
1132
John Zulauf36bcf6a2020-02-03 15:12:52 -07001133 const VkPipelineStageFlags src_exec_scope;
1134 const VkPipelineStageFlags dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06001135 std::vector<ApplyMemoryAccessBarrierFunctor> barrier_functor;
1136};
1137
John Zulauf355e49b2020-04-24 15:11:15 -06001138void AccessContext::UpdateAccessState(AddressType type, SyncStageAccessIndex current_usage, const ResourceAccessRange &range,
1139 const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001140 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, tag);
1141 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001142}
1143
John Zulauf16adfc92020-04-08 10:28:33 -06001144void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001145 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001146 if (!SimpleBinding(buffer)) return;
1147 const auto base_address = ResourceBaseAddress(buffer);
1148 UpdateAccessState(AddressType::kLinearAddress, current_usage, range + base_address, tag);
1149}
John Zulauf355e49b2020-04-24 15:11:15 -06001150
John Zulauf540266b2020-04-06 18:54:53 -06001151void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf355e49b2020-04-24 15:11:15 -06001152 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001153 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001154 if (!SimpleBinding(image)) return;
locke-lunargae26eac2020-04-16 15:29:05 -06001155 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent);
John Zulauf16adfc92020-04-08 10:28:33 -06001156 const auto address_type = ImageAddressType(image);
1157 const auto base_address = ResourceBaseAddress(image);
1158 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, tag);
John Zulauf5f13a792020-03-10 07:31:21 -06001159 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001160 UpdateMemoryAccessState(&GetAccessStateMap(address_type), (*range_gen + base_address), action);
John Zulauf5f13a792020-03-10 07:31:21 -06001161 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001162}
John Zulauf7635de32020-05-29 17:14:15 -06001163void AccessContext::UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, const VkOffset3D &offset,
1164 const VkExtent3D &extent, VkImageAspectFlags aspect_mask, const ResourceUsageTag &tag) {
1165 if (view != nullptr) {
1166 const IMAGE_STATE *image = view->image_state.get();
1167 if (image != nullptr) {
1168 auto *update_range = &view->normalized_subresource_range;
1169 VkImageSubresourceRange masked_range;
1170 if (aspect_mask) { // If present and non-zero, restrict the normalized range to aspects present in aspect_mask
1171 masked_range = view->normalized_subresource_range;
1172 masked_range.aspectMask = aspect_mask & masked_range.aspectMask;
1173 update_range = &masked_range;
1174 }
1175 UpdateAccessState(*image, current_usage, *update_range, offset, extent, tag);
1176 }
1177 }
1178}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001179
John Zulauf355e49b2020-04-24 15:11:15 -06001180void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1181 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1182 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001183 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1184 subresource.layerCount};
1185 UpdateAccessState(image, current_usage, subresource_range, offset, extent, tag);
1186}
1187
John Zulauf540266b2020-04-06 18:54:53 -06001188template <typename Action>
1189void AccessContext::UpdateMemoryAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001190 if (!SimpleBinding(buffer)) return;
1191 const auto base_address = ResourceBaseAddress(buffer);
1192 UpdateMemoryAccessState(&GetAccessStateMap(AddressType::kLinearAddress), (range + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001193}
1194
1195template <typename Action>
1196void AccessContext::UpdateMemoryAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
1197 const Action action) {
John Zulauf16adfc92020-04-08 10:28:33 -06001198 if (!SimpleBinding(image)) return;
1199 const auto address_type = ImageAddressType(image);
1200 auto *accesses = &GetAccessStateMap(address_type);
John Zulauf540266b2020-04-06 18:54:53 -06001201
locke-lunargae26eac2020-04-16 15:29:05 -06001202 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, {0, 0, 0},
locke-lunarg5f7d3c62020-04-07 00:10:39 -06001203 image.createInfo.extent);
John Zulauf540266b2020-04-06 18:54:53 -06001204
John Zulauf16adfc92020-04-08 10:28:33 -06001205 const auto base_address = ResourceBaseAddress(image);
John Zulauf540266b2020-04-06 18:54:53 -06001206 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf16adfc92020-04-08 10:28:33 -06001207 UpdateMemoryAccessState(accesses, (*range_gen + base_address), action);
John Zulauf540266b2020-04-06 18:54:53 -06001208 }
1209}
1210
John Zulauf7635de32020-05-29 17:14:15 -06001211void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1212 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1213 const ResourceUsageTag &tag) {
1214 UpdateStateResolveAction update(*this, tag);
1215 ResolveOperation(update, rp_state, render_area, attachment_views, subpass);
1216}
1217
John Zulaufaff20662020-06-01 14:07:58 -06001218void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
1219 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
1220 const ResourceUsageTag &tag) {
1221 const auto *attachment_ci = rp_state.createInfo.pAttachments;
1222 VkExtent3D extent = CastTo3D(render_area.extent);
1223 VkOffset3D offset = CastTo3D(render_area.offset);
1224
1225 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1226 if (rp_state.attachment_last_subpass[i] == subpass) {
1227 if (attachment_views[i] == nullptr) continue; // UNUSED
1228 const auto &view = *attachment_views[i];
1229 const IMAGE_STATE *image = view.image_state.get();
1230 if (image == nullptr) continue;
1231
1232 const auto &ci = attachment_ci[i];
1233 const bool has_depth = FormatHasDepth(ci.format);
1234 const bool has_stencil = FormatHasStencil(ci.format);
1235 const bool is_color = !(has_depth || has_stencil);
1236 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1237
1238 if (is_color && store_op_stores) {
1239 UpdateAccessState(*image, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, view.normalized_subresource_range,
1240 offset, extent, tag);
1241 } else {
1242 auto update_range = view.normalized_subresource_range;
1243 if (has_depth && store_op_stores) {
1244 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1245 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1246 tag);
1247 }
1248 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_QCOM;
1249 if (has_stencil && stencil_op_stores) {
1250 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1251 UpdateAccessState(*image, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, update_range, offset, extent,
1252 tag);
1253 }
1254 }
1255 }
1256 }
1257}
1258
John Zulauf540266b2020-04-06 18:54:53 -06001259template <typename Action>
1260void AccessContext::ApplyGlobalBarriers(const Action &barrier_action) {
1261 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001262 for (const auto address_type : kAddressTypes) {
1263 UpdateMemoryAccessState(&GetAccessStateMap(address_type), full_range, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001264 }
1265}
1266
1267void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001268 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1269 auto &context = contexts[subpass_index];
John Zulauf16adfc92020-04-08 10:28:33 -06001270 for (const auto address_type : kAddressTypes) {
John Zulauf355e49b2020-04-24 15:11:15 -06001271 context.ResolveAccessRange(address_type, full_range, &context.GetDstExternalTrackBack().barrier,
1272 &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001273 }
1274 }
1275}
1276
John Zulauf355e49b2020-04-24 15:11:15 -06001277void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1278 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1279 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range) {
1280 const ApplyMemoryAccessBarrierFunctor barrier_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
1281 UpdateMemoryAccess(image, subresource_range, barrier_action);
1282}
1283
John Zulauf7635de32020-05-29 17:14:15 -06001284// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001285void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, VkPipelineStageFlags src_exec_scope,
1286 SyncStageAccessFlags src_access_scope, VkPipelineStageFlags dst_exec_scope,
1287 SyncStageAccessFlags dst_access_scope, const VkImageSubresourceRange &subresource_range,
1288 bool layout_transition, const ResourceUsageTag &tag) {
1289 if (layout_transition) {
1290 UpdateAccessState(image, SYNC_IMAGE_LAYOUT_TRANSITION, subresource_range, VkOffset3D{0, 0, 0}, image.createInfo.extent,
1291 tag);
1292 ApplyImageBarrier(image, src_exec_scope, SYNC_IMAGE_LAYOUT_TRANSITION_BIT, dst_exec_scope, dst_access_scope,
1293 subresource_range);
John Zulaufc9201222020-05-13 15:13:03 -06001294 } else {
1295 ApplyImageBarrier(image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range);
John Zulauf355e49b2020-04-24 15:11:15 -06001296 }
John Zulauf355e49b2020-04-24 15:11:15 -06001297}
1298
John Zulauf7635de32020-05-29 17:14:15 -06001299// Note: ImageBarriers do not operate at offset/extent resolution, only at the whole subreources level
John Zulauf355e49b2020-04-24 15:11:15 -06001300void AccessContext::ApplyImageBarrier(const IMAGE_STATE &image, const SyncBarrier &barrier,
1301 const VkImageSubresourceRange &subresource_range, bool layout_transition,
1302 const ResourceUsageTag &tag) {
1303 ApplyImageBarrier(image, barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope,
1304 subresource_range, layout_transition, tag);
1305}
1306
1307// Suitable only for *subpass* access contexts
John Zulauf7635de32020-05-29 17:14:15 -06001308HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001309 if (!attach_view) return HazardResult();
1310 const auto image_state = attach_view->image_state.get();
1311 if (!image_state) return HazardResult();
1312
John Zulauf355e49b2020-04-24 15:11:15 -06001313 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001314 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001315
1316 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulauf7635de32020-05-29 17:14:15 -06001317 auto hazard = track_back.context->DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope,
1318 track_back.barrier.src_access_scope,
1319 attach_view->normalized_subresource_range, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001320 if (!hazard.hazard) {
1321 // The Async hazard check is against the current context's async set.
John Zulauf7635de32020-05-29 17:14:15 -06001322 hazard = DetectImageBarrierHazard(*image_state, track_back.barrier.src_exec_scope, track_back.barrier.src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001323 attach_view->normalized_subresource_range, kDetectAsync);
1324 }
1325 return hazard;
1326}
1327
1328// Class CommandBufferAccessContext: Keep track of resource access state information for a specific command buffer
1329bool CommandBufferAccessContext::ValidateBeginRenderPass(const RENDER_PASS_STATE &rp_state,
1330
1331 const VkRenderPassBeginInfo *pRenderPassBegin,
1332 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
1333 const char *func_name) const {
1334 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
1335 bool skip = false;
1336 uint32_t subpass = 0;
1337 const auto &transitions = rp_state.subpass_transitions[subpass];
1338 if (transitions.size()) {
1339 const std::vector<AccessContext> empty_context_vector;
1340 // Create context we can use to validate against...
1341 AccessContext temp_context(subpass, queue_flags_, rp_state.subpass_dependencies, empty_context_vector,
1342 const_cast<AccessContext *>(&cb_access_context_));
1343
1344 assert(pRenderPassBegin);
1345 if (nullptr == pRenderPassBegin) return skip;
1346
1347 const auto fb_state = sync_state_->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
1348 assert(fb_state);
1349 if (nullptr == fb_state) return skip;
1350
1351 // Create a limited array of views (which we'll need to toss
1352 std::vector<const IMAGE_VIEW_STATE *> views;
1353 const auto count_attachment = GetFramebufferAttachments(*pRenderPassBegin, *fb_state);
1354 const auto attachment_count = count_attachment.first;
1355 const auto *attachments = count_attachment.second;
1356 views.resize(attachment_count, nullptr);
1357 for (const auto &transition : transitions) {
1358 assert(transition.attachment < attachment_count);
1359 views[transition.attachment] = sync_state_->Get<IMAGE_VIEW_STATE>(attachments[transition.attachment]);
1360 }
1361
John Zulauf7635de32020-05-29 17:14:15 -06001362 skip |= temp_context.ValidateLayoutTransitions(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
1363 skip |= temp_context.ValidateLoadOperation(*sync_state_, rp_state, pRenderPassBegin->renderArea, 0, views, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001364 }
1365 return skip;
1366}
1367
locke-lunarg61870c22020-06-09 14:51:50 -06001368bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1369 const char *func_name) const {
1370 bool skip = false;
1371 const PIPELINE_STATE *pPipe = nullptr;
1372 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1373 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1374 if (!pPipe || !per_sets) {
1375 return skip;
1376 }
1377
1378 using DescriptorClass = cvdescriptorset::DescriptorClass;
1379 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1380 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1381 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1382 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1383
1384 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarg37047832020-06-12 13:44:45 -06001385 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001386 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1387 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001388 for (const auto &set_binding : stage_state.descriptor_uses) {
1389 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1390 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1391 set_binding.first.second);
1392 const auto descriptor_type = binding_it.GetType();
1393 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1394 auto array_idx = 0;
1395
1396 if (binding_it.IsVariableDescriptorCount()) {
1397 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1398 }
1399 SyncStageAccessIndex sync_index =
1400 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1401
1402 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1403 uint32_t index = i - index_range.start;
1404 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1405 switch (descriptor->GetClass()) {
1406 case DescriptorClass::ImageSampler:
1407 case DescriptorClass::Image: {
1408 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1409 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1410 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1411 } else {
1412 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1413 }
1414 if (!img_view_state) continue;
1415 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1416 VkExtent3D extent = {};
1417 VkOffset3D offset = {};
1418 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1419 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1420 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1421 } else {
1422 extent = img_state->createInfo.extent;
1423 }
1424 auto hazard = current_context_->DetectHazard(*img_state, sync_index,
1425 img_view_state->normalized_subresource_range, offset, extent);
1426 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06001427 skip |= sync_state_->LogError(
1428 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1429 "%s: Hazard %s for %s in %s, %s, and %s binding #%" PRIu32 " index %" PRIu32 ". Prior access %s.",
1430 func_name, string_SyncHazard(hazard.hazard),
1431 sync_state_->report_data->FormatHandle(img_view_state->image_view).c_str(),
1432 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1433 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1434 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(), set_binding.first.second,
John Zulauf37ceaed2020-07-03 16:18:15 -06001435 index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001436 }
1437 break;
1438 }
1439 case DescriptorClass::TexelBuffer: {
1440 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1441 if (!buf_view_state) continue;
1442 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1443 ResourceAccessRange range =
1444 MakeRange(buf_view_state->create_info.offset,
1445 GetRealWholeSize(buf_view_state->create_info.offset, buf_view_state->create_info.range,
1446 buf_state->createInfo.size));
1447 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
1448 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001449 skip |= sync_state_->LogError(
1450 buf_view_state->buffer_view, string_SyncHazardVUID(hazard.hazard),
1451 "%s: Hazard %s for %s in %s, %s, and %s binding #%d index %d. Prior access %s.", func_name,
1452 string_SyncHazard(hazard.hazard),
1453 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view).c_str(),
1454 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1455 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1456 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(), set_binding.first.second,
John Zulauf37ceaed2020-07-03 16:18:15 -06001457 index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001458 }
1459 break;
1460 }
1461 case DescriptorClass::GeneralBuffer: {
1462 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1463 auto buf_state = buffer_descriptor->GetBufferState();
1464 if (!buf_state) continue;
1465 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1466 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
1467 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001468 skip |= sync_state_->LogError(
1469 buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
1470 "%s: Hazard %s for %s in %s, %s, and %s binding #%d index %d. Prior access %s.", func_name,
1471 string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
1472 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(),
1473 sync_state_->report_data->FormatHandle(pPipe->pipeline).c_str(),
1474 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(), set_binding.first.second,
John Zulauf37ceaed2020-07-03 16:18:15 -06001475 index, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001476 }
1477 break;
1478 }
1479 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1480 default:
1481 break;
1482 }
1483 }
1484 }
1485 }
1486 return skip;
1487}
1488
1489void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1490 const ResourceUsageTag &tag) {
1491 const PIPELINE_STATE *pPipe = nullptr;
1492 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
1493 GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(*cb_state_.get(), pipelineBindPoint, &pPipe, &per_sets);
1494 if (!pPipe || !per_sets) {
1495 return;
1496 }
1497
1498 using DescriptorClass = cvdescriptorset::DescriptorClass;
1499 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1500 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1501 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1502 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1503
1504 for (const auto &stage_state : pPipe->stage_state) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001505 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pPipe->graphicsPipelineCI.pRasterizationState &&
1506 pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)
1507 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001508 for (const auto &set_binding : stage_state.descriptor_uses) {
1509 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.first].bound_descriptor_set;
1510 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
1511 set_binding.first.second);
1512 const auto descriptor_type = binding_it.GetType();
1513 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1514 auto array_idx = 0;
1515
1516 if (binding_it.IsVariableDescriptorCount()) {
1517 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1518 }
1519 SyncStageAccessIndex sync_index =
1520 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1521
1522 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1523 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1524 switch (descriptor->GetClass()) {
1525 case DescriptorClass::ImageSampler:
1526 case DescriptorClass::Image: {
1527 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1528 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1529 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1530 } else {
1531 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1532 }
1533 if (!img_view_state) continue;
1534 const IMAGE_STATE *img_state = img_view_state->image_state.get();
1535 VkExtent3D extent = {};
1536 VkOffset3D offset = {};
1537 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1538 extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1539 offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1540 } else {
1541 extent = img_state->createInfo.extent;
1542 }
1543 current_context_->UpdateAccessState(*img_state, sync_index, img_view_state->normalized_subresource_range,
1544 offset, extent, tag);
1545 break;
1546 }
1547 case DescriptorClass::TexelBuffer: {
1548 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1549 if (!buf_view_state) continue;
1550 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
1551 ResourceAccessRange range =
1552 MakeRange(buf_view_state->create_info.offset, buf_view_state->create_info.range);
1553 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1554 break;
1555 }
1556 case DescriptorClass::GeneralBuffer: {
1557 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1558 auto buf_state = buffer_descriptor->GetBufferState();
1559 if (!buf_state) continue;
1560 ResourceAccessRange range = MakeRange(buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
1561 current_context_->UpdateAccessState(*buf_state, sync_index, range, tag);
1562 break;
1563 }
1564 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1565 default:
1566 break;
1567 }
1568 }
1569 }
1570 }
1571}
1572
1573bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1574 bool skip = false;
1575 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1576 if (!pPipe) {
1577 return skip;
1578 }
1579
1580 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1581 const auto &binding_buffers_size = binding_buffers.size();
1582 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1583
1584 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1585 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1586 if (binding_description.binding < binding_buffers_size) {
1587 const auto &binding_buffer = binding_buffers[binding_description.binding];
1588 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1589
1590 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1591 VkDeviceSize range_start = 0;
1592 VkDeviceSize range_size = 0;
1593 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1594 binding_description.stride);
1595 ResourceAccessRange range = MakeRange(range_start, range_size);
1596 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range);
1597 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001598 skip |= sync_state_->LogError(
1599 buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Prior access %s.",
1600 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001601 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001602 }
1603 }
1604 }
1605 return skip;
1606}
1607
1608void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
1609 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(*cb_state_.get(), VK_PIPELINE_BIND_POINT_GRAPHICS);
1610 if (!pPipe) {
1611 return;
1612 }
1613 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1614 const auto &binding_buffers_size = binding_buffers.size();
1615 const auto &binding_descriptions_size = pPipe->vertex_binding_descriptions_.size();
1616
1617 for (size_t i = 0; i < binding_descriptions_size; ++i) {
1618 const auto &binding_description = pPipe->vertex_binding_descriptions_[i];
1619 if (binding_description.binding < binding_buffers_size) {
1620 const auto &binding_buffer = binding_buffers[binding_description.binding];
1621 if (binding_buffer.buffer == VK_NULL_HANDLE) continue;
1622
1623 auto *buf_state = sync_state_->Get<BUFFER_STATE>(binding_buffer.buffer);
1624 VkDeviceSize range_start = 0;
1625 VkDeviceSize range_size = 0;
1626 GetBufferRange(range_start, range_size, binding_buffer.offset, buf_state->createInfo.size, firstVertex, vertexCount,
1627 binding_description.stride);
1628 ResourceAccessRange range = MakeRange(range_start, range_size);
1629 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_INPUT_VERTEX_ATTRIBUTE_READ, range, tag);
1630 }
1631 }
1632}
1633
1634bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
1635 bool skip = false;
1636 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return skip;
1637
1638 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1639 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1640 VkDeviceSize range_start = 0;
1641 VkDeviceSize range_size = 0;
1642 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1643 indexCount, index_size);
1644 ResourceAccessRange range = MakeRange(range_start, range_size);
1645 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range);
1646 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001647 skip |= sync_state_->LogError(
1648 index_buf_state->buffer, string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Prior access %s.",
1649 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06001650 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001651 }
1652
1653 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1654 // We will detect more accurate range in the future.
1655 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
1656 return skip;
1657}
1658
1659void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
1660 if (cb_state_->index_buffer_binding.buffer == VK_NULL_HANDLE) return;
1661
1662 auto *index_buf_state = sync_state_->Get<BUFFER_STATE>(cb_state_->index_buffer_binding.buffer);
1663 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
1664 VkDeviceSize range_start = 0;
1665 VkDeviceSize range_size = 0;
1666 GetBufferRange(range_start, range_size, cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size, firstIndex,
1667 indexCount, index_size);
1668 ResourceAccessRange range = MakeRange(range_start, range_size);
1669 current_context_->UpdateAccessState(*index_buf_state, SYNC_VERTEX_INPUT_INDEX_READ, range, tag);
1670
1671 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
1672 // We will detect more accurate range in the future.
1673 RecordDrawVertex(UINT32_MAX, 0, tag);
1674}
1675
1676bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06001677 bool skip = false;
1678 if (!current_renderpass_context_) return skip;
1679 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(*sync_state_, *cb_state_.get(),
1680 cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
1681 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06001682}
1683
1684void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
locke-lunarg7077d502020-06-18 21:37:26 -06001685 if (current_renderpass_context_)
1686 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), cb_state_->activeRenderPassBeginInfo.renderArea,
1687 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001688}
1689
John Zulauf355e49b2020-04-24 15:11:15 -06001690bool CommandBufferAccessContext::ValidateNextSubpass(const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001691 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001692 if (!current_renderpass_context_) return skip;
John Zulauf1507ee42020-05-18 11:33:09 -06001693 skip |=
1694 current_renderpass_context_->ValidateNextSubpass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001695
1696 return skip;
1697}
1698
1699bool CommandBufferAccessContext::ValidateEndRenderpass(const char *func_name) const {
1700 // TODO: Things to add here.
John Zulauf7635de32020-05-29 17:14:15 -06001701 // Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001702 bool skip = false;
locke-lunarg7077d502020-06-18 21:37:26 -06001703 if (!current_renderpass_context_) return skip;
John Zulauf7635de32020-05-29 17:14:15 -06001704 skip |= current_renderpass_context_->ValidateEndRenderPass(*sync_state_, cb_state_->activeRenderPassBeginInfo.renderArea,
1705 func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001706
1707 return skip;
1708}
1709
1710void CommandBufferAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
1711 assert(sync_state_);
1712 if (!cb_state_) return;
1713
1714 // Create an access context the current renderpass.
John Zulauf1a224292020-06-30 14:52:13 -06001715 render_pass_contexts_.emplace_back();
John Zulauf16adfc92020-04-08 10:28:33 -06001716 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf1a224292020-06-30 14:52:13 -06001717 current_renderpass_context_->RecordBeginRenderPass(*sync_state_, *cb_state_, &cb_access_context_, queue_flags_, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001718 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06001719}
1720
John Zulauf355e49b2020-04-24 15:11:15 -06001721void CommandBufferAccessContext::RecordNextSubpass(const RENDER_PASS_STATE &rp_state, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001722 assert(current_renderpass_context_);
John Zulauf1507ee42020-05-18 11:33:09 -06001723 current_renderpass_context_->RecordNextSubpass(cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001724 current_context_ = &current_renderpass_context_->CurrentContext();
1725}
1726
John Zulauf355e49b2020-04-24 15:11:15 -06001727void CommandBufferAccessContext::RecordEndRenderPass(const RENDER_PASS_STATE &render_pass, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001728 assert(current_renderpass_context_);
1729 if (!current_renderpass_context_) return;
1730
John Zulauf1a224292020-06-30 14:52:13 -06001731 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, cb_state_->activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001732 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06001733 current_renderpass_context_ = nullptr;
1734}
1735
locke-lunarg61870c22020-06-09 14:51:50 -06001736bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const SyncValidator &sync_state, const CMD_BUFFER_STATE &cmd,
1737 const VkRect2D &render_area, const char *func_name) const {
1738 bool skip = false;
locke-lunarg96dc9632020-06-10 17:22:18 -06001739 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001740 if (!pPipe ||
1741 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001742 return skip;
1743 }
1744 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001745 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1746 VkExtent3D extent = CastTo3D(render_area.extent);
1747 VkOffset3D offset = CastTo3D(render_area.offset);
locke-lunarg37047832020-06-12 13:44:45 -06001748
John Zulauf1a224292020-06-30 14:52:13 -06001749 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001750 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001751 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1752 for (const auto location : list) {
1753 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1754 continue;
1755 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001756 HazardResult hazard = current_context.DetectHazard(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
1757 kColorAttachmentRasterOrder, offset, extent);
locke-lunarg96dc9632020-06-10 17:22:18 -06001758 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001759 skip |= sync_state.LogError(img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1760 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Prior access %s.",
1761 func_name, string_SyncHazard(hazard.hazard),
1762 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1763 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001764 location, string_UsageTag(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001765 }
1766 }
1767 }
locke-lunarg37047832020-06-12 13:44:45 -06001768
1769 // PHASE1 TODO: Add layout based read/vs. write selection.
1770 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1771 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1772 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001773 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001774 bool depth_write = false, stencil_write = false;
1775
1776 // PHASE1 TODO: These validation should be in core_checks.
1777 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1778 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1779 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1780 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1781 depth_write = true;
1782 }
1783 // PHASE1 TODO: It needs to check if stencil is writable.
1784 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1785 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1786 // PHASE1 TODO: These validation should be in core_checks.
1787 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1788 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1789 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1790 stencil_write = true;
1791 }
1792
1793 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1794 if (depth_write) {
1795 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001796 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1797 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_DEPTH_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001798 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001799 skip |= sync_state.LogError(
1800 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1801 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Prior access %s.",
1802 func_name, string_SyncHazard(hazard.hazard),
1803 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1804 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001805 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001806 }
1807 }
1808 if (stencil_write) {
1809 HazardResult hazard =
John Zulauf1a224292020-06-30 14:52:13 -06001810 current_context.DetectHazard(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
1811 kDepthStencilAttachmentRasterOrder, offset, extent, VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06001812 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001813 skip |= sync_state.LogError(
1814 img_view_state->image_view, string_SyncHazardVUID(hazard.hazard),
1815 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Prior access %s.",
1816 func_name, string_SyncHazard(hazard.hazard),
1817 sync_state.report_data->FormatHandle(img_view_state->image_view).c_str(),
1818 sync_state.report_data->FormatHandle(cmd.commandBuffer).c_str(), cmd.activeSubpass,
John Zulauf37ceaed2020-07-03 16:18:15 -06001819 string_UsageTag(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06001820 }
locke-lunarg61870c22020-06-09 14:51:50 -06001821 }
1822 }
1823 return skip;
1824}
1825
locke-lunarg96dc9632020-06-10 17:22:18 -06001826void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const VkRect2D &render_area,
1827 const ResourceUsageTag &tag) {
1828 const auto *pPipe = GetCurrentPipelineFromCommandBuffer(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001829 if (!pPipe ||
1830 (pPipe->graphicsPipelineCI.pRasterizationState && pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable)) {
locke-lunarg96dc9632020-06-10 17:22:18 -06001831 return;
1832 }
1833 const auto &list = pPipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06001834 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
1835 VkExtent3D extent = CastTo3D(render_area.extent);
1836 VkOffset3D offset = CastTo3D(render_area.offset);
1837
John Zulauf1a224292020-06-30 14:52:13 -06001838 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06001839 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06001840 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
1841 for (const auto location : list) {
1842 if (location >= subpass.colorAttachmentCount || subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED)
1843 continue;
1844 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pColorAttachments[location].attachment];
John Zulauf1a224292020-06-30 14:52:13 -06001845 current_context.UpdateAccessState(img_view_state, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, offset, extent,
1846 0, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001847 }
1848 }
locke-lunarg37047832020-06-12 13:44:45 -06001849
1850 // PHASE1 TODO: Add layout based read/vs. write selection.
1851 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
1852 if (pPipe->graphicsPipelineCI.pDepthStencilState && subpass.pDepthStencilAttachment &&
1853 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
locke-lunarg61870c22020-06-09 14:51:50 -06001854 const IMAGE_VIEW_STATE *img_view_state = attachment_views_[subpass.pDepthStencilAttachment->attachment];
locke-lunarg37047832020-06-12 13:44:45 -06001855 bool depth_write = false, stencil_write = false;
1856
1857 // PHASE1 TODO: These validation should be in core_checks.
1858 if (!FormatIsStencilOnly(img_view_state->create_info.format) &&
1859 pPipe->graphicsPipelineCI.pDepthStencilState->depthTestEnable &&
1860 pPipe->graphicsPipelineCI.pDepthStencilState->depthWriteEnable &&
1861 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
1862 depth_write = true;
1863 }
1864 // PHASE1 TODO: It needs to check if stencil is writable.
1865 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
1866 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
1867 // PHASE1 TODO: These validation should be in core_checks.
1868 if (!FormatIsDepthOnly(img_view_state->create_info.format) &&
1869 pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable &&
1870 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
1871 stencil_write = true;
1872 }
1873
1874 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
1875 if (depth_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001876 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1877 extent, VK_IMAGE_ASPECT_DEPTH_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001878 }
1879 if (stencil_write) {
John Zulauf1a224292020-06-30 14:52:13 -06001880 current_context.UpdateAccessState(img_view_state, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, offset,
1881 extent, VK_IMAGE_ASPECT_STENCIL_BIT, tag);
locke-lunarg37047832020-06-12 13:44:45 -06001882 }
locke-lunarg61870c22020-06-09 14:51:50 -06001883 }
1884}
1885
John Zulauf1507ee42020-05-18 11:33:09 -06001886bool RenderPassAccessContext::ValidateNextSubpass(const SyncValidator &sync_state, const VkRect2D &render_area,
1887 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001888 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06001889 bool skip = false;
John Zulaufb027cdb2020-05-21 14:25:22 -06001890 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1891 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001892 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1893 func_name);
1894
John Zulauf355e49b2020-04-24 15:11:15 -06001895 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06001896 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf7635de32020-05-29 17:14:15 -06001897 skip |= next_context.ValidateLayoutTransitions(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1898 skip |= next_context.ValidateLoadOperation(sync_state, *rp_state_, render_area, next_subpass, attachment_views_, func_name);
1899 return skip;
1900}
1901bool RenderPassAccessContext::ValidateEndRenderPass(const SyncValidator &sync_state, const VkRect2D &render_area,
1902 const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001903 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06001904 bool skip = false;
1905 skip |= CurrentContext().ValidateResolveOperations(sync_state, *rp_state_, render_area, attachment_views_, func_name,
1906 current_subpass_);
John Zulaufaff20662020-06-01 14:07:58 -06001907 skip |= CurrentContext().ValidateStoreOperation(sync_state, *rp_state_, render_area, current_subpass_, attachment_views_,
1908 func_name);
John Zulauf7635de32020-05-29 17:14:15 -06001909 skip |= ValidateFinalSubpassLayoutTransitions(sync_state, render_area, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06001910 return skip;
1911}
1912
John Zulauf7635de32020-05-29 17:14:15 -06001913AccessContext *RenderPassAccessContext::CreateStoreResolveProxy(const VkRect2D &render_area) const {
1914 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, render_area, attachment_views_);
1915}
1916
1917bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const SyncValidator &sync_state, const VkRect2D &render_area,
1918 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001919 bool skip = false;
1920
John Zulauf7635de32020-05-29 17:14:15 -06001921 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
1922 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
1923 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1924 // to apply and only copy then, if this proves a hot spot.
1925 std::unique_ptr<AccessContext> proxy_for_current;
1926
John Zulauf355e49b2020-04-24 15:11:15 -06001927 // Validate the "finalLayout" transitions to external
1928 // Get them from where there we're hidding in the extra entry.
1929 const auto &final_transitions = rp_state_->subpass_transitions.back();
1930 for (const auto &transition : final_transitions) {
1931 const auto &attach_view = attachment_views_[transition.attachment];
1932 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
1933 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06001934 auto *context = trackback.context;
1935
1936 if (transition.prev_pass == current_subpass_) {
1937 if (!proxy_for_current) {
1938 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
1939 proxy_for_current.reset(CreateStoreResolveProxy(render_area));
1940 }
1941 context = proxy_for_current.get();
1942 }
1943
1944 auto hazard = context->DetectImageBarrierHazard(
John Zulauf355e49b2020-04-24 15:11:15 -06001945 *attach_view->image_state, trackback.barrier.src_exec_scope, trackback.barrier.src_access_scope,
1946 attach_view->normalized_subresource_range, AccessContext::DetectOptions::kDetectPrevious);
1947 if (hazard.hazard) {
1948 skip |= sync_state.LogError(rp_state_->renderPass, string_SyncHazardVUID(hazard.hazard),
1949 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf1dae9192020-06-16 15:46:44 -06001950 " final image layout transition. Prior access %s.",
1951 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
John Zulauf37ceaed2020-07-03 16:18:15 -06001952 string_UsageTag(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001953 }
1954 }
1955 return skip;
1956}
1957
1958void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
1959 // Add layout transitions...
1960 const auto &transitions = rp_state_->subpass_transitions[current_subpass_];
1961 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulaufc9201222020-05-13 15:13:03 -06001962 std::set<const IMAGE_VIEW_STATE *> view_seen;
John Zulauf355e49b2020-04-24 15:11:15 -06001963 for (const auto &transition : transitions) {
1964 const auto attachment_view = attachment_views_[transition.attachment];
1965 if (!attachment_view) continue;
1966 const auto image = attachment_view->image_state.get();
1967 if (!image) continue;
1968
1969 const auto *barrier = subpass_context.GetTrackBackFromSubpass(transition.prev_pass);
John Zulaufc9201222020-05-13 15:13:03 -06001970 auto insert_pair = view_seen.insert(attachment_view);
1971 if (insert_pair.second) {
1972 // We haven't recorded the transistion yet, so treat this as a normal barrier with transistion.
1973 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, true, tag);
1974
1975 } else {
1976 // We've recorded the transition, but we need to added on the additional dest barriers, and rerecording the transition
1977 // would clear out the prior barrier flags, so apply this as a *non* transition barrier
1978 auto barrier_to_transition = barrier->barrier;
1979 barrier_to_transition.src_access_scope |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
1980 subpass_context.ApplyImageBarrier(*image, barrier->barrier, attachment_view->normalized_subresource_range, false, tag);
1981 }
John Zulauf355e49b2020-04-24 15:11:15 -06001982 }
1983}
1984
John Zulauf1507ee42020-05-18 11:33:09 -06001985void RenderPassAccessContext::RecordLoadOperations(const VkRect2D &render_area, const ResourceUsageTag &tag) {
1986 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
1987 auto &subpass_context = subpass_contexts_[current_subpass_];
1988 VkExtent3D extent = CastTo3D(render_area.extent);
1989 VkOffset3D offset = CastTo3D(render_area.offset);
1990
1991 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
1992 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
1993 if (attachment_views_[i] == nullptr) continue; // UNUSED
1994 const auto &view = *attachment_views_[i];
1995 const IMAGE_STATE *image = view.image_state.get();
1996 if (image == nullptr) continue;
1997
1998 const auto &ci = attachment_ci[i];
1999 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002000 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002001 const bool is_color = !(has_depth || has_stencil);
2002
2003 if (is_color) {
2004 subpass_context.UpdateAccessState(*image, ColorLoadUsage(ci.loadOp), view.normalized_subresource_range, offset,
2005 extent, tag);
2006 } else {
2007 auto update_range = view.normalized_subresource_range;
2008 if (has_depth) {
2009 update_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2010 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.loadOp), update_range, offset, extent, tag);
2011 }
2012 if (has_stencil) {
2013 update_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
2014 subpass_context.UpdateAccessState(*image, DepthStencilLoadUsage(ci.stencilLoadOp), update_range, offset, extent,
2015 tag);
2016 }
2017 }
2018 }
2019 }
2020}
2021
John Zulauf355e49b2020-04-24 15:11:15 -06002022void RenderPassAccessContext::RecordBeginRenderPass(const SyncValidator &state, const CMD_BUFFER_STATE &cb_state,
John Zulauf1a224292020-06-30 14:52:13 -06002023 const AccessContext *external_context, VkQueueFlags queue_flags,
2024 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002025 current_subpass_ = 0;
locke-lunargaecf2152020-05-12 17:15:41 -06002026 rp_state_ = cb_state.activeRenderPass.get();
John Zulauf355e49b2020-04-24 15:11:15 -06002027 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
2028 // Add this for all subpasses here so that they exsist during next subpass validation
2029 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002030 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002031 }
2032 attachment_views_ = state.GetCurrentAttachmentViews(cb_state);
2033
2034 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002035 RecordLoadOperations(cb_state.activeRenderPassBeginInfo.renderArea, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002036}
John Zulauf1507ee42020-05-18 11:33:09 -06002037
2038void RenderPassAccessContext::RecordNextSubpass(const VkRect2D &render_area, const ResourceUsageTag &tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002039 // Resolves are against *prior* subpass context and thus *before* the subpass increment
2040 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002041 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002042
John Zulauf355e49b2020-04-24 15:11:15 -06002043 current_subpass_++;
2044 assert(current_subpass_ < subpass_contexts_.size());
2045 RecordLayoutTransitions(tag);
John Zulauf1507ee42020-05-18 11:33:09 -06002046 RecordLoadOperations(render_area, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002047}
2048
John Zulauf1a224292020-06-30 14:52:13 -06002049void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const VkRect2D &render_area,
2050 const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002051 // Add the resolve and store accesses
John Zulauf7635de32020-05-29 17:14:15 -06002052 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulaufaff20662020-06-01 14:07:58 -06002053 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, render_area, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002054
John Zulauf355e49b2020-04-24 15:11:15 -06002055 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002056 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002057
2058 // Add the "finalLayout" transitions to external
2059 // Get them from where there we're hidding in the extra entry.
2060 const auto &final_transitions = rp_state_->subpass_transitions.back();
2061 for (const auto &transition : final_transitions) {
2062 const auto &attachment = attachment_views_[transition.attachment];
2063 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulauf1a224292020-06-30 14:52:13 -06002064 assert(external_context == last_trackback.context);
2065 external_context->ApplyImageBarrier(*attachment->image_state, last_trackback.barrier,
2066 attachment->normalized_subresource_range, true, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002067 }
2068}
2069
John Zulauf3d84f1b2020-03-09 13:33:25 -06002070SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier) {
2071 const auto src_stage_mask = ExpandPipelineStages(queue_flags, barrier.srcStageMask);
2072 src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2073 src_access_scope = SyncStageAccess::AccessScope(src_stage_mask, barrier.srcAccessMask);
2074 const auto dst_stage_mask = ExpandPipelineStages(queue_flags, barrier.dstStageMask);
2075 dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
2076 dst_access_scope = SyncStageAccess::AccessScope(dst_stage_mask, barrier.dstAccessMask);
2077}
2078
2079void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier) {
2080 ApplyExecutionBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
2081 ApplyMemoryAccessBarrier(barrier.src_exec_scope, barrier.src_access_scope, barrier.dst_exec_scope, barrier.dst_access_scope);
2082}
2083
John Zulauf9cb530d2019-09-30 14:14:10 -06002084HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2085 HazardResult hazard;
2086 auto usage = FlagBit(usage_index);
2087 if (IsRead(usage)) {
John Zulaufc9201222020-05-13 15:13:03 -06002088 if (last_write && IsWriteHazard(usage)) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002089 hazard.Set(READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002090 }
2091 } else {
2092 // Assume write
2093 // TODO determine what to do with READ-WRITE usage states if any
2094 // Write-After-Write check -- if we have a previous write to test against
2095 if (last_write && IsWriteHazard(usage)) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002096 hazard.Set(WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002097 } else {
John Zulauf69133422020-05-20 14:55:53 -06002098 // Look for casus belli for WAR
John Zulauf9cb530d2019-09-30 14:14:10 -06002099 const auto usage_stage = PipelineStageBit(usage_index);
John Zulaufd14743a2020-07-03 09:42:39 -06002100 // Note: kNoAttachmentRead is ~0, and thus the no attachment read hazard check doesn't need a separate path.
2101 if (IsReadHazard(usage_stage, input_attachment_barriers)) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002102 hazard.Set(WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002103 }
2104 if (!hazard.hazard) {
2105 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002106 const auto &read_access = last_reads[read_index];
2107 if (IsReadHazard(usage_stage, read_access)) {
2108 hazard.Set(WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002109 break;
2110 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002111 }
2112 }
2113 }
2114 }
2115 return hazard;
2116}
2117
John Zulauf69133422020-05-20 14:55:53 -06002118HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrderingBarrier &ordering) const {
2119 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2120 HazardResult hazard;
2121 const auto usage = FlagBit(usage_index);
2122 const bool write_is_ordered = (last_write & ordering.access_scope) == last_write; // Is true if no write, and that's good.
2123 if (IsRead(usage)) {
2124 if (!write_is_ordered && IsWriteHazard(usage)) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002125 hazard.Set(READ_AFTER_WRITE, last_write, write_tag);
John Zulauf69133422020-05-20 14:55:53 -06002126 }
2127 } else {
2128 if (!write_is_ordered && IsWriteHazard(usage)) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002129 hazard.Set(WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf69133422020-05-20 14:55:53 -06002130 } else {
2131 const auto usage_stage = PipelineStageBit(usage_index);
2132 const auto unordered_reads = last_read_stages & ~ordering.exec_scope;
2133 if (unordered_reads) {
2134 // Look for any WAR hazards outside the ordered set of stages
2135 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002136 const auto &read_access = last_reads[read_index];
2137 if ((read_access.stage & unordered_reads) && IsReadHazard(usage_stage, read_access)) {
2138 hazard.Set(WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf69133422020-05-20 14:55:53 -06002139 }
2140 }
2141 }
John Zulaufd14743a2020-07-03 09:42:39 -06002142
2143 // This is special case code for the fragment shader input attachment, which unlike all other fragment shader operations
2144 // is framebuffer local, and thus subject to raster ordering guarantees
2145 if (!hazard.hazard && (input_attachment_barriers != kNoAttachmentRead)) {
2146 if (0 == (ordering.access_scope & SyncStageAccessFlagBits::SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT)) {
2147 // NOTE: Currently all ordering barriers include this bit, so this code may never be reached, but it's
2148 // here s.t. if we need to change the ordering barrier/rules we needn't change the code.
John Zulauf37ceaed2020-07-03 16:18:15 -06002149 hazard.Set(WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002150 }
2151 }
John Zulauf69133422020-05-20 14:55:53 -06002152 }
2153 }
2154 return hazard;
2155}
2156
John Zulauf2f952d22020-02-10 11:34:51 -07002157// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf3d84f1b2020-03-09 13:33:25 -06002158HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002159 HazardResult hazard;
2160 auto usage = FlagBit(usage_index);
2161 if (IsRead(usage)) {
2162 if (last_write != 0) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002163 hazard.Set(READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002164 }
2165 } else {
2166 if (last_write != 0) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002167 hazard.Set(WRITE_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002168 } else if (last_read_count > 0) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002169 hazard.Set(WRITE_RACING_READ, last_reads[0].access, last_reads[0].tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002170 } else if (input_attachment_barriers != kNoAttachmentRead) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002171 hazard.Set(WRITE_RACING_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ, input_attachment_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002172 }
2173 }
2174 return hazard;
2175}
2176
John Zulauf36bcf6a2020-02-03 15:12:52 -07002177HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags src_exec_scope,
2178 SyncStageAccessFlags src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002179 // Only supporting image layout transitions for now
2180 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2181 HazardResult hazard;
2182 if (last_write) {
2183 // If the previous write is *not* in the 1st access scope
2184 // *AND* the current barrier is not in the dependency chain
2185 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
2186 // then the barrier access is unsafe (R/W after W)
John Zulauf36bcf6a2020-02-03 15:12:52 -07002187 if (((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0)) {
John Zulauf0cb5be22020-01-23 12:18:22 -07002188 // TODO: Do we need a difference hazard name for this?
John Zulauf37ceaed2020-07-03 16:18:15 -06002189 hazard.Set(WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002190 }
John Zulauf355e49b2020-04-24 15:11:15 -06002191 }
2192 if (!hazard.hazard) {
2193 // Look at the reads if any
John Zulauf0cb5be22020-01-23 12:18:22 -07002194 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
John Zulauf36bcf6a2020-02-03 15:12:52 -07002195 const auto &read_access = last_reads[read_index];
2196 // If the read stage is not in the src sync sync
2197 // *AND* not execution chained with an existing sync barrier (that's the or)
2198 // then the barrier access is unsafe (R/W after R)
2199 if ((src_exec_scope & (read_access.stage | read_access.barriers)) == 0) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002200 hazard.Set(WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002201 break;
2202 }
2203 }
2204 }
John Zulaufd14743a2020-07-03 09:42:39 -06002205 if (!hazard.hazard) {
2206 // Same logic as read acces above for the special case of input attachment read
2207 // Note: kNoReadAttachment is ~0 and thus cannot cause a hazard return.
2208 if ((src_exec_scope & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers)) == 0) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002209 hazard.Set(WRITE_AFTER_READ, SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT, input_attachment_tag);
John Zulaufd14743a2020-07-03 09:42:39 -06002210 }
2211 }
John Zulauf0cb5be22020-01-23 12:18:22 -07002212 return hazard;
2213}
2214
John Zulauf5f13a792020-03-10 07:31:21 -06002215// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2216// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2217// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2218void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2219 if (write_tag.IsBefore(other.write_tag)) {
2220 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent operation
2221 *this = other;
2222 } else if (!other.write_tag.IsBefore(write_tag)) {
2223 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2224 // dependency chaining logic or any stage expansion)
2225 write_barriers |= other.write_barriers;
2226
John Zulaufd14743a2020-07-03 09:42:39 -06002227 // Merge the read states
2228 if (input_attachment_barriers == kNoAttachmentRead) {
2229 // this doesn't have an input attachment read, so we'll take other, unconditionally (even if it's kNoAttachmentRead)
2230 input_attachment_barriers = other.input_attachment_barriers;
2231 input_attachment_tag = other.input_attachment_tag;
2232 } else if (other.input_attachment_barriers != kNoAttachmentRead) {
2233 // Both states have an input attachment read, pick the newest tag and merge barriers.
2234 if (input_attachment_tag.IsBefore(other.input_attachment_tag)) {
2235 input_attachment_tag = other.input_attachment_tag;
2236 }
2237 input_attachment_barriers |= other.input_attachment_barriers;
2238 }
2239 // The else clause is that only this has an attachment read and no merge is needed
2240
John Zulauf5f13a792020-03-10 07:31:21 -06002241 for (uint32_t other_read_index = 0; other_read_index < other.last_read_count; other_read_index++) {
2242 auto &other_read = other.last_reads[other_read_index];
2243 if (last_read_stages & other_read.stage) {
2244 // Merge in the barriers for read stages that exist in *both* this and other
2245 // TODO: This is N^2 with stages... perhaps the ReadStates should be by stage index.
2246 for (uint32_t my_read_index = 0; my_read_index < last_read_count; my_read_index++) {
2247 auto &my_read = last_reads[my_read_index];
2248 if (other_read.stage == my_read.stage) {
2249 if (my_read.tag.IsBefore(other_read.tag)) {
2250 my_read.tag = other_read.tag;
John Zulauf37ceaed2020-07-03 16:18:15 -06002251 my_read.access = other_read.access;
John Zulauf5f13a792020-03-10 07:31:21 -06002252 }
2253 my_read.barriers |= other_read.barriers;
2254 break;
2255 }
2256 }
2257 } else {
2258 // The other read stage doesn't exist in this, so add it.
2259 last_reads[last_read_count] = other_read;
2260 last_read_count++;
2261 last_read_stages |= other_read.stage;
2262 }
2263 }
2264 } // the else clause would be that other write is before this write... in which case we supercede the other state and ignore
2265 // it.
2266}
2267
John Zulauf9cb530d2019-09-30 14:14:10 -06002268void ResourceAccessState::Update(SyncStageAccessIndex usage_index, const ResourceUsageTag &tag) {
2269 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2270 const auto usage_bit = FlagBit(usage_index);
John Zulaufd14743a2020-07-03 09:42:39 -06002271 if (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT) {
2272 // Input attachment requires special treatment for raster/load/store ordering guarantees
2273 input_attachment_barriers = 0;
2274 input_attachment_tag = tag;
2275 } else if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002276 // Mulitple outstanding reads may be of interest and do dependency chains independently
2277 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2278 const auto usage_stage = PipelineStageBit(usage_index);
2279 if (usage_stage & last_read_stages) {
2280 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2281 ReadState &access = last_reads[read_index];
2282 if (access.stage == usage_stage) {
John Zulauf37ceaed2020-07-03 16:18:15 -06002283 access.access = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002284 access.barriers = 0;
2285 access.tag = tag;
2286 break;
2287 }
2288 }
2289 } else {
2290 // We don't have this stage in the list yet...
2291 assert(last_read_count < last_reads.size());
2292 ReadState &access = last_reads[last_read_count++];
2293 access.stage = usage_stage;
John Zulauf37ceaed2020-07-03 16:18:15 -06002294 access.access = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002295 access.barriers = 0;
2296 access.tag = tag;
2297 last_read_stages |= usage_stage;
2298 }
2299 } else {
2300 // Assume write
2301 // TODO determine what to do with READ-WRITE operations if any
John Zulaufd14743a2020-07-03 09:42:39 -06002302 // Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
John Zulauf9cb530d2019-09-30 14:14:10 -06002303 // if the last_reads/last_write were unsafe, we've reported them,
2304 // in either case the prior access is irrelevant, we can overwrite them as *this* write is now after them
2305 last_read_count = 0;
2306 last_read_stages = 0;
2307
John Zulaufd14743a2020-07-03 09:42:39 -06002308 input_attachment_barriers = kNoAttachmentRead; // Denotes no outstanding input attachment read after the last write.
2309 // NOTE: we don't reset the tag, as the equality check ignores it when kNoAttachmentRead is set.
2310
John Zulauf9cb530d2019-09-30 14:14:10 -06002311 write_barriers = 0;
2312 write_dependency_chain = 0;
2313 write_tag = tag;
2314 last_write = usage_bit;
2315 }
2316}
John Zulauf5f13a792020-03-10 07:31:21 -06002317
John Zulauf9cb530d2019-09-30 14:14:10 -06002318void ResourceAccessState::ApplyExecutionBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask) {
2319 // Execution Barriers only protect read operations
2320 for (uint32_t read_index = 0; read_index < last_read_count; read_index++) {
2321 ReadState &access = last_reads[read_index];
2322 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
2323 if (srcStageMask & (access.stage | access.barriers)) {
2324 access.barriers |= dstStageMask;
2325 }
2326 }
John Zulaufd14743a2020-07-03 09:42:39 -06002327 if (srcStageMask & (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | input_attachment_barriers)) {
2328 input_attachment_barriers |= dstStageMask;
2329 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002330 if (write_dependency_chain & srcStageMask) write_dependency_chain |= dstStageMask;
2331}
2332
John Zulauf36bcf6a2020-02-03 15:12:52 -07002333void ResourceAccessState::ApplyMemoryAccessBarrier(VkPipelineStageFlags src_exec_scope, SyncStageAccessFlags src_access_scope,
2334 VkPipelineStageFlags dst_exec_scope, SyncStageAccessFlags dst_access_scope) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002335 // Assuming we've applied the execution side of this barrier, we update just the write
2336 // The || implements the "dependency chain" logic for this barrier
John Zulauf36bcf6a2020-02-03 15:12:52 -07002337 if ((src_access_scope & last_write) || (write_dependency_chain & src_exec_scope)) {
2338 write_barriers |= dst_access_scope;
2339 write_dependency_chain |= dst_exec_scope;
John Zulauf9cb530d2019-09-30 14:14:10 -06002340 }
2341}
2342
John Zulaufd1f85d42020-04-15 12:23:15 -06002343void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002344 auto *access_context = GetAccessContextNoInsert(command_buffer);
2345 if (access_context) {
2346 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06002347 }
2348}
2349
John Zulaufd1f85d42020-04-15 12:23:15 -06002350void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
2351 auto access_found = cb_access_state.find(command_buffer);
2352 if (access_found != cb_access_state.end()) {
2353 access_found->second->Reset();
2354 cb_access_state.erase(access_found);
2355 }
2356}
2357
John Zulauf540266b2020-04-06 18:54:53 -06002358void SyncValidator::ApplyGlobalBarriers(AccessContext *context, VkPipelineStageFlags srcStageMask,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002359 VkPipelineStageFlags dstStageMask, SyncStageAccessFlags src_access_scope,
2360 SyncStageAccessFlags dst_access_scope, uint32_t memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002361 const VkMemoryBarrier *pMemoryBarriers) {
2362 // TODO: Implement this better (maybe some delayed/on-demand integration).
John Zulauf36bcf6a2020-02-03 15:12:52 -07002363 ApplyGlobalBarrierFunctor barriers_functor(srcStageMask, dstStageMask, src_access_scope, dst_access_scope, memoryBarrierCount,
John Zulauf9cb530d2019-09-30 14:14:10 -06002364 pMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002365 context->ApplyGlobalBarriers(barriers_functor);
John Zulauf9cb530d2019-09-30 14:14:10 -06002366}
2367
John Zulauf540266b2020-04-06 18:54:53 -06002368void SyncValidator::ApplyBufferBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
John Zulauf36bcf6a2020-02-03 15:12:52 -07002369 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2370 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf9cb530d2019-09-30 14:14:10 -06002371 const VkBufferMemoryBarrier *barriers) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002372 for (uint32_t index = 0; index < barrier_count; index++) {
locke-lunarg3c038002020-04-30 23:08:08 -06002373 auto barrier = barriers[index];
John Zulauf9cb530d2019-09-30 14:14:10 -06002374 const auto *buffer = Get<BUFFER_STATE>(barrier.buffer);
2375 if (!buffer) continue;
locke-lunarg3c038002020-04-30 23:08:08 -06002376 barrier.size = GetRealWholeSize(barrier.offset, barrier.size, buffer->createInfo.size);
John Zulauf16adfc92020-04-08 10:28:33 -06002377 ResourceAccessRange range = MakeRange(barrier);
John Zulauf540266b2020-04-06 18:54:53 -06002378 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2379 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2380 const ApplyMemoryAccessBarrierFunctor update_action(src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope);
2381 context->UpdateMemoryAccess(*buffer, range, update_action);
John Zulauf9cb530d2019-09-30 14:14:10 -06002382 }
2383}
2384
John Zulauf540266b2020-04-06 18:54:53 -06002385void SyncValidator::ApplyImageBarriers(AccessContext *context, VkPipelineStageFlags src_exec_scope,
2386 SyncStageAccessFlags src_stage_accesses, VkPipelineStageFlags dst_exec_scope,
2387 SyncStageAccessFlags dst_stage_accesses, uint32_t barrier_count,
John Zulauf355e49b2020-04-24 15:11:15 -06002388 const VkImageMemoryBarrier *barriers, const ResourceUsageTag &tag) {
John Zulauf5c5e88d2019-12-26 11:22:02 -07002389 for (uint32_t index = 0; index < barrier_count; index++) {
2390 const auto &barrier = barriers[index];
2391 const auto *image = Get<IMAGE_STATE>(barrier.image);
2392 if (!image) continue;
John Zulauf540266b2020-04-06 18:54:53 -06002393 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
John Zulauf355e49b2020-04-24 15:11:15 -06002394 bool layout_transition = barrier.oldLayout != barrier.newLayout;
2395 const auto src_access_scope = AccessScope(src_stage_accesses, barrier.srcAccessMask);
2396 const auto dst_access_scope = AccessScope(dst_stage_accesses, barrier.dstAccessMask);
2397 context->ApplyImageBarrier(*image, src_exec_scope, src_access_scope, dst_exec_scope, dst_access_scope, subresource_range,
2398 layout_transition, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002399 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002400}
2401
2402bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2403 uint32_t regionCount, const VkBufferCopy *pRegions) const {
2404 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002405 const auto *cb_context = GetAccessContext(commandBuffer);
2406 assert(cb_context);
2407 if (!cb_context) return skip;
2408 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06002409
John Zulauf3d84f1b2020-03-09 13:33:25 -06002410 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06002411 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002412 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002413
2414 for (uint32_t region = 0; region < regionCount; region++) {
2415 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002416 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002417 ResourceAccessRange src_range = MakeRange(
2418 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002419 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002420 if (hazard.hazard) {
2421 // TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002422 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002423 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Prior access %s.",
2424 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002425 string_UsageTag(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06002426 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002427 }
John Zulauf16adfc92020-04-08 10:28:33 -06002428 if (dst_buffer && !skip) {
locke-lunargff255f92020-05-13 18:53:52 -06002429 ResourceAccessRange dst_range = MakeRange(
2430 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf355e49b2020-04-24 15:11:15 -06002431 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002432 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002433 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002434 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Prior access %s.",
2435 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002436 string_UsageTag(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06002437 }
2438 }
2439 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06002440 }
2441 return skip;
2442}
2443
2444void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
2445 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002446 auto *cb_context = GetAccessContext(commandBuffer);
2447 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002448 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002449 auto *context = cb_context->GetCurrentAccessContext();
2450
John Zulauf9cb530d2019-09-30 14:14:10 -06002451 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002452 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06002453
2454 for (uint32_t region = 0; region < regionCount; region++) {
2455 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002456 if (src_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002457 ResourceAccessRange src_range = MakeRange(
2458 copy_region.srcOffset, GetRealWholeSize(copy_region.srcOffset, copy_region.size, src_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002459 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002460 }
John Zulauf16adfc92020-04-08 10:28:33 -06002461 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06002462 ResourceAccessRange dst_range = MakeRange(
2463 copy_region.dstOffset, GetRealWholeSize(copy_region.dstOffset, copy_region.size, dst_buffer->createInfo.size));
John Zulauf16adfc92020-04-08 10:28:33 -06002464 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002465 }
2466 }
2467}
2468
2469bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2470 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2471 const VkImageCopy *pRegions) const {
2472 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002473 const auto *cb_access_context = GetAccessContext(commandBuffer);
2474 assert(cb_access_context);
2475 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002476
John Zulauf3d84f1b2020-03-09 13:33:25 -06002477 const auto *context = cb_access_context->GetCurrentAccessContext();
2478 assert(context);
2479 if (!context) return skip;
2480
2481 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2482 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002483 for (uint32_t region = 0; region < regionCount; region++) {
2484 const auto &copy_region = pRegions[region];
2485 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002486 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06002487 copy_region.srcOffset, copy_region.extent);
2488 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002489 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002490 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
2491 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002492 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002493 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002494 }
2495
2496 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002497 VkExtent3D dst_copy_extent =
2498 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002499 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07002500 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002501 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002502 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002503 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
2504 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002505 string_UsageTag(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07002506 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07002507 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07002508 }
2509 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002510
John Zulauf5c5e88d2019-12-26 11:22:02 -07002511 return skip;
2512}
2513
2514void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2515 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2516 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002517 auto *cb_access_context = GetAccessContext(commandBuffer);
2518 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002519 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002520 auto *context = cb_access_context->GetCurrentAccessContext();
2521 assert(context);
2522
John Zulauf5c5e88d2019-12-26 11:22:02 -07002523 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002524 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002525
2526 for (uint32_t region = 0; region < regionCount; region++) {
2527 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06002528 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002529 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.srcSubresource, copy_region.srcOffset,
2530 copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07002531 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06002532 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07002533 VkExtent3D dst_copy_extent =
2534 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
John Zulauf540266b2020-04-06 18:54:53 -06002535 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.dstSubresource, copy_region.dstOffset,
2536 dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002537 }
2538 }
2539}
2540
2541bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2542 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2543 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2544 uint32_t bufferMemoryBarrierCount,
2545 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2546 uint32_t imageMemoryBarrierCount,
2547 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
2548 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002549 const auto *cb_access_context = GetAccessContext(commandBuffer);
2550 assert(cb_access_context);
2551 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002552
John Zulauf3d84f1b2020-03-09 13:33:25 -06002553 const auto *context = cb_access_context->GetCurrentAccessContext();
2554 assert(context);
2555 if (!context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07002556
John Zulauf3d84f1b2020-03-09 13:33:25 -06002557 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002558 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2559 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf0cb5be22020-01-23 12:18:22 -07002560 // Validate Image Layout transitions
2561 for (uint32_t index = 0; index < imageMemoryBarrierCount; index++) {
2562 const auto &barrier = pImageMemoryBarriers[index];
2563 if (barrier.newLayout == barrier.oldLayout) continue; // Only interested in layout transitions at this point.
2564 const auto *image_state = Get<IMAGE_STATE>(barrier.image);
2565 if (!image_state) continue;
John Zulauf16adfc92020-04-08 10:28:33 -06002566 const auto hazard = context->DetectImageBarrierHazard(*image_state, src_exec_scope, src_stage_accesses, barrier);
John Zulauf0cb5be22020-01-23 12:18:22 -07002567 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002568 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002569 skip |= LogError(barrier.image, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002570 "vkCmdPipelineBarrier: Hazard %s for image barrier %" PRIu32 " %s. Prior access %s.",
2571 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(barrier.image).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06002572 string_UsageTag(hazard).c_str());
John Zulauf0cb5be22020-01-23 12:18:22 -07002573 }
2574 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002575
2576 return skip;
2577}
2578
2579void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
2580 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
2581 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
2582 uint32_t bufferMemoryBarrierCount,
2583 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
2584 uint32_t imageMemoryBarrierCount,
2585 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002586 auto *cb_access_context = GetAccessContext(commandBuffer);
2587 assert(cb_access_context);
2588 if (!cb_access_context) return;
John Zulauf2b151bf2020-04-24 15:37:44 -06002589 const auto tag = cb_access_context->NextCommandTag(CMD_PIPELINEBARRIER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002590 auto access_context = cb_access_context->GetCurrentAccessContext();
2591 assert(access_context);
2592 if (!access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06002593
John Zulauf3d84f1b2020-03-09 13:33:25 -06002594 const auto src_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), srcStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002595 auto src_stage_accesses = AccessScopeByStage(src_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002596 const auto dst_stage_mask = ExpandPipelineStages(cb_access_context->GetQueueFlags(), dstStageMask);
John Zulauf36bcf6a2020-02-03 15:12:52 -07002597 auto dst_stage_accesses = AccessScopeByStage(dst_stage_mask);
2598 const auto src_exec_scope = WithEarlierPipelineStages(src_stage_mask);
2599 const auto dst_exec_scope = WithLaterPipelineStages(dst_stage_mask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002600 ApplyBufferBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
2601 bufferMemoryBarrierCount, pBufferMemoryBarriers);
John Zulauf540266b2020-04-06 18:54:53 -06002602 ApplyImageBarriers(access_context, src_exec_scope, src_stage_accesses, dst_exec_scope, dst_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06002603 imageMemoryBarrierCount, pImageMemoryBarriers, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002604
2605 // 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 -06002606 ApplyGlobalBarriers(access_context, src_exec_scope, dst_exec_scope, src_stage_accesses, dst_stage_accesses, memoryBarrierCount,
John Zulauf0cb5be22020-01-23 12:18:22 -07002607 pMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06002608}
2609
2610void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
2611 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
2612 // The state tracker sets up the device state
2613 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
2614
John Zulauf5f13a792020-03-10 07:31:21 -06002615 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
2616 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06002617 // TODO: Find a good way to do this hooklessly.
2618 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
2619 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
2620 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
2621
John Zulaufd1f85d42020-04-15 12:23:15 -06002622 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2623 sync_device_state->ResetCommandBufferCallback(command_buffer);
2624 });
2625 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
2626 sync_device_state->FreeCommandBufferCallback(command_buffer);
2627 });
John Zulauf9cb530d2019-09-30 14:14:10 -06002628}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002629
John Zulauf355e49b2020-04-24 15:11:15 -06002630bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2631 const VkSubpassBeginInfoKHR *pSubpassBeginInfo, const char *func_name) const {
2632 bool skip = false;
2633 const auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
2634 auto cb_context = GetAccessContext(commandBuffer);
2635
2636 if (rp_state && cb_context) {
2637 skip |= cb_context->ValidateBeginRenderPass(*rp_state, pRenderPassBegin, pSubpassBeginInfo, func_name);
2638 }
2639
2640 return skip;
2641}
2642
2643bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2644 VkSubpassContents contents) const {
2645 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2646 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2647 subpass_begin_info.contents = contents;
2648 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, "vkCmdBeginRenderPass");
2649 return skip;
2650}
2651
2652bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2653 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2654 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2655 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2");
2656 return skip;
2657}
2658
2659bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2660 const VkRenderPassBeginInfo *pRenderPassBegin,
2661 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) const {
2662 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
2663 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, "vkCmdBeginRenderPass2KHR");
2664 return skip;
2665}
2666
John Zulauf3d84f1b2020-03-09 13:33:25 -06002667void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
2668 VkResult result) {
2669 // The state tracker sets up the command buffer state
2670 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
2671
2672 // Create/initialize the structure that trackers accesses at the command buffer scope.
2673 auto cb_access_context = GetAccessContext(commandBuffer);
2674 assert(cb_access_context);
2675 cb_access_context->Reset();
2676}
2677
2678void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf355e49b2020-04-24 15:11:15 -06002679 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002680 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06002681 if (cb_context) {
2682 cb_context->RecordBeginRenderPass(cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002683 }
2684}
2685
2686void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2687 VkSubpassContents contents) {
2688 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
2689 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2690 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002691 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002692}
2693
2694void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
2695 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2696 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002697 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002698}
2699
2700void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
2701 const VkRenderPassBeginInfo *pRenderPassBegin,
2702 const VkSubpassBeginInfo *pSubpassBeginInfo) {
2703 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002704 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
2705}
2706
2707bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2708 const VkSubpassEndInfoKHR *pSubpassEndInfo, const char *func_name) const {
2709 bool skip = false;
2710
2711 auto cb_context = GetAccessContext(commandBuffer);
2712 assert(cb_context);
2713 auto cb_state = cb_context->GetCommandBufferState();
2714 if (!cb_state) return skip;
2715
2716 auto rp_state = cb_state->activeRenderPass;
2717 if (!rp_state) return skip;
2718
2719 skip |= cb_context->ValidateNextSubpass(func_name);
2720
2721 return skip;
2722}
2723
2724bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
2725 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
2726 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2727 subpass_begin_info.contents = contents;
2728 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, "vkCmdNextSubpass");
2729 return skip;
2730}
2731
2732bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
2733 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2734 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2735 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2KHR");
2736 return skip;
2737}
2738
2739bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2740 const VkSubpassEndInfo *pSubpassEndInfo) const {
2741 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
2742 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, "vkCmdNextSubpass2");
2743 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06002744}
2745
2746void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf355e49b2020-04-24 15:11:15 -06002747 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002748 auto cb_context = GetAccessContext(commandBuffer);
2749 assert(cb_context);
2750 auto cb_state = cb_context->GetCommandBufferState();
2751 if (!cb_state) return;
2752
2753 auto rp_state = cb_state->activeRenderPass;
2754 if (!rp_state) return;
2755
John Zulauf355e49b2020-04-24 15:11:15 -06002756 cb_context->RecordNextSubpass(*rp_state, cb_context->NextCommandTag(command));
John Zulauf3d84f1b2020-03-09 13:33:25 -06002757}
2758
2759void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
2760 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
2761 auto subpass_begin_info = lvl_init_struct<VkSubpassBeginInfo>();
2762 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06002763 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002764}
2765
2766void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2767 const VkSubpassEndInfo *pSubpassEndInfo) {
2768 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002769 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002770}
2771
2772void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
2773 const VkSubpassEndInfo *pSubpassEndInfo) {
2774 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06002775 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002776}
2777
John Zulauf355e49b2020-04-24 15:11:15 -06002778bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassEndInfo,
2779 const char *func_name) const {
2780 bool skip = false;
2781
2782 auto cb_context = GetAccessContext(commandBuffer);
2783 assert(cb_context);
2784 auto cb_state = cb_context->GetCommandBufferState();
2785 if (!cb_state) return skip;
2786
2787 auto rp_state = cb_state->activeRenderPass;
2788 if (!rp_state) return skip;
2789
2790 skip |= cb_context->ValidateEndRenderpass(func_name);
2791 return skip;
2792}
2793
2794bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2795 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
2796 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, "vkEndRenderPass");
2797 return skip;
2798}
2799
2800bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer,
2801 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2802 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2803 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2");
2804 return skip;
2805}
2806
2807bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
2808 const VkSubpassEndInfoKHR *pSubpassEndInfo) const {
2809 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2810 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, "vkEndRenderPass2KHR");
2811 return skip;
2812}
2813
2814void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
2815 CMD_TYPE command) {
John Zulaufe5da6e52020-03-18 15:32:18 -06002816 // Resolve the all subpass contexts to the command buffer contexts
2817 auto cb_context = GetAccessContext(commandBuffer);
2818 assert(cb_context);
2819 auto cb_state = cb_context->GetCommandBufferState();
2820 if (!cb_state) return;
2821
locke-lunargaecf2152020-05-12 17:15:41 -06002822 const auto *rp_state = cb_state->activeRenderPass.get();
John Zulaufe5da6e52020-03-18 15:32:18 -06002823 if (!rp_state) return;
2824
John Zulauf355e49b2020-04-24 15:11:15 -06002825 cb_context->RecordEndRenderPass(*rp_state, cb_context->NextCommandTag(command));
John Zulaufe5da6e52020-03-18 15:32:18 -06002826}
John Zulauf3d84f1b2020-03-09 13:33:25 -06002827
2828void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002829 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06002830 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002831}
2832
2833void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06002834 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06002835 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002836}
2837
2838void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06002839 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06002840 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002841}
locke-lunarga19c71d2020-03-02 18:17:04 -07002842
2843bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2844 VkImageLayout dstImageLayout, uint32_t regionCount,
2845 const VkBufferImageCopy *pRegions) const {
2846 bool skip = false;
2847 const auto *cb_access_context = GetAccessContext(commandBuffer);
2848 assert(cb_access_context);
2849 if (!cb_access_context) return skip;
2850
2851 const auto *context = cb_access_context->GetCurrentAccessContext();
2852 assert(context);
2853 if (!context) return skip;
2854
2855 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07002856 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
2857
2858 for (uint32_t region = 0; region < regionCount; region++) {
2859 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06002860 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002861 ResourceAccessRange src_range =
2862 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002863 auto hazard = context->DetectHazard(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002864 if (hazard.hazard) {
John Zulauf7635de32020-05-29 17:14:15 -06002865 // PHASE1 TODO -- add tag information to log msg when useful.
locke-lunarga0003652020-03-10 11:38:51 -06002866 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002867 "vkCmdCopyBufferToImage: Hazard %s for srcBuffer %s, region %" PRIu32 ". Prior access %s.",
2868 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002869 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002870 }
2871 }
2872 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002873 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002874 copy_region.imageOffset, copy_region.imageExtent);
2875 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002876 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002877 "vkCmdCopyBufferToImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
2878 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002879 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002880 }
2881 if (skip) break;
2882 }
2883 if (skip) break;
2884 }
2885 return skip;
2886}
2887
2888void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
2889 VkImageLayout dstImageLayout, uint32_t regionCount,
2890 const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06002891 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07002892 auto *cb_access_context = GetAccessContext(commandBuffer);
2893 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002894 const auto tag = cb_access_context->NextCommandTag(CMD_COPYBUFFERTOIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07002895 auto *context = cb_access_context->GetCurrentAccessContext();
2896 assert(context);
2897
2898 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06002899 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002900
2901 for (uint32_t region = 0; region < regionCount; region++) {
2902 const auto &copy_region = pRegions[region];
2903 if (src_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002904 ResourceAccessRange src_range =
2905 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002906 context->UpdateAccessState(*src_buffer, SYNC_TRANSFER_TRANSFER_READ, src_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002907 }
2908 if (dst_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002909 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002910 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002911 }
2912 }
2913}
2914
2915bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
2916 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
2917 const VkBufferImageCopy *pRegions) const {
2918 bool skip = false;
2919 const auto *cb_access_context = GetAccessContext(commandBuffer);
2920 assert(cb_access_context);
2921 if (!cb_access_context) return skip;
2922
2923 const auto *context = cb_access_context->GetCurrentAccessContext();
2924 assert(context);
2925 if (!context) return skip;
2926
2927 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2928 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2929 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->binding.mem_state->mem : VK_NULL_HANDLE;
2930 for (uint32_t region = 0; region < regionCount; region++) {
2931 const auto &copy_region = pRegions[region];
2932 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002933 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07002934 copy_region.imageOffset, copy_region.imageExtent);
2935 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002936 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002937 "vkCmdCopyImageToBuffer: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
2938 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002939 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002940 }
2941 }
2942 if (dst_mem) {
John Zulauf355e49b2020-04-24 15:11:15 -06002943 ResourceAccessRange dst_range =
2944 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002945 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range);
locke-lunarga19c71d2020-03-02 18:17:04 -07002946 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06002947 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06002948 "vkCmdCopyImageToBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Prior access %s.",
2949 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06002950 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07002951 }
2952 }
2953 if (skip) break;
2954 }
2955 return skip;
2956}
2957
2958void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2959 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06002960 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
locke-lunarga19c71d2020-03-02 18:17:04 -07002961 auto *cb_access_context = GetAccessContext(commandBuffer);
2962 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06002963 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGETOBUFFER);
locke-lunarga19c71d2020-03-02 18:17:04 -07002964 auto *context = cb_access_context->GetCurrentAccessContext();
2965 assert(context);
2966
2967 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07002968 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
2969 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 -06002970 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07002971
2972 for (uint32_t region = 0; region < regionCount; region++) {
2973 const auto &copy_region = pRegions[region];
2974 if (src_image) {
John Zulauf540266b2020-04-06 18:54:53 -06002975 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, copy_region.imageSubresource,
John Zulauf5f13a792020-03-10 07:31:21 -06002976 copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002977 }
2978 if (dst_buffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06002979 ResourceAccessRange dst_range =
2980 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
John Zulauf16adfc92020-04-08 10:28:33 -06002981 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, dst_range, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07002982 }
2983 }
2984}
2985
2986bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
2987 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
2988 const VkImageBlit *pRegions, VkFilter filter) const {
2989 bool skip = false;
2990 const auto *cb_access_context = GetAccessContext(commandBuffer);
2991 assert(cb_access_context);
2992 if (!cb_access_context) return skip;
2993
2994 const auto *context = cb_access_context->GetCurrentAccessContext();
2995 assert(context);
2996 if (!context) return skip;
2997
2998 const auto *src_image = Get<IMAGE_STATE>(srcImage);
2999 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3000
3001 for (uint32_t region = 0; region < regionCount; region++) {
3002 const auto &blit_region = pRegions[region];
3003 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003004 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3005 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3006 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3007 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3008 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3009 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3010 auto hazard =
3011 context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003012 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003013 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003014 "vkCmdBlitImage: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
3015 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003016 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003017 }
3018 }
3019
3020 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003021 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3022 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3023 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3024 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3025 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3026 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3027 auto hazard =
3028 context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003029 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003030 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003031 "vkCmdBlitImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
3032 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003033 string_UsageTag(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003034 }
3035 if (skip) break;
3036 }
3037 }
3038
3039 return skip;
3040}
3041
3042void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3043 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3044 const VkImageBlit *pRegions, VkFilter filter) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003045 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3046 pRegions, filter);
locke-lunarga19c71d2020-03-02 18:17:04 -07003047 auto *cb_access_context = GetAccessContext(commandBuffer);
3048 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003049 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
locke-lunarga19c71d2020-03-02 18:17:04 -07003050 auto *context = cb_access_context->GetCurrentAccessContext();
3051 assert(context);
3052
3053 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003054 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003055
3056 for (uint32_t region = 0; region < regionCount; region++) {
3057 const auto &blit_region = pRegions[region];
3058 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003059 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3060 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3061 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3062 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3063 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3064 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
3065 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003066 }
3067 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003068 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3069 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3070 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3071 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3072 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3073 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
3074 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003075 }
3076 }
3077}
locke-lunarg36ba2592020-04-03 09:42:04 -06003078
locke-lunarg61870c22020-06-09 14:51:50 -06003079bool SyncValidator::ValidateIndirectBuffer(const AccessContext &context, VkCommandBuffer commandBuffer,
3080 const VkDeviceSize struct_size, const VkBuffer buffer, const VkDeviceSize offset,
3081 const uint32_t drawCount, const uint32_t stride, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003082 bool skip = false;
3083 if (drawCount == 0) return skip;
3084
3085 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3086 VkDeviceSize size = struct_size;
3087 if (drawCount == 1 || stride == size) {
3088 if (drawCount > 1) size *= drawCount;
3089 ResourceAccessRange range = MakeRange(offset, size);
3090 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3091 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003092 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
3093 "%s: Hazard %s for indirect %s in %s. Prior access %s.", function, string_SyncHazard(hazard.hazard),
3094 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003095 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003096 }
3097 } else {
3098 for (uint32_t i = 0; i < drawCount; ++i) {
3099 ResourceAccessRange range = MakeRange(offset + i * stride, size);
3100 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3101 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003102 skip |= LogError(buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
3103 "%s: Hazard %s for indirect %s in %s. Prior access %s.", function,
3104 string_SyncHazard(hazard.hazard), report_data->FormatHandle(buffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003105 report_data->FormatHandle(commandBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003106 break;
3107 }
3108 }
3109 }
3110 return skip;
3111}
3112
locke-lunarg61870c22020-06-09 14:51:50 -06003113void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3114 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3115 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003116 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3117 VkDeviceSize size = struct_size;
3118 if (drawCount == 1 || stride == size) {
3119 if (drawCount > 1) size *= drawCount;
3120 ResourceAccessRange range = MakeRange(offset, size);
3121 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3122 } else {
3123 for (uint32_t i = 0; i < drawCount; ++i) {
3124 ResourceAccessRange range = MakeRange(offset + i * stride, size);
3125 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3126 }
3127 }
3128}
3129
locke-lunarg61870c22020-06-09 14:51:50 -06003130bool SyncValidator::ValidateCountBuffer(const AccessContext &context, VkCommandBuffer commandBuffer, VkBuffer buffer,
3131 VkDeviceSize offset, const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003132 bool skip = false;
3133
3134 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3135 ResourceAccessRange range = MakeRange(offset, 4);
3136 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3137 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003138 skip |= LogError(count_buf_state->buffer, string_SyncHazardVUID(hazard.hazard),
3139 "%s: Hazard %s for countBuffer %s in %s. Prior access %s.", function, string_SyncHazard(hazard.hazard),
3140 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003141 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003142 }
3143 return skip;
3144}
3145
locke-lunarg61870c22020-06-09 14:51:50 -06003146void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003147 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
3148 ResourceAccessRange range = MakeRange(offset, 4);
3149 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range, tag);
3150}
3151
locke-lunarg36ba2592020-04-03 09:42:04 -06003152bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003153 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003154 const auto *cb_access_context = GetAccessContext(commandBuffer);
3155 assert(cb_access_context);
3156 if (!cb_access_context) return skip;
3157
locke-lunarg61870c22020-06-09 14:51:50 -06003158 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06003159 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06003160}
3161
3162void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003163 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06003164 auto *cb_access_context = GetAccessContext(commandBuffer);
3165 assert(cb_access_context);
3166 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06003167
locke-lunarg61870c22020-06-09 14:51:50 -06003168 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06003169}
locke-lunarge1a67022020-04-29 00:15:36 -06003170
3171bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06003172 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003173 const auto *cb_access_context = GetAccessContext(commandBuffer);
3174 assert(cb_access_context);
3175 if (!cb_access_context) return skip;
3176
3177 const auto *context = cb_access_context->GetCurrentAccessContext();
3178 assert(context);
3179 if (!context) return skip;
3180
locke-lunarg61870c22020-06-09 14:51:50 -06003181 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
3182 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset, 1,
3183 sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003184 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003185}
3186
3187void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003188 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06003189 auto *cb_access_context = GetAccessContext(commandBuffer);
3190 assert(cb_access_context);
3191 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
3192 auto *context = cb_access_context->GetCurrentAccessContext();
3193 assert(context);
3194
locke-lunarg61870c22020-06-09 14:51:50 -06003195 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
3196 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06003197}
3198
3199bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3200 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003201 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003202 const auto *cb_access_context = GetAccessContext(commandBuffer);
3203 assert(cb_access_context);
3204 if (!cb_access_context) return skip;
3205
locke-lunarg61870c22020-06-09 14:51:50 -06003206 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
3207 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
3208 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003209 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003210}
3211
3212void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
3213 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003214 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003215 auto *cb_access_context = GetAccessContext(commandBuffer);
3216 assert(cb_access_context);
3217 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06003218
locke-lunarg61870c22020-06-09 14:51:50 -06003219 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3220 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
3221 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003222}
3223
3224bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3225 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06003226 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003227 const auto *cb_access_context = GetAccessContext(commandBuffer);
3228 assert(cb_access_context);
3229 if (!cb_access_context) return skip;
3230
locke-lunarg61870c22020-06-09 14:51:50 -06003231 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
3232 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
3233 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06003234 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003235}
3236
3237void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
3238 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003239 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06003240 auto *cb_access_context = GetAccessContext(commandBuffer);
3241 assert(cb_access_context);
3242 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06003243
locke-lunarg61870c22020-06-09 14:51:50 -06003244 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3245 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
3246 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003247}
3248
3249bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3250 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003251 bool skip = false;
3252 if (drawCount == 0) return skip;
3253
locke-lunargff255f92020-05-13 18:53:52 -06003254 const auto *cb_access_context = GetAccessContext(commandBuffer);
3255 assert(cb_access_context);
3256 if (!cb_access_context) return skip;
3257
3258 const auto *context = cb_access_context->GetCurrentAccessContext();
3259 assert(context);
3260 if (!context) return skip;
3261
locke-lunarg61870c22020-06-09 14:51:50 -06003262 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
3263 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
3264 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride,
3265 "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003266
3267 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3268 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3269 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003270 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003271 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003272}
3273
3274void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3275 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003276 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003277 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06003278 auto *cb_access_context = GetAccessContext(commandBuffer);
3279 assert(cb_access_context);
3280 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
3281 auto *context = cb_access_context->GetCurrentAccessContext();
3282 assert(context);
3283
locke-lunarg61870c22020-06-09 14:51:50 -06003284 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3285 cb_access_context->RecordDrawSubpassAttachment(tag);
3286 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003287
3288 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3289 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3290 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003291 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003292}
3293
3294bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3295 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003296 bool skip = false;
3297 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06003298 const auto *cb_access_context = GetAccessContext(commandBuffer);
3299 assert(cb_access_context);
3300 if (!cb_access_context) return skip;
3301
3302 const auto *context = cb_access_context->GetCurrentAccessContext();
3303 assert(context);
3304 if (!context) return skip;
3305
locke-lunarg61870c22020-06-09 14:51:50 -06003306 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
3307 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
3308 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride,
3309 "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003310
3311 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3312 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3313 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003314 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06003315 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003316}
3317
3318void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3319 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003320 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003321 auto *cb_access_context = GetAccessContext(commandBuffer);
3322 assert(cb_access_context);
3323 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
3324 auto *context = cb_access_context->GetCurrentAccessContext();
3325 assert(context);
3326
locke-lunarg61870c22020-06-09 14:51:50 -06003327 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3328 cb_access_context->RecordDrawSubpassAttachment(tag);
3329 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003330
3331 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3332 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3333 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003334 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003335}
3336
3337bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3338 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3339 uint32_t stride, const char *function) const {
3340 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003341 const auto *cb_access_context = GetAccessContext(commandBuffer);
3342 assert(cb_access_context);
3343 if (!cb_access_context) return skip;
3344
3345 const auto *context = cb_access_context->GetCurrentAccessContext();
3346 assert(context);
3347 if (!context) return skip;
3348
locke-lunarg61870c22020-06-09 14:51:50 -06003349 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3350 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3351 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset, maxDrawCount, stride,
3352 function);
3353 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003354
3355 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
3356 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3357 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003358 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003359 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003360}
3361
3362bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3363 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3364 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003365 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3366 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003367}
3368
3369void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3370 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3371 uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003372 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3373 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003374 auto *cb_access_context = GetAccessContext(commandBuffer);
3375 assert(cb_access_context);
3376 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECTCOUNT);
3377 auto *context = cb_access_context->GetCurrentAccessContext();
3378 assert(context);
3379
locke-lunarg61870c22020-06-09 14:51:50 -06003380 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3381 cb_access_context->RecordDrawSubpassAttachment(tag);
3382 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
3383 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003384
3385 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
3386 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
3387 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003388 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003389}
3390
3391bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3392 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3393 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003394 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3395 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003396}
3397
3398void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3399 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3400 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003401 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3402 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003403 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003404}
3405
3406bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3407 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3408 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003409 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3410 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003411}
3412
3413void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3414 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3415 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003416 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
3417 stride);
locke-lunargff255f92020-05-13 18:53:52 -06003418 PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3419}
3420
3421bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3422 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3423 uint32_t stride, const char *function) const {
3424 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003425 const auto *cb_access_context = GetAccessContext(commandBuffer);
3426 assert(cb_access_context);
3427 if (!cb_access_context) return skip;
3428
3429 const auto *context = cb_access_context->GetCurrentAccessContext();
3430 assert(context);
3431 if (!context) return skip;
3432
locke-lunarg61870c22020-06-09 14:51:50 -06003433 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
3434 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
3435 skip |= ValidateIndirectBuffer(*context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, maxDrawCount,
3436 stride, function);
3437 skip |= ValidateCountBuffer(*context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06003438
3439 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
3440 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
3441 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06003442 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06003443 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06003444}
3445
3446bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3447 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3448 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003449 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3450 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06003451}
3452
3453void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3454 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3455 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003456 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3457 maxDrawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06003458 auto *cb_access_context = GetAccessContext(commandBuffer);
3459 assert(cb_access_context);
3460 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECTCOUNT);
3461 auto *context = cb_access_context->GetCurrentAccessContext();
3462 assert(context);
3463
locke-lunarg61870c22020-06-09 14:51:50 -06003464 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
3465 cb_access_context->RecordDrawSubpassAttachment(tag);
3466 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
3467 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06003468
3469 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
3470 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06003471 // We will update the index and vertex buffer in SubmitQueue in the future.
3472 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06003473}
3474
3475bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
3476 VkDeviceSize offset, VkBuffer countBuffer,
3477 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3478 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003479 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3480 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06003481}
3482
3483void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3484 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3485 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003486 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3487 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003488 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3489}
3490
3491bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
3492 VkDeviceSize offset, VkBuffer countBuffer,
3493 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
3494 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06003495 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
3496 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06003497}
3498
3499void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3500 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
3501 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003502 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
3503 maxDrawCount, stride);
locke-lunarge1a67022020-04-29 00:15:36 -06003504 PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride);
3505}
3506
3507bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3508 const VkClearColorValue *pColor, uint32_t rangeCount,
3509 const VkImageSubresourceRange *pRanges) const {
3510 bool skip = false;
3511 const auto *cb_access_context = GetAccessContext(commandBuffer);
3512 assert(cb_access_context);
3513 if (!cb_access_context) return skip;
3514
3515 const auto *context = cb_access_context->GetCurrentAccessContext();
3516 assert(context);
3517 if (!context) return skip;
3518
3519 const auto *image_state = Get<IMAGE_STATE>(image);
3520
3521 for (uint32_t index = 0; index < rangeCount; index++) {
3522 const auto &range = pRanges[index];
3523 if (image_state) {
3524 auto hazard =
3525 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3526 if (hazard.hazard) {
3527 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003528 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Prior access %s.",
3529 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06003530 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003531 }
3532 }
3533 }
3534 return skip;
3535}
3536
3537void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3538 const VkClearColorValue *pColor, uint32_t rangeCount,
3539 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003540 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003541 auto *cb_access_context = GetAccessContext(commandBuffer);
3542 assert(cb_access_context);
3543 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
3544 auto *context = cb_access_context->GetCurrentAccessContext();
3545 assert(context);
3546
3547 const auto *image_state = Get<IMAGE_STATE>(image);
3548
3549 for (uint32_t index = 0; index < rangeCount; index++) {
3550 const auto &range = pRanges[index];
3551 if (image_state) {
3552 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3553 tag);
3554 }
3555 }
3556}
3557
3558bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3559 VkImageLayout imageLayout,
3560 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3561 const VkImageSubresourceRange *pRanges) const {
3562 bool skip = false;
3563 const auto *cb_access_context = GetAccessContext(commandBuffer);
3564 assert(cb_access_context);
3565 if (!cb_access_context) return skip;
3566
3567 const auto *context = cb_access_context->GetCurrentAccessContext();
3568 assert(context);
3569 if (!context) return skip;
3570
3571 const auto *image_state = Get<IMAGE_STATE>(image);
3572
3573 for (uint32_t index = 0; index < rangeCount; index++) {
3574 const auto &range = pRanges[index];
3575 if (image_state) {
3576 auto hazard =
3577 context->DetectHazard(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent);
3578 if (hazard.hazard) {
3579 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003580 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Prior access %s.",
3581 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf37ceaed2020-07-03 16:18:15 -06003582 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003583 }
3584 }
3585 }
3586 return skip;
3587}
3588
3589void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3590 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
3591 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003592 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06003593 auto *cb_access_context = GetAccessContext(commandBuffer);
3594 assert(cb_access_context);
3595 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
3596 auto *context = cb_access_context->GetCurrentAccessContext();
3597 assert(context);
3598
3599 const auto *image_state = Get<IMAGE_STATE>(image);
3600
3601 for (uint32_t index = 0; index < rangeCount; index++) {
3602 const auto &range = pRanges[index];
3603 if (image_state) {
3604 context->UpdateAccessState(*image_state, SYNC_TRANSFER_TRANSFER_WRITE, range, {0, 0, 0}, image_state->createInfo.extent,
3605 tag);
3606 }
3607 }
3608}
3609
3610bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
3611 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
3612 VkDeviceSize dstOffset, VkDeviceSize stride,
3613 VkQueryResultFlags flags) const {
3614 bool skip = false;
3615 const auto *cb_access_context = GetAccessContext(commandBuffer);
3616 assert(cb_access_context);
3617 if (!cb_access_context) return skip;
3618
3619 const auto *context = cb_access_context->GetCurrentAccessContext();
3620 assert(context);
3621 if (!context) return skip;
3622
3623 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3624
3625 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003626 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003627 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3628 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003629 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3630 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Prior access %s.",
3631 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003632 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003633 }
3634 }
locke-lunargff255f92020-05-13 18:53:52 -06003635
3636 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003637 return skip;
3638}
3639
3640void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
3641 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3642 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003643 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
3644 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06003645 auto *cb_access_context = GetAccessContext(commandBuffer);
3646 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06003647 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06003648 auto *context = cb_access_context->GetCurrentAccessContext();
3649 assert(context);
3650
3651 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3652
3653 if (dst_buffer) {
locke-lunargff255f92020-05-13 18:53:52 -06003654 ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
locke-lunarge1a67022020-04-29 00:15:36 -06003655 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3656 }
locke-lunargff255f92020-05-13 18:53:52 -06003657
3658 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06003659}
3660
3661bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3662 VkDeviceSize size, uint32_t data) const {
3663 bool skip = false;
3664 const auto *cb_access_context = GetAccessContext(commandBuffer);
3665 assert(cb_access_context);
3666 if (!cb_access_context) return skip;
3667
3668 const auto *context = cb_access_context->GetCurrentAccessContext();
3669 assert(context);
3670 if (!context) return skip;
3671
3672 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3673
3674 if (dst_buffer) {
3675 ResourceAccessRange range = MakeRange(dstOffset, size);
3676 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3677 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003678 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3679 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Prior access %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06003680 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003681 }
3682 }
3683 return skip;
3684}
3685
3686void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3687 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003688 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06003689 auto *cb_access_context = GetAccessContext(commandBuffer);
3690 assert(cb_access_context);
3691 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
3692 auto *context = cb_access_context->GetCurrentAccessContext();
3693 assert(context);
3694
3695 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3696
3697 if (dst_buffer) {
3698 ResourceAccessRange range = MakeRange(dstOffset, size);
3699 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3700 }
3701}
3702
3703bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3704 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3705 const VkImageResolve *pRegions) const {
3706 bool skip = false;
3707 const auto *cb_access_context = GetAccessContext(commandBuffer);
3708 assert(cb_access_context);
3709 if (!cb_access_context) return skip;
3710
3711 const auto *context = cb_access_context->GetCurrentAccessContext();
3712 assert(context);
3713 if (!context) return skip;
3714
3715 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3716 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3717
3718 for (uint32_t region = 0; region < regionCount; region++) {
3719 const auto &resolve_region = pRegions[region];
3720 if (src_image) {
3721 auto hazard = context->DetectHazard(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3722 resolve_region.srcOffset, resolve_region.extent);
3723 if (hazard.hazard) {
3724 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003725 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Prior access %s.",
3726 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003727 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003728 }
3729 }
3730
3731 if (dst_image) {
3732 auto hazard = context->DetectHazard(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3733 resolve_region.dstOffset, resolve_region.extent);
3734 if (hazard.hazard) {
3735 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003736 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Prior access %s.",
3737 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf37ceaed2020-07-03 16:18:15 -06003738 string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003739 }
3740 if (skip) break;
3741 }
3742 }
3743
3744 return skip;
3745}
3746
3747void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3748 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3749 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003750 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3751 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06003752 auto *cb_access_context = GetAccessContext(commandBuffer);
3753 assert(cb_access_context);
3754 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
3755 auto *context = cb_access_context->GetCurrentAccessContext();
3756 assert(context);
3757
3758 auto *src_image = Get<IMAGE_STATE>(srcImage);
3759 auto *dst_image = Get<IMAGE_STATE>(dstImage);
3760
3761 for (uint32_t region = 0; region < regionCount; region++) {
3762 const auto &resolve_region = pRegions[region];
3763 if (src_image) {
3764 context->UpdateAccessState(*src_image, SYNC_TRANSFER_TRANSFER_READ, resolve_region.srcSubresource,
3765 resolve_region.srcOffset, resolve_region.extent, tag);
3766 }
3767 if (dst_image) {
3768 context->UpdateAccessState(*dst_image, SYNC_TRANSFER_TRANSFER_WRITE, resolve_region.dstSubresource,
3769 resolve_region.dstOffset, resolve_region.extent, tag);
3770 }
3771 }
3772}
3773
3774bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3775 VkDeviceSize dataSize, const void *pData) const {
3776 bool skip = false;
3777 const auto *cb_access_context = GetAccessContext(commandBuffer);
3778 assert(cb_access_context);
3779 if (!cb_access_context) return skip;
3780
3781 const auto *context = cb_access_context->GetCurrentAccessContext();
3782 assert(context);
3783 if (!context) return skip;
3784
3785 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3786
3787 if (dst_buffer) {
3788 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3789 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3790 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003791 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3792 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Prior access %s.", string_SyncHazard(hazard.hazard),
John Zulauf37ceaed2020-07-03 16:18:15 -06003793 report_data->FormatHandle(dstBuffer).c_str(), string_UsageTag(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06003794 }
3795 }
3796 return skip;
3797}
3798
3799void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
3800 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003801 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06003802 auto *cb_access_context = GetAccessContext(commandBuffer);
3803 assert(cb_access_context);
3804 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
3805 auto *context = cb_access_context->GetCurrentAccessContext();
3806 assert(context);
3807
3808 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3809
3810 if (dst_buffer) {
3811 ResourceAccessRange range = MakeRange(dstOffset, dataSize);
3812 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3813 }
3814}
locke-lunargff255f92020-05-13 18:53:52 -06003815
3816bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3817 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
3818 bool skip = false;
3819 const auto *cb_access_context = GetAccessContext(commandBuffer);
3820 assert(cb_access_context);
3821 if (!cb_access_context) return skip;
3822
3823 const auto *context = cb_access_context->GetCurrentAccessContext();
3824 assert(context);
3825 if (!context) return skip;
3826
3827 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3828
3829 if (dst_buffer) {
3830 ResourceAccessRange range = MakeRange(dstOffset, 4);
3831 auto hazard = context->DetectHazard(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range);
3832 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06003833 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3834 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Prior access %s.",
3835 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
John Zulauf37ceaed2020-07-03 16:18:15 -06003836 string_UsageTag(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003837 }
3838 }
3839 return skip;
3840}
3841
3842void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
3843 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06003844 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06003845 auto *cb_access_context = GetAccessContext(commandBuffer);
3846 assert(cb_access_context);
3847 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
3848 auto *context = cb_access_context->GetCurrentAccessContext();
3849 assert(context);
3850
3851 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
3852
3853 if (dst_buffer) {
3854 ResourceAccessRange range = MakeRange(dstOffset, 4);
3855 context->UpdateAccessState(*dst_buffer, SYNC_TRANSFER_TRANSFER_WRITE, range, tag);
3856 }
3857}